figlet demo - auttam/easycli GitHub Wiki

In this example-

  1. Configuration is in a separate file
  2. Main method is executed asynchronously
  3. async/await is used
  4. acceptOnly field of options configuration is set dynamically

Program configuration file: bin/config.json

{
    "help": "Figlet demo created with EasyCli",
    "params": [
        {
            "name": "text",
            "help": "text to generate ascii art",
            "required": true
        }
    ],
    "options": [
        {
            "name": "font",
            "aliases": [
                "f"
            ],
            "help": "font to use for drawing ascii art"
        },
        {
            "name": "horizontal-layout",
            "aliases": [
                "hl"
            ],
            "value": "default",
            "help": "value that indicates the horizontal layout to use",
            "acceptOnly": [
                "default",
                "full",
                "fitted",
                "controlled smushing",
                "universal smushing"
            ]
        },
        {
            "name": "vertical-layout",
            "aliases": [
                "vl"
            ],
            "value": "default",
            "help": "value that indicates the vertical layout to use",
            "acceptOnly": [
                "default",
                "full",
                "fitted",
                "controlled smushing",
                "universal smushing"
            ]
        }
    ]
}

Program script file: bin/figlet-demo.js

#!/usr/bin/env node
const Program = require('@auttam/easycli').Program
const figlet = require('figlet')
const config = require('./config')

class FigletDemo extends Program {

    async main(text, $options) {
        var data = await this.generate(text, {
            font: $options.font,
            horizontalLayout: $options.horizontalLayout,
            verticalLayout: $options.verticalLayout,
        })
        console.log(data)
    }

    generate(msg, options) {
        return new Promise((resolve, reject) => {
            figlet(msg, options, (err, data) => {
                if (err) {
                    reject(err)
                    return
                }
                resolve(data)
            })
        })
    }
}

// loading available font names
figlet.fonts(function (err, fonts) {
    if (err) {
        console.log('There was some error running figlet demo');
        console.dir(err);
        return;
    }

    // setting font names as accepted values for --font option
    config.options[0].acceptOnly = fonts

    // running program
    Program.run(new FigletDemo(config))
});

package.json file:

{
    "bin":{
        "figlet-demo" : "bin/figlet-demo"
    }
}