@lxf2513/mkdir-recursive
Advanced tools
+22
-9
@@ -35,17 +35,17 @@ 'use strict'; | ||
| } | ||
| const isArray = Array.isArray; | ||
| function mkdirSyncRecursive(path, options) { | ||
| function mkdirp(path, options) { | ||
| let mode = 511; | ||
| if (typeof options === "number" || typeof options === "string") { | ||
| mode = parseFileMode(options, "mode"); | ||
| } else if (options) { | ||
| if (options.mode !== void 0) { | ||
| mode = parseFileMode(options.mode, "options.mode"); | ||
| } | ||
| } else if (options?.mode) { | ||
| mode = parseFileMode(options.mode, "options.mode"); | ||
| } | ||
| const dirs = path.split("/"); | ||
| const sep = path.includes("/") ? "/" : nodePath__default.sep; | ||
| const dirs = path.split(sep); | ||
| let dirPath = ""; | ||
| const result = []; | ||
| for (const dir of dirs) { | ||
| dirPath += `${dir}/`; | ||
| dirPath += `${dir}${sep}`; | ||
| mkdir(dirPath, mode); | ||
@@ -68,2 +68,14 @@ } | ||
| } | ||
| function mkdirSyncRecursive(path, options) { | ||
| let mpath; | ||
| if (typeof path === "string") { | ||
| mpath = mkdirp(path, options); | ||
| } else { | ||
| mpath = []; | ||
| for (const p of path) { | ||
| mpath.push(mkdirp(p, options)); | ||
| } | ||
| } | ||
| return isArray(mpath) ? mpath.some((m) => !!m) ? mpath : void 0 : mpath; | ||
| } | ||
@@ -79,7 +91,8 @@ async function mkdirAsyncRecursive(path, options) { | ||
| } | ||
| const dirs = path.split("/"); | ||
| const sep = path.includes("/") ? "/" : nodePath__default.sep; | ||
| const dirs = path.split(sep); | ||
| let dirPath = ""; | ||
| const result = []; | ||
| for (const dir of dirs) { | ||
| dirPath += `${dir}/`; | ||
| dirPath += `${dir}${sep}`; | ||
| await mkdir(dirPath, mode); | ||
@@ -86,0 +99,0 @@ } |
+1
-1
@@ -8,3 +8,3 @@ import { MakeDirectoryOptions } from 'node:fs'; | ||
| declare function mkdirSyncRecursive(path: string, options?: string | number | MakeDirectoryRecursiveOptions): string | undefined; | ||
| declare function mkdirSyncRecursive(path: string | string[], options?: string | number | MakeDirectoryRecursiveOptions): string | (string | undefined)[] | undefined; | ||
@@ -11,0 +11,0 @@ declare function mkdirAsyncRecursive(path: string, options?: string | number | MakeDirectoryRecursiveOptions): Promise<string | undefined>; |
+1
-1
@@ -8,3 +8,3 @@ import { MakeDirectoryOptions } from 'node:fs'; | ||
| declare function mkdirSyncRecursive(path: string, options?: string | number | MakeDirectoryRecursiveOptions): string | undefined; | ||
| declare function mkdirSyncRecursive(path: string | string[], options?: string | number | MakeDirectoryRecursiveOptions): string | (string | undefined)[] | undefined; | ||
@@ -11,0 +11,0 @@ declare function mkdirAsyncRecursive(path: string, options?: string | number | MakeDirectoryRecursiveOptions): Promise<string | undefined>; |
+1
-1
@@ -8,3 +8,3 @@ import { MakeDirectoryOptions } from 'node:fs'; | ||
| declare function mkdirSyncRecursive(path: string, options?: string | number | MakeDirectoryRecursiveOptions): string | undefined; | ||
| declare function mkdirSyncRecursive(path: string | string[], options?: string | number | MakeDirectoryRecursiveOptions): string | (string | undefined)[] | undefined; | ||
@@ -11,0 +11,0 @@ declare function mkdirAsyncRecursive(path: string, options?: string | number | MakeDirectoryRecursiveOptions): Promise<string | undefined>; |
+22
-9
@@ -28,17 +28,17 @@ import fs from 'node:fs'; | ||
| } | ||
| const isArray = Array.isArray; | ||
| function mkdirSyncRecursive(path, options) { | ||
| function mkdirp(path, options) { | ||
| let mode = 511; | ||
| if (typeof options === "number" || typeof options === "string") { | ||
| mode = parseFileMode(options, "mode"); | ||
| } else if (options) { | ||
| if (options.mode !== void 0) { | ||
| mode = parseFileMode(options.mode, "options.mode"); | ||
| } | ||
| } else if (options?.mode) { | ||
| mode = parseFileMode(options.mode, "options.mode"); | ||
| } | ||
| const dirs = path.split("/"); | ||
| const sep = path.includes("/") ? "/" : nodePath.sep; | ||
| const dirs = path.split(sep); | ||
| let dirPath = ""; | ||
| const result = []; | ||
| for (const dir of dirs) { | ||
| dirPath += `${dir}/`; | ||
| dirPath += `${dir}${sep}`; | ||
| mkdir(dirPath, mode); | ||
@@ -61,2 +61,14 @@ } | ||
| } | ||
| function mkdirSyncRecursive(path, options) { | ||
| let mpath; | ||
| if (typeof path === "string") { | ||
| mpath = mkdirp(path, options); | ||
| } else { | ||
| mpath = []; | ||
| for (const p of path) { | ||
| mpath.push(mkdirp(p, options)); | ||
| } | ||
| } | ||
| return isArray(mpath) ? mpath.some((m) => !!m) ? mpath : void 0 : mpath; | ||
| } | ||
@@ -72,7 +84,8 @@ async function mkdirAsyncRecursive(path, options) { | ||
| } | ||
| const dirs = path.split("/"); | ||
| const sep = path.includes("/") ? "/" : nodePath.sep; | ||
| const dirs = path.split(sep); | ||
| let dirPath = ""; | ||
| const result = []; | ||
| for (const dir of dirs) { | ||
| dirPath += `${dir}/`; | ||
| dirPath += `${dir}${sep}`; | ||
| await mkdir(dirPath, mode); | ||
@@ -79,0 +92,0 @@ } |
+4
-4
| { | ||
| "name": "@lxf2513/mkdir-recursive", | ||
| "version": "1.0.1", | ||
| "version": "1.1.0", | ||
| "description": "Recursively creates a directory. Returns undefined or the first directory path created.", | ||
@@ -50,5 +50,5 @@ "type": "module", | ||
| "devDependencies": { | ||
| "@types/node": "^22.9.0", | ||
| "prettier": "^3.3.3", | ||
| "typescript": "^5.6.3", | ||
| "@types/node": "^22.10.0", | ||
| "prettier": "^3.4.1", | ||
| "typescript": "^5.7.2", | ||
| "unbuild": "^2.0.0" | ||
@@ -55,0 +55,0 @@ }, |
+2
-0
@@ -18,2 +18,4 @@ # mkdir-recursive | ||
| const paths = mkdirSyncRecursive(['test', 'build/scripts', 'test/html']) | ||
| // async | ||
@@ -20,0 +22,0 @@ import { mkdirAsyncRecursive } from '@lxf2513/mkdir-recursive' |
10742
10.5%221
13.33%24
9.09%