UNPKG

1.24 kBJavaScriptView Raw
1// require('../mock/worker')
2const fs = require('fs');
3const fork = require('child_process').fork;
4const path = require('path');
5const chokidar = require('chokidar');
6
7let watcher = null;
8let work = null;
9
10// process.on('SIGINT', function() {
11// // watcher && watcher.close();
12// // work && work.kill();
13// process.exit(1);
14// });
15
16const 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 //监听mock文件变化 并重启进程
28 // fs.watch callback会执行多次, 替换为 https://github.com/paulmillr/chokidar
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
46module.exports = {
47 start: start
48};
\No newline at end of file