dthree / vorpal

Node's framework for interactive CLIs
http://vorpal.js.org
MIT License
5.64k stars 280 forks source link

How to separate commands into files #157

Closed minhchu closed 8 years ago

minhchu commented 8 years ago

I have many commands and each of them is long. For example, I have:

I wants to put them in separate files:

and I want to require them in app.js:

require('./commands/create.js');
...

so I can: node app.js create HelloWorld

How can I achieve this. Thank you.

dthree commented 8 years ago

I would do something like this:

// create.js

function create(args, cb) {
  // ... your logic
}

module.exports = function (vorpal) {
  vorpal
    .command('create')
    .action(create);
}

Then in your main file, you can do:

// main.js

const vorpal = Vorpal();

vorpal
  .use(require('./create.js'))
  .use(require('./read.js'))
  .show();

More on this here.


Does this help? If so, would you mind posting this as a StackOverflow question under the vorpal.js tag? In this way, others can search and find the solution as well.

minhchu commented 8 years ago

Thank you. It works. I dont know that extension can be used this way :+1: Btw, here is the question, you can answer it :grin: http://stackoverflow.com/questions/38261365/how-to-separate-commands-into-files-vorpal-js