Skip to content

Commit

Permalink
Adding support for config JSON file to index.js
Browse files Browse the repository at this point in the history
  • Loading branch information
TueeNguyen committed Oct 5, 2021
1 parent e552ada commit 7fd67e0
Showing 1 changed file with 25 additions and 5 deletions.
30 changes: 25 additions & 5 deletions bin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 <file or directory', 'Designate an input file or directory')
.option('-i, --input <file or directory>', 'Designate an input file or directory')
.option('-o, --output <directory>', 'Designate an ouput directory', dist)
.option('-s, --stylesheet <stylesheet url>', 'Link to a stylesheet url', '')
.option('-c, --config <config json file>', '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 <file or directory> 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 <file or directory> is not specified");
process.exit(-1);
}
/*
Function processes the input option
Expand Down

0 comments on commit 7fd67e0

Please sign in to comment.