Browse Source

Fix #11 now using fs-extra

b3z 5 months ago
parent
commit
b40c8b9c78
4 changed files with 70 additions and 38 deletions
  1. 60 8
      package-lock.json
  2. 2 0
      package.json
  3. 8 28
      src/fileHandler.ts
  4. 0 2
      src/index.ts

+ 60 - 8
package-lock.json

@@ -535,6 +535,11 @@
       "integrity": "sha1-x57Zf380y48robyXkLzDZkdLS3k=",
       "dev": true
     },
+    "at-least-node": {
+      "version": "1.0.0",
+      "resolved": "https://registry.npmjs.org/at-least-node/-/at-least-node-1.0.0.tgz",
+      "integrity": "sha512-+q/t7Ekv1EDY2l6Gda6LLiX14rU9TV20Wa3ofeQmwPFZbOMo9DXrLbOjFaaclkXKWidIaopwAObQDqwWtGUjqg=="
+    },
     "author-regex": {
       "version": "1.0.0",
       "resolved": "https://registry.npmjs.org/author-regex/-/author-regex-1.0.0.tgz",
@@ -1304,6 +1309,19 @@
         "rc": "^1.2.1",
         "semver": "^5.4.1",
         "sumchecker": "^2.0.2"
+      },
+      "dependencies": {
+        "fs-extra": {
+          "version": "4.0.3",
+          "resolved": "https://registry.npmjs.org/fs-extra/-/fs-extra-4.0.3.tgz",
+          "integrity": "sha512-q6rbdDd1o2mAnQreO7YADIxf/Whx4AHBiRf6d+/cVT8h44ss+lHgxf1FemcqDnQt9X3ct4McHr+JMGlYSsK7Cg==",
+          "dev": true,
+          "requires": {
+            "graceful-fs": "^4.1.2",
+            "jsonfile": "^4.0.0",
+            "universalify": "^0.1.0"
+          }
+        }
       }
     },
     "electron-installer-dmg": {
@@ -1799,14 +1817,30 @@
       }
     },
     "fs-extra": {
-      "version": "4.0.3",
-      "resolved": "https://registry.npmjs.org/fs-extra/-/fs-extra-4.0.3.tgz",
-      "integrity": "sha512-q6rbdDd1o2mAnQreO7YADIxf/Whx4AHBiRf6d+/cVT8h44ss+lHgxf1FemcqDnQt9X3ct4McHr+JMGlYSsK7Cg==",
-      "dev": true,
-      "requires": {
-        "graceful-fs": "^4.1.2",
-        "jsonfile": "^4.0.0",
-        "universalify": "^0.1.0"
+      "version": "9.0.0",
+      "resolved": "https://registry.npmjs.org/fs-extra/-/fs-extra-9.0.0.tgz",
+      "integrity": "sha512-pmEYSk3vYsG/bF651KPUXZ+hvjpgWYw/Gc7W9NFUe3ZVLczKKWIij3IKpOrQcdw4TILtibFslZ0UmR8Vvzig4g==",
+      "requires": {
+        "at-least-node": "^1.0.0",
+        "graceful-fs": "^4.2.0",
+        "jsonfile": "^6.0.1",
+        "universalify": "^1.0.0"
+      },
+      "dependencies": {
+        "jsonfile": {
+          "version": "6.0.1",
+          "resolved": "https://registry.npmjs.org/jsonfile/-/jsonfile-6.0.1.tgz",
+          "integrity": "sha512-jR2b5v7d2vIOust+w3wtFKZIfpC2pnRmFAhAC/BuweZFQR8qZzxH1OyrQ10HmdVYiXWkYUqPVsz91cG7EL2FBg==",
+          "requires": {
+            "graceful-fs": "^4.1.6",
+            "universalify": "^1.0.0"
+          }
+        },
+        "universalify": {
+          "version": "1.0.0",
+          "resolved": "https://registry.npmjs.org/universalify/-/universalify-1.0.0.tgz",
+          "integrity": "sha512-rb6X1W158d7pRQBg5gkR8uPaSfiids68LTJQYOtEUhoJUWBdaQHsuT/EUduxXYxcrt4r5PJ4fuHW1MHT6p0qug=="
+        }
       }
     },
     "fs-temp": {
@@ -1847,6 +1881,19 @@
         "debug": "^3.1.0",
         "flora-colossus": "^1.0.0",
         "fs-extra": "^4.0.0"
+      },
+      "dependencies": {
+        "fs-extra": {
+          "version": "4.0.3",
+          "resolved": "https://registry.npmjs.org/fs-extra/-/fs-extra-4.0.3.tgz",
+          "integrity": "sha512-q6rbdDd1o2mAnQreO7YADIxf/Whx4AHBiRf6d+/cVT8h44ss+lHgxf1FemcqDnQt9X3ct4McHr+JMGlYSsK7Cg==",
+          "dev": true,
+          "requires": {
+            "graceful-fs": "^4.1.2",
+            "jsonfile": "^4.0.0",
+            "universalify": "^0.1.0"
+          }
+        }
       }
     },
     "generate-function": {
@@ -2870,6 +2917,11 @@
       "resolved": "https://registry.npmjs.org/napi-macros/-/napi-macros-1.8.2.tgz",
       "integrity": "sha512-Tr0DNY4RzTaBG2W2m3l7ZtFuJChTH6VZhXVhkGGjF/4cZTt+i8GcM9ozD+30Lmr4mDoZ5Xx34t2o4GJqYWDGcg=="
     },
