/
index.ts
177 lines (150 loc) · 5.27 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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import { strings } from '@angular-devkit/core';
import {
apply,
chain,
filter,
mergeWith,
move,
Rule,
SchematicContext,
SchematicsException,
template,
Tree,
url
} from '@angular-devkit/schematics';
import { HTMLElement, parse, TextNode } from 'node-html-parser';
import * as ts from 'typescript';
import {
appendToArrayDeclaration,
findNodes,
insertExport,
insertImport,
nodesByPosition
} from '../utils/ast-utils';
import { InsertChange } from '../utils/change';
export function component(options: any): Rule {
return (tree: Tree, _context: SchematicContext) => {
const templateSource = apply(url(`./files/${options.type}`), [
template({
...strings,
...options
}),
filter((path) => !path.endsWith('.DS_Store') && !tree.exists(path)),
move(getClicheAppPath(options.clicheName))
]);
return chain([
mergeWith(templateSource),
addComponentToMetadata(options),
addToAppComponentHtml(options)
])(tree, _context);
};
}
function getFileText(tree: Tree, filePath: string): string {
const text = tree.read(filePath);
if (text === null) {
throw new SchematicsException(`File ${filePath} does not exist.`);
}
return text.toString('utf-8');
}
function readIntoSourceFile(tree: Tree, filePath: string): ts.SourceFile {
const sourceText = getFileText(tree, filePath);
return ts.createSourceFile(
filePath, sourceText, ts.ScriptTarget.Latest, true);
}
function getClicheAppPath(clicheName: string): string {
const dasherizedName = strings.dasherize(clicheName);
return `/src/app/${dasherizedName}`;
}
function getMetadataPath(clicheName: string): string {
const dasherizedName = strings.dasherize(clicheName);
return `${getClicheAppPath(clicheName)}/${dasherizedName}.metadata.ts`;
}
function findLastComponentImportExportPos(source: ts.SourceFile) {
const lastComponentExport = findNodes(source, ts.SyntaxKind.ExportDeclaration)
.filter((node: ts.ExportDeclaration) =>
node.exportClause && node.exportClause.elements
.filter((exportElement) =>
exportElement
.getText()
.match('Component')
).length > 0
)
.sort(nodesByPosition)
.pop();
const lastComponentImport = findNodes(source, ts.SyntaxKind.ImportDeclaration)
.filter((node: ts.ImportDeclaration) => node.moduleSpecifier &&
node.moduleSpecifier
.getText()
.match('component')
)
.sort(nodesByPosition)
.pop();
const insertPosByLastExport = lastComponentExport ?
lastComponentExport.getEnd() : 0;
const insertPosByLastImport = lastComponentImport ?
lastComponentImport.getEnd() : 0;
return Math.max(insertPosByLastExport, insertPosByLastImport);
}
function addComponentToMetadata(options: any): Rule {
return (tree: Tree) => {
if (options.skipMetadataImport) {
return tree;
}
const metadataPath = getMetadataPath(options.clicheName);
const componentPath = `./${strings.dasherize(options.componentName)}/`
+ strings.dasherize(options.componentName)
+ '.component';
const componentName = `${strings.classify(options.componentName)}Component`;
const source = readIntoSourceFile(tree, metadataPath);
const changeRecorder = tree.beginUpdate(metadataPath);
const insertPos = findLastComponentImportExportPos(source);
const importChange = insertImport(source,
metadataPath, componentName, componentPath, insertPos);
if (importChange instanceof InsertChange) {
changeRecorder.insertLeft(importChange.pos, importChange.toAdd);
}
const exportChange = insertExport(
source, componentName, componentPath, insertPos);
if (exportChange instanceof InsertChange) {
changeRecorder.insertLeft(exportChange.pos, exportChange.toAdd);
}
tree.commitUpdate(changeRecorder);
const arrayChangeRecorder = tree.beginUpdate(metadataPath);
const arrayChange = appendToArrayDeclaration(
readIntoSourceFile(tree, metadataPath), 'allComponents', componentName);
if (arrayChange instanceof InsertChange) {
arrayChangeRecorder.insertLeft(arrayChange.pos, arrayChange.toAdd);
}
tree.commitUpdate(arrayChangeRecorder);
return tree;
};
}
function addToAppComponentHtml(options: any): Rule {
return (tree: Tree) => {
if (options.skipAppComponentHtml) {
return tree;
}
const filePath = '/src/app/app.component.html';
const root = parse(getFileText(tree, filePath)) as HTMLElement;
const componentComponentSelector =
`${options.clicheName}-${strings.dasherize(options.componentName)}`;
// skip if it's already there
if (root.querySelector(componentComponentSelector)) {
return tree;
}
const title = `${strings.capitalize(options.type)} ` +
`${strings.capitalize(options.entityName)}`;
const componentHtml =
` <h2>${title}</h2>\n` +
` <${componentComponentSelector}></${componentComponentSelector}>\n`;
const container = root.querySelector('div.container');
if (!container) {
console.log('Could not find div with class container, ' +
`skipping update to ${filePath}`);
return tree;
}
container.appendChild(new TextNode(componentHtml));
tree.overwrite(filePath, root.toString());
return tree;
};
}