I have a node project with index.js as below:
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');
});
What it does:
force-app
folderCurrent Output:
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
Question:
As we can see in the above output that changed file name is coming inside restart event handler but how do I pass that file name to the exec command so that I can print the actual file name using echo
command.
There is no obvious way to achieve your request through nodemon config, but you can use chokidar
, the same package used by nodemon internally to watch for changes:
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);
It allows you to get more details about watched directories and files.