ntcc

1.0.1 • Public • Published

Namespaced Terminal Client Commands

NPM Version Build Status Coverage Status Codacy Badge Dependency Status devDependency Status NPM Downloads Massachusetts Institute of Technology (MIT) Donate

This module aims to provide an easy and structured way to create modular commands for Node commandline clients and load them only on demand.
It extends a client object with an execute function which can execute modules created with a declared namespaced convention. Each namespaced module must contain a method called invoke. This method runs always in context of the client object.

Usage

Create a command with arguments

"test-client-commands/***clientcommand.***testcommand.open-with-arguments.js"

"use strict";
 
var Cmd = function() {
    return this;
};
 
Cmd.prototype.invoke = function(name1,name2,name3) {
    // runs in context of client.
    return "This testcommand is using open called from " + this.id + " (" + name1 + "," + name2 + "," + name3 + ")";
};
 
module.exports = Cmd;

Extend a terminal client

var NTCC = require("ntcc");
var ntcc = new NTCC();
 
var TestClient = function() {
    this.id = "A Testing Client";
    return this;
};
var testClient = new TestClient();
 
ntcc.extend({
    client: testClient,
    dir: path.join(__dirname, "..", "test-client-commands"),
    namespacePrefix: "clientcommand."
});

console.log(testClient.__) yields:

  • execute() | Method to exec a command.
  • listCommands() | Usefull to provide a commandline help.
  • getCommandFilename() | returns a filename for a given command namespace.

Invoke the test command

it("should execute testcommand.open-with-arguments to pass custom arguments", function() {
    var args = ["arg1", "arg2", "argN"];
    var string = testClient.__.execute(["testcommand", "open-with-arguments"], args);
    string.should.equal("This testcommand is using open called from " + testClient.id + " (" + args.join(",") + ")");
});

For more examples see the test-client-commands and the tests.

A more realistic usecase

testClient.__.execute(["open",  "db", "connection"], ["connection-1"]);
testClient.__.execute(["close",  "db", "connection"], ["connection-1"]);
testClient.__.execute(["new",  "db", "connection"]);
testClient.__.execute(["remove",  "db", "connection"], ["connection-1"]);

I prefer to use this module in association with minimist.

Package Sidebar

Install

npm i ntcc

Weekly Downloads

0

Version

1.0.1

License

MIT

Last publish

Collaborators

  • s-a