UNPKG

@necord/schematics

Version:

A collection of schematics for Necord projects with NestJS

75 lines (74 loc) 3.55 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __exportStar = (this && this.__exportStar) || function(m, exports) { for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p); }; 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"); __exportStar(require("./common-options.interface"), exports); 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;