* 安全执行系统命令 * @param {string} command - 要执行的命令 * @param {object} options - 执行选项 * @returns {Promise } 命令输出结果
(command, options = { timeout: 30000 })
| 15 | * @returns {Promise<string>} 命令输出结果 |
| 16 | */ |
| 17 | function execCommand(command, options = { timeout: 30000 }) { |
| 18 | return new Promise((resolve, reject) => { |
| 19 | exec(command, options, (error, stdout, stderr) => { |
| 20 | if (error) { |
| 21 | if (error.killed) { |
| 22 | reject(new Error('执行命令超时')); |
| 23 | } else { |
| 24 | reject(error); |
| 25 | } |
| 26 | return; |
| 27 | } |
| 28 | resolve(stdout.trim() || stderr.trim()); |
| 29 | }); |
| 30 | }); |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * 获取系统信息 |
no outgoing calls
no test coverage detected