Skip to content

Commit 70576aa

Browse files
yharaskrikDominikPieper
authored andcommitted
refactor(nest): add schematics folder from old repo to support nest add
1 parent 019fdf0 commit 70576aa

File tree

6 files changed

+98
-118
lines changed

6 files changed

+98
-118
lines changed

nest-cli.json

Lines changed: 0 additions & 25 deletions
This file was deleted.

packages/in-memory-db/src/schematics/collection.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"$schema": "../node_modules/@angular-devkit/schematics/collection-schema.json",
2+
"$schema": "../../../../node_modules/@angular-devkit/schematics/collection-schema.json",
33
"schematics": {
44
"nest-add": {
55
"description": "Schematics to add In-Memory DB Module.",

packages/in-memory-db/src/schematics/nest-add/index.spec.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,8 @@ describe('nest add function', () => {
2828
runner = new SchematicTestRunner('schematics', collectionPath);
2929
});
3030

31-
it('should add package to module', async () => {
32-
const ngAddTree = await runner
33-
.runSchematicAsync('nest-add', '', tree)
34-
.toPromise();
31+
it('should add package to module', () => {
32+
const ngAddTree = runner.runSchematic('nest-add', '', tree);
3533
const module = ngAddTree.readContent('/src/app.module.ts');
3634
expect(module).toMatchSnapshot();
3735
});

packages/in-memory-db/src/schematics/utils/ast-utils.ts

Lines changed: 86 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -6,94 +6,10 @@
66
* Use of this source code is governed by an MIT-style license that can be
77
* found in the LICENSE file at https://angular.io/license
88
*/
9+
import { ImportClause } from 'typescript';
910
import * as ts from 'typescript';
1011
import { Change, InsertChange, NoopChange } from './change';
1112

12-
/**
13-
* Find all nodes from the AST in the subtree of node of SyntaxKind kind.
14-
* @param node
15-
* @param kind
16-
* @param max The maximum number of items to return.
17-
* @return all nodes of kind, or [] if none is found
18-
*/
19-
export function findNodes(
20-
node: ts.Node,
21-
kind: ts.SyntaxKind,
22-
max = Infinity,
23-
): ts.Node[] {
24-
if (!node || max == 0) {
25-
return [];
26-
}
27-
28-
const arr: ts.Node[] = [];
29-
if (node.kind === kind) {
30-
arr.push(node);
31-
max--;
32-
}
33-
if (max > 0) {
34-
for (const child of node.getChildren()) {
35-
findNodes(child, kind, max).forEach((node) => {
36-
if (max > 0) {
37-
arr.push(node);
38-
}
39-
max--;
40-
});
41-
42-
if (max <= 0) {
43-
break;
44-
}
45-
}
46-
}
47-
48-
return arr;
49-
}
50-
51-
/**
52-
* Helper for sorting nodes.
53-
* @return function to sort nodes in increasing order of position in sourceFile
54-
*/
55-
function nodesByPosition(first: ts.Node, second: ts.Node): number {
56-
return first.getStart() - second.getStart();
57-
}
58-
59-
/**
60-
* Insert `toInsert` after the last occurence of `ts.SyntaxKind[nodes[i].kind]`
61-
* or after the last of occurence of `syntaxKind` if the last occurence is a sub child
62-
* of ts.SyntaxKind[nodes[i].kind] and save the changes in file.
63-
*
64-
* @param nodes insert after the last occurence of nodes
65-
* @param toInsert string to insert
66-
* @param file file to insert changes into
67-
* @param fallbackPos position to insert if toInsert happens to be the first occurence
68-
* @param syntaxKind the ts.SyntaxKind of the subchildren to insert after
69-
* @return Change instance
70-
* @throw Error if toInsert is first occurence but fall back is not set
71-
*/
72-
export function insertAfterLastOccurrence(
73-
nodes: ts.Node[],
74-
toInsert: string,
75-
file: string,
76-
fallbackPos: number,
77-
syntaxKind?: ts.SyntaxKind,
78-
): Change {
79-
// sort() has a side effect, so make a copy so that we won't overwrite the parent's object.
80-
let lastItem = [...nodes].sort(nodesByPosition).pop();
81-
if (!lastItem) {
82-
throw new Error();
83-
}
84-
if (syntaxKind) {
85-
lastItem = findNodes(lastItem, syntaxKind).sort(nodesByPosition).pop();
86-
}
87-
if (!lastItem && fallbackPos == undefined) {
88-
throw new Error(
89-
`tried to insert ${toInsert} as first occurence with no fallback position`,
90-
);
91-
}
92-
const lastItemPosition: number = lastItem ? lastItem.getEnd() : fallbackPos;
93-
94-
return new InsertChange(file, lastItemPosition, toInsert);
95-
}
96-
9713
/**
9814
* Add Import `import { symbolName } from fileName` if the import doesn't exit
9915
* already. Assumes fileToEdit can be resolved and accessed.
@@ -213,6 +129,45 @@ export function insertImport(
213129
);
214130
}
215131

132+
/**
133+
* Find all nodes from the AST in the subtree of node of SyntaxKind kind.
134+
* @param node
135+
* @param kind
136+
* @param max The maximum number of items to return.
137+
* @return all nodes of kind, or [] if none is found
138+
*/
139+
export function findNodes(
140+
node: ts.Node,
141+
kind: ts.SyntaxKind,
142+
max = Infinity,
143+
): ts.Node[] {
144+
if (!node || max == 0) {
145+
return [];
146+
}
147+
148+
const arr: ts.Node[] = [];
149+
if (node.kind === kind) {
150+
arr.push(node);
151+
max--;
152+
}
153+
if (max > 0) {
154+
for (const child of node.getChildren()) {
155+
findNodes(child, kind, max).forEach((node) => {
156+
if (max > 0) {
157+
arr.push(node);
158+
}
159+
max--;
160+
});
161+
162+
if (max <= 0) {
163+
break;
164+
}
165+
}
166+
}
167+
168+
return arr;
169+
}
170+
216171
/**
217172
* Get all the nodes from a source.
218173
* @param sourceFile The source file object.
@@ -254,6 +209,52 @@ export function findNode(
254209
return foundNode;
255210
}
256211

212+
/**
213+
* Helper for sorting nodes.
214+
* @return function to sort nodes in increasing order of position in sourceFile
215+
*/
216+
function nodesByPosition(first: ts.Node, second: ts.Node): number {
217+
return first.getStart() - second.getStart();
218+
}
219+
220+
/**
221+
* Insert `toInsert` after the last occurence of `ts.SyntaxKind[nodes[i].kind]`
222+
* or after the last of occurence of `syntaxKind` if the last occurence is a sub child
223+
* of ts.SyntaxKind[nodes[i].kind] and save the changes in file.
224+
*
225+
* @param nodes insert after the last occurence of nodes
226+
* @param toInsert string to insert
227+
* @param file file to insert changes into
228+
* @param fallbackPos position to insert if toInsert happens to be the first occurence
229+
* @param syntaxKind the ts.SyntaxKind of the subchildren to insert after
230+
* @return Change instance
231+
* @throw Error if toInsert is first occurence but fall back is not set
232+
*/
233+
export function insertAfterLastOccurrence(
234+
nodes: ts.Node[],
235+
toInsert: string,
236+
file: string,
237+
fallbackPos: number,
238+
syntaxKind?: ts.SyntaxKind,
239+
): Change {
240+
// sort() has a side effect, so make a copy so that we won't overwrite the parent's object.
241+
let lastItem = [...nodes].sort(nodesByPosition).pop();
242+
if (!lastItem) {
243+
throw new Error();
244+
}
245+
if (syntaxKind) {
246+
lastItem = findNodes(lastItem, syntaxKind).sort(nodesByPosition).pop();
247+
}
248+
if (!lastItem && fallbackPos == undefined) {
249+
throw new Error(
250+
`tried to insert ${toInsert} as first occurence with no fallback position`,
251+
);
252+
}
253+
const lastItemPosition: number = lastItem ? lastItem.getEnd() : fallbackPos;
254+
255+
return new InsertChange(file, lastItemPosition, toInsert);
256+
}
257+
257258
export function getContentOfKeyLiteral(
258259
_source: ts.SourceFile,
259260
node: ts.Node,
@@ -515,7 +516,6 @@ export function addSymbolToNgModuleMetadata(
515516
}
516517

517518
if (Array.isArray(node)) {
518-
// eslint-disable-next-line @typescript-eslint/ban-types
519519
const nodeArray = (node as {}) as Array<ts.Node>;
520520
const symbolsArray = nodeArray.map((node) => node.getText());
521521
if (symbolsArray.includes(symbolName)) {

packages/in-memory-db/tsconfig.lib.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,5 @@
1515
"skipLibCheck": true
1616
},
1717
"exclude": ["**/*.spec.ts", "**/mocks.ts"],
18-
"include": ["**/*.ts"]
18+
"include": ["**/*.ts", "**/*.json"]
1919
}

workspace.json

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,14 @@
9191
"tsConfig": "packages/in-memory-db/tsconfig.lib.json",
9292
"packageJson": "packages/in-memory-db/package.json",
9393
"main": "packages/in-memory-db/src/index.ts",
94-
"assets": ["packages/in-memory-db/*.md"]
94+
"assets": [
95+
"packages/in-memory-db/*.md",
96+
{
97+
"glob": "**/*.json",
98+
"input": "packages/in-memory-db/src/schematics",
99+
"output": "src/schematics"
100+
}
101+
]
95102
},
96103
"outputs": ["{options.outputPath}"]
97104
}

0 commit comments

Comments
 (0)