From 211964f6352d6f3a9ab7f528310293be78afd477 Mon Sep 17 00:00:00 2001 From: Taimoor Dawami Date: Tue, 11 Oct 2022 12:40:20 -0400 Subject: [PATCH 1/8] Make commander work with -C option --- main.js | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/main.js b/main.js index 99d5b6a..6981aec 100644 --- a/main.js +++ b/main.js @@ -13,10 +13,16 @@ program.version(`${pjson.name}: version-${pjson.version}`); const outputFolder = process.env.OUTPUT_DIRECTORY; +let parsedObj; + program .description("Program Custom Flags") .option("-H, --help") .option(`-I, --input `, "Input to the dist folder") + .option( + `-C, --config `, + "Output to the folder specified in config file" + ) .option(`-O, --output `, "Output to a custom folder") .option( `-L, --lang `, @@ -83,6 +89,11 @@ try { if (options.lang) { sr.changeLanguage(options.lang); } + if (options.config) { + const lines = fs.readFileSync(options.config, "utf8"); + parsedObj = JSON.parse(lines); + console.log(parsedObj); + } } catch (err) { console.error(err.message); } From 6be34b2728540388f76fdec348b91b996af15c09 Mon Sep 17 00:00:00 2001 From: Taimoor Dawami Date: Tue, 11 Oct 2022 12:45:21 -0400 Subject: [PATCH 2/8] Add support for reading directory for --config --- main.js | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/main.js b/main.js index 6981aec..e2052be 100644 --- a/main.js +++ b/main.js @@ -92,7 +92,26 @@ try { if (options.config) { const lines = fs.readFileSync(options.config, "utf8"); parsedObj = JSON.parse(lines); - console.log(parsedObj); + + sr.createFolder(outputFolder); + + const stats = fs.statSync(parsedObj.input); + + // Determines whether the value that was given is a directory or a file + if (stats.isDirectory()) { + const directoryName = parsedObj.input; + + // read the directory, and go through each individual file name using forEach + fs.readdir(directoryName, (err, files) => { + files.forEach((file) => { + const filename = `${directoryName}/${file}`; + textConverter(filename); + }); + }); + } else if (stats.isFile()) { + const filename = parsedObj.input; + textConverter(filename); + } } } catch (err) { console.error(err.message); From 5c49cf579f44a8e968e7cfbed5d7e2fa10a69b4a Mon Sep 17 00:00:00 2001 From: Taimoor Dawami Date: Tue, 11 Oct 2022 12:58:07 -0400 Subject: [PATCH 3/8] Support -C option to process an alternative output destination --- main.js | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/main.js b/main.js index e2052be..396d1fa 100644 --- a/main.js +++ b/main.js @@ -92,8 +92,24 @@ try { if (options.config) { const lines = fs.readFileSync(options.config, "utf8"); parsedObj = JSON.parse(lines); + console.log(parsedObj); - sr.createFolder(outputFolder); + if (parsedObj.output) { + if (fs.existsSync(parsedObj.output)) { + console.log("The name given already exists as a directory or file"); + console.log("Set directory to default: 'dist'"); + sr.replaceDirectory("dist"); + process.exit(); + } + + sr.replaceDirectory(parsedObj.output); + + sr.createFolder(parsedObj.output); + } + + if (!parsedObj.output) { + sr.createFolder(outputFolder); + } const stats = fs.statSync(parsedObj.input); From 1a4f8e30c2d82310a6cb47242ac15f060f48bd04 Mon Sep 17 00:00:00 2001 From: Taimoor Dawami Date: Tue, 11 Oct 2022 13:31:00 -0400 Subject: [PATCH 4/8] Add usage info for Config command --- README.md | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 133ddf1..f6523ab 100644 --- a/README.md +++ b/README.md @@ -29,6 +29,12 @@ Write "my-ssg" before using any commands or it will not work **Command Structure**: "my-ssg --input [fileName/directoryName]"

+**Config** (--config, -C) +
+  This command will allow the user to be able to pass a .json file with supported configuration keys such as input, output, and lang. The command will then be able to convert .txt or .md files specified in the input property to that of an HTML5. The output directory by default is dist, but if output key is defined in config file, the command will replace the default directory with the value of output key. +
+**Command Structure**: "my-ssg --config [fileName]" +
**Output** (--output, -O)
  This command will allow the user to change to create their own default folder for all the HTML5 that they convert. @@ -58,10 +64,10 @@ Write "my-ssg" before using any commands or it will not work - [ ] Allow for multi-level directory conversion - [ ] Generate an index.js which contains all links to the HTML files within a directory - [ ] Stylized each Generated HTML page -<<<<<<< HEAD -======= + <<<<<<< HEAD + ======= - [x] Add Language Command ->>>>>>> 7b62708eb7b2d10f174df447e33d0cf2cd7530c3 + > > > > > > > 7b62708eb7b2d10f174df447e33d0cf2cd7530c3
From b5765f42521bd6e1b10f6bbe9c8aea077f81497a Mon Sep 17 00:00:00 2001 From: Taimoor Dawami Date: Tue, 11 Oct 2022 13:31:38 -0400 Subject: [PATCH 5/8] Add support for lang key --- main.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/main.js b/main.js index 396d1fa..1a83b66 100644 --- a/main.js +++ b/main.js @@ -111,6 +111,10 @@ try { sr.createFolder(outputFolder); } + if (parsedObj.lang) { + sr.changeLanguage(parsedObj.lang); + } + const stats = fs.statSync(parsedObj.input); // Determines whether the value that was given is a directory or a file From 62b09e71d893e5e63b0245fc799f2bf275ecf997 Mon Sep 17 00:00:00 2001 From: Taimoor Dawami Date: Tue, 11 Oct 2022 13:33:40 -0400 Subject: [PATCH 6/8] Fixes #14 by adding support for Config file --- main.js | 1 - 1 file changed, 1 deletion(-) diff --git a/main.js b/main.js index 1a83b66..ea4506b 100644 --- a/main.js +++ b/main.js @@ -92,7 +92,6 @@ try { if (options.config) { const lines = fs.readFileSync(options.config, "utf8"); parsedObj = JSON.parse(lines); - console.log(parsedObj); if (parsedObj.output) { if (fs.existsSync(parsedObj.output)) { From e5d4c95944feb7d5410d738ca2c4c7f14ad8b086 Mon Sep 17 00:00:00 2001 From: Taimoor Dawami Date: Wed, 12 Oct 2022 17:52:47 -0400 Subject: [PATCH 7/8] Restructure readFileEnv func to read/write to file synchronously --- src/text-converter/index.js | 21 ++++++--------------- 1 file changed, 6 insertions(+), 15 deletions(-) diff --git a/src/text-converter/index.js b/src/text-converter/index.js index 12dff44..3216e4e 100644 --- a/src/text-converter/index.js +++ b/src/text-converter/index.js @@ -40,22 +40,13 @@ module.exports = { }, //Lab 3: Created a function to prevent repetition when changing key-pair values readFileEnv: (key, msg, str) => { - let result; + let result = fs.readFileSync("./.env", { encoding: "utf8", flag: "r" }); - fs.readFile("./.env", "utf-8", (err, contents) => { - if (err) { - console.log(err); - return; - } - if (key == "HTML_LANGUAGE") - result = contents.replace(process.env.HTML_LANGUAGE, str); - else if (key == "OUTPUT_DIRECTORY") - result = contents.replace(process.env.OUTPUT_DIRECTORY, str); - - fs.writeFile("./.env", result, "utf-8", () => { - console.log(`${msg} "${str}"`); - }); - }); + if (key == "HTML_LANGUAGE") + result = result.replace(process.env.HTML_LANGUAGE, str); + else if (key == "OUTPUT_DIRECTORY") + result = result.replace(process.env.OUTPUT_DIRECTORY, str); + fs.writeFileSync("./.env", result); }, }; From 63630e4fbdf0cd6d7d1430b90523377066aae98c Mon Sep 17 00:00:00 2001 From: Taimoor Dawami Date: Wed, 12 Oct 2022 17:53:15 -0400 Subject: [PATCH 8/8] Update code to based on feedback --- main.js | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/main.js b/main.js index 9c0ad27..73d58e8 100644 --- a/main.js +++ b/main.js @@ -13,8 +13,6 @@ program.version(`${pjson.name}: version-${pjson.version}`); const outputFolder = process.env.OUTPUT_DIRECTORY; -let parsedObj; - program .description("Program Custom Flags") .option("-H, --help") @@ -46,6 +44,9 @@ if (options.help) { console.log( "Input: By typing 'my-ssg --input(or)-I file.txt' the program will generate a valid HTML5 file\n" ); + console.log( + "Config: By typing 'my-ssg --config(or)-C file.json' the program will generate a valid HTML5 file based on configuration specified\n" + ); console.log( "Lang: By typing 'my-ssg --lang(or)-L the program will add a 'lang' attribute on the root html" ); @@ -89,29 +90,28 @@ try { } if (options.config) { const lines = fs.readFileSync(options.config, "utf8"); - parsedObj = JSON.parse(lines); + const parsedObj = JSON.parse(lines); + + if (parsedObj.lang) { + sr.changeLanguage(parsedObj.lang); + } if (parsedObj.output) { if (fs.existsSync(parsedObj.output)) { console.log("The name given already exists as a directory or file"); console.log("Set directory to default: 'dist'"); sr.replaceDirectory("dist"); - process.exit(); } - sr.replaceDirectory(parsedObj.output); - - sr.createFolder(parsedObj.output); + fs.rmSync(outputFolder, { recursive: true, force: true }); + sr.createFolder(outputFolder); } if (!parsedObj.output) { + fs.rmSync(outputFolder, { recursive: true, force: true }); sr.createFolder(outputFolder); } - if (parsedObj.lang) { - sr.changeLanguage(parsedObj.lang); - } - const stats = fs.statSync(parsedObj.input); // Determines whether the value that was given is a directory or a file