UNPKG

sda

Version:

Software development assistant

85 lines 3.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 fs = __importStar(require("fs")); const path = __importStar(require("path")); const addEnvironmentToHomeConfig_1 = __importDefault(require("../config/addEnvironmentToHomeConfig")); const withId_1 = __importDefault(require("../interfaces/withId")); const Log_1 = __importDefault(require("../Log")); function attachEnvironmentAfterExecute(ec, func) { const params = validateParams(ec); const envId = getEnvId(ec.config, params.template.id); ec.env = { id: envId, path: params.path, templateId: params.template.id, template: params.template }; func(params); addEnvironmentToHomeConfig_1.default(ec.env); } exports.default = attachEnvironmentAfterExecute; /** * Throws if the params are not valid. * Returns the validated params with normalized path if they are valid. */ function validateParams(ec) { const setupParameters = ec.setupParameters; if (!setupParameters) { throw new Error('No parameters passed for the operation.'); } const destinationPath = getDestinationPath(setupParameters.path); if (!fs.existsSync(destinationPath)) { Log_1.default.verbose(`Creating missing directory "${destinationPath}"`); fs.mkdirSync(destinationPath); } if (!ec.config.templates[setupParameters.templateId]) { throw new Error(`Template "${setupParameters.templateId}" doesn't exist.`); } const template = withId_1.default(setupParameters.templateId, ec.config.templates[setupParameters.templateId]); return { template, path: destinationPath }; } /** * Returns the destination path for the new environment in a normalized way. * If defined it makes it absolute, otherwise it uses the current path. */ function getDestinationPath(inputPath) { const currentDir = path.normalize(process.cwd()); if (!inputPath) { return currentDir; } if (path.isAbsolute(inputPath)) { return path.normalize(inputPath); } else { return path.normalize(path.join(currentDir, inputPath)); } } /** * Returns an environment id. * It's either the same as the template id, or if that is used, the first available * name "<templateId>-<number>". */ function getEnvId(config, templateId) { if (!config.environments[templateId]) { return templateId; } let i = 0; while (config.environments[`${templateId}-${i}`]) { i++; } return `${templateId}-${i}`; } //# sourceMappingURL=attachEnvironmentAfterExecute.js.map