UNPKG

nest-commander-schematics

Version:

A set of schematics for generating questions and commands with the NestJS CLI.

68 lines 2.96 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.CommonSchematicFactory = void 0; const core_1 = require("@angular-devkit/core"); const schematics_1 = require("@angular-devkit/schematics"); const schematics_2 = require("@nestjs/schematics"); const path_1 = require("path"); class CommonSchematicFactory { constructor() { this.templatePath = './files'; this.type = 'service'; this.metadata = 'providers'; } create(options) { options = this.transform(options); return (0, schematics_1.branchAndMerge)((0, schematics_1.chain)([ (0, schematics_2.mergeSourceRoot)(options), this.addDeclarationToModule(options), (0, schematics_1.mergeWith)(this.generate(options)), ])); } generate(options) { return (context) => { var _a; return (0, schematics_1.apply)((0, schematics_1.url)((0, path_1.join)(this.templatePath)), [ options.spec ? (0, schematics_1.noop)() : (0, schematics_1.filter)((path) => !path.endsWith('.spec.ts')), (0, schematics_1.applyTemplates)(Object.assign(Object.assign(Object.assign({}, core_1.strings), options), { lowercase: (str) => str.toLowerCase() })), (0, schematics_1.move)((_a = options.path) !== null && _a !== void 0 ? _a : ''), ])(context); }; } addDeclarationToModule(options) { return (tree) => { var _a; options.module = new schematics_2.ModuleFinder(tree).find({ name: options.name, path: options.path, }); if (options.module === undefined || options.module === null) { return tree; } const rawContent = tree.read(options.module); const content = (_a = rawContent === null || rawContent === void 0 ? void 0 : rawContent.toString()) !== null && _a !== void 0 ? _a : ''; const declarator = new schematics_2.ModuleDeclarator(); tree.overwrite(options.module, declarator.declare(content, options)); return tree; }; } transform(source) { const target = Object.assign({}, source); target.metadata = this.metadata; target.type = this.type; if (target.name === null || target.name === undefined) { throw new schematics_1.SchematicsException('Option (name) is required.'); } const location = new schematics_2.NameParser().parse(target); target.name = core_1.strings.dasherize(location.name); target.path = core_1.strings.dasherize(location.path); target.path = target.flat ? target.path : (0, path_1.join)(target.path, target.name); return target; } } exports.CommonSchematicFactory = CommonSchematicFactory; //# sourceMappingURL=index.js.map