Skip to content

Commit

Permalink
1.0.4
Browse files Browse the repository at this point in the history
  • Loading branch information
acegoal07 committed Jan 25, 2023
1 parent d1e8181 commit 3f33041
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 3 deletions.
16 changes: 14 additions & 2 deletions src/lib/UniversalFileTools.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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
Expand Down
33 changes: 32 additions & 1 deletion test.js
Original file line number Diff line number Diff line change
@@ -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)}|--------|`;
///////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -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);

0 comments on commit 3f33041

Please sign in to comment.