lub-command
A base command class to help develop your lub-plugin based on yargs.
It's quit convenient to define your bin's version, help info, description and option description by extending this command class.
Install
npm install lub-command --save
Usage
Usage for developing lub-plugin
Make your subcommand's class extend lub-command
, and export it in your plugin npm package's entry file. Do your biz logic in run method, support async
and generator * run
.
// lib/clone.js "use strict"; const Command = ; // here lub-core will pass raw arguments in the running cli and config from .lubrc { // don't forget this line superrawArgv config; // define your command's usage and description info thisusage = "lub clone <repository> [directory]"; // pass your options to yargs thisoptions = depth: type: "number" description: "Create a shallow clone with a history truncated to the specified number of commits" ; } { return "Clone a repository into a new directory"; } // run method has to be defined to do your biz logic here // supports generator `* run()` and promise `async run()` // arguments context and config will be passed async { if configquiet console; const repository directory = contextargv_; console; } moduleexports = GitClone;
// index.js => equals to package.main'use strict'; const clone = moduleexports = clone
Usage for independent module
The implementation of subcommand class is the of For lub plugin developer.
But you need to run this subcommand by your self.
// bin/git-clone.js#!/usr/bin/env node "use strict"; const Clone = ; const gitClone = processargv quiet: true ;gitClonestart;
API
Command
Method:
start()
- start your program, only use once in your bin file.run(context,config)
- should implement this to provide command handler
- Support generator / async function / normal function which return promise.
context
is{ cwd, env, argv, rawArgv }
cwd
-process.cwd()
env
- clone env object fromprocess.env
argv
- argv parse result by yargs,{ _: [ 'start' ], '$0': '/usr/local/bin/lub', foo: 'bar'}
rawArgv
- the raw argv,[ "--foo=bar" ]
config
is passed from.lubrc.js
if used in lub-plugin
showHelp()
- print usage message to console.options=
- a setter, shortcut foryargs.options
usage=
- a setter, shortcut foryargs.usage
version=
- a setter, set the version of you command
Properties:
description
- {String} a getter, shortcut for yargs.command(cmd, desc, [module]) only show this description when it's a sub command in help consoleyargs
- {Object} yargs instance for advanced custom usagehelper
- {Object} helper instance exported fromconst { helper } = require('lub-command');
tips: lub-command
will read version from your npm package's package.json
You can define options by set this.options
thisoptions = baseDir: alias: 'b' demandOption: true description: 'the target directory' path depth: description: 'level to clone' type: 'number' default: 1 size: description: 'choose a size' choices: 'xs' 's' 'm' 'l' 'xl' ;
You can define version by set this.version
thisversion = 'v1.0.0';
You can define description by define description
getter:
{ return 'this is description';}
Helper
lub-command
also provides some useful utils on helper
when you develop your bin tool:
forkNode(modulePath, args, opt)
- fork child process, wrap with promise and gracefull exitspawn(cmd, args, opt)
- spawn a new process, wrap with promise and gracefull exit* callFn(fn, args, thisArg)
- call fn, support gernerator / async / normal function return promise
how to require:
const helper = ;