Skip to content

Commit 255ab77

Browse files
committed
feat: setup cli plugin
1 parent 5ce2563 commit 255ab77

File tree

5 files changed

+74
-0
lines changed

5 files changed

+74
-0
lines changed

lib/plugin/compiler-plugin.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import type * as ts from 'typescript';
2+
import { mergePluginOptions } from './merge-options';
3+
import { HttpInterfaceVisitor } from './visitors/http-interface.visitor';
4+
5+
const httpInterfaceVisitor = new HttpInterfaceVisitor();
6+
7+
function isFilenameMatched(patterns: string[], filename: string): boolean {
8+
return patterns.some((path) => filename.includes(path));
9+
}
10+
11+
export const before: (
12+
options: Record<string, any> | undefined,
13+
program: ts.Program,
14+
) => (ctx: ts.TransformationContext) => ts.Transformer<any> = (
15+
options,
16+
program,
17+
) => {
18+
const mergedOption = mergePluginOptions(options);
19+
20+
return (ctx: ts.TransformationContext): ts.Transformer<any> => {
21+
return (sf: ts.SourceFile) => {
22+
if (
23+
isFilenameMatched(
24+
mergedOption.interfaceFilenameSuffix as string[],
25+
sf.fileName,
26+
)
27+
) {
28+
return httpInterfaceVisitor.visit(sf, ctx, program);
29+
}
30+
31+
return sf;
32+
};
33+
};
34+
};

lib/plugin/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './compiler-plugin';

lib/plugin/merge-options.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
export interface PluginOptions {
2+
interfaceFilenameSuffix?: string | string[];
3+
}
4+
5+
const defaultOptions: PluginOptions = {
6+
interfaceFilenameSuffix: ['.service.ts'],
7+
};
8+
9+
export const mergePluginOptions = (
10+
options: Record<string, any> = {},
11+
): PluginOptions => {
12+
if (typeof options.interfaceFilenameSuffix === 'string') {
13+
options.interfaceFilenameSuffix = [options.interfaceFilenameSuffix];
14+
}
15+
16+
return {
17+
...defaultOptions,
18+
...options,
19+
};
20+
};
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import type * as ts from 'typescript';
2+
3+
export class HttpInterfaceVisitor {
4+
visit(
5+
sourceFile: ts.SourceFile,
6+
ctx: ts.TransformationContext,
7+
program: ts.Program,
8+
): ts.SourceFile {
9+
return sourceFile;
10+
}
11+
}

plugin.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
'use strict';
2+
Object.defineProperty(exports, '__esModule', { value: true });
3+
const tslib_1 = require('tslib');
4+
const plugin = require('./dist/plugin');
5+
(0, tslib_1.__exportStar)(plugin, exports);
6+
7+
/** Compatibility with ts-patch/ttypescript */
8+
exports.default = (program, options) => plugin.before(options, program);

0 commit comments

Comments
 (0)