如何在nodejs中执行顺序基本命令?
How to execute sequential base commands in nodejs?
我需要在 nodejs 中按顺序执行 运行 4 bash 个命令。
set +o history
sed -i 's/&& !this.peekStartsWith('\/\/')/ /g' dist/vendor.bundle.js
sed -i 's/&& !this.peekStartsWith('\/\/')/ /g' dist/vendor.bundle.js.map
set -o history
这是如何实现的?或者是否可以添加 npm 脚本?
到 运行 shell 来自节点的命令使用 exec
,
https://nodejs.org/api/child_process.html#child_process_child_process_exec_command_options_callback
三种可能的方法是,
创建一个包含所有需要的命令的 bash 脚本文件,然后使用 exec
.
从节点 运行 它
运行 使用 exec
.
单独从节点发出的每个命令
使用 npm 包,例如以下之一(我没试过)
https://www.npmjs.com/package/shelljs
https://www.npmjs.com/package/exec-sh
也可以 promisify
exec
(https://nodejs.org/dist/latest-v8.x/docs/api/util.html#util_util_promisify_original) 并使用 async/await
而不是回调。
例如,
const {promisify} = require('util');
const {exec} = require('child_process');
const execAsync = promisify(exec);
(async () => {
const {stdout, stderr} = await execAsync('set +o history');
...
})();
要扩展@melc 的答案,要按顺序执行请求,您可以这样做:
const {promisify} = require('util');
const {exec} = require('child_process');
const execAsync = promisify(exec);
const sequentialExecution = async (...commands) => {
if (commands.length === 0) {
return 0;
}
const {stderr} = await execAsync(commands.shift());
if (stderr) {
throw stderr;
}
return sequentialExecution(...commands);
}
// Will execute the commands in series
sequentialExecution(
"set +o history",
"sed -i 's/&& !this.peekStartsWith('\/\/')/ /g' dist/vendor.bundle.js",
"sed -i 's/&& !this.peekStartsWith('\/\/')/ /g' dist/vendor.bundle.js.map",
"set -o history",
);
或者如果你不关心stdout/sterr,你可以使用下面的一行:
const commands = [
"set +o history",
"sed -i 's/&& !this.peekStartsWith('\/\/')/ /g' dist/vendor.bundle.js",
"sed -i 's/&& !this.peekStartsWith('\/\/')/ /g' dist/vendor.bundle.js.map",
"set -o history",
];
await commands.reduce((p, c) => p.then(() => execAsync(c)), Promise.resolve());
我需要在 nodejs 中按顺序执行 运行 4 bash 个命令。
set +o history
sed -i 's/&& !this.peekStartsWith('\/\/')/ /g' dist/vendor.bundle.js
sed -i 's/&& !this.peekStartsWith('\/\/')/ /g' dist/vendor.bundle.js.map
set -o history
这是如何实现的?或者是否可以添加 npm 脚本?
到 运行 shell 来自节点的命令使用 exec
,
https://nodejs.org/api/child_process.html#child_process_child_process_exec_command_options_callback
三种可能的方法是,
创建一个包含所有需要的命令的 bash 脚本文件,然后使用
exec
. 从节点 运行 它
运行 使用
exec
. 单独从节点发出的每个命令
使用 npm 包,例如以下之一(我没试过) https://www.npmjs.com/package/shelljs
https://www.npmjs.com/package/exec-sh
也可以 promisify
exec
(https://nodejs.org/dist/latest-v8.x/docs/api/util.html#util_util_promisify_original) 并使用 async/await
而不是回调。
例如,
const {promisify} = require('util');
const {exec} = require('child_process');
const execAsync = promisify(exec);
(async () => {
const {stdout, stderr} = await execAsync('set +o history');
...
})();
要扩展@melc 的答案,要按顺序执行请求,您可以这样做:
const {promisify} = require('util');
const {exec} = require('child_process');
const execAsync = promisify(exec);
const sequentialExecution = async (...commands) => {
if (commands.length === 0) {
return 0;
}
const {stderr} = await execAsync(commands.shift());
if (stderr) {
throw stderr;
}
return sequentialExecution(...commands);
}
// Will execute the commands in series
sequentialExecution(
"set +o history",
"sed -i 's/&& !this.peekStartsWith('\/\/')/ /g' dist/vendor.bundle.js",
"sed -i 's/&& !this.peekStartsWith('\/\/')/ /g' dist/vendor.bundle.js.map",
"set -o history",
);
或者如果你不关心stdout/sterr,你可以使用下面的一行:
const commands = [
"set +o history",
"sed -i 's/&& !this.peekStartsWith('\/\/')/ /g' dist/vendor.bundle.js",
"sed -i 's/&& !this.peekStartsWith('\/\/')/ /g' dist/vendor.bundle.js.map",
"set -o history",
];
await commands.reduce((p, c) => p.then(() => execAsync(c)), Promise.resolve());