@interaktiv/mibuilder-core
Version:
Core libraries to interact with MiBuilder projects.
77 lines (61 loc) • 2 kB
JavaScript
;
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.execute = execute;
var _execa = _interopRequireDefault(require("execa"));
var _stripFinalNewline = _interopRequireDefault(require("strip-final-newline"));
var _mibuilderError = require("./mibuilder-error");
var _ux = require("./ux");
async function execute(cmd, args = [], options = {}) {
const {
printCommand = true,
printAll = false,
suppressOutput = false,
stdio = ['inherit', 'pipe', 'pipe'],
cwd = process.cwd(),
timeout = 0,
env = {}
} = options;
const cliUx = await _ux.UX.create('command-executor');
if (printCommand) {
cliUx.logCommand(`${cmd} ${args.join(' ')}`);
}
const spawned = (0, _execa.default)(cmd, args, {
stdio,
cwd,
timeout,
env
});
const output = [];
const logLine = function (data) {
const line = (0, _stripFinalNewline.default)(data.toString());
output.push(line);
if (printAll !== true) return;
if (suppressOutput !== true) cliUx.logCommandOutput(line);
};
if (spawned.stdout) spawned.stdout.on('data', data => logLine(data));
if (spawned.stderr) spawned.stderr.on('data', data => logLine(data));
let result;
try {
result = await spawned;
const {
exitCode
} = result; // Exit status for build command, should be 0 if build succeeded
if (exitCode !== 0) {
if (suppressOutput !== true) cliUx.info(output.join('\n'));
throw new Error(`[!] Exit status: ${exitCode}`);
}
} catch (err) {
const errorMessage = err.stderr && err.stderr.length > 0 ? err.stderr : err.toString();
output.push(errorMessage);
if (suppressOutput !== true && printAll !== true) {
cliUx.info(output.join('\n'));
} else if (suppressOutput !== true) {
cliUx.error(errorMessage);
}
throw _mibuilderError.MiBuilderError.wrap(err);
}
return result;
}