Recursively mkdir the parent directories
This commit is contained in:
parent
c85f3cbd2c
commit
ec8407c65d
1 changed files with 13 additions and 2 deletions
|
@ -8,6 +8,17 @@ 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 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)
|
||||||
|
}
|
||||||
|
callback && callback(error)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
const readChromeStorageFile = (storageType, extensionId, cb) => {
|
const readChromeStorageFile = (storageType, extensionId, cb) => {
|
||||||
const filePath = getChromeStoragePath(storageType, extensionId)
|
const filePath = getChromeStoragePath(storageType, extensionId)
|
||||||
fs.readFile(filePath, 'utf8', (err, data) => {
|
fs.readFile(filePath, 'utf8', (err, data) => {
|
||||||
|
@ -21,8 +32,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)
|
||||||
|
|
||||||
fs.mkdir(path.dirname(filePath), err => {
|
mkdirParent(path.dirname(filePath), err => {
|
||||||
if (err) { /* we just ignore the errors of mkdir */ }
|
if (err) { /* we just ignore the errors of mkdir or mkdirParent */ }
|
||||||
fs.writeFile(filePath, data, cb)
|
fs.writeFile(filePath, data, cb)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue