-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
53 lines (38 loc) · 1.59 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import path from "node:path"
import fs from "node:fs/promises"
const types = {
keepPathOnKey: "boolean",
prefixKey: "string",
removeExtensionFile: "boolean",
}
export const DEFAULT_EXTENSIONS = Object.freeze([".js", ".cjs", ".mjs", ".jsx", ".ts", ".cts", ".mts", ".tsx"])
export async function importDirectory(dirPath, opts = {}) {
const {keepPathOnKey = false, prefixKey = "", removeExtensionFile = false, extensions = DEFAULT_EXTENSIONS} = opts
for (const key in types) {
if (key in opts && opts[key] !== undefined && typeof opts[key] !== types[key]) {
throw TypeError(`Expected option "${key}" to be type "${types[key]}"`)
}
}
if (!Array.isArray(extensions)) {
throw TypeError(`Expected option "extentions" to be an array`)
}
if (!extensions.length) throw TypeError('Expected option "extentions" to have at least 1 item')
const dir = path.resolve(dirPath)
async function processDirectory(dirPath) {
const stats = await fs.lstat(dirPath)
if (stats.isDirectory()) {
const directory = await fs.readdir(dirPath)
await Promise.all(directory.map(item => processDirectory(path.join(dirPath, item))))
return
}
const hasValidExtension = extensions.some(extension => dirPath.endsWith(extension))
if (!hasValidExtension) return
const module = await import(dirPath)
const key = path.join(prefixKey, keepPathOnKey ? dirPath : dirPath.replace(dir, ""))
const moduleKey = removeExtensionFile ? key.replace(/(.+)\.(.+)/, "$1") : key
results[moduleKey] = module
}
const results = {}
await processDirectory(dir)
return results
}