source: node_modules/fs-extra/lib/mkdirs/make-dir.js

main
Last change on this file was d24f17c, checked in by Aleksandar Panovski <apano77@…>, 15 months ago

Initial commit

  • Property mode set to 100644
File size: 4.3 KB
Line 
1// Adapted from https://github.com/sindresorhus/make-dir
2// Copyright (c) Sindre Sorhus <sindresorhus@gmail.com> (sindresorhus.com)
3// 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:
4// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
5// 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.
6'use strict'
7const fs = require('../fs')
8const path = require('path')
9const atLeastNode = require('at-least-node')
10
11const useNativeRecursiveOption = atLeastNode('10.12.0')
12
13// https://github.com/nodejs/node/issues/8987
14// https://github.com/libuv/libuv/pull/1088
15const checkPath = pth => {
16 if (process.platform === 'win32') {
17 const pathHasInvalidWinCharacters = /[<>:"|?*]/.test(pth.replace(path.parse(pth).root, ''))
18
19 if (pathHasInvalidWinCharacters) {
20 const error = new Error(`Path contains invalid characters: ${pth}`)
21 error.code = 'EINVAL'
22 throw error
23 }
24 }
25}
26
27const processOptions = options => {
28 const defaults = { mode: 0o777 }
29 if (typeof options === 'number') options = { mode: options }
30 return { ...defaults, ...options }
31}
32
33const permissionError = pth => {
34 // This replicates the exception of `fs.mkdir` with native the
35 // `recusive` option when run on an invalid drive under Windows.
36 const error = new Error(`operation not permitted, mkdir '${pth}'`)
37 error.code = 'EPERM'
38 error.errno = -4048
39 error.path = pth
40 error.syscall = 'mkdir'
41 return error
42}
43
44module.exports.makeDir = async (input, options) => {
45 checkPath(input)
46 options = processOptions(options)
47
48 if (useNativeRecursiveOption) {
49 const pth = path.resolve(input)
50
51 return fs.mkdir(pth, {
52 mode: options.mode,
53 recursive: true
54 })
55 }
56
57 const make = async pth => {
58 try {
59 await fs.mkdir(pth, options.mode)
60 } catch (error) {
61 if (error.code === 'EPERM') {
62 throw error
63 }
64
65 if (error.code === 'ENOENT') {
66 if (path.dirname(pth) === pth) {
67 throw permissionError(pth)
68 }
69
70 if (error.message.includes('null bytes')) {
71 throw error
72 }
73
74 await make(path.dirname(pth))
75 return make(pth)
76 }
77
78 try {
79 const stats = await fs.stat(pth)
80 if (!stats.isDirectory()) {
81 // This error is never exposed to the user
82 // it is caught below, and the original error is thrown
83 throw new Error('The path is not a directory')
84 }
85 } catch {
86 throw error
87 }
88 }
89 }
90
91 return make(path.resolve(input))
92}
93
94module.exports.makeDirSync = (input, options) => {
95 checkPath(input)
96 options = processOptions(options)
97
98 if (useNativeRecursiveOption) {
99 const pth = path.resolve(input)
100
101 return fs.mkdirSync(pth, {
102 mode: options.mode,
103 recursive: true
104 })
105 }
106
107 const make = pth => {
108 try {
109 fs.mkdirSync(pth, options.mode)
110 } catch (error) {
111 if (error.code === 'EPERM') {
112 throw error
113 }
114
115 if (error.code === 'ENOENT') {
116 if (path.dirname(pth) === pth) {
117 throw permissionError(pth)
118 }
119
120 if (error.message.includes('null bytes')) {
121 throw error
122 }
123
124 make(path.dirname(pth))
125 return make(pth)
126 }
127
128 try {
129 if (!fs.statSync(pth).isDirectory()) {
130 // This error is never exposed to the user
131 // it is caught below, and the original error is thrown
132 throw new Error('The path is not a directory')
133 }
134 } catch {
135 throw error
136 }
137 }
138 }
139
140 return make(path.resolve(input))
141}
Note: See TracBrowser for help on using the repository browser.