-
Notifications
You must be signed in to change notification settings - Fork 20
/
watchBuild.js
71 lines (57 loc) · 1.68 KB
/
watchBuild.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
#!/usr/bin/env node
/* eslint-disable require-jsdoc */
/* eslint-disable no-console */
'use strict';
const fs = require('fs-extra');
const path = require('path');
const watch = require('watch');
const util = require('util');
const exec = util.promisify(require('child_process').exec);
const curDir = process.cwd();
if (process.argv.length < 3) {
console.log('please specify target path');
console.log('for example ../react-geo/node_modules/@terrestris/react-geo/');
process.exit(0);
}
const sourcePath = path.join(curDir, 'src');
const distPath = path.join(curDir, 'dist');
const targetSourcePath = path.join(curDir, process.argv[2], 'src');
const targetDistPath = path.join(curDir, process.argv[2], 'dist');
if (!fs.existsSync(targetSourcePath) || !fs.existsSync(targetDistPath) ) {
throw new Error('target does not exist');
}
async function buildAndCopy() {
console.log('run build:dist');
try {
const { stdout, stderr} = await exec('npm run build:dist');
console.log(stdout);
console.log(stderr);
console.log('copy dist / src');
await fs.copy(distPath, targetDistPath);
await fs.copy(sourcePath, targetSourcePath);
console.log('done');
} catch (error) {
console.log('error');
const { stdout, stderr } = error;
console.log(stdout);
console.log(stderr);
}
}
buildAndCopy();
let timeout;
function throttle(callback, time) {
if (!timeout) {
timeout = setTimeout(function () {
timeout = null;
callback();
}, time);
}
}
// eslint-disable-next-line no-unused-vars
watch.watchTree(sourcePath, function (f, curr, prev) {
if (typeof f === 'object') {
console.log('watching');
} else {
throttle(buildAndCopy, 1000);
}
});