Correct mkdir

This commit is contained in:
Alexandre Lachèze 2017-07-20 19:50:55 +02:00
parent ec8407c65d
commit 498f344e2e

View file

@ -8,14 +8,19 @@ const getChromeStoragePath = (storageType, extensionId) => {
app.getPath('userData'), `/Chrome Storage/${extensionId}-${storageType}.json`)
}
// recursively create directory and parent directories if needed
const mkdirParent = (dirPath, mode, callback) => {
fs.mkdir(dirPath, mode, error => {
if (error && error.errno === 34) {
fs.mkdirParent(path.dirname(dirPath), mode, callback)
fs.mkdirParent(dirPath, mode, callback)
const mkdirp = (dir, callback) => {
fs.mkdir(dir, (error) => {
if (error && error.code === 'ENOENT') {
mkdirp(path.dirname(dir), (error) => {
if (!error) {
mkdirp(dir, callback)
}
})
} else if (error && error.code === 'EEXIST') {
callback(null)
} else {
callback(error)
}
callback && callback(error)
})
}
@ -32,8 +37,8 @@ const readChromeStorageFile = (storageType, extensionId, cb) => {
const writeChromeStorageFile = (storageType, extensionId, data, cb) => {
const filePath = getChromeStoragePath(storageType, extensionId)
mkdirParent(path.dirname(filePath), err => {
if (err) { /* we just ignore the errors of mkdir or mkdirParent */ }
mkdirp(path.dirname(filePath), err => {
if (err) { /* we just ignore the errors of mkdir or mkdirp */ }
fs.writeFile(filePath, data, cb)
})
}