4.1.11 • Published 8 months ago

sushi v4.1.11

Weekly downloads
3
License
MIT
Repository
github
Last release
8 months ago

sushi

Build Status Coverage Status

Express-like framework for CLI apps.

Installation

$ npm install sushi --save

Usage

myapp.js:

const sushi = require('sushi');

const app = sushi();

app.command('start', function () {
  console.log('start command');
});

app.command('stop', function () {
  console.log('stop command');
});

app.command('index', function () {
  console.log('index command');
});

app.run();

Output:

$ node myapp.js start
start command

$ node myapp.js stop
stop command

$ node myapp.js
index command

Getting Started

Arguments

Program arguments are parsed using minimist. Command can access arguments via req.args:

app.command('start', function (req) {
  var name = req.args._[0];
  var delay = req.args.delay;

  console.log('start', name, 'with', delay, 'delay');
});
$ node myapp.js start my-process --delay 500ms
start my-process with 500ms delay

You can also customize the way minimist parses arguments by passing args options (see minimist):

const app = sushi({
	args: {
		boolean: ['verbose']
	}
});

Index command

Index command is executed when other commands don't match the arguments:

app.command('index', function () {
  console.log('index command');
});
$ node myapp.js
index command

$ node myapp.js hello

Middleware

Middleware is a function, that modifies the context or arguments before target command is executed.

app.use(function (req, next) {
  req.context.ok = true;

  // call `next()` when done
  next();
});

app.command('start', function (req) {
  req.context.ok === true; // true

  console.log('start command');
});

Middleware can also abort execution:

app.use(function (req, next) {
  var err = new Error('Fatal error');
  next(err);
});

app.command('start', function (req) {
  // won't be executed
});

app.on('error', function (err) {
  // err is the Error instance from middleware
  err.message === 'Fatal error'; // true
});

Error handling

When one of the middleware or command itself throws an error, error event is emitted:

app.on('error', function (err) {
	// err is the Error instance
});

You can use it to display a friendly error message, report it, etc.

List of middleware

Here's the list of middleware you can use with Sushi:

  • help - help messages

Tests

$ npm test

License

MIT © Vadym Demedes

4.1.9

8 months ago

4.1.10

8 months ago

4.1.11

8 months ago

4.1.8

8 months ago

4.1.7

8 months ago

3.2.2

10 months ago

3.2.1

10 months ago

3.1.2

10 months ago

3.2.0

10 months ago

3.2.4

10 months ago

3.2.3

10 months ago

4.1.4

8 months ago

4.0.5

9 months ago

4.1.3

9 months ago

4.1.6

8 months ago

4.1.5

8 months ago

4.0.6

9 months ago

4.1.0

9 months ago

4.0.1

10 months ago

4.0.0

10 months ago

4.1.2

9 months ago

4.0.3

9 months ago

4.1.1

9 months ago

4.0.2

10 months ago

3.1.0

11 months ago

3.0.0

2 years ago

2.1.0

9 years ago

2.0.0

9 years ago

1.2.2

10 years ago

1.2.1

10 years ago

1.2.0

10 years ago

1.1.0

10 years ago

1.0.1

10 years ago

1.0.0

10 years ago

0.0.2

10 years ago

0.0.1

11 years ago

0.0.0

11 years ago