Skip to content

Commit 57c4707

Browse files
ricardochlSplaktar
authored andcommitted
feat: add watch script
Use the chokidar lib to watch files.
1 parent 1868308 commit 57c4707

File tree

4 files changed

+171
-0
lines changed

4 files changed

+171
-0
lines changed

package-lock.json

Lines changed: 99 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,16 @@
66
"scripts": {
77
"test": "echo \"Error: no test specified\" && exit 1",
88
"build": "zx tools/build.mjs",
9+
"start": "zx tools/watch.mjs",
910
"update-origin": "zx tools/update-origin.mjs"
1011
},
1112
"keywords": [],
1213
"author": "",
1314
"license": "ISC",
1415
"dependencies": {
1516
"zx": "^7.2.3"
17+
},
18+
"devDependencies": {
19+
"chokidar": "^3.6.0"
1620
}
1721
}

tools/lib/common.mjs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { watch } from 'chokidar';
12
import { resolve } from 'node:path';
23
import { $, cd, chalk, glob, within } from 'zx';
34
import { initDir, cpRf, exists } from './fileutiles.mjs';
@@ -29,6 +30,14 @@ export async function buildADEV() {
2930
});
3031
}
3132

33+
export async function watchADEV() {
34+
await within(async () => {
35+
cd(`${outDir}`);
36+
await $`yarn install`;
37+
await $`yarn docs`;
38+
});
39+
}
40+
3241
/**
3342
* glob patterns of localized files in adev-es
3443
*/
@@ -45,6 +54,18 @@ export async function copyLocalizedFiles() {
4554
}
4655
}
4756

57+
export async function watchLocalizedFiles(signal) {
58+
const watcher = watch(localizedFilePatterns, {
59+
cwd: adevEsDir,
60+
});
61+
watcher.on('change', (path) => {
62+
const src = resolve(adevEsDir, path);
63+
const dest = resolve(outDir, 'adev', path);
64+
cpRf(src, dest);
65+
});
66+
signal.addEventListener('abort', () => watcher.close());
67+
}
68+
4869
export async function syncSubmodule() {
4970
await within(async () => {
5071
cd(rootDir);

tools/watch.mjs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { argv, chalk } from 'zx';
2+
import { copyLocalizedFiles, resetBuildDir, watchADEV, watchLocalizedFiles } from './lib/common.mjs';
3+
4+
try {
5+
const { init = false } = argv;
6+
7+
console.log(chalk.green('==== setup ===='));
8+
await setup({ init });
9+
console.log(chalk.green('==== preWatch ===='));
10+
await preWatch({ init });
11+
console.log(chalk.green('==== watch ===='));
12+
await watch();
13+
} catch (e) {
14+
console.error(chalk.red(e));
15+
process.exit(1);
16+
}
17+
18+
async function setup({ init }) {
19+
console.log('');
20+
console.log(chalk.white('The targets of change monitoring are the files in adev-es and the source code in build/adev.'));
21+
if (init) {
22+
console.log(chalk.yellow('Initialize the build directory and discard the cache.'));
23+
} else {
24+
console.log(chalk.white('Specify the --init option to initialize the build directory.'));
25+
}
26+
27+
await resetBuildDir({ init });
28+
}
29+
30+
async function preWatch({ init }) {
31+
if (init) {
32+
// copy translated files
33+
console.log(chalk.cyan('Copy localized files...'));
34+
await copyLocalizedFiles();
35+
}
36+
}
37+
38+
async function watch() {
39+
const ctrl = new AbortController();
40+
await watchLocalizedFiles(ctrl.signal);
41+
try {
42+
await watchADEV();
43+
} finally {
44+
console.log(chalk.cyan('Abort watching...'));
45+
ctrl.abort();
46+
}
47+
}

0 commit comments

Comments
 (0)