pax_global_header00006660000000000000000000000064137171433350014521gustar00rootroot0000000000000052 comment=9cc5f2c28e1f995ca8763b1c45d800e6ea77d047 mk-dirs-3.0.0/000077500000000000000000000000001371714333500130675ustar00rootroot00000000000000mk-dirs-3.0.0/.editorconfig000066400000000000000000000002711371714333500155440ustar00rootroot00000000000000root = true [*] charset = utf-8 indent_size = 2 indent_style = tab end_of_line = lf trim_trailing_whitespace = true insert_final_newline = true [*.{json,yml,md}] indent_style = space mk-dirs-3.0.0/.github/000077500000000000000000000000001371714333500144275ustar00rootroot00000000000000mk-dirs-3.0.0/.github/workflows/000077500000000000000000000000001371714333500164645ustar00rootroot00000000000000mk-dirs-3.0.0/.github/workflows/ci.yml000066400000000000000000000014301371714333500176000ustar00rootroot00000000000000name: CI on: push jobs: test: name: Node.js v${{ matrix.nodejs }} (${{ matrix.os }}) runs-on: ${{ matrix.os }} strategy: matrix: nodejs: [8, 10, 12, 14] os: [ubuntu-latest, windows-latest, macOS-latest] steps: - uses: actions/checkout@v2 - uses: actions/setup-node@v1 with: node-version: ${{ matrix.nodejs }} - name: Install run: | npm install npm install -g nyc - name: Test w/ Coverage run: npx nyc --include=src npm test - name: Report if: matrix.nodejs >= 14 && matrix.os == 'ubuntu-latest' run: | npx nyc report --reporter=text-lcov > coverage.lcov bash <(curl -s https://codecov.io/bash) env: CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }} mk-dirs-3.0.0/.gitignore000066400000000000000000000001021371714333500150500ustar00rootroot00000000000000node_modules .DS_Store *-lock.* *.lock *.log /dist /*.d.ts /sync mk-dirs-3.0.0/license000066400000000000000000000021201371714333500144270ustar00rootroot00000000000000MIT License Copyright (c) Luke Edwards (lukeed.com) Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. mk-dirs-3.0.0/package.json000066400000000000000000000015431371714333500153600ustar00rootroot00000000000000{ "name": "mk-dirs", "version": "3.0.0", "repository": "lukeed/mk-dirs", "description": "A tiny (381B to 419B) utility to make a directory and its parents, recursively", "module": "dist/index.mjs", "main": "dist/index.js", "types": "index.d.ts", "license": "MIT", "files": [ "*.d.ts", "dist", "sync" ], "author": { "name": "Luke Edwards", "email": "luke.edwards05@gmail.com", "url": "https://lukeed.com" }, "engines": { "node": ">=6" }, "scripts": { "build": "bundt", "pretest": "npm run build", "test": "uvu -r esm test" }, "modes": { "sync": "src/sync.js", "default": "src/async.js" }, "keywords": [ "mkdir", "make dir", "recursive", "mkdirp" ], "devDependencies": { "bundt": "1.1.0", "esm": "3.2.25", "premove": "3.0.1", "uvu": "0.3.3" } } mk-dirs-3.0.0/readme.md000066400000000000000000000106411371714333500146500ustar00rootroot00000000000000# mk-dirs [![CI](https://github.com/lukeed/mk-dirs/workflows/CI/badge.svg)](https://github.com/lukeed/mk-dirs/actions) [![codecov](https://badgen.now.sh/codecov/c/github/lukeed/mk-dirs)](https://codecov.io/gh/lukeed/mk-dirs) > A tiny (381B to 419B) utility to make a directory and its parents, recursively This is a `Promise`-based utility that recursively creates directories. It's effectively `mkdir -p` for Node.js. This module is a fast and lightweight alternative to [`mkdirp`](https://github.com/substack/node-mkdirp). Check out [Comparisons](#comparisons) for more info! > **Notice:** Node v10.12.0 includes the `recursive` option for [`fs.mkdir`](https://nodejs.org/api/fs.html#fs_fs_mkdir_path_options_callback) and [`fs.mkdirSync`](https://nodejs.org/api/fs.html#fs_fs_mkdirsync_path_options). ```js const { mkdir } = require('fs'); const { promisify } = require('util'); const mkdirp = promisify(mkdir); function mkdirs(str, opts={}) { return mkdirp(str, { ...opts, recursive:true }); } ``` ## Install ``` $ npm install --save mk-dirs ``` ## Modes There are two "versions" of `mk-dirs` available: #### "async" > **Node.js:** >= 8.x
> **Size (gzip):** 419 bytes
> **Availability:** [CommonJS](https://unpkg.com/mk-dirs/dist/index.js), [ES Module](https://unpkg.com/mk-dirs/dist/index.mjs) This is the primary/default mode. It makes use of `async`/`await` and [`util.promisify`](https://nodejs.org/api/util.html#util_util_promisify_original). #### "sync" > **Node.js:** >= 6.x
> **Size (gzip):** 381 bytes
> **Availability:** [CommonJS](https://unpkg.com/mk-dirs/sync/index.js), [ES Module](https://unpkg.com/mk-dirs/sync/index.mjs) This is the opt-in mode, ideal for scenarios where `async` usage cannot be supported.
In order to use it, simply make the following changes: ```diff -import { mkdir } from 'mk-dirs'; +import { mkdir } from 'mk-dirs/sync'; ``` ## Usage ```sh $ pwd # /Users/hello/world $ tree # . ``` ```js import { mkdir } from 'mk-dirs'; import { resolve } from 'path'; // Async/await try { let output = await mkdir('foo/bar/baz'); console.log(output); //=> "/Users/hello/world/foo/bar/baz" } catch (err) { // } // Promises mkdir('foo/bar/baz').then(output => { console.log(output); //=> "/Users/hello/world/foo/bar/baz" }).catch(err => { // }); // Using `cwd` option let dir = resolve('foo/bar'); await mkdir('hola/mundo', { cwd: dir }); //=> "/Users/hello/world/foo/bar/hola/mundo" ``` ```sh $ tree # . # └── foo # └── bar # └── baz # └── hola # └── mundo ``` ## API ### mkdir(path, options={}) Returns: `Promise` Returns a `Promise`, which resolves with the full path (string) of the created directory.
Any file system errors will be thrown and must be caught manually. #### path Type: `String` The directory to create. #### options.cwd Type: `String`
Default: `.` The directory to resolve your `path` from.
Defaults to the `process.cwd()` – aka, the directory that your command is run within. #### options.mode Type: `Number`
Default: `0o777 & (~process.umask())` The directory [permissions](https://x-team.com/blog/file-system-permissions-umask-node-js/) to set. > **Important:** Must be in octal format! ## Comparisons ***Versus `make-dir`*** * `mk-dirs` is slightly faster * ...has zero dependencies * ...does offer `cwd` option * ...does not re-wrap an existing Promise * ...does not ship with a `sync` method * ...does not allow custom `fs` option ***Versus `mkdirp`*** * `mk-dirs` is _much_ faster * ...has zero dependencies * ...is a Promise-based API * ...is `async`/`await` ready! * ...is tested on macOS, Linux, and Windows * ... has fixes for `mkdirp` issues: [#96](https://github.com/substack/node-mkdirp/pull/96), [#70](https://github.com/substack/node-mkdirp/issues/70), [#66](https://github.com/substack/node-mkdirp/issues/66) * ...includes a `cwd` option * ...does not ship with a `sync` method * ...does not allow custom `fs` option * ...does not bundle a CLI runtime ## Related - [`totalist`](https://github.com/lukeed/totalist) - A tiny (195B to 224B) utility to recursively list all (total) files in a directory - [`escalade`](https://github.com/lukeed/escalade) - A tiny (183B) and fast utility to ascend parent directories - [`premove`](https://github.com/lukeed/premove) – A tiny (247B) utility to remove items recursively ## License MIT © [Luke Edwards](https://lukeed.com) mk-dirs-3.0.0/src/000077500000000000000000000000001371714333500136565ustar00rootroot00000000000000mk-dirs-3.0.0/src/async.d.ts000066400000000000000000000002211371714333500155600ustar00rootroot00000000000000export interface Options { cwd: string; mode: number; } export function mkdir(filepath: string, options?: Partial): Promise; mk-dirs-3.0.0/src/async.js000066400000000000000000000016461371714333500153400ustar00rootroot00000000000000import { promisify } from 'util'; import { existsSync, mkdir as mk, stat } from 'fs'; import { join, normalize, parse, resolve } from 'path'; const statp = promisify(stat); const mkdirp = promisify(mk); function throws(code, msg, path) { let err = new Error(code + ': ' + msg); err.code=code; err.path=path; throw err; } export async function mkdir(str, opts={}) { if (process.platform === 'win32' && /[<>:"|?*]/.test(str.replace(parse(str).root, ''))) { throws('EINVAL', 'invalid characters', str); } let cwd = resolve(opts.cwd || '.'); let seg, stats, mode = opts.mode || 0o777 & (~process.umask()); let arr = resolve(cwd, normalize(str)).replace(cwd, '').split(/[\\\/]+/); for (seg of arr) { cwd = join(cwd, seg); if (existsSync(cwd)) { stats = await statp(cwd); if (!stats.isDirectory()) { throws('ENOTDIR', 'not a directory', cwd); } } else { await mkdirp(cwd, mode); } } return cwd; } mk-dirs-3.0.0/src/sync.d.ts000066400000000000000000000002101371714333500154150ustar00rootroot00000000000000export interface Options { cwd: string; mode: number; } export function mkdir(filepath: string, options?: Partial): string; mk-dirs-3.0.0/src/sync.js000066400000000000000000000014411371714333500151700ustar00rootroot00000000000000import { existsSync, mkdirSync, statSync } from 'fs'; import { join, normalize, parse, resolve } from 'path'; function throws(code, msg, path) { let err = new Error(code + ': ' + msg); err.code=code; err.path=path; throw err; } export function mkdir(str, opts={}) { if (process.platform === 'win32' && /[<>:"|?*]/.test(str.replace(parse(str).root, ''))) { throws('EINVAL', 'invalid characters', str); } let cwd = resolve(opts.cwd || '.'); let seg, mode = opts.mode || 0o777 & (~process.umask()); let arr = resolve(cwd, normalize(str)).replace(cwd, '').split(/\/|\\/); for (seg of arr) { cwd = join(cwd, seg); if (existsSync(cwd)) { if (!statSync(cwd).isDirectory()) { throws('ENOTDIR', 'not a directory', cwd); } } else { mkdirSync(cwd, mode); } } return cwd; } mk-dirs-3.0.0/test/000077500000000000000000000000001371714333500140465ustar00rootroot00000000000000mk-dirs-3.0.0/test/async.js000066400000000000000000000103621371714333500155230ustar00rootroot00000000000000import { test } from 'uvu'; import { premove } from 'premove'; import * as assert from 'uvu/assert'; import { existsSync, statSync, writeFileSync } from 'fs'; import { join, resolve } from 'path'; import { mkdir } from '../src/async'; const isWin = process.platform === 'win32'; function exists(str, bool, msg) { assert.is(existsSync(str), bool, msg); } function isValid(dir, mode) { let stats = statSync(dir); mode = isWin ? 0o666 : (mode || 0o777 & (~process.umask())); assert.is(stats.mode & 0o777, mode, '~> correct mode'); assert.ok(stats.isDirectory(), '~> is a directory'); } // --- test('exports', () => { assert.type(mkdir, 'function'); }); test('single (relative)', async () => { let out = await mkdir('foo'); assert.is(out, resolve('foo'), '~> returns the absolute file path'); exists(out, true); isValid(out); await premove(out); exists(out, false); }); test('single (absolute)', async () => { let str = resolve('bar'); let out = await mkdir(str); assert.is(out, str, '~> returns the absolute file path'); exists(out, true); isValid(out); await premove(out); exists(out, false); }); test('nested create / recursive', async () => { let dir = resolve('./foo'); let out = await mkdir('./foo/bar/baz'); exists(out, true); isValid(out); await premove(dir); exists(dir, false); }); test('option: mode', async () => { let mode = 0o744; let out = await mkdir('hello', { mode }); exists(out, true); isValid(out, mode); await premove(out); exists(out, false); }); test('option: cwd', async () => { let dir = resolve('foobar'); let str = resolve('foobar/foo/bar'); let out = await mkdir('foo/bar', { cwd:dir }); assert.is(out, str, '~> returns the absolute file path'); exists(out, true); isValid(out); await premove(dir); exists(dir, false); }); test('partially exists: directory', async () => { let foo = resolve('foobar'); let dir = await mkdir('foobar/baz'); let out = await mkdir('hello/world', { cwd: dir }); let str = resolve('foobar/baz/hello/world'); assert.is(out, str, '~> returns the absolute file path'); exists(out, true); isValid(out); await premove(foo); exists(foo, false); }); test('partially exists: file', async () => { let foo = resolve('foobar'); let file = join(foo, 'bar'); let dir = await mkdir(foo); exists(dir, true, '~> (setup) dir exists'); // create "foobar/bar" as a file writeFileSync(file, 'asd'); exists(file, true, '~> (setup) file exists'); try { await mkdir('foobar/bar/hello'); assert.unreachable('should have thrown'); } catch (err) { assert.instance(err, Error, 'throws Error'); assert.is(err.message, 'ENOTDIR: not a directory', '~> message'); assert.is(err.code, 'ENOTDIR', '~> code'); assert.is(err.path, file, '~> path'); } exists('foobar/bar/hello', false, '~> did not create "hello" dir'); exists('foobar/bar', true, '~> file still remains'); await premove(foo); exists(foo, false); }); test('path with null bytes', async () => { let dir = resolve('hello'); let str = resolve('hello/bar\u0000baz'); try { await mkdir(str); assert.unreachable('should have thrown'); } catch (err) { assert.instance(err, Error, 'throws Error'); assert.ok(/ENOENT|ERR_INVALID_ARG_VALUE/.test(err.code), '~> code'); assert.ok(err.message.includes('null bytes'), '~> message'); } exists(dir, true, '~> created "hello" base directory'); await premove(dir); exists(dir, false); }); test('should handle invalid pathname', async () => { let prev = process.platform; Object.defineProperty(process, 'platform', { value: 'win32' }); try { await mkdir('foo"bar'); assert.unreachable('should have thrown'); } catch (err) { assert.instance(err, Error, 'throws Error'); assert.is(err.message, 'EINVAL: invalid characters'); assert.is(err.path, 'foo"bar'); assert.is(err.code, 'EINVAL'); } Object.defineProperty(process, 'platform', { value: prev }); }); if (isWin) { // assume the `o:\` drive doesn't exist on Windows test('handles non-existent root', async () => { // Node changed how it handles between majors let [ver] = process.version.split('.', 1); let CODE = +ver.substring(1) >= 10 ? 'EINVAL' : 'ENOENT'; try { await mkdir('o:\\foo'); assert.unreachable('should have thrown'); } catch (err) { assert.is(err.code, CODE); } }); } test.run(); mk-dirs-3.0.0/test/sync.js000066400000000000000000000102511371714333500153570ustar00rootroot00000000000000import { test } from 'uvu'; import { premove } from 'premove'; import * as assert from 'uvu/assert'; import { existsSync, statSync, writeFileSync } from 'fs'; import { join, resolve } from 'path'; import { mkdir } from '../src/sync'; const isWin = process.platform === 'win32'; function exists(str, bool, msg) { assert.is(existsSync(str), bool, msg); } function isValid(dir, mode) { let stats = statSync(dir); mode = isWin ? 0o666 : (mode || 0o777 & (~process.umask())); assert.is(stats.mode & 0o777, mode, '~> correct mode'); assert.ok(stats.isDirectory(), '~> is a directory'); } // --- test('exports', () => { assert.type(mkdir, 'function'); }); test('single (relative)', async () => { let out = mkdir('foo'); assert.is(out, resolve('foo'), '~> returns the absolute file path'); exists(out, true); isValid(out); await premove(out); exists(out, false); }); test('single (absolute)', async () => { let str = resolve('bar'); let out = mkdir(str); assert.is(out, str, '~> returns the absolute file path'); exists(out, true); isValid(out); await premove(out); exists(out, false); }); test('nested create / recursive', async () => { let dir = resolve('./foo'); let out = mkdir('./foo/bar/baz'); exists(out, true); isValid(out); await premove(dir); exists(dir, false); }); test('option: mode', async () => { let mode = 0o744; let out = mkdir('hello', { mode }); exists(out, true); isValid(out, mode); await premove(out); exists(out, false); }); test('option: cwd', async () => { let dir = resolve('foobar'); let str = resolve('foobar/foo/bar'); let out = mkdir('foo/bar', { cwd:dir }); assert.is(out, str, '~> returns the absolute file path'); exists(out, true); isValid(out); await premove(dir); exists(dir, false); }); test('partially exists: directory', async () => { let foo = resolve('foobar'); let dir = mkdir('foobar/baz'); let out = mkdir('hello/world', { cwd: dir }); let str = resolve('foobar/baz/hello/world'); assert.is(out, str, '~> returns the absolute file path'); exists(out, true); isValid(out); await premove(foo); exists(foo, false); }); test('partially exists: file', async () => { let foo = resolve('foobar'); let file = join(foo, 'bar'); let dir = mkdir(foo); exists(dir, true, '~> (setup) dir exists'); // create "foobar/bar" as a file writeFileSync(file, 'asd'); exists(file, true, '~> (setup) file exists'); try { mkdir('foobar/bar/hello'); assert.unreachable('should have thrown'); } catch (err) { assert.instance(err, Error, 'throws Error'); assert.is(err.message, 'ENOTDIR: not a directory', '~> message'); assert.is(err.code, 'ENOTDIR', '~> code'); assert.is(err.path, file, '~> path'); } exists('foobar/bar/hello', false, '~> did not create "hello" dir'); exists('foobar/bar', true, '~> file still remains'); await premove(foo); exists(foo, false); }); test('path with null bytes', async () => { let dir = resolve('hello'); let str = resolve('hello/bar\u0000baz'); try { mkdir(str); assert.unreachable('should have thrown'); } catch (err) { assert.instance(err, Error, 'throws Error'); assert.ok(/ENOENT|ERR_INVALID_ARG_VALUE/.test(err.code), '~> code'); assert.ok(err.message.includes('null bytes'), '~> message'); } exists(dir, true, '~> created "hello" base directory'); await premove(dir); exists(dir, false); }); test('should handle invalid pathname', async () => { let prev = process.platform; Object.defineProperty(process, 'platform', { value: 'win32' }); try { mkdir('foo"bar'); assert.unreachable('should have thrown'); } catch (err) { assert.instance(err, Error, 'throws Error'); assert.is(err.message, 'EINVAL: invalid characters'); assert.is(err.path, 'foo"bar'); assert.is(err.code, 'EINVAL'); } Object.defineProperty(process, 'platform', { value: prev }); }); if (isWin) { // assume the `o:\` drive doesn't exist on Windows test('handles non-existent root', async () => { // Node changed how it handles between majors let [ver] = process.version.split('.', 1); let CODE = +ver.substring(1) >= 10 ? 'EINVAL' : 'ENOENT'; try { mkdir('o:\\foo'); assert.unreachable('should have thrown'); } catch (err) { assert.is(err.code, CODE); } }); } test.run();