-
Notifications
You must be signed in to change notification settings - Fork 484
/
Copy pathcompiler-plugin.ts
31 lines (27 loc) · 1.27 KB
/
compiler-plugin.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
import * as ts from 'typescript';
import { mergePluginOptions } from './merge-options';
import { pluginDebugLogger } from './plugin-debug-logger';
import { isFilenameMatched } from './utils/is-filename-matched.util';
import { ControllerClassVisitor } from './visitors/controller-class.visitor';
import { ModelClassVisitor } from './visitors/model-class.visitor';
const modelClassVisitor = new ModelClassVisitor();
const controllerClassVisitor = new ControllerClassVisitor();
export const before = (options?: Record<string, any>, program?: ts.Program) => {
options = mergePluginOptions(options);
if (!program) {
const error = `The "program" reference must be provided when using the CLI Plugin. This error is likely caused by the "isolatedModules" compiler option being set to "true".`;
pluginDebugLogger.debug(error);
throw new Error(error);
}
return (ctx: ts.TransformationContext): ts.Transformer<any> => {
return (sf: ts.SourceFile) => {
if (isFilenameMatched(options.dtoFileNameSuffix, sf.fileName)) {
return modelClassVisitor.visit(sf, ctx, program, options);
}
if (isFilenameMatched(options.controllerFileNameSuffix, sf.fileName)) {
return controllerClassVisitor.visit(sf, ctx, program, options);
}
return sf;
};
};
};