0.1.7 • Published 21 days ago

@fibcli/make-cli v0.1.7

Weekly downloads
-
License
ISC
Repository
github
Last release
21 days ago

@fibcli/make-cli

NPM version Build Status

@fibcli/make-cli provides easy, configurable interface to wrap one subcommand for @fxjs/cli instance.

Getting Started

// command-write13.js
const { wrapSubcommand } = require('@fibcli/make-cli');

module.exports = wrapSubcommand({
    name: 'write13',

    inputs: {
        required: '<targetFile>',
        optional: [
        ],
    },

    examples: [],

    options: {
    },

    description: 'one command write 13 to file',

    onAction: ([targetFile]) => {
        const pwd = process.cwd();

        targetFile = path.resolve(pwd, targetFile);

        try {
            if (!fs.exists(path.dirname(targetFile))) {
                fs.mkdir(path.dirname(targetFile), { recursive: true });
            }
            fs.writeTextFile(targetFile, '13');
            console.log('write success!');
        } catch (error) {
            console.error(error);
        }
    }
})

We got one generator function of @fxjs/cli subcommand, you can mount it to @fxjs/cli instance.

// index.js
const CommandWrite13 = require('./command-write13.js');

const cli = require('@fxjs/cli')('mycli');

CommandWrite13(cli);

cli.help();
cli.parse();

Run it with arguements,

$ fibjs ./index.js ./13target.txt

You will see PWD/13target.txt generated, which contains 13 in it, and see stdout log:

write success!

Fast Test

You can test one or more sub-commands generated by wrapSubcommand with TestUtils,

// test.js
const { TestUtils } = require('@fibcli/make-cli');

TestUtils.runCliWith({
        commands: [
            require('./command-write13.js'),
            require('./command-foo.js')
        ]
    });

Run it to see what would happen :),

$ fibjs ./test.js --help
$ fibjs ./test.js write13 --help