@ainc/script
Version:
Script compiler for typescript
40 lines • 1.32 kB
JavaScript
/**
*****************************************
* Created by edonet@163.com
* Created on 2021-06-27 16:00:15
*****************************************
*/
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.watch = void 0;
const chokidar = require("chokidar");
/**
*****************************************
* 监听文件文件变更
*****************************************
*/
function watch(files, handler) {
const opts = typeof handler === 'function' ? { handler } : { ...handler };
const watcher = chokidar.watch(files, opts);
const closeWatcher = watcher.close.bind(watcher);
// 监听变更
if (typeof opts.handler === 'function') {
watcher.on('all', opts.handler.bind(watcher));
}
// 替换关闭函数
watcher.close = () => {
const deferred = {};
// 执行关闭事件
watcher.emit('close', new Promise((resolve, reject) => Object.assign(deferred, { resolve, reject })));
// 执行关闭
return closeWatcher().then(deferred.resolve, deferred.reject);
};
// 等待结束
watcher.waitExit = () => {
return new Promise(resolve => watcher.once('close', resolve));
};
// 返回监听对象
return watcher;
}
exports.watch = watch;
//# sourceMappingURL=watcher.js.map