SCRIPTING REFERENCE!

REVIEW

so far we each have something like this:

#!/usr/bin/env node

const yargs = require('yargs').argv;
const path = require('path');
const myModule = require('./tools/my-module.js');

require('dotenv').config({path: path.resolve(__dirname,'.env')});
global.ROOT_DIR = __dirname;

console.log(`launching the script with these yargs:`);
console.log(JSON.stringify(yargs, null, 4));

myModule(yargs);

and it's requiring in a module that does something with the input (if expected input is there):

const chalk = require('chalk');

const myTool = function(options){
  if (options.text) {
    console.log(chalk.magenta(`making your text magenta: ${options.text}`));
  } else {
    console.log("don't have anything built for the parameters you sent in");
  }
}

module.exports = myTool;

And now when we fire our script with arguments, we get results that do something with our command line input.

ddjkkg --text="my string"

will print that string in magenta.

NEXT

Today the idea was to create a markdown file with some generated content. Here are some steps!

CREATING FILES

Let's start by just learning how to create files with node's fs module (docs here). Let's create a simple markdown string, for now, and then save it as a file.

const fs = require('fs');
const cp = require('child_process');
const myPlaceholderMarkdown = `
# TITLE
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam luctus neque non nisi placerat, sodales ullamcorper nulla sodales.
`

const myTool = function(options){
  fs.writeFileSync(`${ROOT_DIR}/data/test.md`, myPlaceholderMarkdown);
  cp.spawn('open', ['-a', 'Atom', `${ROOT_DIR}/data/test.md`])
}

module.exports = myTool;