71 lines
2.1 KiB
JavaScript
Executable File
71 lines
2.1 KiB
JavaScript
Executable File
"use strict";
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.dryRun = exports.runSafely = exports.run = void 0;
|
|
const tokenizer_1 = require("./newparser/tokenizer");
|
|
const parser_1 = require("./newparser/parser");
|
|
const errorBox_1 = require("./errorBox");
|
|
const Result_1 = require("./Result");
|
|
const effects_1 = require("./effects");
|
|
async function run(ap, strings) {
|
|
const result = await runSafely(ap, strings);
|
|
if (Result_1.isErr(result)) {
|
|
return result.error.run();
|
|
}
|
|
else {
|
|
return result.value;
|
|
}
|
|
}
|
|
exports.run = run;
|
|
/**
|
|
* Runs a command but does not apply any effect
|
|
*/
|
|
async function runSafely(ap, strings) {
|
|
const longFlagKeys = new Set();
|
|
const shortFlagKeys = new Set();
|
|
const longOptionKeys = new Set();
|
|
const shortOptionKeys = new Set();
|
|
const hotPath = [];
|
|
const registerContext = {
|
|
forceFlagShortNames: shortFlagKeys,
|
|
forceFlagLongNames: longFlagKeys,
|
|
forceOptionShortNames: shortOptionKeys,
|
|
forceOptionLongNames: longOptionKeys,
|
|
};
|
|
ap.register(registerContext);
|
|
const tokens = tokenizer_1.tokenize(strings);
|
|
const nodes = parser_1.parse(tokens, registerContext);
|
|
try {
|
|
const result = await ap.run({ nodes, visitedNodes: new Set(), hotPath });
|
|
if (Result_1.isErr(result)) {
|
|
throw new effects_1.Exit({
|
|
message: errorBox_1.errorBox(nodes, result.error.errors, hotPath),
|
|
exitCode: 1,
|
|
into: 'stderr',
|
|
});
|
|
}
|
|
else {
|
|
return Result_1.ok(result.value);
|
|
}
|
|
}
|
|
catch (e) {
|
|
if (e instanceof effects_1.Exit) {
|
|
return Result_1.err(e);
|
|
}
|
|
throw e;
|
|
}
|
|
}
|
|
exports.runSafely = runSafely;
|
|
/**
|
|
* Run a command but don't quit. Returns an `Result` instead.
|
|
*/
|
|
async function dryRun(ap, strings) {
|
|
const result = await runSafely(ap, strings);
|
|
if (Result_1.isErr(result)) {
|
|
return Result_1.err(result.error.dryRun());
|
|
}
|
|
else {
|
|
return result;
|
|
}
|
|
}
|
|
exports.dryRun = dryRun;
|
|
//# sourceMappingURL=runner.js.map
|