Browse Source

Correct mkdir

Alexandre Lachèze 7 years ago
parent
commit
498f344e2e
1 changed files with 14 additions and 9 deletions
  1. 14 9
      lib/renderer/extensions/storage.js

+ 14 - 9
lib/renderer/extensions/storage.js

@@ -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)
   })
 }