UNPKG

crootfast

Version:

大前端工程化命令行脚手架

45 lines (40 loc) 1.24 kB
const watch = require('node-watch'); function watchDirectory(directoryPath, onChange) { const watcher = watch( directoryPath, { recursive: true, delay: 1000, filter(f, skip) { //return /\.vue/.test(f); console.log(f, skip); return true; // /(\/widgets\/|\/components\/)/.test(f); } }, (evt, name) => { console.log(`Directory or file ${name} has been changed. Event Type: ${evt}`); onChange(); }); // console.log(`Watching directory: ${directoryPath}`); // 用于停止监听 return () => watcher.close(); }; function stopWatching(callback) { // 当接收到SIGINT信号时,停止监视并退出进程 process.on('SIGINT', () => { // console.log('Received SIGINT. Stopping watching...'); callback(); // 停止监视 process.exit(); // 退出进程 }); }; // 当接收到SIGINT信号时,停止监视并退出进程 // process.on('SIGINT', () => { // console.log('Received SIGINT. Stopping watching...'); // stopWatching(); // 停止监视 // process.exit(); // 退出进程 // }); // 如果你想在某个时候停止监听,可以调用 stopWatching() // stopWatching(); module.exports = { watchDirectory, stopWatching };