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`) app.getPath('userData'), `/Chrome Storage/${extensionId}-${storageType}.json`)
} }
// recursively create directory and parent directories if needed const mkdirp = (dir, callback) => {
const mkdirParent = (dirPath, mode, callback) => { fs.mkdir(dir, (error) => {
fs.mkdir(dirPath, mode, error => { if (error && error.code === 'ENOENT') {
if (error && error.errno === 34) { mkdirp(path.dirname(dir), (error) => {
fs.mkdirParent(path.dirname(dirPath), mode, callback) if (!error) {
fs.mkdirParent(dirPath, mode, callback) 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 writeChromeStorageFile = (storageType, extensionId, data, cb) => {
const filePath = getChromeStoragePath(storageType, extensionId) const filePath = getChromeStoragePath(storageType, extensionId)
mkdirParent(path.dirname(filePath), err => { mkdirp(path.dirname(filePath), err => {
if (err) { /* we just ignore the errors of mkdir or mkdirParent */ } if (err) { /* we just ignore the errors of mkdir or mkdirp */ }
fs.writeFile(filePath, data, cb) fs.writeFile(filePath, data, cb)
}) })
} }