First commit
This commit is contained in:
53
modules/loader.js
Normal file
53
modules/loader.js
Normal file
@@ -0,0 +1,53 @@
|
||||
const fs = require("node:fs")
|
||||
const path = require("node:path")
|
||||
|
||||
function getRecursiveFiles(dir) {
|
||||
let results = []
|
||||
|
||||
if (!fs.existsSync(dir)) {
|
||||
return results
|
||||
}
|
||||
|
||||
const list = fs.readdirSync(dir)
|
||||
|
||||
for (const file of list) {
|
||||
const fullPath = path.join(dir, file)
|
||||
const stat = fs.statSync(fullPath)
|
||||
|
||||
if (stat && stat.isDirectory()) {
|
||||
results = results.concat(getRecursiveFiles(fullPath))
|
||||
} else {
|
||||
if (fullPath.endsWith(".js")) {
|
||||
results.push(fullPath)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return results
|
||||
}
|
||||
|
||||
function computeRoutePath(baseDir, filePath) {
|
||||
const relativePath = path.relative(baseDir, filePath)
|
||||
const normalizedPath = relativePath.replace(/\\/g, "/")
|
||||
|
||||
let route = "/" + normalizedPath
|
||||
|
||||
if (route.endsWith("index.js")) {
|
||||
route = route.replace("index.js", "")
|
||||
} else {
|
||||
route = route.replace(".js", "")
|
||||
}
|
||||
|
||||
route = route.replace(/\/{2,}/g, "/")
|
||||
|
||||
if (route.length > 1 && route.endsWith('/')) {
|
||||
route = route.slice(0, -1)
|
||||
}
|
||||
|
||||
return route
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
getRecursiveFiles,
|
||||
computeRoutePath
|
||||
}
|
||||
Reference in New Issue
Block a user