+    "ncp": {
+      "version": "2.0.0",
+      "resolved": "https://registry.npmjs.org/ncp/-/ncp-2.0.0.tgz",
+      "integrity": "sha1-GVoh1sRuNh0vsSgbo4uR6d9727M="
+    },
     "nice-try": {
       "version": "1.0.5",
       "resolved": "https://registry.npmjs.org/nice-try/-/nice-try-1.0.5.tgz",

+ 2 - 0
package.json

@@ -38,7 +38,9 @@
         "config": "^3.3.1",
         "diskdb": "^0.1.17",
         "electron-reload": "^1.5.0",
+        "fs-extra": "^9.0.0",
         "highlight.js": "^9.18.1",
+        "ncp": "^2.0.0",
         "node-properties": "^0.2.3",
         "pouchdb": "^7.1.1",
         "pouchdb-browser": "^7.1.1",

+ 8 - 28
src/fileHandler.ts

@@ -3,25 +3,7 @@ import { promises } from "dns";
 import { rejects } from "assert";
 import { get } from "config";
 import { log } from "./logger";
-
-/**
- * Copy a file to the Archive.
- *
- * @param filePath of the file which should be copied.
- */
-export function copy2archive(filePath: string) {
-    // destination.txt will be created or overwritten by default.
-    let filePathArr = filePath.split("/");
-    let destination = (get("file.archive") + "/" + filePathArr[filePathArr.length - 1]).replace(/ /g, "_");
-    console.log(destination);
-    copyFile(filePath, destination, (err) => {
-        if (err) {
-            fixDir(get("file.archive"));
-            log.error("A copy error accured. Trying again with fixed directory. (Ignore if only occures once.)");
-            copy2archive(filePath);
-        }
-    });
-}
+const fs = require("fs-extra");
 
 /**
  * Check if a file exists.
@@ -41,13 +23,11 @@ export async function exist(filePath: string): Promise<boolean> {
     });
 }
 
-/**
- * Fixes broken (nonexistent) archive directory.
- * @param dir
- */
-function fixDir(dir: string) {
-    if (!existsSync(dir)) {
-        log.debug("Fix " + dir);
-        mkdirSync(dir);
-    }
+export function copy2archive(filePath: string) {
+    let filePathArr = filePath.split("/");
+    let destination = (get("file.archive") + "/" + filePathArr[filePathArr.length - 1]).replace(/ /g, "_");
+
+    fs.copy(filePath, destination, (err: Error) => {
+        if (err) return log.error(err);
+    });
 }

+ 0 - 2
src/index.ts

@@ -64,7 +64,6 @@ function createWindow() {
                 if (url.indexOf("http") !== -1) {
                     shell.openExternal(url);
                 } else {
-                    console.log(link);
                     shell.openItem(link);
                 }
             } catch (e) {
@@ -251,7 +250,6 @@ function handleClear() {
 
 ipcMain.on("editor:files:save", async (e: any, files: string[]) => {
     for (let i in files) {
-        console.log(files[i]);
         copy2archive(files[i]);
     }
 });