From 7fd67e09ba060363bc43ea094cf9bea590cecd93 Mon Sep 17 00:00:00 2001 From: Tue Date: Tue, 5 Oct 2021 19:19:08 -0400 Subject: [PATCH 1/3] Adding support for config JSON file to index.js --- bin/index.js | 30 +++++++++++++++++++++++++----- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/bin/index.js b/bin/index.js index b680f22..25e7371 100755 --- a/bin/index.js +++ b/bin/index.js @@ -13,34 +13,54 @@ const dist = path.join(process.cwd(), "dist"); let outputPath = dist; //stylesheet url -let stylesheetUrl = ""; +let stylesheetUrl = undefined; const program = new Command(); program .version(`Name: ${pkjson.name} \nVersion: ${pkjson.version}`, '-v, --version', 'Output the current version') - .requiredOption('-i, --input ', 'Designate an input file or directory') .option('-o, --output ', 'Designate an ouput directory', dist) .option('-s, --stylesheet ', 'Link to a stylesheet url', '') + .option('-c, --config ', 'Link to file that specifies all SSG options') .showHelpAfterError(); program.parse(process.argv); const options = program.opts(); +//Config file option +if(options.config !== undefined) { + try { + let configData = fs.readFileSync(options.config); + let configOptions = JSON.parse(configData); + for(const [key, value] of Object.entries(configOptions)) { + value || value.length > 0 ? options[`${key}`] = `${value}` : options[`${key}`] = undefined; + } + if(!options.input) { + console.log(`error: input is not specified in config file ${options.config}`); + process.exit(-1); + } + } catch(error) { + console.error(`Can't read or parse config file ${options.config}\n ${error}`); + } +} //Output option if(options.output !== dist){ if(fs.existsSync(options.output)){ outputPath = options.output; } else{ - console.log(`Output directory "${options.output}" doesn't exist, outputting all files to ./dist`); + console.log(`Output directory ${options.output} doesn't exist, outputting all files to ./dist`); } } //Stylesheet option -if(options.stylesheet !== '') +if(options.stylesheet !== undefined) stylesheetUrl = options.stylesheet; //Input option if(options.input !== undefined) processInput(options.input); - +else { + console.error("error: required option input is not specified"); + process.exit(-1); +} /* Function processes the input option From 4e36347bf9e35370da30faeca88d47d795f47543 Mon Sep 17 00:00:00 2001 From: Tue Date: Tue, 5 Oct 2021 19:20:36 -0400 Subject: [PATCH 2/3] Updates to README about config file support --- README.md | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/README.md b/README.md index 9d38c5f..364ae30 100644 --- a/README.md +++ b/README.md @@ -12,6 +12,7 @@ Current release 0.1 with the following features. - Allows specification of a stylesheet through linking a stylesheet URL - Automatically generates an index.html file with relative links to each of the generated html files - Markdown support: header(1,2,3), link, bold text, italic text, inline code, horizontal rule + - Config JSON file cotaining SSG options support ## Tool options ``` @@ -19,6 +20,7 @@ Current release 0.1 with the following features. -i, --input Designate an input file or directory -o, --output Designate an output directory, default ./dist -s, --stylesheet Link a stylesheet URL to the html + -c, --config Read and parse json properties from file as options. -h, --help Lists all available options ``` @@ -111,6 +113,29 @@ Three or more "---" |Horizontal Rule | `
` | ---- +## Config file: + +The tool support `.json` config file + +### Usage + +```sh +sitegen -c config.json +``` + +Reading and parsing json properties as options for `sitegen` +**Note**: if `-c` exists, other options are be ignored. + +### Example.json +```json +{ + "input": "./testfiles", + "output": "./output", + "stylesheet": "https://cdn.jsdelivr.net/npm/water.css@2/out/water.css", + "lang": "en" +} +``` + ## Generated Pages live example [Link to the example webpage](https://rclee91.github.io/SiteGen/) From cc6f4545200944e6c0ea89f21132ee52efc6959a Mon Sep 17 00:00:00 2001 From: Tue Date: Wed, 6 Oct 2021 12:53:25 -0400 Subject: [PATCH 3/3] Adding clearer console.error and exit code --- bin/index.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/bin/index.js b/bin/index.js index 25e7371..b64b083 100755 --- a/bin/index.js +++ b/bin/index.js @@ -35,11 +35,12 @@ if(options.config !== undefined) { value || value.length > 0 ? options[`${key}`] = `${value}` : options[`${key}`] = undefined; } if(!options.input) { - console.log(`error: input is not specified in config file ${options.config}`); + console.error(`error: input is not specified in config file ${options.config}`); process.exit(-1); } } catch(error) { console.error(`Can't read or parse config file ${options.config}\n ${error}`); + process.exit(-1); } } //Output option