@polylith/builder 0.2.31 → 1.0.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/Files.js CHANGED
@@ -2,6 +2,7 @@ import fg from 'fast-glob';
2
2
  import path from 'node:path/posix'
3
3
  import {ensureDir} from 'fs-extra';
4
4
  import { copyFile } from 'node:fs/promises';
5
+ import cc from "@ondohers/console-colors";
5
6
  import App from './App.js';
6
7
  import {forceToPosix} from './utils.js'
7
8
  import './types.js'
@@ -148,6 +149,64 @@ export default class Files {
148
149
  return folders;
149
150
  }
150
151
 
152
+ /**
153
+ * Call this method to clear the cached file discovery results. Use this
154
+ * before rebuilding the resource map after files are added or removed.
155
+ */
156
+ resetFiles() {
157
+ this.files = {};
158
+ this.filesFound = false;
159
+ }
160
+
161
+ /**
162
+ * Call this method to refresh the discovered resource files from the current
163
+ * copy specs so new or removed files are reflected in the file map.
164
+ *
165
+ * @returns {Promise<CopyInfoList>} the refreshed copy info list
166
+ */
167
+ async refreshFiles() {
168
+ this.resetFiles();
169
+ return await this.findAllFiles();
170
+ }
171
+
172
+ /**
173
+ * Call this method to register one file in the resource map if it matches
174
+ * one of the configured copy specs.
175
+ *
176
+ * @param {String} filename - the full path of the candidate file.
177
+ * @returns {Promise<Boolean>} true if the file matched a resource spec.
178
+ */
179
+ async addFileIfMatched(filename) {
180
+ var posixFilename = forceToPosix(filename);
181
+
182
+ for (let spec of this.specs) {
183
+ let searchRoot = path.join(this.src, spec.root, spec.cwd);
184
+ let posixSearchRoot = forceToPosix(searchRoot);
185
+ let isInSearchRoot = posixFilename.indexOf(`${posixSearchRoot}/`) === 0;
186
+
187
+ if (!isInSearchRoot) continue;
188
+
189
+ let matchedFiles = await fg(spec.glob, {
190
+ cwd: searchRoot,
191
+ ignore: ['**/node_modules'],
192
+ absolute: true,
193
+ onlyFiles: true,
194
+ unique: true,
195
+ dot: true,
196
+ });
197
+ let normalizedMatches = matchedFiles.map(function(oneFile) {
198
+ return forceToPosix(oneFile);
199
+ });
200
+
201
+ if (!normalizedMatches.includes(posixFilename)) continue;
202
+
203
+ this.addFiles(searchRoot, [posixFilename], spec);
204
+ return true;
205
+ }
206
+
207
+ return false;
208
+ }
209
+
151
210
 
152
211
  /**
153
212
  * Call this method to copy a single file into its destination location
@@ -160,7 +219,8 @@ export default class Files {
160
219
  var destination = forTest ? this.files[file] && this.files[file].testDestFilename : this.files[file] && this.files[file].destFilename;
161
220
 
162
221
  if (destination && updated) {
163
- console.log(`file ${file} updated, copied to ${destination}`);
222
+ console.log(`${cc.set('fg_green', 'copy')} of file ${cc.set('fg_white', file)} to ${cc.set('fg_white', destination)}`);
223
+ await ensureDir(path.dirname(destination));
164
224
  await copyFile(file, destination);
165
225
  }
166
226
  }
@@ -229,6 +289,7 @@ export default class Files {
229
289
  try {
230
290
  await ensureDir(destFilePath);
231
291
  await copyFile(srcFilename, destFilename);
292
+ console.log(`${cc.set('fg_green', 'copy')} of file ${cc.set('fg_white', srcFilename)} to ${cc.set('fg_white', destFilename)}`);
232
293
  } catch (e) {
233
294
  console.error(`Error copying file ${srcFilename} to ${destFilename}`);
234
295
  throw e;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@polylith/builder",
3
- "version": "0.2.31",
3
+ "version": "1.0.3",
4
4
  "description": "The polylith builder",
5
5
  "main": "index.js",
6
6
  "type": "module",
@@ -19,8 +19,8 @@
19
19
  "@babel/preset-env": "^7.16.11",
20
20
  "@babel/preset-react": "^7.22.15",
21
21
  "@ondohers/console-colors": "^0.1.1",
22
- "@polylith/config-store": "0.1.2",
23
- "@polylith/core": "0.1.14",
22
+ "@polylith/config-store": "1.0.3",
23
+ "@polylith/core": "1.0.3",
24
24
  "@rollup/plugin-babel": "^5.3.0",
25
25
  "@rollup/plugin-commonjs": "^24.0.0",
26
26
  "@rollup/plugin-json": "^5.0.2",
@@ -1,4 +1,23 @@
1
- var copied= {}
1
+ import { forceToPosix } from './utils.js';
2
+
3
+ var copied = {}
4
+
5
+ /**
6
+ * Call this method to test whether the changed file is inside one of the
7
+ * resource folders watched by the copy plugin.
8
+ *
9
+ * @param {String} file the changed filename from rollup
10
+ * @param {Array.<String>} folders the resource folders registered for watch
11
+ * @returns {Boolean} true if the file belongs to a watched resource folder
12
+ */
13
+ function isWatchedResourceFile(file, folders) {
14
+ var posixFile = forceToPosix(file);
15
+
16
+ return folders.some(function(folder) {
17
+ var posixFolder = forceToPosix(folder);
18
+ return posixFile === posixFolder || posixFile.indexOf(`${posixFolder}/`) === 0;
19
+ });
20
+ }
2
21
 
3
22
  /**
4
23
  *
@@ -7,18 +26,32 @@ var copied= {}
7
26
  * @returns {Object} the plugin
8
27
  */
9
28
  export default function(name, files, forTest) {
29
+ var folders = [];
30
+
10
31
  return {
11
32
  name: "copy-resources",
12
33
 
13
34
  buildStart() {
14
- var folders = files.getAllFolders();
35
+ folders = files.getAllFolders();
15
36
  folders.forEach(function(name) {
16
37
  this.addWatchFile(name);
17
38
  }, this);
18
39
  },
19
40
 
20
- watchChange(file, event) {
21
- files.copyOneFile(file, true);
41
+ async watchChange(file, event) {
42
+ if (!isWatchedResourceFile(file, folders)) return;
43
+
44
+ if (event.event === 'create') {
45
+ let wasAdded = await files.addFileIfMatched(file);
46
+ if (wasAdded) {
47
+ await files.copyOneFile(file, true, forTest);
48
+ }
49
+ return;
50
+ }
51
+
52
+ if (event.event === 'delete') return;
53
+
54
+ await files.copyOneFile(file, true, forTest);
22
55
  },
23
56
 
24
57
  async generateBundle(outputOptions, bundleInfo) {