@speedkit/cli 2.65.1 → 2.65.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,3 +1,17 @@
1
+ ## [2.65.3](https://gitlab.orestes.info/baqend/speed-kit-cli/compare/v2.65.2...v2.65.3) (2024-11-13)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * **onboarding:** live update styles ([648c145](https://gitlab.orestes.info/baqend/speed-kit-cli/commit/648c14557b1d82235e93f609cdf46d443f7972db))
7
+
8
+ ## [2.65.2](https://gitlab.orestes.info/baqend/speed-kit-cli/compare/v2.65.1...v2.65.2) (2024-11-12)
9
+
10
+
11
+ ### Bug Fixes
12
+
13
+ * **speed-kit-cli:** missing opening tag in force install ([dbee840](https://gitlab.orestes.info/baqend/speed-kit-cli/commit/dbee840057c2ebbd00d032bf482297592025a818))
14
+
1
15
  ## [2.65.1](https://gitlab.orestes.info/baqend/speed-kit-cli/compare/v2.65.0...v2.65.1) (2024-11-08)
2
16
 
3
17
 
package/README.md CHANGED
@@ -21,7 +21,7 @@ $ npm install -g @speedkit/cli
21
21
  $ sk COMMAND
22
22
  running command...
23
23
  $ sk (--version)
24
- @speedkit/cli/2.65.1 linux-x64 node-v20.18.0
24
+ @speedkit/cli/2.65.3 linux-x64 node-v20.18.0
25
25
  $ sk --help [COMMAND]
26
26
  USAGE
27
27
  $ sk COMMAND
@@ -113,9 +113,19 @@ class CustomerDomainDocumentResponse {
113
113
  }
114
114
  rewriteInstallResource(text) {
115
115
  text = text.replace(onboarding_model_1.SpeedKitInstallRegex, "");
116
- return text.replace(/<\s*head\b[^>]*>/, (head) => {
117
- return `${head}\n<script src="${this.customerConfig.installPath}" ${this.customerConfig.installParams}></script>`;
118
- });
116
+ const matchOpeningHead = /<\s*head\b[^>]*>/;
117
+ const matchClosingHead = /<\/\s*head\s*>/;
118
+ if (matchOpeningHead.test(text)) {
119
+ return text.replace(matchOpeningHead, (head) => {
120
+ return `${head}\n<script src="${this.customerConfig.installPath}" ${this.customerConfig.installParams}></script>`;
121
+ });
122
+ }
123
+ if (matchClosingHead.test(text)) {
124
+ return text.replace(matchClosingHead, (head) => {
125
+ return `<script src="${this.customerConfig.installPath}" ${this.customerConfig.installParams}></script>${head}\n`;
126
+ });
127
+ }
128
+ return text;
119
129
  }
120
130
  rewriteHtmlToLocalConfig(html) {
121
131
  const DF_CONFIG_PATTERN = /(<script[^>]* id="speed-kit-df-config"[^>]*>)([\S\s]*?)(<\/script>)/g;
@@ -15,14 +15,89 @@ export declare class FileWatcher {
15
15
  private browserContext;
16
16
  private cache;
17
17
  constructor(cli: CliService, customerConfig: CustomerConfig, files: FileListInterface, documentHandler: DocumentHandlerServer, browserContext: BrowserContext, cache: Cache);
18
+ /**
19
+ * will perform various actions based on the changed file
20
+ *
21
+ * @param page
22
+ * @param serviceWorkerReinstallCallback
23
+ */
18
24
  addCallbackOnFileChange(page: Page, serviceWorkerReinstallCallback: (file: ConfigFileInterface) => Promise<void>): void;
25
+ /**
26
+ * changes on these files will trigger
27
+ * - a rebuild of the serviceWorker
28
+ * - a rebuild of the installResource
29
+ * - currentService worker will be unregistered
30
+ * - onNextNavigate the new serviceWorker will be installed
31
+ *
32
+ * @param page
33
+ * @param installResource
34
+ * @param serviceWorkerReinstallCallback
35
+ * @private
36
+ */
19
37
  private handleServiceWorkerDependencies;
38
+ /**
39
+ * changes on these files will trigger
40
+ * - rebuild of the local documentHandler
41
+ * - clearCache in browser
42
+ * - clearCache in local documentHandler
43
+ *
44
+ * @param page
45
+ * @param installResource
46
+ * @private
47
+ */
20
48
  private handleGenericConfigFiles;
49
+ /**
50
+ * changes on these file will trigger:
51
+ * - rebuild of documentHandler
52
+ * - cacheClear of local documentHandler
53
+ * - cacheClear in browser
54
+ * - live exchange of the styles in the currently opened page
55
+ *
56
+ * @param page
57
+ * @private
58
+ */
21
59
  private handleDynamicStyles;
60
+ /**
61
+ * fileWatcherCallback that will reinstall the serviceWorker
62
+ *
63
+ * @param file
64
+ * @param page
65
+ * @param installResource
66
+ * @param serviceWorkerReinstallCallback
67
+ * @private
68
+ */
22
69
  private onChangeServiceWorkerCallback;
23
- private updateStyles;
70
+ /**
71
+ * fileWatcherCallback that will rebuild the local documentHandler
72
+ *
73
+ * @param page
74
+ * @param installResource
75
+ * @param file
76
+ * @private
77
+ */
24
78
  private onChangeConfigFileCallback;
79
+ /**
80
+ * fileWatcherCallback that will reinstall the serviceWorker
81
+ *
82
+ * @param file
83
+ * @param page
84
+ * @private
85
+ */
25
86
  private onChangeDynamicStyleCallback;
26
- private clearPageFunction;
27
- private clearServiceWorker;
87
+ /**
88
+ * clear speed-kit-caches in browser without interrupting the current workflow on error
89
+ *
90
+ * @param page
91
+ * @private
92
+ */
93
+ private safeClearPageBrowsersCacheStorage;
94
+ /**
95
+ * clear speed-kit-caches in browser
96
+ *
97
+ * @param page
98
+ * @private
99
+ */
100
+ private clearPageBrowsersCacheStorage;
101
+ private unregisterServiceWorker;
102
+ private updateStylesInlineFunction;
28
103
  }
@@ -4,6 +4,7 @@ exports.FileWatcher = void 0;
4
4
  const safe_1 = require("../../../helpers/safe");
5
5
  const integration_api_1 = require("../../integration-api");
6
6
  const files_1 = require("../../../models/files");
7
+ const race_1 = require("../../../helpers/race");
7
8
  /**
8
9
  * Add different callbacks that will be executed if a local file is changed.
9
10
  */
@@ -22,12 +23,30 @@ class FileWatcher {
22
23
  this.browserContext = browserContext;
23
24
  this.cache = cache;
24
25
  }
26
+ /**
27
+ * will perform various actions based on the changed file
28
+ *
29
+ * @param page
30
+ * @param serviceWorkerReinstallCallback
31
+ */
25
32
  addCallbackOnFileChange(page, serviceWorkerReinstallCallback) {
26
33
  const installResource = this.files.getByName(files_1.INTEGRATION_FILES.BUILD.INSTALL);
27
34
  this.handleServiceWorkerDependencies(page, installResource, serviceWorkerReinstallCallback);
28
35
  this.handleGenericConfigFiles(page, installResource);
29
36
  this.handleDynamicStyles(page);
30
37
  }
38
+ /**
39
+ * changes on these files will trigger
40
+ * - a rebuild of the serviceWorker
41
+ * - a rebuild of the installResource
42
+ * - currentService worker will be unregistered
43
+ * - onNextNavigate the new serviceWorker will be installed
44
+ *
45
+ * @param page
46
+ * @param installResource
47
+ * @param serviceWorkerReinstallCallback
48
+ * @private
49
+ */
31
50
  handleServiceWorkerDependencies(page, installResource, serviceWorkerReinstallCallback) {
32
51
  // add watcher to serviceWorker related files
33
52
  const serviceWorkerDependencies = [
@@ -52,6 +71,16 @@ class FileWatcher {
52
71
  });
53
72
  }
54
73
  }
74
+ /**
75
+ * changes on these files will trigger
76
+ * - rebuild of the local documentHandler
77
+ * - clearCache in browser
78
+ * - clearCache in local documentHandler
79
+ *
80
+ * @param page
81
+ * @param installResource
82
+ * @private
83
+ */
55
84
  handleGenericConfigFiles(page, installResource) {
56
85
  // add watcher to configFiles
57
86
  const generalConfigFiles = [
@@ -76,6 +105,16 @@ class FileWatcher {
76
105
  });
77
106
  }
78
107
  }
108
+ /**
109
+ * changes on these file will trigger:
110
+ * - rebuild of documentHandler
111
+ * - cacheClear of local documentHandler
112
+ * - cacheClear in browser
113
+ * - live exchange of the styles in the currently opened page
114
+ *
115
+ * @param page
116
+ * @private
117
+ */
79
118
  handleDynamicStyles(page) {
80
119
  if (!this.files.hasFile(files_1.INTEGRATION_FILES.CONFIG.DYNAMIC_STYLES)) {
81
120
  return;
@@ -90,58 +129,94 @@ class FileWatcher {
90
129
  }
91
130
  });
92
131
  }
