2 const fs = require('fs');
3 const path = require('path');
4 const pify = require('pify');
7 mode: 0o777 & (~process.umask()),
11 // https://github.com/nodejs/node/issues/8987
12 // https://github.com/libuv/libuv/pull/1088
13 const checkPath = pth => {
14 if (process.platform === 'win32') {
15 const pathHasInvalidWinCharacters = /[<>:"|?*]/.test(pth.replace(path.parse(pth).root, ''));
17 if (pathHasInvalidWinCharacters) {
18 const err = new Error(`Path contains invalid characters: ${pth}`);
25 module.exports = (input, opts) => Promise.resolve().then(() => {
27 opts = Object.assign({}, defaults, opts);
29 const mkdir = pify(opts.fs.mkdir);
30 const stat = pify(opts.fs.stat);
33 return mkdir(pth, opts.mode)
36 if (err.code === 'ENOENT') {
37 if (err.message.includes('null bytes') || path.dirname(pth) === pth) {
41 return make(path.dirname(pth)).then(() => make(pth));
45 .then(stats => stats.isDirectory() ? pth : Promise.reject())
52 return make(path.resolve(input));
55 module.exports.sync = (input, opts) => {
57 opts = Object.assign({}, defaults, opts);
61 opts.fs.mkdirSync(pth, opts.mode);
63 if (err.code === 'ENOENT') {
64 if (err.message.includes('null bytes') || path.dirname(pth) === pth) {
68 make(path.dirname(pth));
73 if (!opts.fs.statSync(pth).isDirectory()) {
74 throw new Error('The path is not a directory');
84 return make(path.resolve(input));