UNPKG

easy-aos

Version:

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

140 lines (105 loc) 3.58 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = exports.write = void 0; var _fs = _interopRequireDefault(require("fs")); var _prompts = _interopRequireDefault(require("prompts")); var _questions = _interopRequireDefault(require("./questions/questions")); var _env = _interopRequireDefault(require("./env")); var _platform = _interopRequireDefault(require("../config/platform/platform")); var _packages = _interopRequireDefault(require("../config/package/packages")); var _log = _interopRequireDefault(require("../config/log/log")); var _process = _interopRequireDefault(require("../lib/process")); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } const get_conf = function (path) { path = path.replace("./", `${_env.default.pwd}/`); path = path.replace("~", _env.default.home); try { _fs.default.accessSync(path, _fs.default.F_OK); } catch (error) { return; } return read(path); }; const get_app_name = function () { return _env.default.pwd.split("/").pop(); }; const read = function (path) { let data = _fs.default.readFileSync(path); return JSON.parse(data); }; const write = (path, object) => { _fs.default.writeFile(path, JSON.stringify(object, null, "\t"), err => { if (err) { console.log("Can't write config file"); } }); }; exports.write = write; class EasyConf { constructor() { this.workspace = get_conf("./.easy-aos.json") || {}; this.default_config = { app: get_app_name(), board: null, isAosSource: true, env: "docker", aos_path: "~/.aos/AliOS-Things".replace("~", _env.default.home) }; } config(options) { this.env = { app: options.app || this.default_config.app, board: options.board || this.workspace.board, isAosSource: this.workspace.isAosSource || false, isApp: this.workspace.isApp || false, virtual_env: this.workspace.virtual_env || "docker", pwd: _env.default.pwd, aos_path: this.workspace.aos_path || this.default_config.aos_path, full_log: options.full_log, debug: options.debug, run: options.run, pkg: options.pkg }; if (this.env.isAosSource) { this.app = options.app || this.workspace.app; } this.platfrom = _platform.default.boards.get(this.env.board); this.packages = _packages.default; this.logs = _log.default; } run_command(command) { let run_cmd = {}; if (command == "build" && this.env.virtual_env == "docker") { run_cmd = this.packages.docker.build; } else if (command == "run") { run_cmd = this.packages[this.env.pkg][this.env.run]; } else { run_cmd = this.platfrom.commands[command]; } run_cmd.name = command; run_cmd.logs = { stdout: this.logs.stdout[command], stderr: this.logs.stderr[command] }; (0, _process.default)(run_cmd, this.env); } async get_prompts() { let question = _questions.default.config(this.workspace); return await (0, _prompts.default)(question); } async get_aos_path() { let question = _questions.default.aos_path(this.env.aos_path); return await (0, _prompts.default)(question); } async isAosSource() { let question = _questions.default.isAosSource(this.workspace); return await (0, _prompts.default)(question); } async get_app() { let question = _questions.default.app(this.workspace, get_app_name()); return await (0, _prompts.default)(question); } } exports.default = EasyConf;