-
Notifications
You must be signed in to change notification settings - Fork 79
/
registration.ts
201 lines (175 loc) · 6.02 KB
/
registration.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import { strings } from '@angular-devkit/core';
import { Rule, UpdateRecorder } from '@angular-devkit/schematics';
import {
addDeclarationToModule,
addEntryComponentToModule,
addExportToModule,
addImportToModule,
addProviderToModule,
} from '@schematics/angular/utility/ast-utils';
import { InsertChange } from '@schematics/angular/utility/change';
import { buildRelativePath } from '@schematics/angular/utility/find-module';
import { ImportKind, findImports, forEachToken } from 'tsutils';
import * as ts from 'typescript';
import { readIntoSourceFile } from './filesystem';
export function addExportToNgModule(options: {
module?: string;
artifactName?: string;
moduleImportPath?: string;
}): Rule {
return host => {
const relativePath = buildRelativePath(options.module, options.moduleImportPath);
const source = readIntoSourceFile(host, options.module);
const exportRecorder = host.beginUpdate(options.module);
const exportChanges = addExportToModule(
source,
options.module,
strings.classify(options.artifactName),
relativePath
);
for (const change of exportChanges) {
if (change instanceof InsertChange) {
exportRecorder.insertLeft(change.pos, change.toAdd);
}
}
host.commitUpdate(exportRecorder);
};
}
export function addImportToNgModule(options: {
module?: string;
artifactName?: string;
moduleImportPath?: string;
}): Rule {
return host => {
const relativePath = buildRelativePath(options.module, options.moduleImportPath);
const source = readIntoSourceFile(host, options.module);
const importRecorder = host.beginUpdate(options.module);
const importChanges = addImportToModule(
source,
options.module,
strings.classify(options.artifactName),
relativePath
);
for (const change of importChanges) {
if (change instanceof InsertChange) {
importRecorder.insertLeft(change.pos, change.toAdd);
}
}
host.commitUpdate(importRecorder);
};
}
export function addEntryComponentToNgModule(options: {
module?: string;
artifactName?: string;
moduleImportPath?: string;
}): Rule {
return host => {
const relativePath = buildRelativePath(options.module, options.moduleImportPath);
const source = readIntoSourceFile(host, options.module);
const entryComponentRecorder = host.beginUpdate(options.module);
const entryComponentChanges = addEntryComponentToModule(
source,
options.module,
strings.classify(options.artifactName),
relativePath
);
for (const change of entryComponentChanges) {
if (change instanceof InsertChange) {
entryComponentRecorder.insertLeft(change.pos, change.toAdd);
}
}
host.commitUpdate(entryComponentRecorder);
};
}
export function addDeclarationToNgModule(options: {
module?: string;
artifactName?: string;
moduleImportPath?: string;
}): Rule {
return host => {
const source = readIntoSourceFile(host, options.module);
const relativePath = buildRelativePath(options.module, options.moduleImportPath);
const declarationChanges = addDeclarationToModule(source, options.module, options.artifactName, relativePath);
const declarationRecorder = host.beginUpdate(options.module);
for (const change of declarationChanges) {
if (change instanceof InsertChange) {
declarationRecorder.insertLeft(change.pos, change.toAdd);
}
}
host.commitUpdate(declarationRecorder);
return host;
};
}
export function addProviderToNgModule(options: {
module?: string;
artifactName?: string;
moduleImportPath?: string;
}): Rule {
return host => {
const source = readIntoSourceFile(host, options.module);
const relativePath = options.moduleImportPath
? buildRelativePath(options.module, options.moduleImportPath)
: undefined;
const declarationChanges = addProviderToModule(source, options.module, options.artifactName, relativePath);
const declarationRecorder = host.beginUpdate(options.module);
for (const change of declarationChanges) {
if (change instanceof InsertChange) {
declarationRecorder.insertLeft(change.pos, change.toAdd);
}
}
host.commitUpdate(declarationRecorder);
return host;
};
}
export function insertImport(
source: ts.SourceFile,
recorder: UpdateRecorder,
artifactName: string,
relativePath: string
) {
// insert import statement to imports
const lastImportEnd = findImports(source, ImportKind.All)
.map(x => x.parent.end)
.sort((x, y) => x - y)
.pop();
recorder.insertRight(lastImportEnd, `\nimport { ${artifactName} } from '${relativePath}';`);
}
export function addImportToNgModuleBefore(
options: {
module?: string;
artifactName?: string;
moduleImportPath?: string;
},
beforeToken: string
): Rule {
return host => {
const relativePath = buildRelativePath(options.module, options.moduleImportPath);
const source = readIntoSourceFile(host, options.module);
const importRecorder = host.beginUpdate(options.module);
insertImport(source, importRecorder, options.artifactName, relativePath);
let edited = false;
forEachToken(source, node => {
if (
node.kind === ts.SyntaxKind.Identifier &&
node.getText() === beforeToken &&
node.parent.kind === ts.SyntaxKind.ArrayLiteralExpression
) {
importRecorder.insertLeft(node.getStart(), `${options.artifactName}, `);
edited = true;
}
});
if (!edited) {
throw new Error(`did not find '${beforeToken}' in ${options.module}`);
}
host.commitUpdate(importRecorder);
};
}
export function addImportToFile(options: { module?: string; artifactName?: string; moduleImportPath?: string }): Rule {
return host => {
const relativePath = buildRelativePath(options.module, options.moduleImportPath);
const source = readIntoSourceFile(host, options.module);
const importRecorder = host.beginUpdate(options.module);
insertImport(source, importRecorder, options.artifactName, relativePath);
host.commitUpdate(importRecorder);
};
}