• Stars
    star
    173
  • Rank 220,124 (Top 5 %)
  • Language
    CoffeeScript
  • License
    BSD 3-Clause "New...
  • Created over 13 years ago
  • Updated almost 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Nice looking shell applications with pluggable middlewares for Node.js

Shell: applications with pluggable middleware

Shell brings a Connect inspired API, Express inspired routing, and other similar functionality to console based applications.

  • Run both in shell mode and command mode
  • First class citizen for console application (arrows, ctrl-a, ctrl-u,...)
  • User friendly with history, help messages and many other plugins
  • Foundation to structure and build complex based applications
  • Command matching, parameters and advanced functionnalities found in Express routing
  • Flexible architecture based on middlewares for plugin creation and routing enhancement
  • Familiar API for those of us using Connect or Express
  • Predifined commands through plugins for Redis, HTTP servers, Cloud9, CoffeeScript, ...

Installation

Shell is open source and licensed under the new BSD license.

npm install shell

Quick start

The example below illustrate how to code a simple Redis client.

var shell = require('shell');
// Initialization
var app = new shell( { chdir: __dirname } )
// Middleware registration
app.configure(function() {
  app.use(function(req, res, next){
    app.client = require('redis').createClient()
    next()
  });
  app.use(shell.history({
    shell: app
  }));
  app.use(shell.completer({
    shell: app
  }));
  app.use(shell.redis({
    config: 'redis.conf',
    pidfile: 'redis.pid'
  }));
  app.use(shell.router({
    shell: app
  }));
  app.use(shell.help({
    shell: app,
    introduction: true
  }));
});
// Command registration
app.cmd('redis keys :pattern', 'Find keys', function(req, res, next){
  app.client.keys(req.params.pattern, function(err, keys){
    if(err){ return res.styles.red(err.message), next(); }
    res.cyan(keys.join('\n')||'no keys');
    res.prompt();
  });
});
// Event notification
app.on('quit', function(){
  app.client.quit();
});

Creating and Configuring a Shell

var app = shell();
app.configure(function() {
  app.use(shell.history({shell: app}));
  app.use(shell.completer({shell: app}));
  app.use(shell.help({shell: app, introduction: true}));
});
app.configure('prod', function() {
  app.set('title', 'Production Mode');
});

Shell settings

The constructor shell takes an optional object. Options are:

  • chdir , Changes the current working directory of the process, a string of the directory, boolean true will default to the workspace (in which case workspace must be provided or discoverable)
  • prompt , Character for command prompt, Defaults to ">>"
  • stdin , Source to read from
  • stdout , Destination to write to
  • env , Running environment, Defaults to the env setting (or NODE_ENV if defined, eg: production, development).
  • isShell , Detect whether the command is run inside a shell or as a single command.
  • noPrompt , Do not prompt the user for a command, useful to plug your own starting mechanism (eg: starting with a question).
  • workspace , Project root directory or null if none was found. The discovery strategy starts from the current working directory and traverses each parent dir looking for a node_module directory or a package.json file.

Shell settings may be set by calling app.set('key', value). They can be retrieved by calling the same function without a second argument.

var app = new shell({
  chdir: true
});
app.set('env', 'prod');
app.configure('prod', function() {
  console.log(app.set('env'));
});

As with Express, app.configure allows the customization of plugins for all or specific environments, while app.use registers plugins.

If app.configure is called without specifying the environment as the first argument, the provided callback is always called. Otherwise, the environment must match the env setting or the global variable NODE_ENV.

Middlewares and plugins

