UNPKG

easy-aos

Version:

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

113 lines (99 loc) 2.8 kB
import fs from "fs" import prompts from "prompts" import questions from "./questions/questions" import env from "./env" import platform from "../config/platform/platform" import packages from "../config/package/packages" import logs from "../config/log/log" import run from "../lib/process" const get_conf = function (path) { path = path.replace("./", `${env.pwd}/`) path = path.replace("~", env.home) try { fs.accessSync(path, fs.F_OK) } catch (error) { return } return read(path) } const get_app_name = function () { return env.pwd.split("/").pop() } const read = function (path) { let data = fs.readFileSync(path) return JSON.parse(data) } const write = (path, object) => { fs.writeFile(path, JSON.stringify(object, null, "\t"), (err) => { if (err) { console.log("Can't write config file") } }) } export { write } export default 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.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.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.boards.get(this.env.board) this.packages = packages this.logs = logs } 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] } run(run_cmd, this.env) } async get_prompts() { let question = questions.config(this.workspace) return await prompts(question) } async get_aos_path() { let question = questions.aos_path(this.env.aos_path) return await prompts(question) } async isAosSource() { let question = questions.isAosSource(this.workspace) return await prompts(question) } async get_app() { let question = questions.app(this.workspace, get_app_name()) return await prompts(question) } }