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