@auttam/easycli
Version:
A quick and easy way of creating cli for your npm package.
72 lines (71 loc) • 3.44 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Program = void 0;
const settings_1 = require("./settings");
const program_config_1 = require("./config/program-config");
const Runtime = __importStar(require("./runtime/main"));
const runtime_error_1 = require("./errors/runtime-error");
const Help = __importStar(require("./help"));
class Program {
constructor(configuration) {
this.config = program_config_1.ProgramConfiguration.injectConfiguration(this);
if (this.config.decoratorsEnabled) {
this.config.verify();
}
this.config.merge(configuration);
Object.seal(this.config);
if (settings_1.SettingStore.processArgvStartIndex && isNaN(settings_1.SettingStore.processArgvStartIndex)) {
throw new runtime_error_1.RuntimeError('Cannot read command line arguments, invalid index', settings_1.SettingStore.processArgvStartIndex);
}
if (!settings_1.SettingStore.processArgvStartIndex || settings_1.SettingStore.processArgvStartIndex < 2) {
throw new runtime_error_1.RuntimeError('Index to start read command line arguments from must be greater than equal to 2', settings_1.SettingStore.processArgvStartIndex);
}
}
showHelp(commandName) {
if (commandName) {
return Help.command(this.config, commandName);
}
return Help.program(this.config);
}
showVersion() {
return Help.version(this.config);
}
static run(target) {
return __awaiter(this, void 0, void 0, function* () {
var context = Runtime.createContext(target, Program);
return Runtime.runProgram(target, context);
});
}
static settings(settings) {
settings_1.updateStore(settings);
}
}
exports.Program = Program;