1 |
|
2 | const fs = require('fs');
|
3 | const fork = require('child_process').fork;
|
4 | const path = require('path');
|
5 | const chokidar = require('chokidar');
|
6 |
|
7 | let watcher = null;
|
8 | let work = null;
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 | const start = (serverRoot) => {
|
17 | const actionShell = serverRoot || path.join(__dirname, '../mock/worker.js');
|
18 |
|
19 |
|
20 | let watchDog = function(){
|
21 | let timer = setTimeout(function(){
|
22 | work = fork(actionShell);
|
23 | clearTimeout(timer);
|
24 | }, 100);
|
25 | };
|
26 |
|
27 |
|
28 |
|
29 | const watchedFolder = path.resolve('mock/');
|
30 | watcher = chokidar
|
31 | .watch(watchedFolder, {ignoreInitial: true})
|
32 | .on('all', (event, path) => {
|
33 | if (work) {
|
34 | console.log('trigger watchedFolder and will restart mock server ...');
|
35 | work.kill();
|
36 |
|
37 | watchDog();
|
38 | }
|
39 | });
|
40 |
|
41 |
|
42 | watchDog();
|
43 | };
|
44 |
|
45 |
|
46 | module.exports = {
|
47 | start: start
|
48 | }; |
\ | No newline at end of file |