132
+ /**
133
+ * fileWatcherCallback that will reinstall the serviceWorker
134
+ *
135
+ * @param file
136
+ * @param page
137
+ * @param installResource
138
+ * @param serviceWorkerReinstallCallback
139
+ * @private
140
+ */
93
141
  async onChangeServiceWorkerCallback(file, page, installResource, serviceWorkerReinstallCallback) {
94
142
  this.cli.writeWarning(`changed file: ${file.name} (reinstall serviceWorker)`);
95
143
  // clear speed-kit-service-worker-caches and uninstall serviceWorker
96
- await this.clearPageFunction(page);
97
- await this.clearServiceWorker(page);
144
+ await this.safeClearPageBrowsersCacheStorage(page);
145
+ await this.unregisterServiceWorker(page);
98
146
  // rebuild installResource
99
147
  await installResource.buildContent();
100
148
  // tell puppeteer to reinstall serviceworker on next navigate
101
149
  await serviceWorkerReinstallCallback(file);
102
150
  }
103
- updateStyles(styles) {
104
- const styleElement = document.querySelector("#speed-kit-df-styles");
105
- if (styleElement) {
106
- styleElement.textContent = decodeURI(styles);
107
- }
108
- }
151
+ /**
152
+ * fileWatcherCallback that will rebuild the local documentHandler
153
+ *
154
+ * @param page
155
+ * @param installResource
156
+ * @param file
157
+ * @private
158
+ */
109
159
  async onChangeConfigFileCallback(page, installResource, file) {
110
- await this.clearPageFunction(page);
160
+ this.cli.writeWarning(`changed file: ${file.name}`);
161
+ await this.safeClearPageBrowsersCacheStorage(page);
111
162
  await this.documentHandler.buildDocumentHandler();
112
163
  this.cache.clear();
113
164
  await installResource.buildContent();
114
- this.cli.writeWarning(`changed file: ${file.name}`);
115
165
  }
