UNPKG

sda

Version:

Software development assistant

116 lines 5.1 kB
"use strict"; var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (Object.hasOwnProperty.call(mod, k)) result[k] = mod[k]; result["default"] = mod; return result; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const path = __importStar(require("path")); const getParams_1 = __importDefault(require("./command/getParams")); const getConfig_1 = __importDefault(require("./config/getConfig")); const getEnvironment_1 = require("./getEnvironment"); const Log_1 = __importDefault(require("./Log")); const Operations_1 = require("./operations/Operations"); class ExecutionConfig { constructor() { const args = process.argv.splice(2); this.isVerbose = checkBooleanFlag(args, '--verbose', '-v'); this.isSilent = checkBooleanFlag(args, '--silent', '-s'); Log_1.default.isVerbose = this.isVerbose; const currentDir = path.normalize(process.cwd()); this.config = getConfig_1.default(currentDir, getArgsConfigPath(args)); this.runInAllEnvironments = checkBooleanFlag(args, '--all', '-a', 0); this.parallelOperations = checkNumberFlag(args, '--parallel', '-p') || 1; this.operation = this.getOperation(args); if (this.operation === Operations_1.Operations.SetupEnvironment || this.operation === Operations_1.Operations.AttachEnvironment) { this.setupParameters = { templateId: args[0], path: args[1] }; } // Only try to get an environment for operations that require it if (!this.runInAllEnvironments && (this.operation === Operations_1.Operations.RunCommands || this.operation === Operations_1.Operations.ListCommands)) { this.env = getEnvironment_1.getEnvironment(this.config, args[0], currentDir); if (this.env.id === args[0]) { args.shift(); // Remove the environment from the arguments } } else { this.env = undefined; } this.params = getParams_1.default(args); // getParams already removes them from args this.commands = args; } /** * Returns the operation to execute. * * "sda" with no arguments - List environments * "sda list" - List environments * "sda -a list" - List commands for all environments * "sda <env> list" - List commands for an environment * Other scenarios just run the commands */ getOperation(args) { if (args.length === 0) { return Operations_1.Operations.Help; } if (!this.runInAllEnvironments && checkBooleanFlag(args, 'list', 'l', 0)) { return Operations_1.Operations.ListEnvironments; } if (checkBooleanFlag(args, 'list', 'l', this.runInAllEnvironments ? 0 : 1)) { return Operations_1.Operations.ListCommands; } if (checkBooleanFlag(args, 'listTemplates', 'lt', 0)) { return Operations_1.Operations.ListTemplates; } if (checkBooleanFlag(args, 'setup', 's')) { return Operations_1.Operations.SetupEnvironment; } if (checkBooleanFlag(args, 'attach', 'a')) { return Operations_1.Operations.AttachEnvironment; } if (checkBooleanFlag(args, 'help', 'h', 0)) { return Operations_1.Operations.Help; } return Operations_1.Operations.RunCommands; } } exports.default = ExecutionConfig; function getArgsConfigPath(args) { for (let i = 0; i < args.length - 1; i++) { if (args[i] === '--config' || args[i] === '-c') { const configPath = args.splice(i, 2)[1]; return configPath; } } } /** Checks a flag. Returns true if the flag is present and removes it from the args variable. */ function checkBooleanFlag(args, flag, shorthand, index) { const foundIndex = Math.max(args.indexOf(flag), shorthand ? args.indexOf(shorthand) : -1); if ((index !== undefined && foundIndex === index) || (index === undefined && foundIndex > -1)) { args.splice(foundIndex, 1); return true; } else { return false; } } /** Checks a flag. Returns true if the flag is present and removes it from the args variable. */ function checkNumberFlag(args, flag, shorthand, index) { const foundIndex = Math.max(args.indexOf(flag), shorthand ? args.indexOf(shorthand) : -1); if ((index !== undefined && foundIndex === index) || (index === undefined && foundIndex > -1)) { const removedArgs = args.splice(foundIndex, 2); return parseInt(removedArgs[1], undefined); } else { return undefined; } } //# sourceMappingURL=ExecutionConfig.js.map