UNPKG

easy-aos

Version:

帮助配置arm-gcc开发环境,简化命令行。

89 lines (84 loc) 2.23 kB
import { spawn } from "child_process" import readline from "readline" import chalk from "chalk" import i18n from "../lib/i18n" const replace_params = function (params, env) { let regex = /\${([^}]+)}/g let match = regex.exec(params) while (match) { params = params.replace(/\${[^}]+}/, env[match[1]]) match = regex.exec(params) } return params } const parse_params = function (params, env) { let new_params = [] params.forEach(element => { if (typeof (element) == "object") { let condition = element.condition.match(/\${([^}]+)}/)[1] if (env[condition]) { element.params.forEach(e => { new_params.push(replace_params(e, env)) }) } } else { new_params.push(replace_params(element, env)) } }) return new_params } const match_line = function (line, logs, t) { if (logs) { for (let [reg, color] of logs.entries()) { let match = line.match(reg) if (match) { match.shift() let message = [], i = 0 for (const m of match) { let set_color = color[i] || "white" if (i == 0) { message.push(chalk[set_color](t.__(m))) } else { message.push(chalk[set_color](m)) } i += 1 } console.log(message.join(" ")) } } } } const run_command = (command, env) => { let t = new i18n(`log.${command.name}`) if (command.run) { command.params = parse_params(command.params, env) if (env.debug) { console.log(t.__("run command:"), command.run, command.params.join(" ")) } else { const child = spawn(command.run, command.params.join(" ").split(" "), { stdio: [process.stdin, null, null] }) const stdout = readline.createInterface({ input: child.stdout, terminal: true }).on("line", function (line) { if (env.full_log) { console.log(line) } else { match_line(line, command.logs.stdout, t) } }) const stderr = readline.createInterface({ input: child.stderr, terminal: true }).on("line", function (line) { if (env.full_log) { console.log(line) } else { match_line(line, command.logs.stderr, t) } }) } } } export { parse_params } export default run_command