You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

81 lines
2.5 KiB

// @ts-nocheck
const path = require("path")
const fs = require("fs")
export function removeIndex(ss: any) {
const remove = (str: any) => {
if (str.endsWith("/index")) {
return str.slice(0, -6)
}
if (str.endsWith("index")) {
return str.slice(0, -5)
}
return str ? str : "/"
}
let r = true
let rr = ss
while (r) {
if (rr.endsWith("/index")) {
rr = remove(rr)
} else {
r = false
}
}
return rr ? rr : "/"
}
export function isIndexEnd(str: any) {
return str.length == 1 && str.endsWith("/")
}
export function walkDir(
filePath: any,
exclude = ["node_modules", "^_", ".git", ".idea", ".gitignore", "client", ".txt$", ".test.js$", ".test.ts$"],
) {
let files: any[] = []
function Data(opts: any) {
this.relativeDir = opts.relativeDir
this.relativeFile = opts.relativeFile
this.filename = opts.filename
this.file = opts.file
this.absoluteFile = opts.absoluteFile
this.relativeFileNoExt = opts.relativeFileNoExt
this.absoluteDir = opts.absoluteDir
}
function readDir(filePath, dirname = ".") {
let res = fs.readdirSync(filePath)
res.forEach(filename => {
const filepath = path.resolve(filePath, filename)
const stat = fs.statSync(filepath)
const name = filepath.split(path.sep).slice(-1)[0]
if (typeof exclude === "string" && new RegExp(exclude).test(name)) {
return
}
if (Array.isArray(exclude)) {
for (let i = 0; i < exclude.length; i++) {
const excludeItem = exclude[i]
if (new RegExp(excludeItem).test(name)) {
return
}
}
}
if (!stat.isFile()) {
readDir(filepath, dirname + path.sep + name)
} else {
const data = new Data({
relativeDir: dirname,
relativeFile: dirname + path.sep + path.parse(filepath).base,
relativeFileNoExt: dirname + path.sep + path.parse(filepath).name,
file: path.parse(filepath).base,
filename: path.parse(filepath).name,
absoluteFile: filepath,
absoluteDir: path.parse(filepath).dir,
})
files.push(data)
}
})
}
readDir(filePath)
return files
}