From d900b307ff198275be7934b45f7b54bf1c20cbcc Mon Sep 17 00:00:00 2001 From: Jia Hua Zou Date: Wed, 6 Oct 2021 21:04:40 -0400 Subject: [PATCH 1/3] add the option to read the json file --- index.js | 30 ++++++++++++++++++++++++++++++ ssg-config.json | 4 ++++ 2 files changed, 34 insertions(+) create mode 100644 ssg-config.json diff --git a/index.js b/index.js index 4ac39e9..219f86c 100644 --- a/index.js +++ b/index.js @@ -6,6 +6,7 @@ const { name, version } = require('../SSGNode/package.json') let sourcePath = "./Sherlock Holmes Selected Stories/The Adventure of the Six Napoleans.txt" let endPath = "./dist" let lang = "en" +let config = "" function run() { let texts = ""; @@ -176,6 +177,21 @@ function generateFile(html) { } } +function configReader(){ + try { + //parse the file and check for the arguments. + const configJSON = JSON.parse(fs.readFileSync(config, 'utf8')); + if(configJSON.lang !== "" && configJSON.lang !== undefined){ + lang = configJSON.lang; + } + if(configJSON.input !== "" && configJSON.input !== undefined){ + sourcePath = configJSON.input; + } + } catch (error) { + console.log(`Fail reading file: ${error}`); + } +} + function genCss(dir) { mkExist(dir); mkExist(`${dir}/styles.css`, false); @@ -195,6 +211,7 @@ const defaultFolder = "Sherlock Holmes Selected Stories"; //check if the argument corresponds with anything we can use let pathchanged = false; let langchanged = false; +let configchanged = false; process.argv.forEach(function (val, index, array) { //if path isn't the default one, change it for this next value @@ -214,6 +231,15 @@ process.argv.forEach(function (val, index, array) { } } + if (configchanged) { + if(!val.match("^-")) { + //means this might be the config + config = val; + console.log(`Config file is now ${config}`) + configReader(); + } + } + switch(val) { case "--version": case "-v": @@ -244,6 +270,10 @@ process.argv.forEach(function (val, index, array) { case "-l": langchanged = true; break; + case "--config": + case "-c": + configchanged = true; + break; } }); diff --git a/ssg-config.json b/ssg-config.json new file mode 100644 index 0000000..2c68bd4 --- /dev/null +++ b/ssg-config.json @@ -0,0 +1,4 @@ +{ + "input": "./cats", + "lang": "fr" +} From a25b22286fd6147c30068b5a4a33cff15fcfb162 Mon Sep 17 00:00:00 2001 From: Jia Hua Zou Date: Wed, 6 Oct 2021 21:12:42 -0400 Subject: [PATCH 2/3] added the output option for the config feature --- index.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 219f86c..f9dadb3 100644 --- a/index.js +++ b/index.js @@ -187,6 +187,9 @@ function configReader(){ if(configJSON.input !== "" && configJSON.input !== undefined){ sourcePath = configJSON.input; } + if(configJSON.output !== "" && configJSON.output !== undefined){ + endPath = configJSON.output; + } } catch (error) { console.log(`Fail reading file: ${error}`); } @@ -235,7 +238,7 @@ process.argv.forEach(function (val, index, array) { if(!val.match("^-")) { //means this might be the config config = val; - console.log(`Config file is now ${config}`) + console.log(`Config file is now ${config}`); configReader(); } } From 7e3cab8a537ba6e117706514db2f4066e19171aa Mon Sep 17 00:00:00 2001 From: Jia Hua Zou Date: Thu, 7 Oct 2021 20:57:51 -0400 Subject: [PATCH 3/3] will print an error if the input file is non-existing when given from the json config file --- index.js | 9 +++++++-- ssg-config.json | 3 ++- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/index.js b/index.js index f9dadb3..38c13a9 100644 --- a/index.js +++ b/index.js @@ -184,14 +184,19 @@ function configReader(){ if(configJSON.lang !== "" && configJSON.lang !== undefined){ lang = configJSON.lang; } - if(configJSON.input !== "" && configJSON.input !== undefined){ + + //validate the input for a file/folder else throws an error and close to program. + if(fs.lstatSync(configJSON.input).isDirectory() || fs.lstatSync(configJSON.input).isFile()){ sourcePath = configJSON.input; } + + //Output folder else default to ./dist if(configJSON.output !== "" && configJSON.output !== undefined){ endPath = configJSON.output; } } catch (error) { - console.log(`Fail reading file: ${error}`); + console.log(`ERROR: ${error}`); + exit(-1); } } diff --git a/ssg-config.json b/ssg-config.json index 2c68bd4..e952219 100644 --- a/ssg-config.json +++ b/ssg-config.json @@ -1,4 +1,5 @@ { - "input": "./cats", + "input": "./text.txt", + "output": "folder", "lang": "fr" }