DEV Community

Cover image for Adding support for the `--config` option in an SSG
TD
TD

Posted on • Edited on

Adding support for the `--config` option in an SSG

This week I got the opportunity to contribute to Ririio's SSG named ririio-ssg.

Overview

ririo-ssg is similar to other Static Site Generators I have worked with in the past few weeks, given that it is programmed in Javascript with similar requirements. ririo-ssg is a great CLI tool that can convert .txt and .md files into a static site. However, the tool could not work with config files in .json format to override the tool's default behaviour.

Filing an Issue

As usual, I started by filing an issue ririo-ssg Github repo. The owner was quick to respond by assigning the issue to me. After the assignment, I started working on the implementation I had in mind that would enable the SSG to process JSON config file.

Writing the Code

Writing the code was not as difficult as I initially thought. I started by creating a new git branch to write my code. Also, I set up a git remote named upstream to stay in sync with the original repository.
ririo-ssg utilizes the commander module to process commands, which is simple. I just had to update the options so the CLI can work with the --config and -C options with argument. Parsing the JSON object in the config file was even easier, given that Javascript has a built-in JSON.parse function to do the job.

Creating the PR

I created the PR with the initial implementation for the -C option. I received feedback from the owner and reflected the changes in subsequent commits.

Challenges Encountered

  • I have a habit of writing modular code spanning into multiple files. Initially, I created a new file that would be read as the JSON config file. However, I ran into some issues with some of the functions by doing that. I consulted with the code owner and worked with his recommended approach instead.
  • The central issue was reading and writing to the .env file. This was a problem because the SSG utilized functions such as readFile and writeFile from the fs module. Consequently, some values in the .env file are not overwritten unless the tool is run with the -C or -config option multiple times. I was able to fix this problem by replacing readFile function with readFileSync and writeFile with writeFileSync. Replacing the code with synchronous functions ensured the program execution was halted until all the changes were properly reflected in the .env file.

Outcome

Users can now pass a config file containing a valid JSON object when using either the -C or --config options.
An example of a valid JSON object accepted by this tool is:

{
  "input" : "./text-files",
  "output" : "output-folder",
  "lang" : "uk",
}
Enter fullscreen mode Exit fullscreen mode

Invalid and unsupported key-value pairs will be ignored.

Top comments (0)