-
Notifications
You must be signed in to change notification settings - Fork 27
/
watch.js
84 lines (73 loc) · 1.8 KB
/
watch.js
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
const { dirname } = require('path');
const childProcess = require('child_process');
const fs = require('fs');
const { sync } = require('find-up');
const chokidar = require('chokidar');
const dir = dirname(sync('.git'));
const pattern = ['ac/*/src/**', 'op/*/src/**', 'frog-utils/src/**'];
const distFlow = path => path.replace('/src/', '/dist/');
const dist = path => path.replace('/src/', '/dist/').replace('.jsx', '.js');
const transpile = (event, src) => {
childProcess.exec(`cp ${src} ${distFlow(src)}.flow`);
childProcess.exec(
`${dir}/node_modules/.bin/babel ${src} -o ${dist(src)}`,
error => {
if (error) {
log(`Babel error: ${error}`);
return;
}
log(`${event}: ${src}`);
}
);
};
const deleteLogs = (err, src) => {
if (err) {
log(`delete: ${src} failed.`);
} else {
log(`delete: ${src}`);
}
};
const rm = (event, src) => {
if (event === 'file') {
fs.unlink(dist(src), err => deleteLogs(err, src));
fs.unlink(`${dist(src)}.flow`, err => deleteLogs(err, src));
}
if (event === 'directory') fs.rmdir(dist(src), err => deleteLogs(err, src));
};
const mkdir = src => {
fs.mkdir(dist(src), err => {
if (err) {
log(`addDir: ${src} failed.`);
} else {
log(`addDir: ${src}`);
}
});
};
const watcher = chokidar
.watch(pattern, {
persistent: true,
cwd: dir,
ignored: /\.(json|snap)/,
ignoreInitial: true
})
.on('add', src => {
transpile('add', src);
})
.on('addDir', src => {
mkdir(src);
})
.on('change', src => {
transpile('change', src);
})
.on('unlink', src => {
rm('file', src);
})
.on('unlinkDir', src => {
rm('directory', src);
});
const log = console.info.bind(console);
process.on('SIGINT', () => {
log('Quitting Watch');
watcher.close();
process.exit();
});