【发布时间】:2020-04-28 13:21:38
【问题描述】:
编辑: 我仍然有一个错误,如果我单独使用命令 watchit 而不过滤参数,我会得到一个无限循环的进程打开而不会被杀死。我认为这可能是因为我为 watchit 设置二进制文件以运行 index.js 的方式,同时也在 watch() 内部,如果没有提供,我将文件名默认设置为 index.js
所以我试图创建一个类似于 nodemon 的程序,它可以持续监视您的程序以了解文件的更改和添加/删除。
我正在使用我通过 npm 安装的 2 个库,chokidar 用于实际观看程序,然后我使用 caporal 创建了一个 CLI。我也在使用lodash.debounce 和chalk。粉笔只是为了改变输出的颜色,让它看起来更干净一些
在文件夹中,我有 index.js、package.json、package-lock.json、node modules 文件夹,然后是一个 test.js,我以前只是使用简单的 console.log() 测试程序;
在 package.json 内 我将 bin 设置为:
"bin": {
"watchit": "index.js"
}
index.js
#!/usr/bin/env node
const debounce = require('lodash.debounce');
const chokidar = require('chokidar');
const prog = require('caporal');
const chalk = require('chalk');
const fs = require('fs');
const { spawn } = require('child_process');
prog
.version('0.0.1')
.argument('[filename]', 'Name of a file to execute')
.action(async ({ filename }) => {
const name = filename || 'index.js';
try {
await fs.promises.access(name);
} catch (err) {
throw new Error(`Could not find the file: ${name}`);
}
let pid;
const start = debounce(() => {
if (pid) {
console.log(chalk.red('------- Ending Process -------'));
pid.kill();
}
console.log(chalk.green('------- Starting Process -------'));
pid = spawn('node', [name], { stdio: 'inherit' });
}, 100);
chokidar
.watch('.')
.on('add', start)
.on('change', start)
.on('unlink', start);
});
prog.parse(process.argv);
test.js
console.log('how many times do i run this');
从终端
$ watchit test.js
------- Starting Process -------
how many times do i run this
------- Ending Process -------
------- Starting Process -------
how many times do i run this
------- Ending Process -------
------- Starting Process -------
how many times do i run this
显然,我正在创建一个新进程并运行程序,因为在显示之前的输出时没有跟踪更改。那是来自 test.js 的单次执行,而无需对其进行编辑。
另外,当我从我的计算机上有这个项目的目录中简单地运行命令 watchit 时,我得到了上面的一些输出,然后是一个关于未处理的承诺拒绝警告的大错误:"UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 27)"
任何人都可以分享一些关于为什么运行太多次的见解。去抖动计时器是太低了还是比这更大?
---edit--- 将event 添加到start 函数后,console.log(event) 在我收到console.log(-----start process-----) 之后
------- Starting Process -------
.git/packed-refs
how many times do i run this
------- Ending Process -------
------- Starting Process -------
node_modules/winston/package.json
how many times do i run this
------- Ending Process -------
------- Starting Process -------
node_modules/winston/node_modules/colors/lib/system/supports-colors.js
how many times do i run this
在忽略 .git 和 node_modules 之后 要忽略我在选项 obj 中更改的文件/文件夹:
chokidar
.watch('.', {
ignored: '*.git',
ignored: 'node_modules'
})
.on('add', start)
.on('change', start)
.on('unlink', start);
});
$ watchit test.js
------- Starting Process -------
.git/logs/HEAD
how many times do i run this
------- Ending Process -------
------- Starting Process -------
.git/logs/refs/remotes/origin/HEAD
how many times do i run this
【问题讨论】:
-
让我们调试它。记录事件名称,当启动运行时...将事件名称作为参数和日志传递,或其他东西
-
@PedroMutter 所以我将开始更改为接受(事件)并在启动进程时输出它,我将在底部编辑输出
-
嗯...似乎是每次都被调用的
change对吗?您必须忽略.git文件夹和node_modules并再次运行,看看会发生什么。 -
@PedroMutter 我在前面的下方添加了另一个调试编辑,显示了我在 .watch() 和相应的控制台输出中所做的更改。看来 git 还在叫它
标签: javascript node.js