UNPKG

@nu-art/commando

Version:
52 lines (51 loc) 2.07 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Commando = void 0; const ts_common_1 = require("@nu-art/ts-common"); const SimpleShell_1 = require("./SimpleShell"); const BaseCommando_1 = require("../core/BaseCommando"); const CliError_1 = require("../core/CliError"); class Commando extends BaseCommando_1.BaseCommando { static create(...plugins) { const _commando = BaseCommando_1.BaseCommando._create(Commando, ...plugins); return _commando; } constructor() { super(); } setUID(uid) { this.uid = uid; return this; } executeFile(filePath, interpreter) { let command = filePath; // If an interpreter is provided, prefix the command with it. if (interpreter) { command = `${interpreter} ${filePath}`; } return new SimpleShell_1.SimpleShell().execute(command); } executeRemoteFile(pathToFile, interpreter) { const command = `curl -o- "${pathToFile}" | ${interpreter}`; return new SimpleShell_1.SimpleShell().execute(command); } async execute(callback) { var _a; const command = this.builder.reset(); const simpleShell = new SimpleShell_1.SimpleShell().debug(this._debug); try { if (this.uid) simpleShell.setUID(this.uid); const { stdout, stderr } = await simpleShell.execute(command); return callback === null || callback === void 0 ? void 0 : callback(stdout, stderr, 0); } catch (_error) { simpleShell.logError(_error); const cliError = _error; if ('isInstanceOf' in cliError && cliError.isInstanceOf(CliError_1.CliError)) return callback === null || callback === void 0 ? void 0 : callback(cliError.stdout, cliError.stderr, (_a = cliError.cause.code) !== null && _a !== void 0 ? _a : -1); throw new ts_common_1.ThisShouldNotHappenException('Unhandled error', _error); } } } exports.Commando = Commando;