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 cd1e730 commit d1e8181
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 25 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
dist
types
node_modules
testFiles
testFiles
package-lock.json
26 changes: 17 additions & 9 deletions src/lib/UniversalFileTools.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,16 @@ exports.UniversalFileTools = class {
* Creates a file at the location you provided
*
* @param {String} path The path to the file you want to create
* @param {Anything} data The data you want to put in the file
* @param {String} data The data you want to put in the file
*/
createFile(path, content = "") {
createFile(path, data = null) {
if (!path) {
throw new Error("ERROR with createFile: Path is null");
}
return fs.writeFileSync(path, content);
fs.writeFileSync(path, "")
if (data) {
this.writeFile(path, data);
}
}
/**
* Deletes the file you specify
Expand Down Expand Up @@ -141,26 +144,26 @@ exports.UniversalFileTools = class {
}
/**
* Deletes specified directory
*
*
* @param {String} dir The path to the folder you want deleted
* @param {Boolean} force Whether or not it should force delete the folder
* @returns
* @returns
*/
deleteDir(dir, force) {
if (!dir) {
throw new Error("ERROR with deleteDir: dir is null");
}
if (!this.dirExists(dir)) {
throw new Error("ERROR with deleteDir: The directory your trying to delete does not exits");
throw new Error("ERROR with deleteDir: The directory your trying to delete does not exits");
}
if (!this.isDirEmpty(dir) && !force) {
throw new Error("ERROR with deleteDir: The directory your trying to delete is not empty to delete this folder you need force enabled");
throw new Error("ERROR with deleteDir: The directory your trying to delete is not empty to delete this folder you need force enabled");
}
return fs.rmSync(dir, { recursive: true, force: force });
}
/**
* Checks whether or not the folder specified contains any files
*
*
* @param {String} dir The path to the folder you want to check
* @returns {Boolean}
*/
Expand Down Expand Up @@ -192,7 +195,12 @@ exports.UniversalFileTools = class {
if (!data) {
throw new Error("ERROR with writeFile: data is null");
}
return fs.writeFileSync(path, data);
if (typeof data == "object") {
return fs.writeFileSync(path, JSON.stringify(data, null, 2));
}
else {
return fs.writeFileSync(path, data);
}
}
/**
* Writes the data from one file to another
Expand Down
51 changes: 36 additions & 15 deletions test.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
const fileTools = require("./dist");
///////////////////////////////////////////////////////////////////////////
// References ////////////////////////////////////////////////////////////
const fileTools = require("./src");
const top_bottom_size = 28;
const top_bottom = `|${"-".repeat(top_bottom_size + 2)}|--------|`;

///////////////////////////////////////////////////////////////////////////
// Log output ////////////////////////////////////////////////////////////
function logFormatter (testName, value = false) {
console.log(`| ${testName}${" ".repeat((top_bottom_size - testName.length))} | ${value? "\x1b[92mPASSED\x1b[0m" : "\x1b[91mFAILED\x1b[0m"} |`);
}
///////////////////////////////////////////////////////////////////////////
// Universal File Tools tests ////////////////////////////////////////////
console.log(top_bottom);
console.log("| TEST | STATUS |")
console.log("| UniversalFileTools |");
console.log(top_bottom);
console.log("| TEST | STATUS |");
console.log(top_bottom);

// Create directory
try {
fileTools.UniversalFileTools().createDir("testFiles");
Expand All @@ -21,7 +27,6 @@ try {
logFormatter('Directory creation');
throw new Error(error);
}

// Create file
try {
fileTools.UniversalFileTools().createFile("testFiles/test.txt");
Expand All @@ -40,7 +45,6 @@ try {
logFormatter('File creation');
throw new Error(error);
}

// Copy file
try {
fileTools.UniversalFileTools().copyFile("testFiles/test.txt");
Expand All @@ -53,7 +57,6 @@ try {
logFormatter('File copy');
throw new Error(error);
}

// Delete file
try {
fileTools.UniversalFileTools().deleteFile("testFiles/test-copy.txt");
Expand All @@ -66,7 +69,6 @@ try {
logFormatter('File deletion');
throw new Error(error);
}

// Is directory empty
try {
if (!fileTools.UniversalFileTools().isDirEmpty("testFiles")) {
Expand All @@ -78,7 +80,6 @@ try {
logFormatter('Is directory empty');
throw new Error(error);
}

// Rename file
try {
fileTools.UniversalFileTools().renameFile("testFiles/test.txt", "test2.txt");
Expand All @@ -91,7 +92,6 @@ try {
logFormatter('Rename file');
throw new Error(error);
}

// Move file
try {
fileTools.UniversalFileTools().moveFile("testFiles/test2.txt", "test2.txt");
Expand All @@ -105,7 +105,6 @@ try {
logFormatter('Move file');
throw new Error(error);
}

// Write file
try {
fileTools.UniversalFileTools().writeFile("testFiles/testTxt.txt", "passed");
Expand All @@ -118,7 +117,6 @@ try {
logFormatter('Write file');
throw new Error(error);
}

// Write copy file
try {
fileTools.UniversalFileTools().writeCopy("testFiles/testTxt.txt", "testFiles/test.txt");
Expand All @@ -131,7 +129,6 @@ try {
logFormatter('Write copy');
throw new Error(error);
}

// Get file type
try {
if (fileTools.UniversalFileTools().getFileType("testFiles", "txt").length == 2) {
Expand All @@ -144,7 +141,6 @@ try {
logFormatter('Get file type');
throw new Error(error);
}

// Delete directory
try {
fileTools.UniversalFileTools().deleteDir("testFiles", true);
Expand All @@ -157,5 +153,30 @@ try {
logFormatter('Directory deletion');
throw new Error(error);
}

console.log(top_bottom);
///////////////////////////////////////////////////////////////////////////
// Json File Tools tests /////////////////////////////////////////////////
console.log();
console.log(top_bottom);
console.log("| JsonFileTools |");
console.log(top_bottom);
console.log("| TEST | STATUS |");
console.log(top_bottom);
// Read file
try {
fileTools.UniversalFileTools().createDir("testFiles");
fileTools.UniversalFileTools().createFile("testFiles/test.json",
{
"Hello": "World!"
}
)
if (fileTools.JsonFileTools().readFile("testFiles/test.json").Hello == "World!") {
logFormatter('Read file', true);
} else {
logFormatter('Read file');
}
} catch (error) {
logFormatter('Read file');
throw new Error(error);
}
console.log(top_bottom);

0 comments on commit d1e8181

Please sign in to comment.