Node.js 用于构建 Marp 降价的 exec 不起作用
Node.js exec to build Marp markdowns doesn't work
我编写了这个简单的 node.js 脚本来构建 Marp 降价并将它们转换为 PDF。但它什么都不做,也不会终止。我在末尾添加了一个 console.log("test")
我看到 node.js 不会等待所有命令执行完成但 exec
也不会 运行 Marp 构建命令。
如果我 运行 来自终端的“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}`);
});
});
});
你可以试试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}`);
});
});
});
我编写了这个简单的 node.js 脚本来构建 Marp 降价并将它们转换为 PDF。但它什么都不做,也不会终止。我在末尾添加了一个 console.log("test")
我看到 node.js 不会等待所有命令执行完成但 exec
也不会 运行 Marp 构建命令。
如果我 运行 来自终端的“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}`);
});
});
});
你可以试试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}`);
});
});
});