瀏覽代碼

Ajout fonction put dans file

Loquicom 5 年之前
父節點
當前提交
8645d382f1
共有 1 個文件被更改,包括 26 次插入4 次删除
  1. 26 4
      src/file.js

+ 26 - 4
src/file.js

@@ -3,24 +3,46 @@
  */
 const fs = require('fs');
 
-module.exports.exist = function (path, show = false) {
+module.exports.exist = function (path, verbose = false) {
     try {
         return fs.existsSync(path);
     } catch (err) {
-        if (show) {
+        if (verbose) {
             console.log(err);
         }
         return false;
     }
 };
 
-module.exports.makedir = function (path, isFilePath = false) {
+module.exports.makedir = function (path, isFilePath = false, verbose = false) {
     if (isFilePath) {
         let split = path.split('/');
         delete split[split.length - 1];
         path = split.join('/');
     }
     if (!module.exports.exist(path)) {
-        fs.mkdirSync(path, {recursive: true});
+        try {
+            fs.mkdirSync(path, {recursive: true});
+            return true;
+        } catch (err) {
+            if (verbose) {
+                console.error(err);
+                return false;
+            }
+        }
     }
 };
+
+module.exports.put = function (path, content, verbose = false) {
+    try {
+        fs.writeFileSync(path, content);
+        return true;
+    } catch (err) {
+        if (verbose) {
+            console.error(err);
+        }
+        return false;
+    }
+};
+
+module.exports.fs = fs;