const fs = require("fs");
const child_process = require('child_process');
process.title = 'Tool';
function main(config) {
let run = 0;
let f = (dir, watchs) => {
fs.watch(dir, { recursive: true }, (et, fn) => {
if (run || !fn || (et != 'change' && et != 'rename')) return;
let watch = watchs.filter(p => fn.toLowerCase().endsWith(p.fileExtension))[0];
if (!watch) return;
fn = `${dir}/${fn}`;
run = 1;
setTimeout(() => {
run = 0;
fs.stat(fn, (e, s) => {
if (!e && s && s.isFile()) {
let p = child_process.spawn(watch.cmd, watch.args && watch.args.map(p => p === null ? fn : p) || [fn]);
p.stdout.pipe(process.stdout);
p.stderr.pipe(process.stderr);
}
});
}, 200);
}).on('error', console.error);
};
let paths = [];
for (let watch of config) {
watch.fileExtension = watch.fileExtension.toLowerCase();
for (let dir of watch.paths) paths.push(dir);
}
paths = [...new Set(paths)];
for (let v of paths) f(v, config.filter(p => p.paths.indexOf(v) != -1));
}
const config = [
{
fileExtension: ".te.js",
cmd: "node",
args: ["d:/localTool/output/tool/app.js", null, "d:/localTool/TemplateHelper.js", "d:/localTool/env.js"],
paths: [
"E:/Projects/",
"d:/localTool/"
]
}
];
main(config);
require('child_process').exec('"C:/Program Files (x86)/IIS Express/iisexpress.exe" /config:"E:/Projects/test/.vs/config/applicationhost.config" /site:"test" /apppool:"Clr4IntegratedAppPool"');
查看进程命令行参数:wmic process get caption,commandline /value