@ifaxity/mkdirp
Advanced tools
Comparing version 1.0.1 to 1.0.2
26
main.js
@@ -0,7 +1,8 @@ | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
const { promisify } = require('util'); | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
const stat = promisify(fs.stat); | ||
const mkdir = promisify(fs.mkdir); | ||
const version = process.version.substring(1).split('.').map(i => i | 0); | ||
@@ -11,22 +12,25 @@ module.exports = async function mkdirp(dirpath, mode = 0777) { | ||
// Use native mkdirp function if supported (v10.12.0) | ||
if (version[0] > 10 || (version[0] == 10 && version[1] >= 12)) { | ||
return mkdir(dirpath, { recursive: true, mode }); | ||
} | ||
try { | ||
await mkdir(dirpath, mode); | ||
return true; | ||
} | ||
catch(ex) { | ||
// If parent dir doesnt exist then create parent directory first then try again | ||
} catch(ex) { | ||
if(ex.code == 'ENOENT') { | ||
// Create parent directory then try again | ||
await mkdirp(path.dirname(dirpath), mode); | ||
return await mkdirp(dirpath, mode); | ||
} else { | ||
// In the case of any other error, just see if there's a dir there already. | ||
try { | ||
// Check if something else is borker or if directory already exists | ||
// If not a directory then throw error, if not then just return false to indicate we didn't create a directory. | ||
const dirstat = await stat(dirpath); | ||
if(!dirstat.isDirectory()) throw ex; | ||
// Directory exists already yay! report that none was created | ||
return false; | ||
} catch(_) { | ||
// If stat fails then throw first exception it's for weirdness | ||
} catch (_) { | ||
// If stat fails then throw first error | ||
throw ex; | ||
@@ -36,2 +40,2 @@ } | ||
} | ||
}; | ||
}; |
{ | ||
"name": "@ifaxity/mkdirp", | ||
"version": "1.0.1", | ||
"description": "A promise based implementation of the bash command mkdir -p", | ||
"version": "1.0.2", | ||
"description": "A async based implementation of a recursive mkdir function.", | ||
"main": "main.js", | ||
"author": "iFaxity <izze96@gmail.com>", | ||
"author": "Christian Norrman <christian@faxity.se>", | ||
"license": "MIT", | ||
"files": [ | ||
"main.js" | ||
], | ||
"engines": { | ||
@@ -9,0 +12,0 @@ "node": ">= 8" |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
2502
33