Skip to content

Commit

Permalink
Merge branch 'exit-code'
Browse files Browse the repository at this point in the history
  • Loading branch information
Soham Thaker authored and Soham Thaker committed Sep 18, 2023
2 parents 6e30df5 + fe4d09d commit cd87b5c
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
6 changes: 4 additions & 2 deletions src/index.js
Expand Up @@ -7,7 +7,7 @@ if (args.length === 0) {
console.log(
'Please provide a command! Run the program with --help / -h for more information.'
);
process.exit(1);
process.exit(-1);
}

let command = args[0];
Expand All @@ -21,13 +21,14 @@ if (command === '-v' || command === '--version') {
'An error occured while obtaining name and version of the tool: ',
err
);
return;
process.exit(-1);
}

const packageJson = JSON.parse(data);
console.log('Name: ', packageJson.name);
console.log('Version: ', packageJson.version);
});
process.exit(0);
}
//User has provided a valid help command. Output the help menu.
else if (command === '-h' || command === '--help') {
Expand All @@ -46,6 +47,7 @@ else if (command === '-h' || command === '--help') {
node src/index.js ./path/to/directory -o ./path/to/output
`
);
process.exit(0);
}
//User has provided a valid input command. Sanitize the input command.
else {
Expand Down
16 changes: 12 additions & 4 deletions src/utils/helper.js
Expand Up @@ -13,12 +13,13 @@ function sanitizeInputCommand(args) {
//Input is a file
if (fs.lstatSync(pathToInputFileOrDir).isFile()) {
//Input file doesnt end in .txt or .md

if (
!pathToInputFileOrDir.endsWith('.txt') &&
!pathToInputFileOrDir.endsWith('.md')
) {
console.log('Input file must end with .txt or .md extension!');
process.exit(1);
process.exit(-1);
}

//Output dir exists, delete it
Expand All @@ -30,16 +31,19 @@ function sanitizeInputCommand(args) {
console.log(
`HTML file generated successfully stored at ${pathToOutputDir}!`
);
process.exit(0);
}
//Input is a directory
else {
generateHTMLForDir(pathToInputFileOrDir, pathToOutputDir);
console.log(
`HTML files generated successfully stored at ${pathToOutputDir}!`
);
process.exit(0);
}
} else {
console.log('Please provide a valid input file or directory!');
process.exit(-1);
}
}

Expand All @@ -48,16 +52,18 @@ function sanitizeOutputCommand(outputCommand, pathToOutputDir) {
//User has not provided an output directory path
if (!pathToOutputDir) {
console.log('Please provide a path to output directory!');
process.exit(1);
process.exit(-1);
}

//User has provided an output directory path but it is not a directory
else if (
fs.existsSync(pathToOutputDir) &&
!fs.lstatSync(pathToOutputDir).isDirectory()
) {
console.log('Please provide a valid output directory!');
process.exit(1);
process.exit(-1);
}

//User has provided an output directory path but it does not exist
else if (!fs.existsSync(pathToOutputDir)) {
fs.mkdirSync(pathToOutputDir);
Expand Down Expand Up @@ -248,8 +254,9 @@ function generateHTMLForDir(pathToInputDir, pathToOutputDir) {
//No .txt files in the directory, exit execution
if (txtFiles.length === 0) {
console.log('No text files found in directory!');
process.exit(1);
process.exit(-1);
}

//Output dir exists, delete it
if (pathToOutputDir === './til' && fs.existsSync(pathToOutputDir)) {
fs.rmSync(pathToOutputDir, { recursive: true });
Expand All @@ -266,6 +273,7 @@ function outputHTMLToDir(pathToOutputDir, htmlContent, fileName) {
if (!fs.existsSync(pathToOutputDir)) {
fs.mkdirSync(pathToOutputDir);
}

//Write the HTML content to the output dir
fs.writeFileSync(`${pathToOutputDir}/${fileName}`, htmlContent);
}
Expand Down

0 comments on commit cd87b5c

Please sign in to comment.