UNPKG

@ionic/angular-toolkit

Version:

Schematics for @ionic/angular apps.

139 lines (138 loc) 6.95 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = default_1; 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 validation_1 = require("@schematics/angular/utility/validation"); const workspace_1 = require("@schematics/angular/utility/workspace"); const ts = require("typescript"); const util_1 = require("../util"); const ast_util_1 = require("../util/ast-util"); const change_1 = require("../util/change"); function readIntoSourceFile(host, modulePath) { const text = host.read(modulePath); if (text === null) { throw new schematics_1.SchematicsException(`File ${modulePath} does not exist.`); } const sourceText = text.toString('utf-8'); return ts.createSourceFile(modulePath, sourceText, ts.ScriptTarget.Latest, true); } function addImportToNgModule(options) { return (host) => { if (!options.module) { return host; } if (!options.createModule && options.module) { addImportToDeclarations(host, options); } if (options.createModule && options.module) { addImportToImports(host, options); } return host; }; } function addImportToDeclarations(host, options) { if (options.module) { const modulePath = options.module; let source = readIntoSourceFile(host, modulePath); const componentPath = `/${options.path}/` + (options.flat ? '' : core_1.strings.dasherize(options.name) + '/') + core_1.strings.dasherize(options.name) + '.component'; const relativePath = (0, find_module_1.buildRelativePath)(modulePath, componentPath); const classifiedName = core_1.strings.classify(`${options.name}Component`); const declarationChanges = (0, ast_util_1.addDeclarationToModule)(source, modulePath, classifiedName, relativePath); const declarationRecorder = host.beginUpdate(modulePath); for (const change of declarationChanges) { if (change instanceof change_1.InsertChange) { declarationRecorder.insertLeft(change.pos, change.toAdd); } } host.commitUpdate(declarationRecorder); if (options.export) { // Need to refresh the AST because we overwrote the file in the host. source = readIntoSourceFile(host, modulePath); const exportRecorder = host.beginUpdate(modulePath); const exportChanges = (0, ast_util_1.addExportToModule)(source, modulePath, core_1.strings.classify(`${options.name}Component`), relativePath); for (const change of exportChanges) { if (change instanceof change_1.InsertChange) { exportRecorder.insertLeft(change.pos, change.toAdd); } } host.commitUpdate(exportRecorder); } if (options.entryComponent) { // Need to refresh the AST because we overwrote the file in the host. source = readIntoSourceFile(host, modulePath); const entryComponentRecorder = host.beginUpdate(modulePath); const entryComponentChanges = (0, ast_util_1.addEntryComponentToModule)(source, modulePath, core_1.strings.classify(`${options.name}Component`), relativePath); for (const change of entryComponentChanges) { if (change instanceof change_1.InsertChange) { entryComponentRecorder.insertLeft(change.pos, change.toAdd); } } host.commitUpdate(entryComponentRecorder); } } } function addImportToImports(host, options) { if (options.module) { const modulePath = options.module; const moduleSource = readIntoSourceFile(host, modulePath); const componentModulePath = `/${options.path}/` + (options.flat ? '' : core_1.strings.dasherize(options.name) + '/') + core_1.strings.dasherize(options.name) + '.module'; const relativePath = (0, find_module_1.buildRelativePath)(modulePath, componentModulePath); const classifiedName = core_1.strings.classify(`${options.name}ComponentModule`); const importChanges = (0, ast_util_1.addSymbolToNgModuleMetadata)(moduleSource, modulePath, 'imports', classifiedName, relativePath); const importRecorder = host.beginUpdate(modulePath); for (const change of importChanges) { if (change instanceof change_1.InsertChange) { importRecorder.insertLeft(change.pos, change.toAdd); } } host.commitUpdate(importRecorder); } } function default_1(options) { return async (host) => { var _a; const workspace = await (0, workspace_1.getWorkspace)(host); const project = workspace.projects.get(options.project); if (!project) { throw new schematics_1.SchematicsException(`Project "${options.project}" does not exist.`); } 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 = parsedPath.path; options.selector = options.selector ? options.selector : (0, util_1.buildSelector)(options, (_a = project === null || project === void 0 ? void 0 : project.prefix) !== null && _a !== void 0 ? _a : 'app'); (0, validation_1.validateHtmlSelector)(options.selector); 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.template')), options.createModule ? (0, schematics_1.noop)() : (0, schematics_1.filter)((p) => !p.endsWith('.module.ts.template')), (0, schematics_1.applyTemplates)({ ...core_1.strings, 'if-flat': (s) => (options.flat ? '' : s), ...options, }), !options.type ? (0, schematics_1.forEach)(((file) => { return file.path.includes('..') ? { content: file.content, path: file.path.replace('..', '.'), } : file; })) : (0, schematics_1.noop)(), (0, schematics_1.move)(parsedPath.path), ]); return (0, schematics_1.chain)([(0, schematics_1.branchAndMerge)((0, schematics_1.chain)([addImportToNgModule(options), (0, schematics_1.mergeWith)(templateSource)]))]); }; }