-
Notifications
You must be signed in to change notification settings - Fork 63
Expand file tree
/
Copy pathwatch.ts
More file actions
53 lines (44 loc) · 1.37 KB
/
watch.ts
File metadata and controls
53 lines (44 loc) · 1.37 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
/* eslint @typescript-eslint/explicit-function-return-type:0, no-shadow: 0 */
import * as glob from 'glob';
import * as path from 'path';
import * as fs from 'fs-extra';
import { watch } from 'chokidar';
import { run } from './fn/shell';
let watcher;
(async () => {
await run('npm run clean');
const fileParten = '*/src/**/!(*.ts|*.tsx)';
console.log(`[COPY]: ${fileParten}`);
const cwd = path.join(__dirname, '../packages');
const files = glob.sync(fileParten, { cwd, nodir: true });
const fileSet = new Set();
/* eslint no-restricted-syntax:0 */
for (const file of files) {
/* eslint no-await-in-loop:0 */
await copyOneFile(file, cwd);
fileSet.add(path.join(cwd, file));
}
const watcher = await watch(cwd, {
ignored: [/lib\//],
ignoreInitial: true,
});
watcher.on('add', reactFileChange.bind(null, cwd));
watcher.on('change', reactFileChange.bind(null, cwd));
await run('npx tsc --build ./tsconfig.json -w');
})().catch(async (e) => {
console.trace(e);
await watcher?.close();
process.exit(128);
});
async function copyOneFile(file, cwd) {
const from = path.join(cwd, file);
const to = path.join(cwd, file.replace(/\/src\//, '/lib/'));
await fs.copy(from, to, {
overwrite: true,
});
}
function reactFileChange(cwd, file) {
if (!/(\.tsx?)|(package\.json)$/.test(file)) {
copyOneFile(path.relative(cwd, file), cwd);
}
}