-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
74 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './compiler-plugin'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |