UNPKG

@nestia/sdk

Version:

Nestia SDK and Swagger generator

109 lines 5.22 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.NestiaSdkCommand = void 0; const NestiaSdkApplication_1 = require("../../NestiaSdkApplication"); const NestiaConfigLoader_1 = require("./NestiaConfigLoader"); const NestiaSdkWatcher_1 = require("./NestiaSdkWatcher"); var NestiaSdkCommand; (function (NestiaSdkCommand) { NestiaSdkCommand.sdk = () => main({ title: "SDK library", generate: (app) => app.sdk(), validate: (config) => !!config.output, solution: "configure INestiaConfig.output property.", }); NestiaSdkCommand.swagger = () => main({ title: "Swagger Document", generate: (app) => app.swagger(), validate: (config) => { var _a; return !!((_a = config.swagger) === null || _a === void 0 ? void 0 : _a.output); }, solution: "configure INestiaConfig.swagger property.", watch: hasFlagArgument("watch"), }); NestiaSdkCommand.e2e = () => main({ title: "E2E Functions", generate: (app) => app.e2e(), validate: (config) => !!config.e2e, solution: [ "configure two properties:", "", " - INestiaConfig.output", " - INestiaConfig.e2e", ].join("\n"), }); NestiaSdkCommand.all = () => main({ title: "everything", generate: (app) => app.all(), validate: () => true, solution: [ "configure at least one property of below:", "", " - INestiaConfig.output", " - INestiaConfig.swagger.output", ].join("\n"), }); const main = (props) => __awaiter(this, void 0, void 0, function* () { var _a, _b; // LOAD CONFIG INFO const project = (_a = getFileArgument({ type: "project", extension: "json", })) !== null && _a !== void 0 ? _a : "tsconfig.json"; process.env.NESTIA_PROJECT = project; const configFile = (_b = getFileArgument({ type: "config", extension: "ts", })) !== null && _b !== void 0 ? _b : "nestia.config.ts"; const configurations = () => __awaiter(this, void 0, void 0, function* () { var _a; const command = yield NestiaConfigLoader_1.NestiaConfigLoader.compilerOptions(project); return NestiaConfigLoader_1.NestiaConfigLoader.configurations(configFile, (_a = command.raw.compilerOptions) !== null && _a !== void 0 ? _a : {}); }); const generate = (configurations) => __awaiter(this, void 0, void 0, function* () { if (configurations.length > 1 && configurations.some(props.validate) === false) throw new Error(`Every configurations are invalid to generate ${props.title}, ${props.solution}`); for (const config of configurations) { if (configurations.length > 1 && props.validate(config) === false) continue; const app = new NestiaSdkApplication_1.NestiaSdkApplication(config); yield props.generate(app); } }); if (props.watch === true) return NestiaSdkWatcher_1.NestiaSdkWatcher.watch({ configFile, configurations, generate, projectFile: project, }); yield generate(yield configurations()); }); const getFileArgument = (props) => { const argv = process.argv.slice(3); const index = argv.findIndex((str) => str === `--${props.type}`); if (index === -1) return null; // Bound the VALUE position, not the whole argument list. The two only // coincide when the flag is the sole argument, so any preceding token used // to send `undefined` into endsWith() and report a TypeError instead of this // diagnostic. A following flag is a missing value too, not a badly named // file. const file = argv[index + 1]; if (file === undefined || file.startsWith("--")) throw new Error(`${props.type} file must be provided`); if (file.endsWith(props.extension) === false) throw new Error(`${props.type} file must be ${props.extension} file`); return file; }; const hasFlagArgument = (type) => process.argv.slice(3).some((str) => str === `--${type}`); })(NestiaSdkCommand || (exports.NestiaSdkCommand = NestiaSdkCommand = {})); //# sourceMappingURL=NestiaSdkCommand.js.map