Skip to content

Commit

Permalink
Add support to read a JSON config file
Browse files Browse the repository at this point in the history
  • Loading branch information
JerryHue committed Oct 9, 2021
1 parent 716c465 commit 090f2c8
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 11 deletions.
20 changes: 15 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,27 @@ A CLI tool which creates a static html file from a text file that was provided.
_Once you run the command, you should receive the html files in the dist folder in the corresponding language directory_

## Flags:
`--i` : Flag to indicate the files or directories to create HTML files for.
`--v` : Indicates version of the ssg.
`--h` : Will bring up the help menu.
`--lang`: allows you to include a language to display the html page in. You can input multiple choices followed by spaces. See below:
* `--i` : Flag to indicate the files or directories to create HTML files for.
* `--v` : Indicates version of the ssg.
* `--h` : Will bring up the help menu.
* `--lang`: allows you to include a language to display the html page in.
* `--config`: allows you to pass a configuration file that can pass the same options as the command line. See the section for `Configuration File` for more information.

You can input multiple choices followed by spaces. See below:

![](https://i.imgur.com/RZoad46.png)
![](https://i.imgur.com/AstdjnR.png)

### Configuration File

The configuration file is a JSON formatted file, where each property is can be of the following:

* `input`: a string that works as a path to file that will be used to generate HTML files. It can also be an array of strings.
* `lang`: a string that specifies the language tag that the HTML page will be annotated with. It can also be an array of strings.

## Input File Types:
1. Directories: Recursive strategy will automatically be done.
2. Text Files: Any file that is created with the extension `.txt`
_Supports: creates paragraphs of each line_
3. Markdown: Any file that is created using markdown and ends in the extention `.md`
_Supports: h1, h2, h3, blockquotes, bold, italics, image, links, breaks, in-line code blocks_

20 changes: 17 additions & 3 deletions src/cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import chalk from "chalk";
import { createHtml, readDirectory } from "./main";
import fs, { stat } from "fs";
const p = require("../package");
import { getOptions } from "./config";

async function parseArguments(argsRaw) {
const args = arg({
Expand All @@ -17,7 +18,9 @@ async function parseArguments(argsRaw) {
"--h": "--help",
"--version": Boolean,
"--v": "--version",
"--lang": Boolean
"--lang": Boolean,
"--config": String,
"--c": "--config"
});

const values = {
Expand All @@ -37,6 +40,17 @@ async function parseArguments(argsRaw) {
return;
}

if (args["--config"]) {
const jsonValues = await getOptions(args["--config"]);
if (jsonValues && jsonValues.input) {
getFiles(jsonValues.input, jsonValues);
}

// exit early, since the rest is
// dealing with the CLI args
return jsonValues;
}

// Prints out the dialogue for the version of the package, alongside the name of the package.
if (values.version) {
console.error(
Expand Down Expand Up @@ -70,7 +84,7 @@ async function parseArguments(argsRaw) {
values.lang = ['en']
getFiles(files, values)
}

return values;
}

Expand Down Expand Up @@ -132,7 +146,7 @@ function printHelp() {
);

console.log(
"%s \n--version: provides version of package\n--input: flag used to indicate files to convert",
"%s \n--version: provides version of package\n--input: flag used to indicate files to convert\n--config: option to indicate config file to read from",
chalk.inverse.bold("Flag Directory")
);
}
Expand Down
52 changes: 52 additions & 0 deletions src/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import fs from "fs";

export async function getOptions(pathToJsonFile) {
if (!pathToJsonFile.endsWith(".json")) {
console.error("This is not a JSON file.\n" +
"Please, provide a proper file with '.json' extension.");
return null;
}

let fileContents = null;
try {
fileContents = fs.readFileSync(pathToJsonFile, 'utf8');
} catch (err) {
console.error("An error occurred while trying to access the file.\n" +
"The file might not exist or you may not have permission to read it.\n" +
"Please, provide a file that can be read.");
return null;
}

let config = null;

try {
config = JSON.parse(fileContents);
} catch (err) {
console.error("An error occurred while reading the JSON file.\n" +
"This might indicate that the JSON file is not valid.\n" +
"Please, ensure that it is a valid JSON file.");
return null;
}

// if the input is not an array of elements, then
// we create an array with a single element
let input_arr = !config["input"] ? false :
(Array.isArray(config["input"]) ? config["input"] :
Array(config["input"]));

// if the language is not an array of elements, then
// we create an array with a single element
let lang_arr = !config["lang"] ? ["en"] :
(Array.isArray(config["lang"]) ? config["lang"] :
Array(config["lang"]));

return {
input: input_arr,
lang: lang_arr,
empty: false,
version: false,
help: false,
files: [],
directories: []
};
}
6 changes: 3 additions & 3 deletions src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,9 @@ async function writeHTML(data, filename, filetype) {
`;
const newname = filename.replace(/\.[^/.]+$/, ".html");

if (recursiveSearch > 0)
!fs.existsSync(`./dist/${language}`) &&
fs.mkdirSync(`./dist/${language}`, { recursive: true });
if (!fs.existsSync(`./dist/${language}`)) {
fs.mkdirSync(`./dist/${language}`, { recursive: true });
}

fs.writeFile(`dist/${language}/${newname}`, datatoHTML, (err, data) => {
if (err) {
Expand Down

0 comments on commit 090f2c8

Please sign in to comment.