UNPKG

ng-afelio

Version:
85 lines (84 loc) 4.01 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const core_1 = require("@angular-devkit/core"); const schematics_1 = require("@angular-devkit/schematics"); const find_module_1 = require("@schematics/angular/utility/find-module"); const parse_name_1 = require("@schematics/angular/utility/parse-name"); const workspace_1 = require("@schematics/angular/utility/workspace"); const ts = require("typescript"); const ast_util_1 = require("../util/ast-util"); const barrel_1 = require("../util/barrel"); const change_1 = require("../util/change"); const validation_1 = require("../util/validation"); function getStoreNode(source) { const keywords = (0, ast_util_1.findNodes)(source, ts.SyntaxKind.VariableStatement); for (const keyword of keywords) { if (ts.isVariableStatement(keyword)) { const [declaration] = keyword.declarationList.declarations; if (ts.isVariableDeclaration(declaration) && declaration.initializer && declaration.name.getText() === 'stores') { return declaration.initializer.getChildAt(1); } } } } function addIntoIndex(path, options) { return host => { if (options.barrel) { const changes = []; const indexPath = (0, core_1.join)(path, 'index.ts'); const text = host.read(indexPath); if (!text) { throw new schematics_1.SchematicsException(`Can not add to barrel, ${indexPath} does not exist.`); } const sourceText = text.toString('utf8'); const source = ts.createSourceFile(indexPath, sourceText, ts.ScriptTarget.Latest, true); // Add Store to ts import const storePath = (0, core_1.join)(path, options.name, `${core_1.strings.dasherize(options.name)}.store`); const relativeStorePath = (0, find_module_1.buildRelativePath)(indexPath, `${storePath}.ts`).slice(0, -3); const stateName = `${core_1.strings.classify(options.name)}State`; changes.push((0, ast_util_1.insertImport)(source, indexPath, stateName, relativeStorePath)); // Add Store to Barrel array const node = getStoreNode(source); if (node) { const commat = node.getChildCount() > 0 ? ',' : ''; changes.push(new change_1.InsertChange(indexPath, node.getEnd(), `${commat}\n ${stateName}`)); } // Save changes (0, change_1.applyChangesToHost)(host, indexPath, changes); } return host; }; } function default_1(options) { return async (host) => { if (!options.project) { throw new schematics_1.SchematicsException('Option (project) is required.'); } const workspace = await (0, workspace_1.getWorkspace)(host); const project = workspace.projects.get(options.project); if (project && options.path === undefined) { options.path = (0, workspace_1.buildDefaultPath)(project); } const parsedPath = (0, parse_name_1.parseName)(options.path, options.name); options.name = parsedPath.name; options.path = (0, barrel_1.relativeCwdFromRelativeProjectPath)(parsedPath.path); (0, validation_1.validateName)(options.name); const templateSource = (0, schematics_1.apply)((0, schematics_1.url)('./files'), [ options.spec ? (0, schematics_1.noop)() : (0, schematics_1.filter)(p => !p.endsWith('.spec.ts')), (0, schematics_1.template)({ ...core_1.strings, ...options, }), (0, schematics_1.move)(parsedPath.path), ]); return (0, schematics_1.chain)([ (0, schematics_1.branchAndMerge)((0, schematics_1.chain)([ (0, schematics_1.mergeWith)(templateSource), addIntoIndex(options.path, options), ])), ]); }; } exports.default = default_1;