47 lines
2.0 KiB
JavaScript
47 lines
2.0 KiB
JavaScript
"use strict";
|
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
};
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.run = void 0;
|
|
const assert_1 = __importDefault(require("assert"));
|
|
const commander_1 = require("commander");
|
|
const index_1 = __importDefault(require("./index"));
|
|
const pkg = require("../package.json");
|
|
const run = (argv = process.argv) => {
|
|
const program = new commander_1.Command();
|
|
return new Promise((resolve, reject) => {
|
|
program.version(pkg.version);
|
|
index_1.default.commands.forEach((spec) => {
|
|
let subcommand = program.command(spec.name).description(spec.description);
|
|
const options = spec.options || [];
|
|
options.forEach((spec) => {
|
|
if (spec.values) {
|
|
const template = `--${spec.name} <${spec.name}>`;
|
|
const description = `${spec.description} (${spec.values.map(JSON.stringify).join(", ")})`;
|
|
subcommand = subcommand.option(template, description, (value, _dummyPrevious) => {
|
|
(0, assert_1.default)(spec.values.includes(value), `Invalid value ${value} for ${spec.name}`);
|
|
return value;
|
|
}, spec.defaults);
|
|
}
|
|
else {
|
|
subcommand = subcommand.option(spec.defaults ? `--${spec.name} <type>` : `--${spec.name}`, spec.description, spec.defaults);
|
|
}
|
|
});
|
|
subcommand = subcommand.action((...joinedArgs) => {
|
|
const args = joinedArgs.slice(0, -1);
|
|
const kwargs = joinedArgs.slice(-1)[0];
|
|
spec.exec(args, kwargs)
|
|
.then(() => {
|
|
resolve();
|
|
})
|
|
.catch((err) => {
|
|
reject(err);
|
|
});
|
|
});
|
|
});
|
|
program.parse(argv);
|
|
});
|
|
};
|
|
exports.run = run;
|