|
1 | 1 | import fs from 'fs'; |
2 | 2 | import { join, resolve, dirname, basename } from 'path'; |
3 | 3 |
|
4 | | -interface Options { |
5 | | - extensions: string[]; |
| 4 | +export interface RequireOptions { |
| 5 | + relativePath?: string; |
| 6 | + extensions?: string[]; |
6 | 7 | include?: RegExp | ((path: string, filename: string) => boolean); |
7 | 8 | exclude?: RegExp | ((path: string, filename: string) => boolean); |
8 | 9 | } |
@@ -42,18 +43,19 @@ export interface RequireAstResult { |
42 | 43 | subscription?: RequireAstRootTypeNode; |
43 | 44 | } |
44 | 45 |
|
45 | | -export const defaultOptions: Options = { |
| 46 | +export const defaultOptions: RequireOptions = { |
46 | 47 | extensions: ['js', 'ts'], |
47 | 48 | }; |
48 | 49 |
|
49 | 50 | export function requireSchemaDirectory( |
50 | 51 | m: NodeModule, |
51 | | - path?: string, |
52 | | - options: Options = defaultOptions |
| 52 | + options: RequireOptions = defaultOptions |
53 | 53 | ): RequireAstResult { |
54 | 54 | // if no path was passed in, assume the equivelant of __dirname from caller |
55 | 55 | // otherwise, resolve path relative to the equivalent of __dirname |
56 | | - const schemaPath = !path ? dirname(m.filename) : resolve(dirname(m.filename), path); |
| 56 | + const schemaPath = options?.relativePath |
| 57 | + ? resolve(dirname(m.filename), options.relativePath) |
| 58 | + : dirname(m.filename); |
57 | 59 |
|
58 | 60 | const result = {} as RequireAstResult; |
59 | 61 |
|
@@ -94,7 +96,7 @@ export function requireSchemaDirectory( |
94 | 96 | export function requireSubDirectory( |
95 | 97 | m: NodeModule, |
96 | 98 | path: string, |
97 | | - options: Options = defaultOptions |
| 99 | + options: RequireOptions = defaultOptions |
98 | 100 | ): RequireAstDirNode { |
99 | 101 | const result: RequireAstDirNode = { |
100 | 102 | kind: 'dir', |
@@ -153,7 +155,7 @@ function skipName(filename: string): boolean { |
153 | 155 | return /^__.*/i.test(filename); |
154 | 156 | } |
155 | 157 |
|
156 | | -function checkFileInclusion(absPath: string, filename: string, options: Options): boolean { |
| 158 | +function checkFileInclusion(absPath: string, filename: string, options: RequireOptions): boolean { |
157 | 159 | return ( |
158 | 160 | // verify file has valid extension |
159 | 161 | new RegExp('\\.(' + options.extensions.join('|') + ')$', 'i').test(filename) && |
|
0 commit comments