DEV Community

Cover image for PRs, Merges and Json
Antonio-Bennett
Antonio-Bennett

Posted on • Edited on

PRs, Merges and Json

Hey everyone! So this week in my OSD600 class we had to add a config feature to a fellow student's static site generator.

Feature

The config feature enables a user to pass a -c or --config flag with the path to a json file that contains the equivalent relevant info that could be passed via the command line. E.g

# Option 1: use command line arguments:
ssg --input ./site --output ./build --stylesheet https://cdn.jsdelivr.net/npm/water.css@2/out/water.css --lang fr

# Option 2: use a config file
ssg --config ./ssg-config.json
Enter fullscreen mode Exit fullscreen mode

the ssg-config.json contains

{
  "input": "./site",
  "output": "./build",
  "stylesheet": "https://cdn.jsdelivr.net/npm/water.css@2/out/water.css",
  "lang": "fr"
}
Enter fullscreen mode Exit fullscreen mode

Issue and PR

The first thing I did was to create an issue after forking OSD600-SSG

The matching PR can be found here

Thoughts

This was quite an easy addition. I decoupled the main logic into a proccessInput function then all I had to do was parse the json file if that was a given flag and call proccessInput. All changes can be found in the PR

f (command.c || command.config) {
  if (command.i || command.input)
    return console.log("Cannot provide both a config and input file");

  if (command._.length <= 0) {
    return console.log(
      "Please enter a config file name e.g: --config config.json"
    );
  }

  if (!yargs.argv._[0].endsWith(".json"))
    return console.log("Config file must be a json file");

  if (!fs.existsSync(yargs.argv._[0]))
    return console.log("Config file does not exist");

  const configFile = fs.readFileSync(yargs.argv._[0]);
  const config = JSON.parse(configFile);

  inputFileorDir = config.input;
  command.s = config.stylesheet;
  command.l = config.lang;

  processInput();
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)