Skip to content

Commit 4338ea6

Browse files
committed
Boilerplate for our own ts transformer for our resource DTOs
1 parent 9723e1c commit 4338ea6

File tree

3 files changed

+64
-0
lines changed

3 files changed

+64
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import type * as ts from 'typescript';
2+
import {
3+
ResourceReadonlyVisitor,
4+
ResourceVisitor,
5+
// @ts-expect-error the .cts extension is ok and needed for this one spot.
6+
// This allows Nest CLI to CJS load it via ts-node
7+
} from './resources.visitor.cts';
8+
9+
const visitor = new ResourceVisitor();
10+
11+
// For ts-patch
12+
// eslint-disable-next-line import/no-default-export
13+
export default function (program: ts.Program) {
14+
return before(undefined, program);
15+
}
16+
17+
// For Nest CLI
18+
export { ResourceReadonlyVisitor as ReadonlyVisitor };
19+
export const before = (_: unknown, program: ts.Program) => {
20+
return (ctx: ts.TransformationContext) => {
21+
return (sf: ts.SourceFile) => {
22+
return visitor.visit(sf, ctx, program);
23+
};
24+
};
25+
};
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import type { ReadonlyVisitor } from '@nestjs/cli/lib/compiler/interfaces/readonly-visitor.interface';
2+
import * as ts from 'typescript';
3+
4+
export class ResourceVisitor {
5+
constructor(readonly readonly = false) {}
6+
7+
visit(sf: ts.SourceFile, ctx: ts.TransformationContext, program: ts.Program) {
8+
const visitNode = (node: ts.Node): ts.Node => {
9+
// TODO logic
10+
11+
if (this.readonly) {
12+
ts.forEachChild(node, visitNode);
13+
} else {
14+
return ts.visitEachChild(node, visitNode, ctx);
15+
}
16+
return node;
17+
};
18+
return ts.visitNode(sf, visitNode);
19+
}
20+
}
21+
22+
export class ResourceReadonlyVisitor implements ReadonlyVisitor {
23+
readonly key = '@cord/resources';
24+
private readonly visitor = new ResourceVisitor(true);
25+
26+
get typeImports() {
27+
return {};
28+
}
29+
30+
visit(program: ts.Program, sf: ts.SourceFile) {
31+
const factoryHost = { factory: ts.factory } as any;
32+
return this.visitor.visit(sf, factoryHost, program);
33+
}
34+
35+
collect() {
36+
return {};
37+
}
38+
}

tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
// Transform plugins
3737
"plugins": [
3838
{ "transform": "typescript-transform-paths" },
39+
{ "transform": "./src/core/resources/plugin/index.cts" },
3940
{ "transform": "ts-transformer-keys/transformer" },
4041
],
4142

0 commit comments

Comments
 (0)