* Make a directory (or series of nested directories) without throwing an error if it already exists. * * @param {string} filepath - The path to the directory. * @param {boolean} [_selfCall=false] - Internal flag to prevent infinite recursion. * @returns {Promise }
(filepath, _selfCall = false)
| 162 | * @returns {Promise<void>} |
| 163 | */ |
| 164 | async mkdir(filepath, _selfCall = false) { |
| 165 | try { |
| 166 | await this._mkdir(filepath) |
| 167 | } catch (err) { |
| 168 | // If err is null then operation succeeded! |
| 169 | if (err === null) return |
| 170 | // If the directory already exists, that's OK! |
| 171 | if (err.code === 'EEXIST') return |
| 172 | // Avoid infinite loops of failure |
| 173 | if (_selfCall) throw err |
| 174 | // If we got a "no such file or directory error" backup and try again. |
| 175 | if (err.code === 'ENOENT') { |
| 176 | const parent = dirname(filepath) |
| 177 | // Check to see if we've gone too far |
| 178 | if (parent === '.' || parent === '/' || parent === filepath) throw err |
| 179 | // Infinite recursion, what could go wrong? |
| 180 | await this.mkdir(parent) |
| 181 | await this.mkdir(filepath, true) |
| 182 | } |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | /** |
| 187 | * Delete a file without throwing an error if it is already deleted. |