我编写了这个简单的node.js脚本来构建Marp markdown并将其转换为PDF。但它不会做任何事情,也不会终止。我在最后添加了一个构建,我看到node.js不会等待所有命令执行完成,但是exec也不会运行Marp console.log("test")命令。
如果我从终端运行"cmd“字符串,一切正常。我认为我使用exec的方式有问题
var glob = require("glob");
var cp = require("child_process");
glob("lab*/*.marp.md", {}, function (err, files) {
files.forEach((file) => {
var destination = file.replace(".md", "");
var cmd = `marp ${file} --allow-local-files --pdf -o ${destination}.pdf`;
var dir = cp.exec(cmd, (err, stdout, stderr) => {
if (err) {
console.log("node couldn't execute the command");
return;
}
// the *entire* stdout and stderr (buffered)
console.log(`stdout: ${stdout}`);
console.log(`stderr: ${stderr}`);
});
dir.on("exit", function (code) {
// exit code is code
console.log(`finished building: ${file}`);
});
});
});发布于 2021-10-24 20:27:03
你可以试试execSync()函数,它会解决这个问题
var glob = require("glob");
var cp = require("child_process");
glob("lab*/*.marp.md", {}, function (err, files) {
files.forEach((file) => {
var destination = file.replace(".md", "");
var cmd = `marp ${file} --allow-local-files --pdf -o ${destination}.pdf`;
var dir = cp.execSync(cmd, (err, stdout, stderr) => {
if (err) {
console.log("node couldn't execute the command");
return;
}
// the *entire* stdout and stderr (buffered)
console.log(`stdout: ${stdout}`);
console.log(`stderr: ${stderr}`);
});
dir.on("exit", function (code) {
// exit code is code
console.log(`finished building: ${file}`);
});
});
});https://stackoverflow.com/questions/69700292
复制相似问题