Shell is build on a middleware architecture. When a command is issued, multiple callbacks are executed sequentially until one decide to stop the process (calling res.prompt() or shell.quit. Those callbacks are called middlewares. A callback recieves 3 arguments: a request object, a response object and the next callback. Traditionnaly, request deals with stdin while response deals with stdout.

A plugin is simply a function which configure and return a middleware. Same plugin also enrich the Shell application with new routes and functions.

Shell events

The following events may be emitted:

  • "command" , listen to all executed commands, provide the command name as first argument.
  • #{command} , listen to a particular event.
  • "quit" , called when the application is about to quit.
  • "error" , called on error providing the error object as the first callback argument.
  • "exit" , called when the process exit.

Request parameter

The request object contains the following properties:

  • shell , (required) A reference to your shell application.
  • command , Command entered by the user
  • params , Parameters object extracted from the command, defined by the shell.router middleware
  • question , Ask questions with optionally suggested and default answers
  • confirm , Ask a question expecting a boolean answer

Response parameter

The response object inherits from styles containing methods for printing, coloring and bolding:

Colors:

  • black
  • white
  • yellow
  • blue
  • cyan
  • green
  • magenta
  • red
  • bgcolor
  • color
  • nocolor

Style:

  • regular
  • weight
  • bold

Display:

  • prompt , Exits the current command and return user to the prompt.
  • ln , Print a new line
  • print , Print a text
  • println , Print a text followed by a new line
  • reset , Stop any formating like color or bold
  • pad , Print a text with a fixed padding
  • raw , Return a text

Router plugin

The functionality provided by the 'routes' module is very similar to that of express. Options passed during creation are:

  • shell , (required) A reference to your shell application.
  • sensitive , (optional) Defaults to false, set to true if the match should be case sensitive.

New routes are defined with the cmd method. A route is made of pattern against which the user command is matched, an optional description and one or more route specific middlewares to handle the command. The pattern is either a string or a regular expression. Middlewares receive three parameters: a request object, a response object, and a function. Command parameters are substituted and made available in the params object of the request parameter.

Parameters can have restrictions in parenthesis immediately following the keyword, as in express: :id([0-9]+). See the list route in the example:

var app = new shell();
app.configure(function(){
  app.use(shell.router({
    shell: app
  }));
});

// Route middleware
var auth = function(req, res, next){
  if(req.params.uid == process.getuid()){
    next()
  }else{
    throw new Error('Not me');
  }
}

// Global parameter substitution
app.param('uid', function(req, res, next){
  exec('whoami', function(err, stdout, sdterr){
    req.params.username = stdout;
    next();
  });
});

// Simple command
app.cmd('help', function(req, res){
  res.cyan('Run this command `./ami user ' + process.getuid() + '`');
  res.prompt()
});

// Command with parameter
app.cmd('user :uid', auth, function(req, res){
  res.cyan('Yes, you are ' + req.params.username);
});

// Command with contrained parameter
app.cmd('user :id([0-9]+)', function(req, res) {
  res.cyan('User id is ' + req.params.id);
  res.prompt();
});

Contributors

More Repositories

1

node-csv

Full featured CSV parser with simple api and tested against large datasets.
CoffeeScript
3,957
star
2

node-csv-parse

CSV parsing implementing the Node.js `stream.Transform` API
804
star
3

node-http-status

Utility to interact with HTTP status code in Node.js
CoffeeScript
440
star
4

node-hbase

Asynchronous HBase client for NodeJs using REST
CoffeeScript
241
star
5

node-csv-stringify

CSV stringifier implementing the Node.js `stream.Transform` API
187
star
6

node-printf

Write formatted data (complete implementation of printf and sprintf) in Node.js
JavaScript
89
star
7

node-csv-generate

CSV and object generation implementing the Node.js `stream.Readable` API
71
star
8

ece-devops-2022-fall

Resources for the ECE 2022 DevOps course
JavaScript
66
star
9

node-nikita

Automation and deployment solution with Node.js
CoffeeScript
61
star
10

ece-webtech-2022-fall

Materials for the WebTech course at ECE.
JavaScript
61
star
11

ece-webtech-2023-fall

Supporting materials and labs for the Web Technologies course at ECE.
JavaScript
58
star
12

node-ron

Redis object relational mapper for NodeJs with a minimum of magic
CoffeeScript
58
star
13

node-connect-coffee-script

Connect middleware to serve CoffeeScript files
CoffeeScript
56
star
14

ece-devops-2023-fall

JavaScript
53
star
15

node-plug-and-play

Easily create hooks and let users plug their own logic across your code to make it extensible by everyone with new features.
CoffeeScript
52
star
16

node-stream-transform

Object transformations implementing the Node.js `stream.Transform` API
49
star
17

ece-webtech-2021-fall

Materials for the Web technologies course at ECE.
JavaScript
44
star
18

node-pad

Left and right string padding for NodeJs
JavaScript
43
star
19

node-krb5

Kerberos native library for Node.js
C++
34
star
20

ece-devops-2020-fall

Materials for the DevOps course at the ECE.
JavaScript
32
star
21

node-thrift-hive

Hive client using the Apache Thrift RPC system
Thrift
28
star
22

ece-devops-2021-fall

JavaScript
28
star
23

node-each

Chained and parallel async iterator in one elegant function
CoffeeScript
25
star
24

jumbo

🐘 A local Hadoop cluster bootstrapper using Vagrant, Ansible, and Ambari.
Python
19
star
25

ece-bigdata-2023-spring

Python
17
star
26

node-shell

Command line arguments parser and stringifier
CoffeeScript
16
star
27

ece-devops-2023-fall-corrections

JavaScript
15
star
28

node-masson

Module execution engine for cluster deployments.
CoffeeScript
14
star
29

ece-webtech-2024-fall

12
star
30

ece-devops-2023-spring-app

JavaScript
11
star
31

dsti-mlops-2023-spring

Jupyter Notebook
11
star
32

node-csv-docs

Website for the csv projects
JavaScript
10
star
33

ece-spark-2023-fall

Content for the Spark's course at ECE in fall 2023
Jupyter Notebook
9
star
34

dsti-bigdata-2023-spring

dsti-bigdata-2023-spring
Jupyter Notebook
9
star
35

spark-streaming-pyspark

Build and run Spark Structured Streaming pipelines in Hadoop - project using PySpark.
Python
9
star
36

cs-bigdata-2023-spring

Jupyter Notebook
8
star
37

ece-spark-2021-fall

Material for Data Engineering with Spark.
Jupyter Notebook
7
star
38

ece-webtech-2023-spring-app

JavaScript
7
star
39

ece-versioning-2022

Materials for the "Versioning" class at ECE.
6
star
40

ece-bigdata-2022-fall

Resources for ECE Big Data course
HiveQL
6
star
41

dsti-mlops-2022-autumn

Jupyter Notebook
6
star
42

node-ssh2-exec

Transparent use of `child_process.exec` and `ssh2.prototype.exec`
JavaScript
6
star
43

spark-mllib-streaming

Build Spark MLlib pipeline and integrate it with Spark Structured Streaming pipeline running in Hadoop - project using Scala
Scala
6
star
44

ece-spark-2024-fall-gr01

Jupyter Notebook
6
star
45

ece-bigdata-2024-spring-app

Courses content for the 2024 Apprentis ECE class
Python
6
star
46

node-ssh2-fs

Transparent use of the `fs` module locally or over SSH
CoffeeScript
5
star
47

spark-streaming-scala

Build, run, and test Spark Structured Streaming pipelines in Hadoop - project using Scala
Scala
5
star
48

dsti-devops-2023-spring

JavaScript
5
star
49

ece-versioning-2022-fall

versionning course
4
star
50

formations

CSS
4
star
51

ece-bigdata2-2023-fall

Lectures and labs for big data 2 classes
Jupyter Notebook
4
star
52

esgf-2020-fall-bigdata

HTML
4
star
53

ece-webtech-2022-spring

JavaScript
3
star
54

node-hadoop

The HTTP REST API supports the complete FileSystem interface for HDFS
CoffeeScript
3
star
55

node-ssh2-connect

Callback-based api behind ssh2 to open an SSH connection
CoffeeScript
3
star
56

dsti_2022_s22_1

Jupyter Notebook
3
star
57

ece-spark-2020-fall

Resources of "Data Engineering with Spark" course of ECE Paris
Python
3
star
58

dsti-mlops-2021-spring

Resources for MLOps course @ DSTI (spring 2021).
Jupyter Notebook
3
star
59

esgf-bigdata-2022-fall

Introduction to Big Data
HiveQL
3
star
60

node-mixme

A library for recursive merging of Javascript objects
TypeScript
3
star
61

node-nikita-arch

CoffeeScript
3
star
62

cs-bigdata-2021-spring

Jupyter Notebook
2
star
63

node-nikita-docs

Nikita Official website
JavaScript
2
star
64

dsti-devops-2021

JavaScript
2
star
65

ece-spark-2022-fall

Jupyter Notebook
2
star
66

dsti-devops-2020-fall

JavaScript
2
star
67

dsti-bigdata-2022-spring

Jupyter Notebook
2
star
68

dsti-bigdata-2024-spring

Jupyter Notebook
2
star
69

dsti-devops-2022-fall

JavaScript
2
star
70

dsti-mlops-2022-spring

The material for MLOps courses and labs.
Jupyter Notebook
2
star
71

node-redac

JavaScript
2
star
72

ece-spark-2024-fall-gr02

Jupyter Notebook
2
star
73

dsti_spoc

Course for DSTI SPOC students
Jupyter Notebook
1
star
74

dsti-devops-2022-spring

JavaScript
1
star
75

dsti-mlops-2023-autumn

Jupyter Notebook
1
star
76

we-are-hiring

Join Adaltas and build open source big data architectures
1
star
77

node-prink

Human Readable pretty convertions (stringify, parse, compare) for filesize, file mode...
CoffeeScript
1
star
78

spnego_world

C
1
star
79

dsti-devops-2022-autumn

JavaScript
1
star
80

node-ssh2-they

Extends Mocha with the function `they` to transparently run tests in local and ssh mode
CoffeeScript
1
star
81

keyser

Keyser - encryption key and certificate management
Shell
1
star
82

ece-bigdata-2022-spring

Python
1
star
83

gatsby-custom-graphql-schema

Gatsby bloging website with the custom GraphQL schema
JavaScript
1
star
84

rss-feeds-subscription

RSS Feeds for technology watch
1
star
85

remark-gatsby-plugins

A selection of Gatsby plugins developed and used by Adaltas
JavaScript
1
star
86

support-ukrain

1
star
87

dsti-devops-2020-fall-iac

Vagrant and Ansible lab
1
star
88

ece-devops-2022-spring

Materials for the DevOps course at ECE.
JavaScript
1
star
89

demo-dbnomics-graphql

Expose the DBnomics REST API with a GraphQL interface using the GraphiQL UI.
JavaScript
1
star
90

ece-spark

Coding resources for the ECE Paris Spark course
Python
1
star
91

dsti-generate

Generate datasets with random data.
CoffeeScript
1
star
92

dsti-devops-2023-fall

JavaScript
1
star