@travetto/manifest 3.0.0-rc.8 → 3.0.0

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/README.md CHANGED
@@ -1,14 +1,269 @@
1
1
  <!-- This file was generated by @travetto/doc and should not be modified directly -->
2
2
  <!-- Please modify https://github.com/travetto/travetto/tree/main/module/manifest/DOC.ts and execute "npx trv doc" to rebuild -->
3
3
  # Manifest
4
- ## Manifest support
4
+ ## Support for project indexing, manifesting, along with file watching
5
5
 
6
6
  **Install: @travetto/manifest**
7
7
  ```bash
8
8
  npm install @travetto/manifest
9
+
10
+ # or
11
+
12
+ yarn add @travetto/manifest
13
+ ```
14
+
15
+ This module aims to be the boundary between the file system and the code. The module provides:
16
+
17
+
18
+ * Project Manifesting
19
+ * Manifest Delta
20
+ * Class and Function Metadata
21
+ * Runtime Indexing
22
+ * Path Normalization
23
+ * File Watching
24
+
25
+ ## Project Manifesting
26
+ The project manifest fulfills two main goals: Compile-time Support, and Runtime Knowledge of the project.
27
+
28
+ ### Compile-time Support
29
+ During the compilation process, the compiler needs to know every file that is eligible for compilation, when the file was last created/modified, and any specific patterns for interacting with a given file (e.g. transformers vs. testing code vs. support files that happen to share a common extension with code).
30
+
31
+ ### Runtime Knowledge
32
+ Additionally, once the code has been compiled (or even bundled after that), the executing process needs to know what files are available for loading, and any patterns necessary for knowing which files to load versus which ones to ignore. This allows for dynamic loading of modules/files without knowledge/access to the file system, and in a more performant manner.
33
+
34
+ ## Manifest Delta
35
+ During the compilation process, it is helpful to know how the output content differs from the manifest, which is produced from the source input. The [ManifestDeltaUtil](https://github.com/travetto/travetto/tree/main/module/manifest/src/delta.ts#L21) provides the functionality for a given manifest, and will produce a stream of changes grouped by module. This is the primary input into the [Compiler](https://github.com/travetto/travetto/tree/main/module/compiler#readme "The compiler infrastructure for the Travetto framework")'s incremental behavior to know when a file has changed and needs to be recompiled.
36
+
37
+ ## Class and Function Metadata
38
+
39
+ For the framework to work properly, metadata needs to be collected about files, classes and functions to uniquely identify them, with support for detecting changes during live reloads. To achieve this, every `class` is decorated with an additional field of `Ⲑid`. `Ⲑid` represents a computed id that is tied to the file/class combination.
40
+
41
+ `Ⲑid` is used heavily throughout the framework for determining which classes are owned by the framework, and being able to lookup the needed data from the [RootIndex](https://github.com/travetto/travetto/tree/main/module/manifest/src/root-index.ts) using the `getFunctionMetadata` method.
42
+
43
+ **Code: Test Class**
44
+ ```typescript
45
+ export class TestClass {
46
+ async doStuff(): Promise<void> { }
47
+ }
48
+ ```
49
+
50
+ **Code: Test Class Compiled**
51
+ ```javascript
52
+ "use strict";
53
+ Object.defineProperty(exports, "__esModule", { value: true });
54
+ exports.TestClass = void 0;
55
+ const tslib_1 = require("tslib");
56
+ const Ⲑ_root_index_1 = tslib_1.__importStar(require("@travetto/manifest/src/root-index.js"));
57
+ var ᚕf = "@travetto/manifest/doc/test-class.js";
58
+ class TestClass {
59
+ static Ⲑinit = Ⲑ_root_index_1.RootIndex.registerFunction(TestClass, ᚕf, 197152026, { doStuff: { hash: 51337554 } }, false, false);
60
+ async doStuff() { }
61
+ }
62
+ exports.TestClass = TestClass;
63
+ ```
64
+
65
+ **Terminal: Index Lookup at Runtime**
66
+ ```bash
67
+ $ trv main ./doc/lookup.ts
68
+
69
+ {
70
+ id: '@travetto/manifest:doc/test-class○TestClass',
71
+ source: './doc/test-class.ts',
72
+ hash: 197152026,
73
+ methods: { doStuff: { hash: 51337554 } },
74
+ abstract: false,
75
+ synthetic: false
76
+ }
77
+ ```
78
+
79
+ ## Module Indexing
80
+ Once the manifest is created, the application runtime can now read this manifest, which allows for influencing runtime behavior. The most common patterns include:
81
+
82
+ * Loading all source files
83
+ * Iterating over every test file
84
+ * Finding all source folders for watching
85
+ * Find all output folders for watching
86
+ * Determining if a module is available or not
87
+ * Resource scanning
88
+ * Providing contextual information when provided a filename, import name, etc (e.g. logging, testing output)
89
+
90
+ ## Path Normalization
91
+ By default, all paths within the framework are assumed to be in a POSIX style, and all input paths are converted to the POSIX style. This works appropriately within a Unix and a Windows environment. This module offers up [path](https://github.com/travetto/travetto/tree/main/module/manifest/src/path.ts#L7) as an equivalent to [Node](https://nodejs.org)'s [http](https://nodejs.org/api/path.html) library. This allows for consistent behavior across all file-interactions, and also allows for easy analysis if [Node](https://nodejs.org)'s [http](https://nodejs.org/api/path.html) library is ever imported.
92
+
93
+ ## File Watching
94
+ The module also leverages [fetch](https://www.npmjs.com/package/@parcel/watcher), to expose a single function of `watchFolders`. Only the [Compiler](https://github.com/travetto/travetto/tree/main/module/compiler#readme "The compiler infrastructure for the Travetto framework") module packages [fetch](https://www.npmjs.com/package/@parcel/watcher) as a direct dependency. This means, that in production, by default all watch operations will fail with a missing dependency.
95
+
96
+ **Code: Watch Folder Signature**
97
+ ```typescript
98
+ export type WatchEvent = { action: 'create' | 'update' | 'delete', file: string };
99
+ type EventFilter = (ev: WatchEvent) => boolean;
100
+
101
+ export type WatchEventListener = (ev: WatchEvent, folder: string) => void;
102
+ export type WatchConfig = {
103
+ /**
104
+ * Predicate for filtering events
105
+ */
106
+ filter?: EventFilter;
107
+ /**
108
+ * List of top level folders to ignore
109
+ */
110
+ ignore?: string[];
111
+ /**
112
+ * If watching a folder that doesn't exist, should it be created?
113
+ */
114
+ createMissing?: boolean;
115
+ /**
116
+ * Include files that start with '.'
117
+ */
118
+ includeHidden?: boolean;
119
+ };
120
+
121
+ /**
122
+ * Leverages @parcel/watcher to watch a series of folders
123
+ * @param folders
124
+ * @param onEvent
125
+ * @private
126
+ */
127
+ export async function watchFolders(
128
+ folders: string[] | [folder: string, targetFolder: string][],
129
+ onEvent: WatchEventListener,
130
+ config: WatchConfig = {}
131
+ ): Promise<() => Promise<void>> {
132
+ ```
133
+
134
+ This method allows for watching one or more folders, and registering a callback that will fire every time a file changes, and which of the registered folders it was triggered within. The return of the `watchFolders` is a cleanup method, that when invoked will remove and stop all watching behavior.
135
+
136
+ ## Anatomy of a Manifest
137
+
138
+ **Code: Manifest for @travetto/manifest**
139
+ ```typescript
140
+ {
141
+ "generated": 1868155200000,
142
+ "moduleType": "commonjs",
143
+ "mainModule": "@travetto/manifest",
144
+ "mainFolder": "module/manifest",
145
+ "workspacePath": "<generated>",
146
+ "monoRepo": true,
147
+ "outputFolder": ".trv_output",
148
+ "toolFolder": ".trv_build",
149
+ "compilerFolder": ".trv_compiler",
150
+ "packageManager": "npm",
151
+ "modules": {
152
+ "@travetto/manifest": {
153
+ "main": true,
154
+ "name": "@travetto/manifest",
155
+ "version": "x.x.x",
156
+ "local": true,
157
+ "internal": false,
158
+ "sourceFolder": "module/manifest",
159
+ "outputFolder": "node_modules/@travetto/manifest",
160
+ "files": {
161
+ "$root": [
162
+ [ "DOC.html", "unknown", 1868155200000 ],
163
+ [ "LICENSE", "unknown", 1868155200000 ],
164
+ [ "README.md", "md", 1868155200000 ]
165
+ ],
166
+ "doc": [
167
+ [ "DOC.ts", "ts", 1868155200000, "doc" ],
168
+ [ "doc/lookup.ts", "ts", 1868155200000, "doc" ],
169
+ [ "doc/test-class.ts", "ts", 1868155200000, "doc" ]
170
+ ],
171
+ "$index": [
172
+ [ "__index__.ts", "ts", 1868155200000 ]
173
+ ],
174
+ "$package": [
175
+ [ "package.json", "package-json", 1868155200000 ]
176
+ ],
177
+ "test": [
178
+ [ "test/path.ts", "ts", 1868155200000, "test" ],
179
+ [ "test/root-index.ts", "ts", 1868155200000, "test" ]
180
+ ],
181
+ "test/fixtures": [
182
+ [ "test/fixtures/simple.ts", "fixture", 1868155200000, "test" ]
183
+ ],
184
+ "$transformer": [
185
+ [ "support/transformer.function-metadata.ts", "ts", 1868155200000, "compile" ]
186
+ ],
187
+ "src": [
188
+ [ "src/delta.ts", "ts", 1868155200000 ],
189
+ [ "src/dependencies.ts", "ts", 1868155200000 ],
190
+ [ "src/manifest-index.ts", "ts", 1868155200000 ],
191
+ [ "src/module.ts", "ts", 1868155200000 ],
192
+ [ "src/package.ts", "ts", 1868155200000 ],
193
+ [ "src/path.ts", "ts", 1868155200000 ],
194
+ [ "src/root-index.ts", "ts", 1868155200000 ],
195
+ [ "src/types.ts", "ts", 1868155200000 ],
196
+ [ "src/typings.d.ts", "typings", 1868155200000 ],
197
+ [ "src/util.ts", "ts", 1868155200000 ],
198
+ [ "src/watch.ts", "ts", 1868155200000 ]
199
+ ],
200
+ "bin": [
201
+ [ "bin/context.d.ts", "typings", 1868155200000 ],
202
+ [ "bin/context.js", "js", 1868155200000 ]
203
+ ]
204
+ },
205
+ "profiles": [ "std" ],
206
+ "parents": []
207
+ }
208
+ }
209
+ }
9
210
  ```
10
211
 
11
- This module provides functionality for basic path functionality and common typings for manifests
212
+ ### General Context
213
+ The general context describes the project-space and any important information for how to build/execute the code.
214
+
215
+ The context contains:
216
+
217
+ * A generated timestamp
218
+ * Module Type: [commonjs](https://nodejs.org/api/modules.html) or [module](https://nodejs.org/api/esm.html)
219
+ * The main module to execute. **Note**: This primarily pertains to mono-repo support when there are multiple modules in the project
220
+ * The root path of the project/workspace
221
+ * Whether or not the project is a mono-repo. **Note**: This is determined by using the 'workspaces' field in your [object Object]
222
+ * The location where all compiled code will be stored. Defaults to: `.trv_output`. **Note**: Can be overridden in your [object Object] in 'travetto.outputFolder'
223
+ * The location where the intermediate compiler will be created. Defaults to: `.trv_compiler`
224
+ * The location where tooling will be able to write to. Defaults to: `.trv_output`
225
+ * Which package manager is in use [Npm](https://docs.npmjs.com/downloading-and-installing-node-js-and-npm) or [Yarn](https://yarnpg.com)
226
+
227
+ ### Modules
228
+ The modules represent all of the [Travetto](https://travetto.dev)-aware dependencies (including dev dependencies) used for compiling, testing and executing. A prod-only version is produced when packaging the final output.
12
229
 
13
- ### Module Indexing
14
- The bootstrap process will also produce an index of all source files, which allows for fast in-memory scanning. This allows for all the automatic discovery that is used within the framework.
230
+ Each module contains:
231
+
232
+ * The dependency npm name
233
+ * The dependency version
234
+ * A flag to determine if its a local module
235
+ * A flag to determine if the module is public (could be published to npm)
236
+ * The path to the source folder, relative to the workspace root
237
+ * The path to the output folder, relative to the workspace output root
238
+ * The list of all files
239
+ * The profiles a module applies to. Values are std, test, compile, doc. Any empty value implies std
240
+ * Parent modules that imported this module
241
+
242
+ ### Module Files
243
+ The module files are a simple categorization of files into a predetermined set of folders:
244
+
245
+ * $root - All uncategorized files at the module root
246
+ * $index - __index__.ts, index.ts files at the root of the project
247
+ * $package - The [Package JSON](https://docs.npmjs.com/cli/v9/configuring-npm/package-json) for the project
248
+ * src - Code that should be automatically loaded at runtime. All .ts files under the src/ folder
249
+ * test - Code that contains test files. All .ts files under the test/ folder
250
+ * test/fixtures - Test resource files, pertains to the main module only. Located under test/fixtures/
251
+ * resources - Packaged resource, meant to pertain to the main module only. Files, under resources/
252
+ * support - All .ts files under the support/ folder
253
+ * support/resources - Packaged resource files, meant to be included by other modules, under support/resources/
254
+ * support/fixtures - Test resources meant to shared across modules. Under support/fixtures/
255
+ * doc - Documentation files. All .ts files under the doc/ folder
256
+ * $transformer - All .ts files under the pattern support/transform*. These are used during compilation and never at runtime
257
+ * bin - Entry point .js files. All .js files under the bin/ folder
258
+
259
+ Within each file there is a pattern of either a 3 or 4 element array:
260
+
261
+ **Code: Sample file**
262
+ ```typescript
263
+ [
264
+ "test/path.ts", // The module relative source path
265
+ "ts", // The file type ts, js, package-json, typings, md, json, unknown
266
+ 1676751649201.1897, // Stat timestamp
267
+ "test" // Optional profile
268
+ ]
269
+ ```
package/bin/context.js CHANGED
@@ -101,15 +101,19 @@ export async function getManifestContext(folder) {
101
101
 
102
102
  const moduleType = (await $getPkg(workspacePath)).type ?? 'commonjs';
103
103
  const mainFolder = mainPath === workspacePath ? '' : mainPath.replace(`${workspacePath}/`, '');
104
+ /** @type {'yarn'|'npm'} */
105
+ const packageManager = await fs.stat(path.resolve(workspacePath, 'yarn.lock')).then(() => 'yarn', () => 'npm');
104
106
 
105
107
  const res = {
106
108
  moduleType,
107
- mainModule,
109
+ mainModule: mainModule ?? 'untitled', // When root package.json is missing a name
108
110
  mainFolder,
109
111
  workspacePath,
110
112
  monoRepo,
111
113
  outputFolder,
112
- compilerFolder: '.trv_compiler'
114
+ toolFolder: '.trv_build',
115
+ compilerFolder: '.trv_compiler',
116
+ packageManager
113
117
  };
114
118
  return res;
115
119
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@travetto/manifest",
3
- "version": "3.0.0-rc.8",
4
- "description": "Manifest support",
3
+ "version": "3.0.0",
4
+ "description": "Support for project indexing, manifesting, along with file watching",
5
5
  "keywords": [
6
6
  "path",
7
7
  "package",
@@ -99,11 +99,11 @@ export class ManifestIndex {
99
99
  this.#importToEntry.clear();
100
100
  this.#sourceToEntry.clear();
101
101
 
102
- this.#modules = Object.values(this.manifest.modules)
102
+ this.#modules = Object.values(this.#manifest.modules)
103
103
  .map(m => ({
104
104
  ...m,
105
105
  outputPath: this.#resolveOutput(m.outputFolder),
106
- sourcePath: path.resolve(this.manifest.workspacePath, m.sourceFolder),
106
+ sourcePath: path.resolve(this.#manifest.workspacePath, m.sourceFolder),
107
107
  // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
108
108
  files: Object.fromEntries(
109
109
  Object.entries(m.files).map(([folder, files]) => [folder, this.#moduleFiles(m, files ?? [])])
@@ -200,7 +200,7 @@ export class ManifestIndex {
200
200
  * Is module installed?
201
201
  */
202
202
  hasModule(name: string): boolean {
203
- return name in this.manifest.modules;
203
+ return name in this.#manifest.modules;
204
204
  }
205
205
 
206
206
  /**
@@ -261,7 +261,7 @@ export class ManifestIndex {
261
261
  * Build module list from an expression list (e.g. `@travetto/app,-@travetto/log)
262
262
  */
263
263
  getModuleList(mode: 'local' | 'all', exprList: string = ''): Set<string> {
264
- const allMods = Object.keys(this.manifest.modules);
264
+ const allMods = Object.keys(this.#manifest.modules);
265
265
  const active = new Set<string>(
266
266
  mode === 'local' ? this.getLocalModules().map(x => x.name) :
267
267
  (mode === 'all' ? allMods : [])
@@ -297,4 +297,23 @@ export class ManifestIndex {
297
297
  }
298
298
  return out;
299
299
  }
300
+
301
+ /**
302
+ * Get local folders that represent the user's controlled input
303
+ */
304
+ getLocalInputFolderMapping(): [folder: string, moduleSourceRoot: string][] {
305
+ return this.getLocalModules().flatMap(x =>
306
+ ((!this.manifest.monoRepo || x.sourcePath !== this.manifest.workspacePath) ?
307
+ [x.sourcePath] : [...Object.keys(x.files)].filter(y => !y.startsWith('$')).map(y => path.resolve(this.#manifest.workspacePath, x.sourcePath, y))
308
+ // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
309
+ ).map(f => [f, path.resolve(this.#manifest.workspacePath, x.sourceFolder)] as [string, string])
310
+ );
311
+ }
312
+
313
+ /**
314
+ * Get local output folders
315
+ */
316
+ getLocalOutputFolders(): string[] {
317
+ return this.getLocalModules().map(x => x.outputPath);
318
+ }
300
319
  }
package/src/package.ts CHANGED
@@ -13,6 +13,14 @@ export class PackageUtil {
13
13
  static #cache: Record<string, Package> = {};
14
14
  static #workspaces: Record<string, PackageWorkspaceEntry[]> = {};
15
15
 
16
+ /**
17
+ * Clear out cached package file reads
18
+ */
19
+ static clearCache(): void {
20
+ this.#cache = {};
21
+ this.#workspaces = {};
22
+ }
23
+
16
24
  static resolveImport = (library: string): string => this.#req.resolve(library);
17
25
 
18
26
  /**
@@ -83,10 +91,14 @@ export class PackageUtil {
83
91
  if (forceRead) {
84
92
  delete this.#cache[modulePath];
85
93
  }
86
- return this.#cache[modulePath] ??= JSON.parse(readFileSync(
94
+ const res = this.#cache[modulePath] ??= JSON.parse(readFileSync(
87
95
  modulePath.endsWith('.json') ? modulePath : path.resolve(modulePath, 'package.json'),
88
96
  'utf8'
89
97
  ));
98
+
99
+ res.name ??= 'untitled'; // If a package.json (root-only) is missing a name, allows for npx execution
100
+
101
+ return res;
90
102
  }
91
103
 
92
104
  /**
@@ -171,9 +183,24 @@ export class PackageUtil {
171
183
  try {
172
184
  return JSON.parse(await fs.readFile(cache, 'utf8'));
173
185
  } catch {
174
- const text = execSync('npm query .workspace', { cwd: rootPath, encoding: 'utf8', env: {} });
175
- const res: { location: string, name: string }[] = JSON.parse(text);
176
- const out = this.#workspaces[rootPath] = res.map(d => ({ sourcePath: d.location, name: d.name }));
186
+ let out: PackageWorkspaceEntry[];
187
+ switch (ctx.packageManager) {
188
+ case 'npm': {
189
+ const text = execSync('npm query .workspace', { cwd: rootPath, encoding: 'utf8', env: { PATH: process.env.PATH, NODE_PATH: process.env.NODE_PATH } });
190
+ out = JSON.parse(text)
191
+ .map((d: { location: string, name: string }) => ({ sourcePath: d.location, name: d.name }));
192
+ break;
193
+ }
194
+ case 'yarn': {
195
+ const text = execSync('yarn -s workspaces info', { cwd: rootPath, encoding: 'utf8', env: { PATH: process.env.PATH, NODE_PATH: process.env.NODE_PATH } });
196
+ out = Object.entries<{ location: string }>(JSON.parse(text))
197
+ .map(([name, { location }]) => ({ sourcePath: location, name }));
198
+ break;
199
+ }
200
+ }
201
+
202
+ this.#workspaces[rootPath] = out;
203
+
177
204
  await fs.writeFile(cache, JSON.stringify(out), 'utf8');
178
205
  }
179
206
  }
package/src/root-index.ts CHANGED
@@ -118,24 +118,6 @@ class $RootIndex extends ManifestIndex {
118
118
  const id = clsId === undefined ? '' : typeof clsId === 'string' ? clsId : clsId.Ⲑid;
119
119
  return this.#metadata.get(id);
120
120
  }
121
-
122
- /**
123
- * Get local folders that represent the user's controlled input
124
- */
125
- getLocalInputFolders(): string[] {
126
- return this.getLocalModules()
127
- .flatMap(x =>
128
- (!this.manifest.monoRepo || x.sourcePath !== this.manifest.workspacePath) ?
129
- [x.sourcePath] : [...Object.keys(x.files)].filter(y => !y.startsWith('$')).map(y => path.resolve(x.sourcePath, y))
130
- );
131
- }
132
-
133
- /**
134
- * Get local output folders
135
- */
136
- getLocalOutputFolders(): string[] {
137
- return this.getLocalModules().map(x => x.outputPath);
138
- }
139
121
  }
140
122
 
141
123
  let index: $RootIndex | undefined;
package/src/types.ts CHANGED
@@ -30,9 +30,11 @@ export type ManifestContext = {
30
30
  mainFolder: string;
31
31
  workspacePath: string;
32
32
  outputFolder: string;
33
+ toolFolder: string;
33
34
  compilerFolder: string;
34
35
  monoRepo?: boolean;
35
36
  moduleType: 'module' | 'commonjs';
37
+ packageManager: 'yarn' | 'npm';
36
38
  };
37
39
 
38
40
  export type ManifestRoot = ManifestContext & {
package/src/util.ts CHANGED
@@ -6,18 +6,20 @@ import { path } from './path';
6
6
  import { ManifestContext, ManifestRoot } from './types';
7
7
  import { ManifestModuleUtil } from './module';
8
8
 
9
- export const MANIFEST_FILE = 'manifest.json';
9
+ const MANIFEST_FILE = 'manifest.json';
10
10
 
11
11
  /**
12
12
  * Manifest utils
13
13
  */
14
14
  export class ManifestUtil {
15
-
16
- static async writeJsonWithBuffer(ctx: ManifestContext, filename: string, obj: object): Promise<string> {
17
- const tempName = path.resolve(ctx.workspacePath, ctx.mainFolder, filename).replace(/[\/\\: ]/g, '_');
15
+ /**
16
+ * Write file and copy over when ready
17
+ */
18
+ static async #writeJsonWithBuffer(ctx: ManifestContext, filename: string, obj: object): Promise<string> {
19
+ const tempName = `manifest.${process.ppid}.${process.pid}.json.${Date.now()}`;
18
20
  const file = path.resolve(ctx.workspacePath, ctx.outputFolder, 'node_modules', ctx.mainModule, filename);
19
21
  await fs.mkdir(path.dirname(file), { recursive: true });
20
- const temp = path.resolve(os.tmpdir(), `${tempName}.${Date.now()}`);
22
+ const temp = path.resolve(os.tmpdir(), tempName);
21
23
  await fs.writeFile(temp, JSON.stringify(obj), 'utf8');
22
24
  await fs.copyFile(temp, file);
23
25
  fs.unlink(temp);
@@ -44,6 +46,24 @@ export class ManifestUtil {
44
46
  };
45
47
  }
46
48
 
49
+ /**
50
+ * Produce a production manifest from a given manifest
51
+ */
52
+ static createProductionManifest(manifest: ManifestRoot): ManifestRoot {
53
+ return {
54
+ ...manifest,
55
+ // If in prod mode, only include std modules
56
+ modules: Object.fromEntries(
57
+ Object.values(manifest.modules)
58
+ .filter(x => x.profiles.includes('std'))
59
+ .map(m => [m.name, m])
60
+ ),
61
+ // Mark output folder/workspace path as portable
62
+ outputFolder: '',
63
+ workspacePath: '',
64
+ };
65
+ }
66
+
47
67
  /**
48
68
  * Read manifest, synchronously
49
69
  *
@@ -67,7 +87,21 @@ export class ManifestUtil {
67
87
  * Write manifest for a given context, return location
68
88
  */
69
89
  static writeManifest(ctx: ManifestContext, manifest: ManifestRoot): Promise<string> {
70
- return this.writeJsonWithBuffer(ctx, MANIFEST_FILE, manifest);
90
+ return this.#writeJsonWithBuffer(ctx, MANIFEST_FILE, manifest);
91
+ }
92
+
93
+ /**
94
+ * Write a manifest to a specific file, if no file extension provided, the file is assumed to be a folder
95
+ */
96
+ static async writeManifestToFile(location: string, manifest: ManifestRoot): Promise<string> {
97
+ if (!location.endsWith('.json')) {
98
+ location = path.resolve(location, MANIFEST_FILE);
99
+ }
100
+
101
+ await fs.mkdir(path.dirname(location), { recursive: true });
102
+ await fs.writeFile(location, JSON.stringify(manifest), 'utf8');
103
+
104
+ return location;
71
105
  }
72
106
 
73
107
  /**
package/src/watch.ts CHANGED
@@ -1,10 +1,5 @@
1
1
  import fs from 'fs/promises';
2
-
3
- export type WatchEvent = { action: 'create' | 'update' | 'delete', file: string };
4
-
5
- type EventListener = (ev: WatchEvent, folder: string) => void;
6
- type EventFilter = (ev: WatchEvent) => boolean;
7
- type WatchConfig = { filter?: EventFilter, ignore?: string[] };
2
+ import { path } from './path';
8
3
 
9
4
  async function getWatcher(): Promise<typeof import('@parcel/watcher')> {
10
5
  try {
@@ -15,22 +10,61 @@ async function getWatcher(): Promise<typeof import('@parcel/watcher')> {
15
10
  }
16
11
  }
17
12
 
13
+ export type WatchEvent = { action: 'create' | 'update' | 'delete', file: string };
14
+ type EventFilter = (ev: WatchEvent) => boolean;
15
+
16
+ export type WatchEventListener = (ev: WatchEvent, folder: string) => void;
17
+ export type WatchConfig = {
18
+ /**
19
+ * Predicate for filtering events
20
+ */
21
+ filter?: EventFilter;
22
+ /**
23
+ * List of top level folders to ignore
24
+ */
25
+ ignore?: string[];
26
+ /**
27
+ * If watching a folder that doesn't exist, should it be created?
28
+ */
29
+ createMissing?: boolean;
30
+ /**
31
+ * Include files that start with '.'
32
+ */
33
+ includeHidden?: boolean;
34
+ };
35
+
18
36
  /**
19
37
  * Leverages @parcel/watcher to watch a series of folders
20
38
  * @param folders
21
39
  * @param onEvent
22
40
  * @private
23
41
  */
24
- export async function watchFolders(folders: string[], onEvent: EventListener, config: WatchConfig = {}): Promise<() => Promise<void>> {
42
+ export async function watchFolders(
43
+ folders: string[] | [folder: string, targetFolder: string][],
44
+ onEvent: WatchEventListener,
45
+ config: WatchConfig = {}
46
+ ): Promise<() => Promise<void>> {
25
47
  const lib = await getWatcher();
26
- const subs = await Promise.all(folders.map(async folder => {
27
- if (await fs.stat(folder).then(() => true, () => false)) {
48
+ const createMissing = config.createMissing ?? false;
49
+ const validFolders = new Set(folders.map(x => typeof x === 'string' ? x : x[0]));
50
+
51
+ const subs = await Promise.all(folders.map(async value => {
52
+ const folder = typeof value === 'string' ? value : value[0];
53
+ const targetFolder = typeof value === 'string' ? value : value[1];
54
+
55
+ if (await fs.stat(folder).then(() => true, () => createMissing)) {
56
+ await fs.mkdir(folder, { recursive: true });
28
57
  const ignore = (await fs.readdir(folder)).filter(x => x.startsWith('.') && x.length > 2);
29
58
  return lib.subscribe(folder, (err, events) => {
30
59
  for (const ev of events) {
31
- const finalEv = { action: ev.type, file: ev.path };
32
- if (!config.filter || config.filter(finalEv)) {
33
- onEvent(finalEv, folder);
60
+ const finalEv = { action: ev.type, file: path.toPosix(ev.path) };
61
+ if (ev.type === 'delete' && validFolders.has(finalEv.file)) {
62
+ return process.exit(0); // Exit when watched folder is removed
63
+ }
64
+ const isHidden = !config.includeHidden && finalEv.file.replace(targetFolder, '').includes('/.');
65
+ const matches = !isHidden && (!config.filter || config.filter(finalEv));
66
+ if (matches) {
67
+ onEvent(finalEv, targetFolder);
34
68
  }
35
69
  }
36
70
  }, { ignore: [...ignore, ...config.ignore ?? []] });