@ton-actions/tondev-contest
Version:
TON Dev Environment
196 lines • 7.21 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.run = void 0;
const core_1 = require("../core");
const controllers_1 = require("../controllers");
const utils_1 = require("../core/utils");
const help_1 = require("./help");
const summary_info_1 = require("./summary-info");
function findOptionArg(command, name) {
var _a, _b;
if (name.startsWith("--")) {
name = name.substr(2).toLowerCase();
return (_a = command.args) === null || _a === void 0 ? void 0 : _a.find(x => !x.isArg && x.name === name);
}
if (name.startsWith("-")) {
name = name.substr(1).toLowerCase();
return (_b = command.args) === null || _b === void 0 ? void 0 : _b.find(x => !x.isArg && x.alias === name);
}
return undefined;
}
class CommandLine {
constructor() {
this.args = {};
this.controller = undefined;
this.command = undefined;
this.positional = [];
this.unresolved = new Map();
this.pending = undefined;
this.printSummaryInfo = false;
}
setArgValue(arg, value) {
const name = arg.name
.split("-")
.map((x, i) => i > 0 ? (x.substr(0, 1).toUpperCase() + x.substr(1)) : x)
.join("");
this.args[name] = value;
}
async resolveValue(arg, value) {
if (arg.type === "boolean" && value === undefined) {
value = "true";
}
if (value === undefined) {
throw new Error(`Missing value for ${arg.name}`);
}
let resolved;
if (arg.type === "boolean") {
resolved = value.toLowerCase() === "true";
}
else {
resolved = value;
}
const variants = await core_1.getArgVariants(arg);
if (variants && !variants.find(x => x.value === value)) {
throw await missingArgError(arg);
}
this.setArgValue(arg, resolved);
this.unresolved.delete(arg.name);
const i = this.positional.indexOf(arg);
if (i >= 0) {
this.positional.splice(i, 1);
}
if (this.pending === arg) {
this.pending = undefined;
}
}
async resolveDefault(arg) {
if (arg.defaultValue !== undefined) {
this.setArgValue(arg, arg.type === "boolean" ? arg.defaultValue === "true" : arg.defaultValue);
}
else if (arg.type === "folder") {
this.setArgValue(arg, process.cwd());
}
else {
throw await missingArgError(arg);
}
}
async parseOptionName(name) {
if (this.pending) {
await this.resolveValue(this.pending, undefined);
}
const optionName = name.toLowerCase();
if (optionName === "--help" || optionName === "-h") {
this.args.help = true;
}
else if (this.command) {
this.pending = findOptionArg(this.command, optionName);
if (!this.pending) {
throw new Error(`Unknown option ${optionName}`);
}
if (this.pending.type === "boolean") {
await this.resolveValue(this.pending, undefined);
}
}
else if (this.controller) {
throw new Error(`Unexpected option ${optionName} before command name.`);
}
else {
throw new Error(`Unexpected option ${optionName} before tool name.`);
}
}
setCommand(command) {
var _a;
this.command = command;
for (const arg of (_a = this.command.args) !== null && _a !== void 0 ? _a : []) {
this.unresolved.set(arg.name, arg);
if (arg.isArg) {
this.positional.push(arg);
}
}
}
async parse(programArgs) {
for (let arg of programArgs) {
if (arg.startsWith("-") && !this.pending) {
await this.parseOptionName(arg);
}
else {
arg = arg.trim();
if (this.pending) {
await this.resolveValue(this.pending, arg);
}
else if (this.controller && this.command) {
if (this.positional.length === 0) {
throw new Error(`Unexpected argument ${arg}`);
}
await this.resolveValue(this.positional[0], arg);
}
else if (this.controller) {
const command = this.controller.commands.find(x => core_1.matchName(x, arg));
if (command) {
this.setCommand(command);
}
else {
throw new Error(`Unknown command: ${arg}`);
}
}
else {
this.controller = controllers_1.controllers.find(x => core_1.matchName(x, arg));
if (!this.controller) {
const byAlias = controllers_1.findControllerAndCommandByAlias(arg);
if (byAlias) {
this.controller = byAlias.controller;
this.setCommand(byAlias.command);
}
else if (arg.toLowerCase().trim() === "info") {
this.printSummaryInfo = true;
break;
}
else {
throw new Error(`Unknown tool: ${arg}.`);
}
}
}
}
}
if (this.pending) {
await this.resolveValue(this.pending, undefined);
}
if (this.args.help || this.printSummaryInfo) {
return;
}
for (const arg of this.unresolved.values()) {
await this.resolveDefault(arg);
}
}
}
async function missingArgError(arg) {
const variants = await core_1.getArgVariants(arg);
const variantsString = variants
? "\n" +
utils_1.formatTable([
["Available variants:", ""],
...variants.map(x => { var _a; return [x.value, (_a = x.description) !== null && _a !== void 0 ? _a : ""]; }),
], { headerSeparator: true })
: "";
throw new Error(`Missing required ${arg.name}${variantsString}`);
}
async function run() {
const parser = new CommandLine();
await parser.parse(process.argv.slice(2));
if (parser.printSummaryInfo) {
await summary_info_1.printSummaryInfo();
return;
}
const { controller, command, args, } = parser;
if (!controller || !command) {
await help_1.printUsage(controller, command);
return;
}
if (parser.args.help) {
await help_1.printUsage(controller, command);
return;
}
await command.run(utils_1.consoleTerminal, args);
}
exports.run = run;
//# sourceMappingURL=index.js.map