【问题标题】:Pass changed filename to command in nodemon将更改的文件名传递给 nodemon 中的命令
【发布时间】: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');
});

它的作用:

  1. 导入 nodemon 模块
  2. 配置 nodemon 模块以监视内部的任何文件更改 force-app文件夹
  3. 为启动和重启添加了事件监听器
  4. 我想将导致 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 命令

标签: node.js nodemon


【解决方案1】:

没有明显的方法可以通过 nodemon config 实现您的请求,但您可以使用 chokidar,nodemon 内部使用的相同包来监视更改:

var nodemon = require('nodemon');
var chokidar = require('chokidar');

var obj = {};
obj.watch = [];
obj.watch.push("force-app");
obj.exec = "echo 'Watching for changes ...'";
obj.ext = "cls,xml,json,js,trigger,cpm,css,design,svg";
obj.delay = "2500";
obj.verbose = true;

chokidar.watch(obj.watch).on('all', (event, path) => {
  console.log(event, path);
});

nodemon(obj);

它允许您获取有关监视的目录和文件的更多详细信息。

【讨论】:

  • 非常感谢。它解决了我的要求。感谢您的宝贵时间。
猜你喜欢
  • 2017-08-24
  • 1970-01-01
  • 1970-01-01
  • 2013-06-15
  • 2018-09-16
  • 2013-04-19
  • 1970-01-01
  • 2019-03-23
  • 2013-04-02
相关资源
最近更新 更多