(
dir: string,
req: Function,
callerFile: string,
opts?: RequireDirectoryOptions
)
| 54 | this.validation = validation; |
| 55 | } |
| 56 | addDirectory( |
| 57 | dir: string, |
| 58 | req: Function, |
| 59 | callerFile: string, |
| 60 | opts?: RequireDirectoryOptions |
| 61 | ): void { |
| 62 | opts = opts || {}; |
| 63 | this.requireCache.add(callerFile); |
| 64 | const fullDirPath = this.shim.path.resolve( |
| 65 | this.shim.path.dirname(callerFile), |
| 66 | dir |
| 67 | ); |
| 68 | const files = this.shim.readdirSync(fullDirPath, { |
| 69 | recursive: opts.recurse ? true : false, |
| 70 | }); |
| 71 | // exclude 'json', 'coffee' from require-directory defaults |
| 72 | if (!Array.isArray(opts.extensions)) opts.extensions = ['js']; |
| 73 | // allow consumer to define their own visitor function |
| 74 | const visit = typeof opts.visit === 'function' ? opts.visit : (o: any) => o; |
| 75 | for (const fileb of files) { |
| 76 | const file = fileb.toString(); |
| 77 | |
| 78 | // Support include / exclude logic from require-directory. |
| 79 | if (opts.exclude) { |
| 80 | let exclude = false; |
| 81 | if (typeof opts.exclude === 'function') { |
| 82 | exclude = opts.exclude(file); |
| 83 | } else { |
| 84 | exclude = opts.exclude.test(file); |
| 85 | } |
| 86 | if (exclude) continue; |
| 87 | } |
| 88 | if (opts.include) { |
| 89 | let include = false; |
| 90 | if (typeof opts.include === 'function') { |
| 91 | include = opts.include(file); |
| 92 | } else { |
| 93 | include = opts.include.test(file); |
| 94 | } |
| 95 | if (!include) continue; |
| 96 | } |
| 97 | |
| 98 | let supportedExtension = false; |
| 99 | for (const ext of opts.extensions) { |
| 100 | if (file.endsWith(ext)) supportedExtension = true; |
| 101 | } |
| 102 | if (supportedExtension) { |
| 103 | const joined = this.shim.path.join(fullDirPath, file); |
| 104 | const module = req(joined); |
| 105 | const extendableModule = Object.create( |
| 106 | null, |
| 107 | Object.getOwnPropertyDescriptors({...module}) |
| 108 | ); |
| 109 | const visited = visit(extendableModule, joined, file); |
| 110 | if (visited) { |
| 111 | if (this.requireCache.has(joined)) continue; |
| 112 | else this.requireCache.add(joined); |
| 113 | // Infer command from directory structure if none is given: |
no test coverage detected