166
+ /**
167
+ * fileWatcherCallback that will reinstall the serviceWorker
168
+ *
169
+ * @param file
170
+ * @param page
171
+ * @private
172
+ */
116
173
  async onChangeDynamicStyleCallback(file, page) {
117
- this.cli.writeWarning(`changed file: ${file.name} (update styles on page)`);
174
+ this.cli.writeWarning(`changed file: ${file.name}`);
175
+ await this.documentHandler.buildDocumentHandler();
176
+ this.cache.clear();
118
177
  const escapedStyles = encodeURI(file.getContent());
119
- const evaluateResult = await (0, safe_1.safe)(page.evaluate(`(function ${this.updateStyles.toString()})("${escapedStyles}");`));
120
- if (evaluateResult.success === false) {
178
+ const evaluateResult = await (0, safe_1.safe)((0, race_1.race)(Promise.all([
179
+ page.evaluate(this.updateStylesInlineFunction, escapedStyles),
180
+ this.safeClearPageBrowsersCacheStorage(page),
181
+ ]), 200));
182
+ if (evaluateResult.success === false &&
183
+ !evaluateResult.error.includes("timeout")) {
121
184
  this.cli.writeError(evaluateResult.error);
122
- console.log(evaluateResult.errorObj);
185
+ console.error(evaluateResult.errorObj);
123
186
  }
124
- const clearPageResult = await (0, safe_1.safe)(this.clearPageFunction(page));
125
- if (clearPageResult.success === false) {
126
- this.cli.writeError(clearPageResult.error);
187
+ }
188
+ /**
189
+ * clear speed-kit-caches in browser without interrupting the current workflow on error
190
+ *
191
+ * @param page
192
+ * @private
193
+ */
194
+ async safeClearPageBrowsersCacheStorage(page) {
195
+ const result = await (0, safe_1.safe)(this.clearPageBrowsersCacheStorage(page));
196
+ if (result.success === false) {
197
+ this.cli.writeError(result.error);
127
198
  }
128
- await this.documentHandler.buildDocumentHandler();
129
- this.cache.clear();
130
199
  }
131
- async clearPageFunction(page) {
200
+ /**
201
+ * clear speed-kit-caches in browser
202
+ *
203
+ * @param page
204
+ * @private
205
+ */
206
+ async clearPageBrowsersCacheStorage(page) {
132
207
  const client = await page.target().createCDPSession();
133
208
  const cacheResponse = await client.send("CacheStorage.requestCacheNames", {
134
209
  securityOrigin: this.browserContext.domain,
135
210
  });
136
211
  for (const cache of cacheResponse.caches) {
137
212
  if (cache.cacheName.includes("baqend")) {
138
- await client.send("CacheStorage.deleteCache", {
213
+ await (0, safe_1.safe)((0, race_1.race)(client.send("CacheStorage.deleteCache", {
139
214
  cacheId: cache.cacheId,
140
- });
215
+ }), 20));
141
216
  }
142
217
  }
143
218
  }
144
- async clearServiceWorker(page) {
219
+ async unregisterServiceWorker(page) {
145
220
  const client = await page.target().createCDPSession();
146
221
  const currentOrigin = new URL(page.url()).origin;
147
222
  await client.send("ServiceWorker.enable");
@@ -149,5 +224,18 @@ class FileWatcher {
149
224
  scopeURL: currentOrigin,
150
225
  });
151
226
  }
227
+ updateStylesInlineFunction(styles) {
228
+ try {
229
+ const styleElement = document.querySelector("#speed-kit-df-styles");
230
+ if (styleElement) {
231
+ styleElement.textContent = decodeURI(styles);
232
+ styleElement.setAttribute("last-onboarding-live-update", new Date().toLocaleTimeString());
233
+ }
234
+ }
235
+ catch (error) {
236
+ console.error(error);
237
+ }
238
+ return true;
239
+ }
152
240
  }
153
241
  exports.FileWatcher = FileWatcher;
@@ -33,7 +33,7 @@ class Cache {
33
33
  }
34
34
  }
35
35
  clear() {
36
- this.cli.writeWarning("clear request-caches");
36
+ this.cli.writeSuccess("clear cached requests from local documentHandler");
37
37
  this.inMemoryCache.clear();
38
38
  }
39
39
  }
@@ -712,5 +712,5 @@
712
712
  ]
713
713
  }
714
714
  },
715
- "version": "2.65.1"
715
+ "version": "2.65.3"
716
716
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@speedkit/cli",
3
3
  "description": "Speed Kit CLI",
4
- "version": "2.65.1",
4
+ "version": "2.65.3",
5
5
  "author": {
6
6
  "name": "Baqend.com",
7
7
  "email": "info@baqend.com"