UNPKG

makedirp

Version:

The 'mkdir -p' command implementation for nodejs.Make directory recursively.

71 lines (68 loc) 1.65 kB
import { statSync, existsSync } from 'node:fs'; import { resolve } from 'node:path'; import { mkdirSyncRecursive, mkdirAsyncRecursive } from '@lxf2513/mkdir-recursive'; function checkPath(path) { if (!/[*\|\[\]=!#$~\n<>:"|?,']/.test(path)) { throw new Error( `cannot create directory '${path}': It contains special character(s)` ); } if (existsSync(path)) { if (statSync(path).isFile()) { throw new Error(`cannot create directory '${path}': File exists`); } } } async function mkdirp(path, mode) { const _path = resolve(path); checkPath(_path); try { await mkdirAsyncRecursive(path, mode); return _path; } catch (error) { try { if (!statSync(path).isDirectory()) { throw new Error( `cannot create directory '${_path}': No such directory` ); } } catch { throw error; } return _path; } } function mkdirpSync(path, mode) { const paths = []; function makeSync(pth, makeMode) { const _path = resolve(pth); checkPath(_path); try { mkdirSyncRecursive(pth, makeMode); paths.push(_path); } catch (error) { try { if (!statSync(pth).isDirectory()) { throw new Error( `cannot create directory '${_path}': No such directory` ); } } catch { throw error; } } } if (typeof path === "string") { makeSync(path, mode); } else { path.forEach((p) => { if (typeof p === "string") { makeSync(p, mode); } else { makeSync(p.path, p.mode); } }); } return paths; } export { mkdirp, mkdirpSync };