mirror of
https://github.com/fluencelabs/js-libp2p
synced 2025-03-17 08:00:51 +00:00
29 lines
621 B
JavaScript
29 lines
621 B
JavaScript
|
'use strict'
|
||
|
const fs = require('fs')
|
||
|
const path = require('path')
|
||
|
|
||
|
/**
|
||
|
* mkdirp recursively creates needed folders for the given dir path
|
||
|
* @param {string} dir
|
||
|
* @returns {string} The path that was created
|
||
|
*/
|
||
|
module.exports.mkdirp = (dir) => {
|
||
|
return path
|
||
|
.resolve(dir)
|
||
|
.split(path.sep)
|
||
|
.reduce((acc, cur) => {
|
||
|
const currentPath = path.normalize(acc + path.sep + cur)
|
||
|
|
||
|
try {
|
||
|
fs.statSync(currentPath)
|
||
|
} catch (e) {
|
||
|
if (e.code === 'ENOENT') {
|
||
|
fs.mkdirSync(currentPath)
|
||
|
} else {
|
||
|
throw e
|
||
|
}
|
||
|
}
|
||
|
return currentPath
|
||
|
}, '')
|
||
|
}
|