@ms-cloudpack/file-watcher 0.0.2

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 ADDED
@@ -0,0 +1,34 @@
1
+ # @ms-cloudpack/file-watcher
2
+
3
+ An abstraction on the file watching capabilities used in Cloudpack.
4
+
5
+ ## Example usage
6
+
7
+ 1. Create a watcher:
8
+
9
+ ```ts
10
+ const watcher = createWatcher();
11
+ ```
12
+
13
+ 2. Subscribe to a particular package path using `watch`. (Returns an `unwatch` function.)
14
+
15
+ ```ts
16
+ const unwatch = watcher.watch({ path: 'path/to/package' }, () => console.log(`package changed`));
17
+ ```
18
+
19
+ 3. To dispose, call the returned `unwatch`, or call `watcher.unwatchAll` to unsubscribe from all watchers.
20
+
21
+ ```ts
22
+ // Dispose an individual watcher.
23
+ await unwatch();
24
+
25
+ // Dispose all watchers.
26
+ await watcher.unwatchAll();
27
+ ```
28
+
29
+ ### `watcher.watch` options
30
+
31
+ | Name | Type | Description |
32
+ | -------------- | -------- | ----------------------------------------------------------------------------------------- |
33
+ | `path` | string | The absolute base path to be watched. |
34
+ | `watchedPaths` | string[] | Relative paths from the base path to watch. Defaults to `package.json` and `src/` folder. |
@@ -0,0 +1,12 @@
1
+ /**
2
+ * Creates a watcher which can watch a package and notify the client when it changes.
3
+ */
4
+ export declare function createWatcher(): {
5
+ watch({ path, watchPaths, }: {
6
+ path: string;
7
+ watchPaths?: string[] | undefined;
8
+ }, onPackageChanged: () => void): () => Promise<void>;
9
+ unwatchAll(): Promise<void>;
10
+ };
11
+ export type Watcher = ReturnType<typeof createWatcher>;
12
+ //# sourceMappingURL=createWatcher.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"createWatcher.d.ts","sourceRoot":"","sources":["../src/createWatcher.ts"],"names":[],"mappings":"AAUA;;GAEG;AACH,wBAAgB,aAAa;;cAUf,MAAM;;yBAGI,MAAM,IAAI;;EAuCjC;AAED,MAAM,MAAM,OAAO,GAAG,UAAU,CAAC,OAAO,aAAa,CAAC,CAAC"}
@@ -0,0 +1,46 @@
1
+ import chokidar from 'chokidar';
2
+ import { resolve } from 'path';
3
+ // TODO: This list of watched paths is hardcoded, but really it shouldn't be. In fact, this whole file
4
+ // probably shouldn't be here. I think lage should be responsible for understanding what "bundle" means
5
+ // (we can provide a definition) and what its inputs are and what it depends on. Then when we call
6
+ // ensurePackageBundled, lage can ensure watching is happening for the full graph of tasks.
7
+ const defaultWatchPaths = ['./package.json', './src'];
8
+ /**
9
+ * Creates a watcher which can watch a package and notify the client when it changes.
10
+ */
11
+ export function createWatcher() {
12
+ const watchedPaths = new Set();
13
+ const watchers = new Set();
14
+ return {
15
+ watch({ path, watchPaths = defaultWatchPaths, }, onPackageChanged) {
16
+ const paths = watchPaths.map((relPath) => resolve(path, relPath)).filter((p) => !watchedPaths.has(p));
17
+ let watcher;
18
+ if (paths.length) {
19
+ // Add paths to watchedPaths to avoid watching them again.
20
+ paths.forEach((p) => watchedPaths.add(p));
21
+ // Watch source and register a handler which will rebundle the changed package and notify the client on
22
+ // success. Note that if failures happen, the client will receive the error notification automatically.
23
+ watcher = chokidar.watch(paths).on('change', onPackageChanged);
24
+ watchers.add(watcher);
25
+ }
26
+ // Return a dispose function to individually clean up.
27
+ return async () => {
28
+ if (watcher) {
29
+ const w = watcher;
30
+ watcher = undefined;
31
+ watchers.delete(w);
32
+ for (const watchedPath of paths) {
33
+ watchedPaths.delete(watchedPath);
34
+ }
35
+ await w.close();
36
+ }
37
+ };
38
+ },
39
+ async unwatchAll() {
40
+ await Promise.all(Array.from(watchers).map((w) => w.close()));
41
+ watchers.clear();
42
+ watchedPaths.clear();
43
+ },
44
+ };
45
+ }
46
+ //# sourceMappingURL=createWatcher.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"createWatcher.js","sourceRoot":"","sources":["../src/createWatcher.ts"],"names":[],"mappings":"AACA,OAAO,QAAQ,MAAM,UAAU,CAAC;AAChC,OAAO,EAAE,OA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CAAC,MAAM,CAAC,WAAW,CAAC,CAAC;qBAClC;oBAED,MAAM,CAAC,CAAC,KAAK,EAAE,CAAC;iBACjB;YACH,CAAC,CAAC;QACJ,CAAC;QAED,KAAK,CAAC,UAAU;YACd,MAAM,OAAO,CAAC,GAAG,CAAC,KAAK,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,KAAK,EAAE,CAAC,CAAC,CAAC;YAC9D,QAAQ,CAAC,KAAK,EAAE,CAAC;YACjB,YAAY,CAAC,KAAK,EAAE,CAAC;QACvB,CAAC;KACF,CAAC;AACJ,CAAC","sourcesContent":["import type { FSWatcher } from 'chokidar';\nimport chokidar from 'chokidar';\nimport { resolve } from 'path';\n\n// TODO: This list of watched paths is hardcoded, but really it shouldn't be. In fact, this whole file\n// probably shouldn't be here. I think lage should be responsible for understanding what \"bundle\" means\n// (we can provide a definition) and what its inputs are and what it depends on. Then when we call\n// ensurePackageBundled, lage can ensure watching is happening for the full graph of tasks.\nconst defaultWatchPaths = ['./package.json', './src'];\n\n/**\n * Creates a watcher which can watch a package and notify the client when it changes.\n */\nexport function createWatcher() {\n const watchedPaths = new Set<string>();\n const watchers = new Set<FSWatcher>();\n\n return {\n watch(\n {\n path,\n watchPaths = defaultWatchPaths,\n }: {\n path: string;\n watchPaths?: string[];\n },\n onPackageChanged: () => void,\n ) {\n const paths = watchPaths.map((relPath) => resolve(path, relPath)).filter((p) => !watchedPaths.has(p));\n let watcher: FSWatcher | undefined;\n\n if (paths.length) {\n // Add paths to watchedPaths to avoid watching them again.\n paths.forEach((p) => watchedPaths.add(p));\n\n // Watch source and register a handler which will rebundle the changed package and notify the client on\n // success. Note that if failures happen, the client will receive the error notification automatically.\n watcher = chokidar.watch(paths).on('change', onPackageChanged);\n\n watchers.add(watcher);\n }\n\n // Return a dispose function to individually clean up.\n return async () => {\n if (watcher) {\n const w = watcher;\n\n watcher = undefined;\n watchers.delete(w);\n\n for (const watchedPath of paths) {\n watchedPaths.delete(watchedPath);\n }\n\n await w.close();\n }\n };\n },\n\n async unwatchAll() {\n await Promise.all(Array.from(watchers).map((w) => w.close()));\n watchers.clear();\n watchedPaths.clear();\n },\n };\n}\n\nexport type Watcher = ReturnType<typeof createWatcher>;\n"]}
package/lib/index.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ export { createWatcher } from './createWatcher.js';
2
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC"}
package/lib/index.js ADDED
@@ -0,0 +1,2 @@
1
+ export { createWatcher } from './createWatcher.js';
2
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAC","sourcesContent":["export { createWatcher } from './createWatcher.js';\n"]}
@@ -0,0 +1,11 @@
1
+ // This file is read by tools that parse documentation comments conforming to the TSDoc standard.
2
+ // It should be published with your NPM package. It should not be tracked by Git.
3
+ {
4
+ "tsdocVersion": "0.12",
5
+ "toolPackages": [
6
+ {
7
+ "packageName": "@microsoft/api-extractor",
8
+ "packageVersion": "7.36.2"
9
+ }
10
+ ]
11
+ }
package/package.json ADDED
@@ -0,0 +1,38 @@
1
+ {
2
+ "name": "@ms-cloudpack/file-watcher",
3
+ "version": "0.0.2",
4
+ "description": "A file watcher abstraction for use with Cloudpack.",
5
+ "license": "MIT",
6
+ "type": "module",
7
+ "types": "./lib/index.d.ts",
8
+ "sideEffects": false,
9
+ "exports": {
10
+ ".": {
11
+ "types": "./lib/index.d.ts",
12
+ "require": "./lib-commonjs/index.cjs",
13
+ "import": "./lib/index.js"
14
+ }
15
+ },
16
+ "dependencies": {
17
+ "chokidar": "^3.5.3"
18
+ },
19
+ "devDependencies": {
20
+ "@ms-cloudpack/eslint-plugin-internal": "*",
21
+ "@ms-cloudpack/scripts": "*"
22
+ },
23
+ "scripts": {
24
+ "api": "cloudpack-scripts api",
25
+ "build:watch": "cloudpack-scripts build-watch",
26
+ "build": "cloudpack-scripts build",
27
+ "lint:update": "cloudpack-scripts lint-update",
28
+ "lint": "cloudpack-scripts lint",
29
+ "start": "node ./lib/test.js",
30
+ "test:update": "cloudpack-scripts test-update",
31
+ "test:watch": "cloudpack-scripts test-watch",
32
+ "test": "cloudpack-scripts test"
33
+ },
34
+ "files": [
35
+ "lib/**/!(*.test.*)",
36
+ "lib-commonjs/**/!(*.test.*)"
37
+ ]
38
+ }