|
| 1 | +import { strings } from '@angular-devkit/core'; |
| 2 | +import { Rule, SchematicsException, Tree, apply, branchAndMerge, chain, filter, mergeWith, move, noop, template, url } from '@angular-devkit/schematics'; |
| 3 | +import { addDeclarationToModule, addEntryComponentToModule, addExportToModule, addSymbolToNgModuleMetadata } from '@schematics/angular/utility/ast-utils'; |
| 4 | +import { InsertChange } from '@schematics/angular/utility/change'; |
| 5 | +import { buildRelativePath } from '@schematics/angular/utility/find-module'; |
| 6 | +import { parseName } from '@schematics/angular/utility/parse-name'; |
| 7 | +import { buildDefaultPath, getProject } from '@schematics/angular/utility/project'; |
| 8 | +import { validateHtmlSelector, validateName } from '@schematics/angular/utility/validation'; |
| 9 | +import * as ts from 'typescript'; |
| 10 | + |
| 11 | +import { buildSelector } from '../util'; |
| 12 | + |
| 13 | +import { Schema as ComponentOptions } from './schema'; |
| 14 | + |
| 15 | +function readIntoSourceFile(host: Tree, modulePath: string): ts.SourceFile { |
| 16 | + const text = host.read(modulePath); |
| 17 | + if (text === null) { |
| 18 | + throw new SchematicsException(`File ${modulePath} does not exist.`); |
| 19 | + } |
| 20 | + const sourceText = text.toString('utf-8'); |
| 21 | + |
| 22 | + return ts.createSourceFile(modulePath, sourceText, ts.ScriptTarget.Latest, true); |
| 23 | +} |
| 24 | + |
| 25 | +function addImportToNgModule(options: ComponentOptions): Rule { |
| 26 | + return (host: Tree) => { |
| 27 | + if (!options.module) { |
| 28 | + return host; |
| 29 | + } |
| 30 | + if (!options.createModule && options.module) { |
| 31 | + addImportToDeclarations(host, options); |
| 32 | + } |
| 33 | + if (options.createModule && options.module) { |
| 34 | + addImportToImports(host, options); |
| 35 | + } |
| 36 | + return host; |
| 37 | + }; |
| 38 | +} |
| 39 | + |
| 40 | +function addImportToDeclarations(host: Tree, options: ComponentOptions): void { |
| 41 | + if (options.module) { |
| 42 | + const modulePath = options.module; |
| 43 | + let source = readIntoSourceFile(host, modulePath); |
| 44 | + |
| 45 | + const componentPath = `/${options.path}/` |
| 46 | + + (options.flat ? '' : strings.dasherize(options.name) + '/') |
| 47 | + + strings.dasherize(options.name) |
| 48 | + + '.component'; |
| 49 | + const relativePath = buildRelativePath(modulePath, componentPath); |
| 50 | + const classifiedName = strings.classify(`${options.name}Component`); |
| 51 | + const declarationChanges = addDeclarationToModule(source, |
| 52 | + modulePath, |
| 53 | + classifiedName, |
| 54 | + relativePath); |
| 55 | + |
| 56 | + const declarationRecorder = host.beginUpdate(modulePath); |
| 57 | + for (const change of declarationChanges) { |
| 58 | + if (change instanceof InsertChange) { |
| 59 | + declarationRecorder.insertLeft(change.pos, change.toAdd); |
| 60 | + } |
| 61 | + } |
| 62 | + host.commitUpdate(declarationRecorder); |
| 63 | + |
| 64 | + if (options.export) { |
| 65 | + // Need to refresh the AST because we overwrote the file in the host. |
| 66 | + source = readIntoSourceFile(host, modulePath); |
| 67 | + |
| 68 | + const exportRecorder = host.beginUpdate(modulePath); |
| 69 | + const exportChanges = addExportToModule(source, modulePath, |
| 70 | + strings.classify(`${options.name}Component`), |
| 71 | + relativePath); |
| 72 | + |
| 73 | + for (const change of exportChanges) { |
| 74 | + if (change instanceof InsertChange) { |
| 75 | + exportRecorder.insertLeft(change.pos, change.toAdd); |
| 76 | + } |
| 77 | + } |
| 78 | + host.commitUpdate(exportRecorder); |
| 79 | + } |
| 80 | + |
| 81 | + if (options.entryComponent) { |
| 82 | + // Need to refresh the AST because we overwrote the file in the host. |
| 83 | + source = readIntoSourceFile(host, modulePath); |
| 84 | + |
| 85 | + const entryComponentRecorder = host.beginUpdate(modulePath); |
| 86 | + const entryComponentChanges = addEntryComponentToModule( |
| 87 | + source, modulePath, |
| 88 | + strings.classify(`${options.name}Component`), |
| 89 | + relativePath); |
| 90 | + |
| 91 | + for (const change of entryComponentChanges) { |
| 92 | + if (change instanceof InsertChange) { |
| 93 | + entryComponentRecorder.insertLeft(change.pos, change.toAdd); |
| 94 | + } |
| 95 | + } |
| 96 | + host.commitUpdate(entryComponentRecorder); |
| 97 | + } |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +function addImportToImports(host: Tree, options: ComponentOptions): void { |
| 102 | + if (options.module) { |
| 103 | + const modulePath = options.module; |
| 104 | + const moduleSource = readIntoSourceFile(host, modulePath); |
| 105 | + |
| 106 | + const componentModulePath = `/${options.path}/` |
| 107 | + + (options.flat ? '' : strings.dasherize(options.name) + '/') |
| 108 | + + strings.dasherize(options.name) |
| 109 | + + '.module'; |
| 110 | + |
| 111 | + const relativePath = buildRelativePath(modulePath, componentModulePath); |
| 112 | + const classifiedName = strings.classify(`${options.name}ComponentModule`); |
| 113 | + const importChanges = addSymbolToNgModuleMetadata(moduleSource, modulePath, 'imports', classifiedName, relativePath); |
| 114 | + |
| 115 | + const importRecorder = host.beginUpdate(modulePath); |
| 116 | + for (const change of importChanges) { |
| 117 | + if (change instanceof InsertChange) { |
| 118 | + importRecorder.insertLeft(change.pos, change.toAdd); |
| 119 | + } |
| 120 | + } |
| 121 | + host.commitUpdate(importRecorder); |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +export default function(options: ComponentOptions): Rule { |
| 126 | + return (host, context) => { |
| 127 | + if (!options.project) { |
| 128 | + throw new SchematicsException('Option (project) is required.'); |
| 129 | + } |
| 130 | + |
| 131 | + const project = getProject(host, options.project); |
| 132 | + |
| 133 | + if (options.path === undefined) { |
| 134 | + options.path = buildDefaultPath(project); |
| 135 | + } |
| 136 | + |
| 137 | + const parsedPath = parseName(options.path, options.name); |
| 138 | + options.name = parsedPath.name; |
| 139 | + options.path = parsedPath.path; |
| 140 | + options.selector = options.selector ? options.selector : buildSelector(options, project.prefix); |
| 141 | + |
| 142 | + validateName(options.name); |
| 143 | + validateHtmlSelector(options.selector); |
| 144 | + |
| 145 | + const templateSource = apply(url('./files'), [ |
| 146 | + options.spec ? noop() : filter(p => !p.endsWith('.spec.ts')), |
| 147 | + options.createModule ? noop() : filter(p => !p.endsWith('.module.ts')), |
| 148 | + template({ |
| 149 | + ...strings, |
| 150 | + 'if-flat': (s: string) => options.flat ? '' : s, |
| 151 | + ...options, |
| 152 | + }), |
| 153 | + move(parsedPath.path), |
| 154 | + ]); |
| 155 | + |
| 156 | + return chain([ |
| 157 | + branchAndMerge(chain([ |
| 158 | + addImportToNgModule(options), |
| 159 | + mergeWith(templateSource), |
| 160 | + ])), |
| 161 | + ])(host, context); |
| 162 | + }; |
| 163 | +} |
0 commit comments