UNPKG

@quo0/stiletto

Version:

With stiletto library you will be able to mock requests and choose between preconfigured responses right on the fly via UI

123 lines 5.8 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.StructureService = void 0; const tslib_1 = require("tslib"); const ioc_1 = require("../../../ioc"); const inversify_1 = require("inversify"); const path_1 = require("path"); const launch_setup_questions_1 = require("../../workflows/launch/launch-setup-questions"); let StructureService = class StructureService { constructor(StructureModel, fsService, processService, promptService) { this.StructureModel = StructureModel; this.fsService = fsService; this.processService = processService; this.promptService = promptService; this.defaultStructureModelConfig = { rootPath: this.processService.cwd(), setupPath: (0, path_1.join)(this.processService.cwd(), 'stiletto', 'default'), setupName: 'default', defaultExtension: 'js', // TODO: move to constants }; this.default = new this.StructureModel(this.defaultStructureModelConfig); } setCurrent(partialConfig) { return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () { const config = yield this.getConfigFromPartialData(partialConfig); this.current = new this.StructureModel(config); return this.current; }); } getConfigFromPartialData(partialConfig) { return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () { if (!partialConfig) { return this.defaultStructureModelConfig; } const { setupName, setupPath } = yield this.getSetupNameAndPath(partialConfig); return { rootPath: partialConfig.rootPath || this.defaultStructureModelConfig.rootPath, setupName, setupPath, defaultExtension: partialConfig.defaultExtension || this.defaultStructureModelConfig.defaultExtension, }; }); } getSetupNameAndPath({ setupPath, setupName }) { return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () { if (setupPath && setupName) { return { setupName, setupPath: (0, path_1.join)(this.processService.cwd(), setupPath), }; } if (setupPath) { const isSetupPath = yield this.checkPathIsSetupPath(setupPath); if (isSetupPath) { const name = setupPath.slice(setupPath.lastIndexOf('/') + 1); return { setupPath, setupName: name, }; } throw `Could not launch. Path ${setupPath} is not a stiletto setup path`; } if (setupName) { const pathByName = yield this.findSetupPathByName(setupName); const isSetupPath = yield this.checkPathIsSetupPath(pathByName); if (isSetupPath) { return { setupName, setupPath: pathByName, }; } throw `Could not find the setup ${setupName} . Path ${pathByName} is not a stiletto setup path`; } return this.defaultStructureModelConfig; }); } checkPathIsSetupPath(setupPath) { return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () { const absolutePath = (0, path_1.join)(this.processService.cwd(), setupPath); const isDir = (yield this.fsService.pathExists(absolutePath)) && (yield this.fsService.isDirectory(absolutePath)); if (isDir) { const files = yield this.fsService.readdir(absolutePath); return true && files.includes(this.default.ROUTES_FOLDER.name) && files.some(f => f.includes(this.default.SETUP_SETTINGS_CONFIG.name)); } return false; }); } findSetupPathByName(setupName) { return (0, tslib_1.__awaiter)(this, void 0, void 0, function* () { const rootConfigData = yield this.default.ROOT_CONFIG.read(); if (!rootConfigData || !rootConfigData.setups) { throw `Could not get the list of setups`; } const possiblePaths = rootConfigData.setups.filter(p => p.trim().endsWith(setupName)); if (possiblePaths.length === 0) { throw `Could not get the setup path of "${setupName}" setup`; } if (possiblePaths.length === 1) { return possiblePaths[0]; } const question = (0, launch_setup_questions_1.generateLaunchSetupQuestions)(possiblePaths, setupName); const choice = yield this.promptService.ask(question); if (choice) { return choice.setupPath; } else { throw `You didn't chose the setup path. Cannot launch`; } }); } }; StructureService = (0, tslib_1.__decorate)([ (0, inversify_1.injectable)(), (0, tslib_1.__param)(0, (0, inversify_1.inject)(ioc_1.TOKENS.structureModel)), (0, tslib_1.__param)(1, (0, inversify_1.inject)(ioc_1.TOKENS.fsService)), (0, tslib_1.__param)(2, (0, inversify_1.inject)(ioc_1.TOKENS.processService)), (0, tslib_1.__param)(3, (0, inversify_1.inject)(ioc_1.TOKENS.promptService)), (0, tslib_1.__metadata)("design:paramtypes", [Object, Object, Object, Object]) ], StructureService); exports.StructureService = StructureService; //# sourceMappingURL=structure.service.js.map