From 3f33041d830d391f7601b653b8572c4c6ed13529 Mon Sep 17 00:00:00 2001 From: acegoal07 Date: Wed, 25 Jan 2023 14:52:13 +0000 Subject: [PATCH] 1.0.4 --- src/lib/UniversalFileTools.js | 16 ++++++++++++++-- test.js | 33 ++++++++++++++++++++++++++++++++- 2 files changed, 46 insertions(+), 3 deletions(-) diff --git a/src/lib/UniversalFileTools.js b/src/lib/UniversalFileTools.js index 0046db8..c179b80 100644 --- a/src/lib/UniversalFileTools.js +++ b/src/lib/UniversalFileTools.js @@ -70,7 +70,19 @@ exports.UniversalFileTools = class { } if (!copyPath) { const fileType = path.split(".").pop(); - return fs.copyFileSync(path, path.replace(`.${fileType}`, `-copy.${fileType}`)); + if (this.fileExists(path.replace(`.${fileType}`, `-copy.${fileType}`))) { + let count = 1; + while (true) { + if (!this.fileExists(path.replace(`.${fileType}`, `-copy${count}.${fileType}`))) { + fs.copyFileSync(path, path.replace(`.${fileType}`, `-copy${count}.${fileType}`)); + break; + } else { + count += 1; + } + } + } else { + return fs.copyFileSync(path, path.replace(`.${fileType}`, `-copy.${fileType}`)); + } } else { return fs.copyFileSync(path, copyPath); } @@ -215,7 +227,7 @@ exports.UniversalFileTools = class { if (!copyPath) { throw new Error("ERROR with writeFile: copyPath is null"); } - return fs.writeFileSync(copyPath, fs.readFileSync(path, 'utf8')); + return this.writeFile(copyPath, fs.readFileSync(path, 'utf8')) } /** * Returns an array of file names with the specified file type diff --git a/test.js b/test.js index 3ea227e..6e1cc2c 100644 --- a/test.js +++ b/test.js @@ -1,6 +1,6 @@ /////////////////////////////////////////////////////////////////////////// // References //////////////////////////////////////////////////////////// -const fileTools = require("./src"); +const fileTools = require("./dist"); const top_bottom_size = 28; const top_bottom = `|${"-".repeat(top_bottom_size + 2)}|--------|`; /////////////////////////////////////////////////////////////////////////// @@ -179,4 +179,35 @@ try { logFormatter('Read file'); throw new Error(error); } +// Read all files +try { + fileTools.UniversalFileTools().copyFile("testFiles/test.json"); + fileTools.UniversalFileTools().copyFile("testFiles/test.json"); + let count = 0; + for (const data of fileTools.JsonFileTools().readAllFiles("testFiles", "Array")) { + if (!data.Hello == "World!") { + logFormatter('Read all files'); + } else { + count += 1; + } + if (count == fileTools.JsonFileTools().readAllFiles("testFiles", "Array").length) { + logFormatter('Read all files', true); + } + } +} catch (error) { + logFormatter('Read all files'); + throw new Error(error); +} +// Get files +try { + if (fileTools.JsonFileTools().getFiles("testFiles").length == 3) { + logFormatter('Get files', true); + } else { + logFormatter('Get files'); + } +} catch (error) { + logFormatter('Get files'); + throw new Error(error); +} +fileTools.UniversalFileTools().deleteDir("testFiles", true); console.log(top_bottom); \ No newline at end of file