UNPKG

easy-aos

Version:

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

117 lines (93 loc) 2.85 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = exports.parse_params = void 0; var _child_process = require("child_process"); var _readline = _interopRequireDefault(require("readline")); var _chalk = _interopRequireDefault(require("chalk")); var _i18n = _interopRequireDefault(require("../lib/i18n")); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } 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; }; exports.parse_params = parse_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.default[set_color](t.__(m))); } else { message.push(_chalk.default[set_color](m)); } i += 1; } console.log(message.join(" ")); } } } }; const run_command = (command, env) => { let t = new _i18n.default(`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 = (0, _child_process.spawn)(command.run, command.params.join(" ").split(" "), { stdio: [process.stdin, null, null] }); const stdout = _readline.default.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.default.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); } }); } } }; var _default = run_command; exports.default = _default;