Skip to content

Commit 9785ad9

Browse files
Merge pull request #34 from nestjs-addons/jaybell/support-nest-cli
refactor(nest): add support for nest cli
2 parents f07a86d + a3cd749 commit 9785ad9

File tree

7 files changed

+685
-152
lines changed

7 files changed

+685
-152
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
"rxjs": "~6.6.3"
4343
},
4444
"devDependencies": {
45+
"@nestjs/cli": "^7.5.4",
4546
"@nestjs/schematics": "^7.2.7",
4647
"@nestjs/testing": "^7.6.11",
4748
"@nrwl/cli": "11.2.12",

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: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@ 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', async (done) => {
32+
const ngAddTree = await runner.runSchematicAsync('nest-add', '', tree).toPromise();
3533
const module = ngAddTree.readContent('/src/app.module.ts');
3634
expect(module).toMatchSnapshot();
35+
36+
done();
3737
});
3838
});

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

Lines changed: 86 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -9,91 +9,6 @@
99
import * as ts from 'typescript';
1010
import { Change, InsertChange, NoopChange } from './change';
1111

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-
9712
/**
9813
* Add Import `import { symbolName } from fileName` if the import doesn't exit
9914
* already. Assumes fileToEdit can be resolved and accessed.
@@ -213,6 +128,45 @@ export function insertImport(
213128
);
214129
}
215130

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

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

517517
if (Array.isArray(node)) {
518-
// eslint-disable-next-line @typescript-eslint/ban-types
519-
const nodeArray = (node as {}) as Array<ts.Node>;
518+
const nodeArray = (node as unknown) as Array<ts.Node>;
520519
const symbolsArray = nodeArray.map((node) => node.getText());
521520
if (symbolsArray.includes(symbolName)) {
522521
return [];

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)