generated from MetaMask/metamask-module-template
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathwatch.ts
More file actions
100 lines (88 loc) · 3.01 KB
/
watch.ts
File metadata and controls
100 lines (88 loc) · 3.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
import { makePromiseKit } from '@endo/promise-kit';
import { Logger } from '@ocap/logger';
import { watch } from 'chokidar';
import type { FSWatcher, MatchFunction } from 'chokidar';
import { unlink } from 'fs/promises';
import { resolve } from 'path';
import { bundleFile } from './bundle.ts';
import { resolveBundlePath } from '../path.ts';
type CloseWatcher = () => Promise<void>;
type WatchDirReturn = {
ready: Promise<CloseWatcher>;
error: Promise<never>;
};
export const makeWatchEvents = (
watcher: FSWatcher,
readyResolve: ReturnType<typeof makePromiseKit<CloseWatcher>>['resolve'],
throwError: ReturnType<typeof makePromiseKit<never>>['reject'],
logger: Logger,
): {
ready: () => void;
add: (path: string) => void;
change: (path: string) => void;
unlink: (path: string) => void;
error: (error: Error) => void;
} => ({
ready: () => readyResolve(watcher.close.bind(watcher)),
add: (path) => {
logger.info(`Source file added:`, path);
bundleFile(path, { logger, targetPath: resolveBundlePath(path) }).catch(
throwError,
);
},
change: (path) => {
logger.info(`Source file changed:`, path);
bundleFile(path, { logger, targetPath: resolveBundlePath(path) }).catch(
throwError,
);
},
unlink: (path) => {
logger.info('Source file removed:', path);
const bundlePath = resolveBundlePath(path);
unlink(bundlePath)
.then(() => logger.info(`removed ${bundlePath}`))
.catch((reason: unknown) => {
if (reason instanceof Error && reason.message.match(/ENOENT/u)) {
// If associated bundle does not exist, do nothing.
return;
}
throwError(reason);
});
},
error: (error: Error) => throwError(error),
});
export const filterOnlyJsFiles: MatchFunction = (file, stats) =>
(stats?.isFile() ?? false) && file.endsWith('.js');
/**
* Start a watcher that bundles `.js` files in the target dir.
*
* @param dir - The directory to watch.
* @param logger - The logger to use.
* @returns A {@link WatchDirReturn} object with `ready` and `error` properties which are promises.
* The `ready` promise resolves to an awaitable method to close the watcher.
* The `error` promise never resolves, but rejects when any of the watcher's behaviors encounters an irrecoverable error.
*/
export function watchDir(dir: string, logger: Logger): WatchDirReturn {
const resolvedDir = resolve(dir);
const { resolve: readyResolve, promise: readyPromise } =
makePromiseKit<CloseWatcher>();
const { reject: throwError, promise: errorPromise } = makePromiseKit<never>();
let watcher = watch(resolvedDir, {
ignoreInitial: true,
ignored: [
'**/node_modules/**',
'**/*.test.js',
'**/*.test.ts',
'**/*.bundle',
filterOnlyJsFiles,
],
});
const events = makeWatchEvents(watcher, readyResolve, throwError, logger);
for (const key of Object.keys(events)) {
watcher = watcher.on(key, events[key as keyof typeof events] as never);
}
return {
ready: readyPromise,
error: errorPromise,
};
}