@lxf2513/readdir-sync-recursive
Advanced tools
+16
-8
@@ -5,2 +5,3 @@ 'use strict'; | ||
| const nodePath = require('node:path'); | ||
| const process = require('node:process'); | ||
@@ -11,2 +12,3 @@ function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e.default : e; } | ||
| const nodePath__default = /*#__PURE__*/_interopDefaultCompat(nodePath); | ||
| const process__default = /*#__PURE__*/_interopDefaultCompat(process); | ||
@@ -17,16 +19,22 @@ function isDirectory(path) { | ||
| return stat.isDirectory(); | ||
| } catch (error) { | ||
| } catch { | ||
| return false; | ||
| } | ||
| } | ||
| function index(basePath) { | ||
| function index(path, returnType) { | ||
| if (!isDirectory(path)) { | ||
| return []; | ||
| } | ||
| const readdirResults = []; | ||
| const pathsQueue = [basePath]; | ||
| function readdir(path) { | ||
| const pathsQueue = [path]; | ||
| function readdir(dirpath) { | ||
| try { | ||
| const readdirResult = fs__default.readdirSync(path, { encoding: "utf-8" }); | ||
| const readdirResult = fs__default.readdirSync(dirpath, { encoding: "utf-8" }); | ||
| for (let i = 0; i < readdirResult.length; i++) { | ||
| const resultPath = nodePath__default.join(path, readdirResult[i]); | ||
| const relativeResultPath = nodePath__default.relative(basePath, resultPath); | ||
| readdirResults.push(relativeResultPath); | ||
| const resultPath = nodePath__default.join(dirpath, readdirResult[i]); | ||
| const relativeResultPath = nodePath__default.relative(path, resultPath); | ||
| const absolutePath = nodePath__default.join(process__default.cwd(), resultPath); | ||
| let pushPath = relativeResultPath; | ||
| returnType === "relativePath" ? pushPath = resultPath : returnType === "absolutePath" ? pushPath = absolutePath : relativeResultPath; | ||
| readdirResults.push(pushPath); | ||
| if (isDirectory(resultPath)) { | ||
@@ -33,0 +41,0 @@ pathsQueue.push(resultPath); |
+1
-1
@@ -1,3 +0,3 @@ | ||
| declare function export_default(basePath: string): string[]; | ||
| declare function export_default(path: string, returnType?: 'absolutePath' | 'relativePath'): string[]; | ||
| export { export_default as default }; |
+1
-1
@@ -1,3 +0,3 @@ | ||
| declare function export_default(basePath: string): string[]; | ||
| declare function export_default(path: string, returnType?: 'absolutePath' | 'relativePath'): string[]; | ||
| export { export_default as default }; |
+1
-1
@@ -1,3 +0,3 @@ | ||
| declare function export_default(basePath: string): string[]; | ||
| declare function export_default(path: string, returnType?: 'absolutePath' | 'relativePath'): string[]; | ||
| export { export_default as default }; |
+15
-8
| import fs from 'node:fs'; | ||
| import nodePath from 'node:path'; | ||
| import process from 'node:process'; | ||
@@ -8,16 +9,22 @@ function isDirectory(path) { | ||
| return stat.isDirectory(); | ||
| } catch (error) { | ||
| } catch { | ||
| return false; | ||
| } | ||
| } | ||
| function index(basePath) { | ||
| function index(path, returnType) { | ||
| if (!isDirectory(path)) { | ||
| return []; | ||
| } | ||
| const readdirResults = []; | ||
| const pathsQueue = [basePath]; | ||
| function readdir(path) { | ||
| const pathsQueue = [path]; | ||
| function readdir(dirpath) { | ||
| try { | ||
| const readdirResult = fs.readdirSync(path, { encoding: "utf-8" }); | ||
| const readdirResult = fs.readdirSync(dirpath, { encoding: "utf-8" }); | ||
| for (let i = 0; i < readdirResult.length; i++) { | ||
| const resultPath = nodePath.join(path, readdirResult[i]); | ||
| const relativeResultPath = nodePath.relative(basePath, resultPath); | ||
| readdirResults.push(relativeResultPath); | ||
| const resultPath = nodePath.join(dirpath, readdirResult[i]); | ||
| const relativeResultPath = nodePath.relative(path, resultPath); | ||
| const absolutePath = nodePath.join(process.cwd(), resultPath); | ||
| let pushPath = relativeResultPath; | ||
| returnType === "relativePath" ? pushPath = resultPath : returnType === "absolutePath" ? pushPath = absolutePath : relativeResultPath; | ||
| readdirResults.push(pushPath); | ||
| if (isDirectory(resultPath)) { | ||
@@ -24,0 +31,0 @@ pathsQueue.push(resultPath); |
+4
-4
| { | ||
| "name": "@lxf2513/readdir-sync-recursive", | ||
| "version": "1.0.1", | ||
| "version": "1.1.0", | ||
| "description": "Reads the contents of the directory synchronously and recursively.", | ||
@@ -50,5 +50,5 @@ "type": "module", | ||
| "devDependencies": { | ||
| "@types/node": "^22.9.0", | ||
| "prettier": "^3.3.3", | ||
| "typescript": "^5.6.3", | ||
| "@types/node": "^22.10.1", | ||
| "prettier": "^3.4.1", | ||
| "typescript": "^5.7.2", | ||
| "unbuild": "^2.0.0" | ||
@@ -55,0 +55,0 @@ }, |
+6
-0
@@ -6,2 +6,4 @@ # readdir-sync-recursive | ||
| Return `[]` if the path not exist or not a directory | ||
| ## Installation | ||
@@ -19,2 +21,6 @@ | ||
| readdirSyncRecursive('/path/source') | ||
| readdirSyncRecursive('/path/source', 'relativePath') | ||
| readdirSyncRecursive('/path/source', 'absolutePath') | ||
| ``` |
6445
19.42%89
20.27%24
33.33%