瀏覽代碼

Ajout fonctions utilitaires

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

+ 29 - 0
src/file.js

@@ -83,4 +83,33 @@ module.exports.isDir = function (path) {
     return fs.statSync(path).isDirectory();
 };
 
+module.exports.isFile = function (path) {
+    if (!module.exports.exist(path)) {
+        return false;
+    }
+    return fs.statSync(path).isFile();
+};
+
+module.exports.list = function (path) {
+    if (!module.exports.isDir(path)) {
+        return [];
+    }
+    return fs.readdirSync(path);
+};
+
+module.exports.copy = function (source, dest, verbose = false) {
+    if (!module.exports.isFile(source)) {
+        return false;
+    }
+    try {
+        fs.copyFileSync(source, dest);
+        return true;
+    } catch (err) {
+        if (verbose) {
+            console.error(err);
+        }
+        return false;
+    }
+};
+
 module.exports.fs = fs;