UNPKG

@veecode-platform/safira-cli

Version:

Generate a microservice project from your spec.

54 lines (53 loc) 2.12 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.CommandExecute = void 0; const child_process_1 = require("child_process"); const command_exception_1 = require("../exception/command-exception"); class CommandExecute { async runInteractive(command) { return new Promise((resolve, reject) => { const commandList = command.replace(/\s+/g, " ").trim().split(" "); const spawnProcess = (0, child_process_1.spawn)(commandList[0], commandList.slice(1), { stdio: "inherit" }); spawnProcess.stdout?.on("data", result => console.log(result)); spawnProcess.on("close", code => { if (code === 0) resolve(); if (code === 1) reject(new Error("Command cancelled.")); reject(new Error("Command failed.")); }); }); } async exec(command, consoleShow = true) { return new Promise((resolve, reject) => { let errorLog = ""; let successLog = ""; if (process.env.TERM_PROGRAM === "vscode") process.env.NODE_OPTIONS = ""; const commandExec = (0, child_process_1.exec)(command); commandExec.stdout?.on("data", result => { successLog += result; if (consoleShow) console.log(result); }); commandExec.stderr?.on("data", result => { errorLog += result; }); commandExec.on("error", error => reject(error)); commandExec.on("exit", code => { if (code === 0) resolve(successLog); if (code === 1) reject(new Error("Command cancelled")); reject(new command_exception_1.CommandException(errorLog || "Generic error")); }); }); } static get instance() { if (!this._instance) { this._instance = new this(); } return this._instance; } } exports.CommandExecute = CommandExecute;