【发布时间】:2019-05-26 16:32:00
【问题描述】:
我有一个带有 index.js 的节点项目,如下所示:
var nodemon = require('nodemon');
var obj = {};
obj.watch = [];
obj.watch.push("force-app");
obj.exec = "echo changedFileName"; //I want to print actual name of the file here.
obj.ext = "cls,xml,json,js,trigger,cpm,css,design,svg";
obj.delay = "2500";
obj.verbose = true;
nodemon(obj);
var fileChanged = undefined;
nodemon.on('start', function () {
console.log(fileChanged);
console.log('nodemon started');
}).on('crash', function () {
console.log('script crashed for some reason');
}).on('restart', function (filesList) {
fileChanged = filesList[0];
console.log(filesList[0]);
console.log('nodemon restarted');
});
它的作用:
- 导入 nodemon 模块
- 配置 nodemon 模块以监视内部的任何文件更改
force-app文件夹 - 为启动和重启添加了事件监听器
- 我想将导致 nodemon 重新启动的文件名作为参数传递给 echo 命令
电流输出:
John@John-Mac:~/workspace/TEST_PROJECT$ node index.js
changedFileName
undefined
nodemon started
changedFileName
undefined
nodemon started
/home/John/workspace/TEST_PROJECT/force-app/main/default/classes/Test.cls
nodemon restarted
问题:
正如我们在上面的输出中看到的,更改的文件名正在重新启动事件处理程序中,但是我如何将该文件名传递给 exec 命令,以便我可以使用 echo 命令打印实际文件名。
【问题讨论】:
-
force-app是您项目中的一个目录。对吗? -
是的,它在 TEST_PROJECT 文件夹中
-
并且您想在 nodemon 再次启动后显示更改的文件名。对吗?
-
是的,使用 echo 命令