Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option --config to parse JSON configs #16

Merged
merged 2 commits into from
Oct 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,18 @@ dodo-SSG --input file.txt
dodo-SSG --input file.md
dodo-SSG -i ./folder-name
dodo-SSG -i folder-name -s [CSS Stylesheet URL]
dodo-SSG -c path-to-json
```

### JSON Configuration

You can use a JSON configuration file to pass options to `dodo-SSG`.

The JSON can accept the options `input` and `stylesheet`.
These ones will override the ones passed in the command.

The `input` option in the JSON is required.

## Example Input

###### Dodo-Facts.txt
Expand Down
59 changes: 57 additions & 2 deletions dodo-SSG.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ const argv = require("yargs")
.usage("Usage: $0 [options]")
.alias("i", "input")
.describe("i", "Load input file or directory")
.demandOption(["i"])
.alias("s", "stylesheet")
.describe("s", "Provide stylesheet")
.alias("v", "version")
.version(true, `dodo-SSG version: ${version}`)
.alias("h", "help").argv;
.alias("h", "help")
.alias("c", "config")
.describe("c", "Provide a path to a JSON file").argv;

const writeHTMLFile = (title, body, file, fileType) => {
// if user provided a stylesheet, include stylesheet in the html
Expand Down Expand Up @@ -46,6 +47,60 @@ let mdFiles = [];
let currentDir = process.cwd();
let stylesheet = "";

if (argv.config) {
if (!(typeof argv.config === 'string' || argv.config instanceof String)) {
console.error("Please enter a path to a JSON file.");
process.exit(1);
}

if (!argv.config.endsWith(".json")) {
console.error("Please provide a file that ends with \"json\" extension.");
process.exit(1);
}

if (!fs.existsSync(argv.config)) {
console.error("The file does not exist.");
process.exit(1);
}

if (!fs.statSync(argv.config).isFile()) {
console.error("This is not a regular input file. Please enter a text file. ");
process.exit(1);
}

let fileContent = '';
try {
fileContent = fs.readFileSync(argv.config);
} catch (err) {
console.error("There has been an error while reading the file.");
console.error(err);
process.exit(1);
}

let configFile = {};
try {
configFile = JSON.parse(fileContent);
} catch (err) {
console.error("This file cannot be parsed as a JSON.");
process.exit(1);
}

if (configFile["input"]) {
argv.input = configFile["input"];
}
else {
console.error("Please provide a path to a text file.");
process.exit(1);
}

argv.stylesheet = configFile["stylesheet"];
}

if (!argv.input) {
console.error("Please, enter a path to a file or folder.");
process.exit(1);
}

if (fs.existsSync(argv.input)) {
// if input name is a '.txt' file, save it to files[0]
if (fs.statSync(argv.input).isFile()) {
Expand Down