1 Star 0 Fork 343

flashpig8014/ShadowEditor

forked from 超腾开源/ShadowEditor 
加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
exec.js 1.54 KB
一键复制 编辑 原始数据 按行查看 历史
超腾开源 提交于 2020-06-26 15:13 +08:00 . fix script bug
/*
* Copyright 2017-2020 The ShadowEditor Authors. All rights reserved.
*
* Use of this source code is governed by a MIT-style
* license that can be found in the LICENSE file.
*
* For more information, please visit: https://github.com/tengge1/ShadowEditor
* You can also visit: https://gitee.com/tengge1/ShadowEditor
*/
const { spawn } = require('child_process');
/**
* Execute a command
* @param {String} cmd bat or shell command
* @param {Array} args parameter array
* @param {Object} options exec options
* @param {String} options.title the title before the output
* @param {Boolean} options.showCmd whether to print the command
* @param {Boolean} options.trimSpace whether to trim the space of output
* @param {String} options.cwd current work directory
* @returns a promise that the command ended
*/
function exec(cmd, args = [], options = {}) {
if (options.showCmd === undefined) {
options.showCmd = true;
}
options.showCmd && console.log(`${cmd} ${args.join(' ')}`);
const cp = spawn(cmd, args, {
cwd: options.cwd
});
cp.stdout.on('data', data => {
let result = data.toString()
if (options.trimSpace) {
result = result.trim(' ');
}
if (options.title) {
result = `${options.title}: ${result}`;
}
console.log(result);
});
cp.stderr.on('data', data => {
console.error(data.toString());
});
return new Promise(resolve => {
cp.on('close', () => {
resolve();
});
});
}
module.exports = exec;
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
JavaScript
1
https://gitee.com/flashpig8014/ShadowEditor.git
git@gitee.com:flashpig8014/ShadowEditor.git
flashpig8014
ShadowEditor
ShadowEditor
master

搜索帮助