@travetto/manifest 3.0.0-rc.17 → 3.0.0-rc.18

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.
Files changed (3) hide show
  1. package/README.md +259 -4
  2. package/package.json +2 -2
  3. package/src/watch.ts +23 -6
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": "3.0.0-rc.17",
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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@travetto/manifest",
3
- "version": "3.0.0-rc.17",
4
- "description": "Manifest support",
3
+ "version": "3.0.0-rc.18",
4
+ "description": "Support for project indexing, manifesting, along with file watching",
5
5
  "keywords": [
6
6
  "path",
7
7
  "package",
package/src/watch.ts CHANGED
@@ -1,12 +1,6 @@
1
1
  import fs from 'fs/promises';
2
2
  import { path } from './path';
3
3
 
4
- export type WatchEvent = { action: 'create' | 'update' | 'delete', file: string };
5
-
6
- export type WatchEventListener = (ev: WatchEvent, folder: string) => void;
7
- type EventFilter = (ev: WatchEvent) => boolean;
8
- type WatchConfig = { filter?: EventFilter, ignore?: string[], createMissing?: boolean, includeHidden?: boolean };
9
-
10
4
  async function getWatcher(): Promise<typeof import('@parcel/watcher')> {
11
5
  try {
12
6
  return await import('@parcel/watcher');
@@ -16,6 +10,29 @@ async function getWatcher(): Promise<typeof import('@parcel/watcher')> {
16
10
  }
17
11
  }
18
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
+
19
36
  /**
20
37
  * Leverages @parcel/watcher to watch a series of folders
21
38
  * @param folders