-
Notifications
You must be signed in to change notification settings - Fork 3
/
watchBuild.js
59 lines (44 loc) · 1.48 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
#!/usr/bin/env node
'use strict';
const fs = require('fs-extra');
const path = require('path');
const chokidar = require('chokidar');
const util = require('util');
const throttle = require('lodash/throttle');
const exec = util.promisify(require('child_process').exec);
const curDir = process.cwd();
if (process.argv.length < 3) {
console.log('Please specify the target path');
console.log('For example: ../shogun-gis-client/node_modules/@terrestris/shogun-util/');
process.exit(0);
}
const sourcePath = path.join(curDir, 'src');
const distPath = path.join(curDir, 'dist');
const targetDistPath = path.join(curDir, process.argv[2], 'dist');
const targetSrcPath = path.join(curDir, process.argv[2], 'src');
if (!fs.existsSync(targetDistPath) ) {
throw new Error('Target path does not exist');
}
async function buildAndCopy() {
console.log('Run build');
try {
const { stdout, stderr} = await exec('npm run build');
console.log(stdout);
console.log(stderr);
console.log(`Copy dist & src from ${curDir} to ${path.join(curDir, process.argv[2])}`);
await fs.copy(distPath, targetDistPath);
await fs.copy(sourcePath, targetSrcPath);
console.log('Done');
} catch (error) {
console.log('Error');
const { stdout, stderr } = error;
console.log(stdout);
console.log(stderr);
}
}
buildAndCopy();
const throttled = throttle(buildAndCopy, 1000);
chokidar.watch(sourcePath)
.on('add', throttled)
.on('change', throttled)
.on('unlink', throttled);