我大约有100个JS文件,每个文件都有不同的脚本。
file1.js , file2.js , file3.js , ... , file100.js
现在,要执行每个文件,我必须去我的终端并执行以下操作:
node file1
node file2
node file3
.
.
node file100
这意味着我必须在终端上执行100次此操作。是否有脚本可以写入JS文件,所以我只能执行一个JS文件,该脚本将依次执行所有100个JS文件?
我还想在每次执行之间等待3秒,我相信我可以使用以下代码实现:
var interval = 3000;
var promise = Promise.resolve();
promise = promise.then(function () {
return new Promise(function (resolve) {
setTimeout(resolve, interval);
});
});
有什么建议如何执行所有100个JS文件而不node file#
在终端中键入100次?
这是执行此操作的脚本,您也可以选择其他文件格式,而不是
node file1
..
node filex
您可以执行此操作file1,file2,file3...filex
,也可以在使用此代码之前查看child_process。
const { readFileSync } = require('fs');
const {exec} = require("child_process");
function executeEachFile(command){
exec(command, (error, stdout, stderr) => {
if(error){
console.log(error);
}
console.log("stdout: " + stdout)
});
}
function readFile(path){
const output = readFileSync(path,"utf8").split("\n").map(file=>{
return file.replace("\r","");
});
console.log(output)
return output
};
function IntervalLoop(ArrayofFiles){
let counter = 0;
const interval = setInterval(()=>{
const commandName = "node "+ ArrayofFiles[counter];
console.log(commandName);
executeEachFile(commandName);
counter++;
console.log(counter);
if (ArrayofFiles.length == counter){
clearInterval(interval);
}
},300)
}
const fileNames = readFile("exec.txt");
IntervalLoop(fileNames);
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句