pax_global_header00006660000000000000000000000064131044056650014516gustar00rootroot0000000000000052 comment=5fea1cde511edf07f0ac8101b376269de3f4c98a make-dir-1.0.0/000077500000000000000000000000001310440566500132055ustar00rootroot00000000000000make-dir-1.0.0/.editorconfig000066400000000000000000000002571310440566500156660ustar00rootroot00000000000000root = true [*] indent_style = tab end_of_line = lf charset = utf-8 trim_trailing_whitespace = true insert_final_newline = true [*.yml] indent_style = space indent_size = 2 make-dir-1.0.0/.gitattributes000066400000000000000000000000351310440566500160760ustar00rootroot00000000000000* text=auto *.js text eol=lf make-dir-1.0.0/.gitignore000066400000000000000000000000311310440566500151670ustar00rootroot00000000000000node_modules .nyc_output make-dir-1.0.0/.travis.yml000066400000000000000000000002521310440566500153150ustar00rootroot00000000000000os: - linux - osx language: node_js node_js: - '6' - '4' after_success: - './node_modules/.bin/nyc report --reporter=text-lcov | ./node_modules/.bin/coveralls' make-dir-1.0.0/appveyor.yml000066400000000000000000000005371310440566500156020ustar00rootroot00000000000000environment: matrix: - nodejs_version: '6' - nodejs_version: '4' install: - ps: Install-Product node $env:nodejs_version - set CI=true - npm -g install npm@latest - set PATH=%APPDATA%\npm;%PATH% - npm install matrix: fast_finish: true build: off shallow_clone: true test_script: - node --version - npm --version - npm test make-dir-1.0.0/index.js000066400000000000000000000033571310440566500146620ustar00rootroot00000000000000'use strict'; const fs = require('fs'); const path = require('path'); const pify = require('pify'); const defaults = { mode: 0o777 & (~process.umask()), fs }; // https://github.com/nodejs/node/issues/8987 // https://github.com/libuv/libuv/pull/1088 const checkPath = pth => { if (process.platform === 'win32') { const pathHasInvalidWinCharacters = /[<>:"|?*]/.test(pth.replace(path.parse(pth).root, '')); if (pathHasInvalidWinCharacters) { const err = new Error(`Path contains invalid characters: ${pth}`); err.code = 'EINVAL'; throw err; } } }; module.exports = (input, opts) => Promise.resolve().then(() => { checkPath(input); opts = Object.assign({}, defaults, opts); const fsP = pify(opts.fs); const make = pth => { return fsP.mkdir(pth, opts.mode) .then(() => pth) .catch(err => { if (err.code === 'ENOENT') { if (err.message.includes('null bytes') || path.dirname(pth) === pth) { throw err; } return make(path.dirname(pth)).then(() => make(pth)); } return fsP.stat(pth) .then(stats => stats.isDirectory() ? pth : Promise.reject()) .catch(() => { throw err; }); }); }; return make(path.resolve(input)); }); module.exports.sync = (input, opts) => { checkPath(input); opts = Object.assign({}, defaults, opts); const make = pth => { try { opts.fs.mkdirSync(pth, opts.mode); } catch (err) { if (err.code === 'ENOENT') { if (err.message.includes('null bytes') || path.dirname(pth) === pth) { throw err; } make(path.dirname(pth)); return make(pth); } try { if (!opts.fs.statSync(pth).isDirectory()) { throw new Error(); } } catch (_) { throw err; } } return pth; }; return make(path.resolve(input)); }; make-dir-1.0.0/license000066400000000000000000000021371310440566500145550ustar00rootroot00000000000000The MIT License (MIT) Copyright (c) Sindre Sorhus (sindresorhus.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. make-dir-1.0.0/package.json000066400000000000000000000017051310440566500154760ustar00rootroot00000000000000{ "name": "make-dir", "version": "1.0.0", "description": "Make a directory and its parents if needed - Think `mkdir -p`", "license": "MIT", "repository": "sindresorhus/make-dir", "author": { "name": "Sindre Sorhus", "email": "sindresorhus@gmail.com", "url": "sindresorhus.com" }, "engines": { "node": ">=4" }, "scripts": { "test": "xo && nyc ava" }, "files": [ "index.js" ], "keywords": [ "mkdir", "mkdirp", "make", "directories", "dir", "dirs", "folders", "directory", "folder", "path", "parent", "parents", "intermediate", "recursively", "recursive", "create", "fs", "filesystem", "file-system" ], "dependencies": { "pify": "^2.3.0" }, "devDependencies": { "ava": "*", "coveralls": "^2.13.0", "graceful-fs": "^4.1.11", "nyc": "^10.2.0", "path-type": "^2.0.0", "tempy": "^0.1.0", "xo": "*" } } make-dir-1.0.0/readme.md000066400000000000000000000046741310440566500147770ustar00rootroot00000000000000# make-dir [![Build Status: macOS & Linux](https://travis-ci.org/sindresorhus/make-dir.svg?branch=master)](https://travis-ci.org/sindresorhus/make-dir) [![Build status: Windows](https://ci.appveyor.com/api/projects/status/e0vtt8y600w91gcs/branch/master?svg=true)](https://ci.appveyor.com/project/sindresorhus/make-dir/branch/master) [![Coverage Status](https://coveralls.io/repos/github/sindresorhus/make-dir/badge.svg)](https://coveralls.io/github/sindresorhus/make-dir) > Make a directory and its parents if needed - Think `mkdir -p` ## Advantages over [`mkdirp`](https://github.com/substack/node-mkdirp) - Promise API *(Async/await ready!)* - Fixes many `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) - 100% test coverage - CI-tested on macOS, Linux, and Windows - Actively maintained - Doesn't bundle a CLI ## Install ``` $ npm install --save make-dir ``` ## Usage ``` $ pwd /Users/sindresorhus/fun $ tree . ``` ```js const makeDir = require('make-dir'); makeDir('unicorn/rainbow/cake').then(path => { console.log(path); //=> '/Users/sindresorhus/fun/unicorn/rainbow/cake' }); ``` ``` $ tree . └── unicorn └── rainbow └── cake ``` Multiple directories: ```js const makeDir = require('make-dir'); Promise.all([ makeDir('unicorn/rainbow') makeDir('foo/bar') ]).then(paths => { console.log(paths); /* [ '/Users/sindresorhus/fun/unicorn/rainbow', '/Users/sindresorhus/fun/foo/bar' ] */ }); ``` ## API ### makeDir(path, [options]) Returns a `Promise` for the path to the created directory. ### makeDir.sync(path, [options]) Returns the path to the created directory. #### path Type: `string` Directory to create. #### options Type: `Object` ##### mode Type: `integer`
Default: `0o777 & (~process.umask())` Directory [permissions](https://x-team.com/blog/file-system-permissions-umask-node-js/). ##### fs Type: `Object`
Default: `require('fs')` Use a custom `fs` implementation. For example [`graceful-fs`](https://github.com/isaacs/node-graceful-fs). ## Related - [make-dir-cli](https://github.com/sindresorhus/make-dir-cli) - CLI for this module - [del](https://github.com/sindresorhus/del) - Delete files and directories - [globby](https://github.com/sindresorhus/globby) - User-friendly glob matching ## License MIT © [Sindre Sorhus](https://sindresorhus.com) make-dir-1.0.0/test/000077500000000000000000000000001310440566500141645ustar00rootroot00000000000000make-dir-1.0.0/test/async.js000066400000000000000000000043301310440566500156370ustar00rootroot00000000000000import fs from 'fs'; import path from 'path'; import test from 'ava'; import tempy from 'tempy'; import gracefulFs from 'graceful-fs'; import {getFixture, assertDir} from './helpers/util'; import m from '..'; test('main', async t => { const dir = getFixture(); const madeDir = await m(dir); t.true(madeDir.length > 0); assertDir(t, madeDir); }); test('`fs` option', async t => { const dir = getFixture(); await m(dir, {fs: gracefulFs}); assertDir(t, dir); }); test('`mode` option', async t => { const dir = getFixture(); const mode = 0o744; await m(dir, {mode}); assertDir(t, dir, mode); // Ensure it's writable await m(dir); assertDir(t, dir, mode); }); test('dir exists', async t => { const dir = await m(tempy.directory()); t.true(dir.length > 0); assertDir(t, dir); }); test('file exits', async t => { const fp = tempy.file(); fs.writeFileSync(fp, ''); const err = await t.throws(m(fp)); t.is(err.code, 'EEXIST'); }); test('root dir', async t => { const dir = await m('/'); t.true(dir.length > 0); assertDir(t, dir); }); test('race two', async t => { const dir = getFixture(); await Promise.all([m(dir), m(dir)]); assertDir(t, dir); }); test('race many', async t => { const dir = getFixture(); const all = []; for (let i = 0; i < 100; i++) { all.push(m(dir)); } await Promise.all(all); assertDir(t, dir); }); test('handles null bytes in path', async t => { const dir = path.join(tempy.directory(), 'foo\u0000bar'); const err = await t.throws(m(dir), /null bytes/); t.is(err.code, 'ENOENT'); }); test.serial('handles invalid path characters', async t => { // We do this to please `nyc` const platform = process.platform; Object.defineProperty(process, 'platform', { value: 'win32' }); // Also to please `nyc` await m(tempy.directory()); const dir = path.join(tempy.directory(), 'foo"bar'); const err = await t.throws(m(dir), /invalid characters/); t.is(err.code, 'EINVAL'); Object.defineProperty(process, 'platform', { value: platform }); }); if (process.platform === 'win32') { test('handles non-existent root', async t => { // We assume the `o:\` drive doesn't exist on Windows const err = await t.throws(m('o:\\foo'), /no such file or directory/); t.is(err.code, 'ENOENT'); }); } make-dir-1.0.0/test/helpers/000077500000000000000000000000001310440566500156265ustar00rootroot00000000000000make-dir-1.0.0/test/helpers/util.js000066400000000000000000000007461310440566500171500ustar00rootroot00000000000000import fs from 'fs'; import path from 'path'; import tempy from 'tempy'; import pathType from 'path-type'; export const getFixture = () => path.join(tempy.directory(), 'a/b/c/unicorn_unicorn_unicorn/d/e/f/g/h'); export const assertDir = (t, dir, mode = 0o777 & (~process.umask())) => { // Setting `mode` on `mkdir` on Windows doesn't seem to work if (process.platform === 'win32') { mode = 0o666; } t.true(pathType.dirSync(dir)); t.is(fs.statSync(dir).mode & 0o777, mode); }; make-dir-1.0.0/test/sync.js000066400000000000000000000032641310440566500155030ustar00rootroot00000000000000import fs from 'fs'; import path from 'path'; import test from 'ava'; import tempy from 'tempy'; import gracefulFs from 'graceful-fs'; import {getFixture, assertDir} from './helpers/util'; import m from '..'; test('main', t => { const dir = getFixture(); const madeDir = m.sync(dir); t.true(madeDir.length > 0); assertDir(t, madeDir); }); test('`fs` option', t => { const dir = getFixture(); m.sync(dir, {fs: gracefulFs}); assertDir(t, dir); }); test('`mode` option', t => { const dir = getFixture(); const mode = 0o744; m.sync(dir, {mode}); assertDir(t, dir, mode); // Ensure it's writable m.sync(dir); assertDir(t, dir, mode); }); test('dir exists', t => { const dir = m.sync(tempy.directory()); t.true(dir.length > 0); assertDir(t, dir); }); test('file exits', t => { const fp = tempy.file(); fs.writeFileSync(fp, ''); const err = t.throws(() => { m.sync(fp); }); t.is(err.code, 'EEXIST'); }); test('root dir', t => { const dir = m.sync('/'); t.true(dir.length > 0); assertDir(t, dir); }); test('race two', t => { const dir = getFixture(); m.sync(dir); m.sync(dir); assertDir(t, dir); }); test('race many', t => { const dir = getFixture(); for (let i = 0; i < 100; i++) { m.sync(dir); } assertDir(t, dir); }); test('handles null bytes in path', t => { const dir = path.join(tempy.directory(), 'foo\u0000bar'); const err = t.throws(() => { m.sync(dir); }, /null bytes/); t.is(err.code, 'ENOENT'); }); if (process.platform === 'win32') { test('handles non-existent root', t => { // We assume the `o:\` drive doesn't exist on Windows const err = t.throws(() => { m.sync('o:\\foo'); }, /no such file or directory/); t.is(err.code, 'ENOENT'); }); }