-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactoring to improve code maintainability:
* Fixing the error handling bug introduced by the config feature * Refactor options validation and parsing into a new Options class * Move the HTML generation and tool options module to bin * Refactor paths for options and generate html modules * Improve linting and indentation
- Loading branch information
Showing
3 changed files
with
97 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
const fs = require("fs"); | ||
const path = require("path"); | ||
const chalk = require("chalk"); | ||
|
||
class Options { | ||
constructor(input, output, stylesheet, lang){ | ||
this.input = input; | ||
this.output = output; | ||
this.stylesheet = stylesheet; | ||
this.lang = lang; | ||
} | ||
static get DEFAULT_OUTPUT(){ return './dist'; } | ||
static get DEFAULT_LANG(){ return 'en-CA'; } | ||
|
||
//Returns true if the input path is validated, otherwise throws an error | ||
validateInput(){ | ||
if(!fs.existsSync(this.input)){ | ||
throw new Error(`${this.input} does not exist. Input path must be a file or directory`); | ||
} | ||
return true; | ||
} | ||
|
||
//Returns true if the output path is validated, otherwise throws an error | ||
validateOutput(){ | ||
if(this.output != Options.DEFAULT_OUTPUT){ | ||
if(fs.existsSync((this.output))){ | ||
if(!fs.lstatSync((this.output)).isDirectory()){ | ||
throw new Error("Output path points to a file. Output directory must be valid") | ||
} | ||
} | ||
else throw new Error(`${this.output} does not exist. Output directory must be valid`); | ||
} | ||
return true; | ||
} | ||
|
||
/** | ||
* Parses options using a file path | ||
* @param {string} filePath | ||
* @return {Boolean} true if the options were parsed | ||
*/ | ||
parseConfig(filePath){ | ||
if(fs.existsSync(filePath)){ | ||
if(path.extname(filePath).toLocaleLowerCase() == '.json'){ | ||
let fileContents = fs.readFileSync(filePath); | ||
let configData; | ||
|
||
try{ | ||
configData = JSON.parse(fileContents); | ||
} | ||
catch(err){ | ||
console.log('Error while parsing JSON: ', err); | ||
process.exit(-1); | ||
} | ||
|
||
this.input = configData.input; | ||
this.output = configData.output ? configData.output : Options.DEFAULT_OUTPUT; | ||
this.lang = configData.lang ? configData.lang : Options.DEFAULT_LANG; | ||
this.stylesheet = configData.s; | ||
|
||
} | ||
else { | ||
console.log("Config file must be JSON!", path.extname(filePath)); | ||
process.exit(-1); | ||
} | ||
} | ||
else { | ||
console.log(`${filePath} not found! A JSON config file must be supplied`); | ||
process.exit(-1); | ||
} | ||
|
||
console.log(chalk.green(`Options successfully retrieved from ${filePath}`)); | ||
return true; | ||
} | ||
} | ||
|
||
module.exports.Options = Options; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
b406d2f
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs: