Skip to content

Commit

Permalink
feat: setup cli plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
jbl428 committed May 7, 2023
1 parent 5ce2563 commit 255ab77
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 0 deletions.
34 changes: 34 additions & 0 deletions lib/plugin/compiler-plugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import type * as ts from 'typescript';
import { mergePluginOptions } from './merge-options';
import { HttpInterfaceVisitor } from './visitors/http-interface.visitor';

const httpInterfaceVisitor = new HttpInterfaceVisitor();

function isFilenameMatched(patterns: string[], filename: string): boolean {
return patterns.some((path) => filename.includes(path));
}

export const before: (
options: Record<string, any> | undefined,
program: ts.Program,
) => (ctx: ts.TransformationContext) => ts.Transformer<any> = (
options,
program,
) => {
const mergedOption = mergePluginOptions(options);

return (ctx: ts.TransformationContext): ts.Transformer<any> => {
return (sf: ts.SourceFile) => {
if (
isFilenameMatched(
mergedOption.interfaceFilenameSuffix as string[],
sf.fileName,
)
) {
return httpInterfaceVisitor.visit(sf, ctx, program);
}

return sf;
};
};
};
1 change: 1 addition & 0 deletions lib/plugin/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './compiler-plugin';
20 changes: 20 additions & 0 deletions lib/plugin/merge-options.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
export interface PluginOptions {
interfaceFilenameSuffix?: string | string[];
}

const defaultOptions: PluginOptions = {
interfaceFilenameSuffix: ['.service.ts'],
};

export const mergePluginOptions = (
options: Record<string, any> = {},
): PluginOptions => {
if (typeof options.interfaceFilenameSuffix === 'string') {
options.interfaceFilenameSuffix = [options.interfaceFilenameSuffix];
}

return {
...defaultOptions,
...options,
};
};
11 changes: 11 additions & 0 deletions lib/plugin/visitors/http-interface.visitor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import type * as ts from 'typescript';

export class HttpInterfaceVisitor {
visit(
sourceFile: ts.SourceFile,
ctx: ts.TransformationContext,
program: ts.Program,
): ts.SourceFile {
return sourceFile;
}
}
8 changes: 8 additions & 0 deletions plugin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
'use strict';
Object.defineProperty(exports, '__esModule', { value: true });
const tslib_1 = require('tslib');
const plugin = require('./dist/plugin');
(0, tslib_1.__exportStar)(plugin, exports);

/** Compatibility with ts-patch/ttypescript */
exports.default = (program, options) => plugin.before(options, program);

0 comments on commit 255ab77

Please sign in to comment.