mkdir-async-recursive
Advanced tools
+10
-20
| 'use strict'; | ||
| const fs = require('node:fs'); | ||
| const nodePath = require('node:path'); | ||
| const node_fs = require('node:fs'); | ||
| const node_path = require('node:path'); | ||
| function _interopDefaultCompat (e) { return e && typeof e === 'object' && 'default' in e ? e.default : e; } | ||
| const fs__default = /*#__PURE__*/_interopDefaultCompat(fs); | ||
| const nodePath__default = /*#__PURE__*/_interopDefaultCompat(nodePath); | ||
| const validateUint32 = (value, name) => { | ||
@@ -45,28 +40,23 @@ if (typeof value !== "number") { | ||
| } | ||
| const sep = path.includes("/") ? "/" : nodePath__default.sep; | ||
| const dirs = path.split(sep); | ||
| const _sep = path.includes("/") ? "/" : node_path.sep; | ||
| const dirs = path.split(_sep); | ||
| let dirPath = ""; | ||
| const result = []; | ||
| for (const dir of dirs) { | ||
| dirPath += `${dir}${sep}`; | ||
| await mkdir(dirPath, mode); | ||
| dirPath += `${dir}${_sep}`; | ||
| mkdir(dirPath, mode); | ||
| } | ||
| async function mkdir(path2, mode2) { | ||
| if (!fs__default.existsSync(dirPath)) { | ||
| function mkdir(path2, mode2) { | ||
| if (!node_fs.existsSync(dirPath)) { | ||
| try { | ||
| fs__default.mkdirSync(path2, mode2); | ||
| node_fs.mkdirSync(path2, mode2); | ||
| result.push(path2); | ||
| return await Promise.resolve(true); | ||
| } catch (error) { | ||
| console.error(error); | ||
| return await Promise.resolve(false); | ||
| } | ||
| } | ||
| return await Promise.resolve(void 0); | ||
| } | ||
| return await Promise.resolve( | ||
| result.length ? nodePath__default.resolve(result[0]) : void 0 | ||
| ); | ||
| return result.length ? node_path.resolve(result[0]) : void 0; | ||
| } | ||
| module.exports = mkdirRecursive; |
+1
-5
| import { MakeDirectoryOptions } from 'node:fs'; | ||
| type MakeDirectoryRecursiveOptions = Omit< | ||
| MakeDirectoryOptions, | ||
| 'recursive' | ||
| >; | ||
| type MakeDirectoryRecursiveOptions = Omit<MakeDirectoryOptions, 'recursive'>; | ||
| declare function mkdirRecursive(path: string, options?: string | number | MakeDirectoryRecursiveOptions): Promise<string | undefined>; | ||
| export { mkdirRecursive as default }; |
+1
-5
| import { MakeDirectoryOptions } from 'node:fs'; | ||
| type MakeDirectoryRecursiveOptions = Omit< | ||
| MakeDirectoryOptions, | ||
| 'recursive' | ||
| >; | ||
| type MakeDirectoryRecursiveOptions = Omit<MakeDirectoryOptions, 'recursive'>; | ||
| declare function mkdirRecursive(path: string, options?: string | number | MakeDirectoryRecursiveOptions): Promise<string | undefined>; | ||
| export { mkdirRecursive as default }; |
+1
-5
| import { MakeDirectoryOptions } from 'node:fs'; | ||
| type MakeDirectoryRecursiveOptions = Omit< | ||
| MakeDirectoryOptions, | ||
| 'recursive' | ||
| >; | ||
| type MakeDirectoryRecursiveOptions = Omit<MakeDirectoryOptions, 'recursive'>; | ||
| declare function mkdirRecursive(path: string, options?: string | number | MakeDirectoryRecursiveOptions): Promise<string | undefined>; | ||
| export { mkdirRecursive as default }; |
+10
-15
@@ -1,3 +0,3 @@ | ||
| import fs from 'node:fs'; | ||
| import nodePath from 'node:path'; | ||
| import { existsSync, mkdirSync } from 'node:fs'; | ||
| import { sep, resolve } from 'node:path'; | ||
@@ -38,28 +38,23 @@ const validateUint32 = (value, name) => { | ||
| } | ||
| const sep = path.includes("/") ? "/" : nodePath.sep; | ||
| const dirs = path.split(sep); | ||
| const _sep = path.includes("/") ? "/" : sep; | ||
| const dirs = path.split(_sep); | ||
| let dirPath = ""; | ||
| const result = []; | ||
| for (const dir of dirs) { | ||
| dirPath += `${dir}${sep}`; | ||
| await mkdir(dirPath, mode); | ||
| dirPath += `${dir}${_sep}`; | ||
| mkdir(dirPath, mode); | ||
| } | ||
| async function mkdir(path2, mode2) { | ||
| if (!fs.existsSync(dirPath)) { | ||
| function mkdir(path2, mode2) { | ||
| if (!existsSync(dirPath)) { | ||
| try { | ||
| fs.mkdirSync(path2, mode2); | ||
| mkdirSync(path2, mode2); | ||
| result.push(path2); | ||
| return await Promise.resolve(true); | ||
| } catch (error) { | ||
| console.error(error); | ||
| return await Promise.resolve(false); | ||
| } | ||
| } | ||
| return await Promise.resolve(void 0); | ||
| } | ||
| return await Promise.resolve( | ||
| result.length ? nodePath.resolve(result[0]) : void 0 | ||
| ); | ||
| return result.length ? resolve(result[0]) : void 0; | ||
| } | ||
| export { mkdirRecursive as default }; |
+3
-3
| { | ||
| "name": "mkdir-async-recursive", | ||
| "version": "1.0.1", | ||
| "version": "1.0.2", | ||
| "description": "Asynchronously and recursively creates a directory. Returns undefined or the first directory path created.", | ||
@@ -51,4 +51,4 @@ "type": "module", | ||
| "devDependencies": { | ||
| "@types/node": "^22.10.0", | ||
| "prettier": "^3.4.1", | ||
| "@types/node": "^22.10.1", | ||
| "prettier": "^3.4.2", | ||
| "typescript": "^5.7.2", | ||
@@ -55,0 +55,0 @@ "unbuild": "^2.0.0" |
7004
-8.08%117
-12.03%