Skip to content

Commit

Permalink
Merge pull request #34 from nestjs-addons/jaybell/support-nest-cli
Browse files Browse the repository at this point in the history
refactor(nest): add support for nest cli
  • Loading branch information
DominikPieper authored Apr 20, 2021
2 parents f07a86d + a3cd749 commit 9785ad9
Show file tree
Hide file tree
Showing 7 changed files with 685 additions and 152 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"rxjs": "~6.6.3"
},
"devDependencies": {
"@nestjs/cli": "^7.5.4",
"@nestjs/schematics": "^7.2.7",
"@nestjs/testing": "^7.6.11",
"@nrwl/cli": "11.2.12",
Expand Down
2 changes: 1 addition & 1 deletion packages/in-memory-db/src/schematics/collection.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"$schema": "../node_modules/@angular-devkit/schematics/collection-schema.json",
"$schema": "../../../../node_modules/@angular-devkit/schematics/collection-schema.json",
"schematics": {
"nest-add": {
"description": "Schematics to add In-Memory DB Module.",
Expand Down
8 changes: 4 additions & 4 deletions packages/in-memory-db/src/schematics/nest-add/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ describe('nest add function', () => {
runner = new SchematicTestRunner('schematics', collectionPath);
});

it('should add package to module', async () => {
const ngAddTree = await runner
.runSchematicAsync('nest-add', '', tree)
.toPromise();
it('should add package to module', async (done) => {
const ngAddTree = await runner.runSchematicAsync('nest-add', '', tree).toPromise();
const module = ngAddTree.readContent('/src/app.module.ts');
expect(module).toMatchSnapshot();

done();
});
});
173 changes: 86 additions & 87 deletions packages/in-memory-db/src/schematics/utils/ast-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,91 +9,6 @@
import * as ts from 'typescript';
import { Change, InsertChange, NoopChange } from './change';

/**
* Find all nodes from the AST in the subtree of node of SyntaxKind kind.
* @param node
* @param kind
* @param max The maximum number of items to return.
* @return all nodes of kind, or [] if none is found
*/
export function findNodes(
node: ts.Node,
kind: ts.SyntaxKind,
max = Infinity,
): ts.Node[] {
if (!node || max == 0) {
return [];
}

const arr: ts.Node[] = [];
if (node.kind === kind) {
arr.push(node);
max--;
}
if (max > 0) {
for (const child of node.getChildren()) {
findNodes(child, kind, max).forEach((node) => {
if (max > 0) {
arr.push(node);
}
max--;
});

if (max <= 0) {
break;
}
}
}

return arr;
}

/**
* Helper for sorting nodes.
* @return function to sort nodes in increasing order of position in sourceFile
*/
function nodesByPosition(first: ts.Node, second: ts.Node): number {
return first.getStart() - second.getStart();
}

/**
* Insert `toInsert` after the last occurence of `ts.SyntaxKind[nodes[i].kind]`
* or after the last of occurence of `syntaxKind` if the last occurence is a sub child
* of ts.SyntaxKind[nodes[i].kind] and save the changes in file.
*
* @param nodes insert after the last occurence of nodes
* @param toInsert string to insert
* @param file file to insert changes into
* @param fallbackPos position to insert if toInsert happens to be the first occurence
* @param syntaxKind the ts.SyntaxKind of the subchildren to insert after
* @return Change instance
* @throw Error if toInsert is first occurence but fall back is not set
*/
export function insertAfterLastOccurrence(
nodes: ts.Node[],
toInsert: string,
file: string,
fallbackPos: number,
syntaxKind?: ts.SyntaxKind,
): Change {
// sort() has a side effect, so make a copy so that we won't overwrite the parent's object.
let lastItem = [...nodes].sort(nodesByPosition).pop();
if (!lastItem) {
throw new Error();
}
if (syntaxKind) {
lastItem = findNodes(lastItem, syntaxKind).sort(nodesByPosition).pop();
}
if (!lastItem && fallbackPos == undefined) {
throw new Error(
`tried to insert ${toInsert} as first occurence with no fallback position`,
);
}
const lastItemPosition: number = lastItem ? lastItem.getEnd() : fallbackPos;

return new InsertChange(file, lastItemPosition, toInsert);
}

/**
* Add Import `import { symbolName } from fileName` if the import doesn't exit
* already. Assumes fileToEdit can be resolved and accessed.
Expand Down Expand Up @@ -213,6 +128,45 @@ export function insertImport(
);
}

/**
* Find all nodes from the AST in the subtree of node of SyntaxKind kind.
* @param node
* @param kind
* @param max The maximum number of items to return.
* @return all nodes of kind, or [] if none is found
*/
export function findNodes(
node: ts.Node,
kind: ts.SyntaxKind,
max = Infinity,
): ts.Node[] {
if (!node || max == 0) {
return [];
}

const arr: ts.Node[] = [];
if (node.kind === kind) {
arr.push(node);
max--;
}
if (max > 0) {
for (const child of node.getChildren()) {
findNodes(child, kind, max).forEach((node) => {
if (max > 0) {
arr.push(node);
}
max--;
});

if (max <= 0) {
break;
}
}
}

return arr;
}

/**
* Get all the nodes from a source.
* @param sourceFile The source file object.
Expand Down Expand Up @@ -254,6 +208,52 @@ export function findNode(
return foundNode;
}

/**
* Helper for sorting nodes.
* @return function to sort nodes in increasing order of position in sourceFile
*/
function nodesByPosition(first: ts.Node, second: ts.Node): number {
return first.getStart() - second.getStart();
}

/**
* Insert `toInsert` after the last occurence of `ts.SyntaxKind[nodes[i].kind]`
* or after the last of occurence of `syntaxKind` if the last occurence is a sub child
* of ts.SyntaxKind[nodes[i].kind] and save the changes in file.
*
* @param nodes insert after the last occurence of nodes
* @param toInsert string to insert
* @param file file to insert changes into
* @param fallbackPos position to insert if toInsert happens to be the first occurence
* @param syntaxKind the ts.SyntaxKind of the subchildren to insert after
* @return Change instance
* @throw Error if toInsert is first occurence but fall back is not set
*/
export function insertAfterLastOccurrence(
nodes: ts.Node[],
toInsert: string,
file: string,
fallbackPos: number,
syntaxKind?: ts.SyntaxKind,
): Change {
// sort() has a side effect, so make a copy so that we won't overwrite the parent's object.
let lastItem = [...nodes].sort(nodesByPosition).pop();
if (!lastItem) {
throw new Error();
}
if (syntaxKind) {
lastItem = findNodes(lastItem, syntaxKind).sort(nodesByPosition).pop();
}
if (!lastItem && fallbackPos == undefined) {
throw new Error(
`tried to insert ${toInsert} as first occurence with no fallback position`,
);
}
const lastItemPosition: number = lastItem ? lastItem.getEnd() : fallbackPos;

return new InsertChange(file, lastItemPosition, toInsert);
}

export function getContentOfKeyLiteral(
_source: ts.SourceFile,
node: ts.Node,
Expand Down Expand Up @@ -515,8 +515,7 @@ export function addSymbolToNgModuleMetadata(
}

if (Array.isArray(node)) {
// eslint-disable-next-line @typescript-eslint/ban-types
const nodeArray = (node as {}) as Array<ts.Node>;
const nodeArray = (node as unknown) as Array<ts.Node>;
const symbolsArray = nodeArray.map((node) => node.getText());
if (symbolsArray.includes(symbolName)) {
return [];
Expand Down
2 changes: 1 addition & 1 deletion packages/in-memory-db/tsconfig.lib.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@
"skipLibCheck": true
},
"exclude": ["**/*.spec.ts", "**/mocks.ts"],
"include": ["**/*.ts"]
"include": ["**/*.ts", "**/*.json"]
}
9 changes: 8 additions & 1 deletion workspace.json
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,14 @@
"tsConfig": "packages/in-memory-db/tsconfig.lib.json",
"packageJson": "packages/in-memory-db/package.json",
"main": "packages/in-memory-db/src/index.ts",
"assets": ["packages/in-memory-db/*.md"]
"assets": [
"packages/in-memory-db/*.md",
{
"glob": "**/*.json",
"input": "packages/in-memory-db/src/schematics",
"output": "src/schematics"
}
]
},
"outputs": ["{options.outputPath}"]
}
Expand Down
Loading

0 comments on commit 9785ad9

Please sign in to comment.