@bb-cli/base

Common utilities and interface parser for the pluggable CLIs

Downloads in past

Stats

StarsIssuesVersionUpdatedCreatedSize
@bb-cli/base
2.7.04 years ago8 years agoMinified + gzip package size for @bb-cli/base in KB

Readme

Modules

cli : object

CLI parser based on commander with some additions and overrides.

error : object

Create the new instance of the CommandError object, ensuring that it properly extends from the Error class.

log : object

Shared logging system based on the default npmlog system.

sh : object

Portable (Windows/Linux/OS X) implementation of Unix shell commands based on shelljs

ui : object

User Interface for the terminal (Prompting / Styles / Spinners / Progress)

utils : object

Set of utilities.

Functions

fileExists(file)boolean

cli : object

CLI parser based on commander with some additions and overrides.
Link: https://github.com/tj/commander.js/
Example
Create a main command with 3 sub-commands:
import { cli } from '@bb-cli/base';

cli.version('0.0.1')
  .command('install [...packages]', 'install one or more packages')
  .command('search [query]', 'search with optional query')
  .command('list', 'list packages installed', {isDefault: true})
  .parse(process.argv);

Create the install sub-command:
import { cli } from '@bb-cli/base';

cli
  .description('Install package from registry')
  .arguments('[...packages]', 'install one or more packages')
  .option('-f, --force [force]', 'force installation ', false)
  .option('-r, --registry [registry]', 'registry host ')
  .alias('i')
  .man( path.resolve(__dirname, '../../man/install.md'))
  .parse(process.argv);

let packages = cli.args; // get the packages as variadic arguments
let force = cli.force  // get the force option

error : object

Create the new instance of the CommandError object, ensuring that it properly extends from the Error class.
Example
Setting up the log header:
import { error } from '@bb-cli/base';

const out = sh.exec('ls', { silent: true });
if (out.code !== 0) {
  throw error({
    type: 'Shell.Exec',
    code: out.code,
    message: out.stderr.trim(),
  });
}

log : object

Shared logging system based on the default npmlog system.
Link: https://github.com/npm/npmlog
Example
Setting up the log header:
import { log } from '@bb-cli/base';
import { name as packageName } from './package.json';
log.heading = `[${packageName}]`;
Basic commands:
log.silly(prefix, message, ...)
log.verbose(prefix, message, ...)
log.info(prefix, message, ...)
log.http(prefix, message, ...)
log.warn(prefix, message, ...)
log.error(prefix, message, ...)

Setting up the global env LOGLEVEL:
LOG_LEVEL=verbose bb-my-command

sh : object

Portable (Windows/Linux/OS X) implementation of Unix shell commands based on shelljs
Link: https://github.com/shelljs/shelljs
Example
Basic Example
import { sh } from '@bb-cli/base';
sh.ls(`./`)
// or
sh.exec(`ls ./`, function(exitCode, stdOut, stdErr) {
    log.verbose('cmd exitcode:', exitCode);
    log.verbose('cmd output:', stdOut);
    log.verbose('cmd stderr:', stdErr);
});

ui : object

User Interface for the terminal (Prompting / Styles / Spinners / Progress)
  • ui : object
* [.prompt](#module_ui.prompt) ⇒ <code>Promise</code>
* [.colors](#module_ui.colors) : <code>object</code>
    * [.defaultTheme](#module_ui.colors.defaultTheme) : <code>enum</code>

ui.prompt ⇒ Promise

Create a self contained inquirer module via inquirer.createPromptModule method.
Kind: static constant of ui
Access: public
Link: https://github.com/sboudrias/Inquirer.js
| Param | Type | Description | | --- | --- | --- | | questions | array | object | A collection of inquirer questions |
Example
Basic example:
import { ui } from '@bb-cli/base';
const questions = [{
  type: 'input',
  name: 'name',
  message: 'Who are you?',
  default: 'Guest',
  validate: value => true,
  filter: value => value,
}];

ui.prompt(questions).then( answers => {
    // do something with the answers here.
})

ui.colors : object

Colors styles console based on colors

Kind: static constant of ui
Access: public
Link: https://github.com/Marak/colors.js
Example
Basic example:
import { ui } from '@bb-cli/base';
let logInfo = ui.colors.info('Some info style output');
log.verbose(`Show ${logInfo}`);
Custom heading style:
let logHead = ui.colors.heading('Some bold white heading');
log.info(logHead);

colors.defaultTheme : enum

Colors default styles
Kind: static enum of colors
Properties
| Name | Type | Default | | --- | --- | --- | | silly | object | rainbow | | input | object | grey | | verbose | object | cyan | | prompt | object | grey | | info | object | "green","underline" | | data | object | grey | | help | object | cyan | | warn | object | yellow | | debug | object | blue | | error | object | red | | heading | object | "white","bold" |

utils : object

Set of utilities.
* [.md2html(mdStr)](#module_utils.md2html) ⇒ <code>string</code>
* [.md2term(mdStr)](#module_utils.md2term) ⇒ <code>string</code>

utils.md2html(mdStr) ⇒ string

Converts markdown to html output
Kind: static method of utils
Returns: string - html string
| Param | Type | Description | | --- | --- | --- | | mdStr | string | Markdown content string |
Example
Basic Example
const htmlStr = utils.md2html(fs.readFileSync('some-markdown-file.md', 'utf8').toString());

utils.md2term(mdStr) ⇒ string

Kind: static method of utils
Todo
  • Make it work with colors instead of ansi-styles

| Param | Type | Description | | --- | --- | --- | | mdStr | string | markdown content string |
Example
Basic Example
const termMdOut = utils.md2term(fs.readFileSync('some-markdown-file.md', 'utf8').toString());

fileExists(file) ⇒ boolean

Kind: global function
| Param | Type | | --- | --- | | file | string |