Skip to content

Commit

Permalink
Added appropriate error codes where program exits
Browse files Browse the repository at this point in the history
  • Loading branch information
mismathh committed Sep 27, 2023
1 parent 5a6bcce commit 0a30c9d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
6 changes: 6 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,17 @@ if (process.argv.length === 2) {
console.error(
"Please provide a path to a directory as an argument. Enter TILerator --help for more information.\n"
);
process.exit(-1);
} else {
// Remove first two arguments from process.argv
const input = process.argv.slice(2);

if (input.includes("-v") || input.includes("--version")) {
utils.displayVersion();
process.exit(0);
} else if (input.includes("-h") || input.includes("--help")) {
utils.helpManual();
process.exit(0);
} else if (
(input.includes("-o") || input.includes("--output")) &&
input.length === 3
Expand All @@ -29,11 +32,14 @@ if (process.argv.length === 2) {
filePath = input[0];
}
utils.determinePath([filePath], outputFolder);
process.exit(0);
} else if (input.length === 1 && !input[0].startsWith("-")) {
utils.determinePath([input[0]]);
process.exit(0);
} else {
console.error(
"Invalid arguments provided. Enter TILerator --help for more information.\n"
);
process.exit(-1);
}
}
11 changes: 8 additions & 3 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ const manageOutputFolder = (outputFolder) => {
console.log("Directory successfully created");
} catch {
console.error(err);
process.exit(-1);
}
} else {
console.log("Directory does not exist... Creating output directory");
Expand All @@ -44,6 +45,7 @@ const manageOutputFolder = (outputFolder) => {
console.log("Directory successfully created");
} catch (err) {
console.error(err);
process.exit(-1);
}
}
};
Expand Down Expand Up @@ -78,6 +80,7 @@ const generateHTML = (fileData, filePath, outputFolder) => {
console.log(`File successfully written at: ${outputFolder}/${path.basename(filePath[i], path.extname(filePath[i]))}.html`);
} catch (err) {
console.error(err);
process.exit(-1);
}
}
};
Expand Down Expand Up @@ -145,7 +148,7 @@ const readFileFromPath = (filePath, outputFolder) => {
data = fs.readFileSync(filePath[a], "utf8");
} catch (err) {
console.error(`Error while processing text file\nError: ${err}`);
return;
process.exit(-1);
}
const fileType = filePath[a].split(".").pop();
markupData.push(addHTMLMarkup(data.split("\r\n"), fileType));
Expand All @@ -159,7 +162,7 @@ const readFileFromPath = (filePath, outputFolder) => {

} catch (err) {
console.error(`Error while processing text file\nError: ${err}`);
return;
process.exit(-1);
}
};

Expand All @@ -183,7 +186,7 @@ const determinePath = (inputPath, outputFolder = "./til") => {
fs.readdir(inputPath[0], (err, files) => {
if (err) {
console.error(`Unable to read directory.\nError: ${err}`);
return;
process.exit(-1);
} else {
// Get all file paths from directory that end with .txt or .md
for (let i = 0; i < files.length; i++) {
Expand All @@ -197,6 +200,7 @@ const determinePath = (inputPath, outputFolder = "./til") => {
});
} else {
console.error(`Path does not point to a text file. \n`);
process.exit(-1);
}
} catch (err) {
console.error(`Unable to access path.`);
Expand All @@ -205,6 +209,7 @@ const determinePath = (inputPath, outputFolder = "./til") => {
} else {
console.error(err);
}
process.exit(-1);
}
};

Expand Down

0 comments on commit 0a30c9d

Please sign in to comment.