UNPKG

@ngrx/schematics

Version:

NgRx Schematics for Angular

125 lines 6.04 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 __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.default = default_1; const schematics_1 = require("@angular-devkit/schematics"); const ts = __importStar(require("typescript")); const schematics_core_1 = require("../../schematics-core"); function addStateToComponent(options) { return (host) => { if (!options.state && !options.stateInterface) { return host; } const statePath = `/${options.path}/${options.state}`; if (options.state) { if (!host.exists(statePath)) { throw new Error(`The Specified state path ${statePath} does not exist`); } } let componentPath = `/${options.path}/` + (options.flat ? '' : schematics_core_1.stringUtils.dasherize(options.name) + '/') + schematics_core_1.stringUtils.dasherize(options.name) + '.component.ts'; if (!host.exists(componentPath)) { componentPath = `/${options.path}/` + (options.flat ? '' : schematics_core_1.stringUtils.dasherize(options.name) + '/') + schematics_core_1.stringUtils.dasherize(options.name) + '.ts'; if (!host.exists(componentPath)) { throw new schematics_1.SchematicsException(`File ${componentPath} does not exist.`); } } const text = host.read(componentPath); if (text === null) { throw new schematics_1.SchematicsException(`File content ${componentPath} does not exist.`); } const sourceText = text.toString('utf-8'); const source = ts.createSourceFile(componentPath, sourceText, ts.ScriptTarget.Latest, true); const stateImportPath = (0, schematics_core_1.buildRelativePath)(componentPath, statePath); const storeImport = (0, schematics_core_1.insertImport)(source, componentPath, 'Store', '@ngrx/store'); const stateImport = options.state ? (0, schematics_core_1.insertImport)(source, componentPath, `* as fromStore`, stateImportPath, true) : new schematics_core_1.NoopChange(); const componentClass = source.statements.find((stm) => stm.kind === ts.SyntaxKind.ClassDeclaration); const constructorUpdate = new schematics_core_1.ReplaceChange(componentPath, componentClass.members.pos, '\n', `\n constructor(private store: Store) {}`); const changes = [storeImport, stateImport, constructorUpdate]; const recorder = host.beginUpdate(componentPath); for (const change of changes) { if (change instanceof schematics_core_1.InsertChange) { recorder.insertLeft(change.pos, change.toAdd); } else if (change instanceof schematics_core_1.ReplaceChange) { recorder.remove(change.pos, change.oldText.length); recorder.insertLeft(change.order, change.newText); } } host.commitUpdate(recorder); return host; }; } function default_1(options) { return (host, context) => { options.path = (0, schematics_core_1.getProjectPath)(host, options); const parsedPath = (0, schematics_core_1.parseName)(options.path, options.name); options.name = parsedPath.name; options.path = parsedPath.path; const opts = ['state', 'stateInterface', 'testDepth'].reduce((current, key) => { return (0, schematics_core_1.omit)(current, key); }, options); const templateSource = (0, schematics_1.apply)((0, schematics_1.url)(options.testDepth === 'unit' ? './files' : './integration-files'), [ options.skipTests ? (0, schematics_1.filter)((path) => !path.endsWith('.spec.ts.template')) : (0, schematics_1.noop)(), (0, schematics_1.applyTemplates)({ 'if-flat': (s) => (options.flat ? '' : s), ...schematics_core_1.stringUtils, ...options, }), (0, schematics_1.move)(parsedPath.path), ]); // Remove all undefined values to use the schematic defaults (in angular.json or the Angular schema) Object.keys(opts).forEach((key) => opts[key] === undefined ? delete opts[key] : {}); return (0, schematics_1.chain)([ (0, schematics_1.externalSchematic)('@schematics/angular', 'component', { ...opts, skipTests: true, }), addStateToComponent(options), (0, schematics_1.mergeWith)(templateSource), ])(host, context); }; } //# sourceMappingURL=index.js.map