UNPKG

@auttam/easycli

Version:

A quick and easy way of creating cli for your npm package.

100 lines (99 loc) 4.56 kB
"use strict"; 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.runProgram = exports.getContext = exports.createContext = exports.handleRejections = void 0; const settings_1 = require("../settings"); const context_1 = require("./context"); const program_config_1 = require("../config/program-config"); var rejectionHandlerAttached = false; const contextPropertyName = '__contextID'; const contextContainer = {}; function handleRejections(handler) { if (handler && typeof handler == 'function' && !rejectionHandlerAttached) { process.on('unhandledRejection', handler); rejectionHandlerAttached = true; } } exports.handleRejections = handleRejections; function validateProgram(target) { if (!target) { throw new TypeError('Unable to create runtime context, target program is null'); } if (!target.constructor) { throw new TypeError('Unable to create runtime context, constructor is missing'); } if (!target.constructor.name) { throw new TypeError('Unable to create runtime context, constructor name missing'); } if (target.constructor.name == 'Program') { throw new TypeError('Unable to create runtime context, target program is not derived from Program Class'); } } function createContext(target, type) { validateProgram(target); if ((target instanceof type) == false) { throw new TypeError('Unable to create runtime context, target program is not an instance of Program Class'); } if (target.constructor[contextPropertyName]) { throw new TypeError('Unable to create runtime context, context already created'); } var context = new context_1.RuntimeContext(target); target.constructor[contextPropertyName] = target.constructor.name + context.contextId; contextContainer[target.constructor[contextPropertyName]] = context; return context; } exports.createContext = createContext; function getContext(target) { validateProgram(target); if (!target.constructor[contextPropertyName]) { throw new TypeError('Unable to get execution context, context not created'); } return contextContainer[target.constructor[contextPropertyName]]; } exports.getContext = getContext; function runProgram(program, context) { return __awaiter(this, void 0, void 0, function* () { handleRejections(settings_1.SettingStore.rejectionHandler); context = context || getContext(program); if (!context) { throw new TypeError('Cannot run program, context missing'); } if (!program.constructor[contextPropertyName] || program.constructor[contextPropertyName].indexOf(context.contextId) < 0) { throw new TypeError('Cannot run program, context mismatch'); } if (context.state != context_1.RuntimeStates.CREATED) { throw new TypeError('Cannot run program due to incorrect program state'); } if (!program.config || !(program.config instanceof program_config_1.ProgramConfiguration)) { throw new SyntaxError('Cannot run program, configuration missing'); } context.init(); if (context.helpRequested) { return program.showHelp(context.requestedCommand); } if (context.versionRequested) { return program.showVersion(); } var error, executionResult, exitCode = 0; try { executionResult = yield context.runProgram(); } catch (err) { if (typeof err == 'string') err = 'Error: ' + err; error = err; exitCode = 1; } return yield context.exitProgram(error, executionResult, exitCode); }); } exports.runProgram = runProgram;