@lxf2513/readdir-sync-recursive
Advanced tools
+15
-15
@@ -7,3 +7,3 @@ 'use strict'; | ||
| function isDirectory(path) { | ||
| function isDir(path) { | ||
| try { | ||
@@ -15,18 +15,18 @@ return node_fs.statSync(path).isDirectory(); | ||
| } | ||
| function index(path, returnType) { | ||
| if (!isDirectory(path)) { | ||
| function index(path, type) { | ||
| if (!isDir(path)) { | ||
| return []; | ||
| } | ||
| const readdirResults = []; | ||
| function readdir(dirpath) { | ||
| const results = []; | ||
| function readdir(dir) { | ||
| try { | ||
| const readdirResult = node_fs.readdirSync(dirpath, { encoding: "utf-8" }); | ||
| for (let i = 0; i < readdirResult.length; i++) { | ||
| const resultPath = node_path.join(dirpath, readdirResult[i]); | ||
| const relativeResultPath = node_path.relative(path, resultPath); | ||
| const absolutePath = node_path.join(node_process.cwd(), resultPath); | ||
| const pushPath = returnType === "relativePath" ? resultPath : returnType === "absolutePath" ? absolutePath : relativeResultPath; | ||
| readdirResults.push(pushPath); | ||
| if (isDirectory(resultPath)) { | ||
| readdir(resultPath); | ||
| const result = node_fs.readdirSync(dir, { encoding: "utf-8" }); | ||
| for (let i = 0; i < result.length; i++) { | ||
| const rpath = node_path.join(dir, result[i]); | ||
| const rrpath = node_path.relative(path, rpath); | ||
| const apath = node_path.join(node_process.cwd(), rpath); | ||
| const ppath = type === "relativePath" ? rpath : type === "absolutePath" ? apath : rrpath; | ||
| results.push(ppath); | ||
| if (isDir(rpath)) { | ||
| readdir(rpath); | ||
| } | ||
@@ -39,5 +39,5 @@ } | ||
| readdir(path); | ||
| return readdirResults; | ||
| return results; | ||
| } | ||
| module.exports = index; |
+2
-2
@@ -1,3 +0,3 @@ | ||
| declare function export_default(path: string, returnType?: 'absolutePath' | 'relativePath'): string[]; | ||
| declare function export_default(path: string, type?: 'absolutePath' | 'relativePath'): string[]; | ||
| export { export_default as default }; | ||
| export = export_default; |
+1
-1
@@ -1,3 +0,3 @@ | ||
| declare function export_default(path: string, returnType?: 'absolutePath' | 'relativePath'): string[]; | ||
| declare function export_default(path: string, type?: 'absolutePath' | 'relativePath'): string[]; | ||
| export { export_default as default }; |
+1
-1
@@ -1,3 +0,3 @@ | ||
| declare function export_default(path: string, returnType?: 'absolutePath' | 'relativePath'): string[]; | ||
| declare function export_default(path: string, type?: 'absolutePath' | 'relativePath'): string[]; | ||
| export { export_default as default }; |
+15
-15
@@ -5,3 +5,3 @@ import { statSync, readdirSync } from 'node:fs'; | ||
| function isDirectory(path) { | ||
| function isDir(path) { | ||
| try { | ||
@@ -13,18 +13,18 @@ return statSync(path).isDirectory(); | ||
| } | ||
| function index(path, returnType) { | ||
| if (!isDirectory(path)) { | ||
| function index(path, type) { | ||
| if (!isDir(path)) { | ||
| return []; | ||
| } | ||
| const readdirResults = []; | ||
| function readdir(dirpath) { | ||
| const results = []; | ||
| function readdir(dir) { | ||
| try { | ||
| const readdirResult = readdirSync(dirpath, { encoding: "utf-8" }); | ||
| for (let i = 0; i < readdirResult.length; i++) { | ||
| const resultPath = join(dirpath, readdirResult[i]); | ||
| const relativeResultPath = relative(path, resultPath); | ||
| const absolutePath = join(cwd(), resultPath); | ||
| const pushPath = returnType === "relativePath" ? resultPath : returnType === "absolutePath" ? absolutePath : relativeResultPath; | ||
| readdirResults.push(pushPath); | ||
| if (isDirectory(resultPath)) { | ||
| readdir(resultPath); | ||
| const result = readdirSync(dir, { encoding: "utf-8" }); | ||
| for (let i = 0; i < result.length; i++) { | ||
| const rpath = join(dir, result[i]); | ||
| const rrpath = relative(path, rpath); | ||
| const apath = join(cwd(), rpath); | ||
| const ppath = type === "relativePath" ? rpath : type === "absolutePath" ? apath : rrpath; | ||
| results.push(ppath); | ||
| if (isDir(rpath)) { | ||
| readdir(rpath); | ||
| } | ||
@@ -37,5 +37,5 @@ } | ||
| readdir(path); | ||
| return readdirResults; | ||
| return results; | ||
| } | ||
| export { index as default }; |
+4
-4
| { | ||
| "name": "@lxf2513/readdir-sync-recursive", | ||
| "version": "1.2.2", | ||
| "version": "1.3.0", | ||
| "description": "Reads the contents of the directory synchronously and recursively.", | ||
@@ -50,6 +50,6 @@ "type": "module", | ||
| "devDependencies": { | ||
| "@types/node": "^22.10.1", | ||
| "@types/node": "^22.10.6", | ||
| "prettier": "^3.4.2", | ||
| "typescript": "^5.7.2", | ||
| "unbuild": "^2.0.0" | ||
| "typescript": "^5.7.3", | ||
| "unbuild": "^3.3.1" | ||
| }, | ||
@@ -56,0 +56,0 @@ "publishConfig": { |
5399
-6.23%