Skip to content

Commit 287c1f2

Browse files
committed
Created watch copy script - can now work on the module on your own projects \o/
1 parent 65091f5 commit 287c1f2

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

bin/watchCopy.js

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
const { watch } = require('cpx');
2+
const { resolve } = require('path');
3+
const packageJson = require('./../package.json');
4+
5+
const readline = require('readline');
6+
7+
const rl = readline.createInterface({
8+
input: process.stdin,
9+
output: process.stdout
10+
});
11+
12+
const PROJECT_DIR = resolve(__dirname, './../');
13+
let TARGET_DIR = process.env.TARGET_DIR;
14+
15+
if (!TARGET_DIR) {
16+
console.error('Missing TARGET_DIR process env, aborting!');
17+
console.error('EXAMPLE USAGE: TARGET_DIR=/Users/YOU/Documents/someproject npm run watchcpx');
18+
process.exit(1);
19+
}
20+
21+
if (!TARGET_DIR.includes('node_modules')) {
22+
TARGET_DIR = `${TARGET_DIR}/node_modules/${packageJson.name}`;
23+
}
24+
25+
26+
rl.question(`Watch for changes in '${PROJECT_DIR}' and copy to '${TARGET_DIR}'? (y/n): `, (answer) => {
27+
if (answer.toLowerCase() === 'y') {
28+
console.log('For the watch! (watching has begun)');
29+
const watcher = watch(PROJECT_DIR + '/**/*.*', TARGET_DIR, { verbose: true});
30+
watcher.on('copy', (e) => {
31+
if (!e.srcPath.startsWith('node_modules')) {
32+
console.log(`Copied ${e.srcPath} to ${e.dstPath}`);
33+
}
34+
});
35+
} else {
36+
console.log('Aborting watch.');
37+
process.exit();
38+
}
39+
rl.close();
40+
});
41+

0 commit comments

Comments
 (0)