2020import java .io .IOException ;
2121import java .io .Serializable ;
2222import java .nio .charset .StandardCharsets ;
23+ import java .nio .file .Path ;
24+ import java .nio .file .Paths ;
2325
2426import org .apache .commons .io .FileUtils ;
2527import org .slf4j .Logger ;
2628import org .slf4j .LoggerFactory ;
2729
30+ import com .vaadin .flow .server .PwaConfiguration ;
31+
2832import elemental .json .Json ;
2933import elemental .json .JsonObject ;
3034import static com .vaadin .flow .server .Constants .VAADIN_WEBAPP_RESOURCES ;
35+ import static com .vaadin .flow .server .frontend .FrontendUtils .SERVICE_WORKER_SRC ;
36+ import static com .vaadin .flow .server .frontend .FrontendUtils .SERVICE_WORKER_SRC_JS ;
3137import static com .vaadin .flow .shared .ApplicationConstants .VAADIN_STATIC_FILES_PATH ;
3238import static elemental .json .impl .JsonUtil .stringify ;
3339
@@ -46,14 +52,17 @@ public class TaskUpdateSettingsFile implements FallibleCommand, Serializable {
4652 File webappResourcesDirectory ;
4753 String buildDirectory ;
4854 String themeName ;
55+ PwaConfiguration pwaConfiguration ;
4956
50- TaskUpdateSettingsFile (NodeTasks .Builder builder , String themeName ) {
57+ TaskUpdateSettingsFile (NodeTasks .Builder builder , String themeName ,
58+ PwaConfiguration pwaConfiguration ) {
5159 this .npmFolder = builder .getNpmFolder ();
5260 this .frontendDirectory = builder .getFrontendDirectory ();
5361 this .generatedFolder = builder .getGeneratedFolder ();
5462 this .webappResourcesDirectory = builder .getWebappResourcesDirectory ();
5563 this .buildDirectory = builder .getBuildDirectory ();
5664 this .themeName = themeName ;
65+ this .pwaConfiguration = pwaConfiguration ;
5766 }
5867
5968 @ Override
@@ -86,6 +95,12 @@ public void execute() {
8695
8796 settings .put ("themeName" , themeName );
8897
98+ settings .put ("clientServiceWorkerSource" , getServiceWorkerFile ());
99+
100+ settings .put ("pwaEnabled" , pwaConfiguration .isEnabled ());
101+
102+ settings .put ("offlinePath" , getOfflinePath ());
103+
89104 File settingsFile = new File (npmFolder ,
90105 buildDirectory + "/" + DEV_SETTINGS_FILE );
91106
@@ -108,6 +123,42 @@ private String combinePath(String... parts) {
108123 return path .toString ();
109124 }
110125
126+ private String getServiceWorkerFile () {
127+ boolean exists = new File (frontendDirectory , SERVICE_WORKER_SRC )
128+ .exists ()
129+ || new File (frontendDirectory , SERVICE_WORKER_SRC_JS ).exists ();
130+
131+ String serviceWorkerFile = SERVICE_WORKER_SRC ;
132+ if (new File (frontendDirectory , SERVICE_WORKER_SRC_JS ).exists ()) {
133+ serviceWorkerFile = SERVICE_WORKER_SRC_JS ;
134+ }
135+
136+ if (!exists ) {
137+ Path path = Paths .get (npmFolder .toString (), buildDirectory ,
138+ serviceWorkerFile );
139+ return path .toString ();
140+ } else {
141+ return Paths .get (frontendDirectory .toString (), serviceWorkerFile )
142+ .toString ();
143+ }
144+ }
145+
146+ private String getOfflinePath () {
147+ if (pwaConfiguration .isOfflinePathEnabled ()) {
148+ return "'" + getEscapedRelativePath (
149+ Paths .get (pwaConfiguration .getOfflinePath ())) + "'" ;
150+ }
151+ return "'.'" ;
152+ }
153+
154+ private String getEscapedRelativePath (Path path ) {
155+ if (path .isAbsolute ()) {
156+ return FrontendUtils .getUnixRelativePath (npmFolder .toPath (), path );
157+ } else {
158+ return FrontendUtils .getUnixPath (path );
159+ }
160+ }
161+
111162 private Logger log () {
112163 return LoggerFactory .getLogger (this .getClass ());
113164 }
0 commit comments