Minh Chu
Minh Chu

Reputation: 289

How to separate commands into files Vorpal.js

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

I want 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?

Upvotes: 3

Views: 457

Answers (1)

dthree
dthree

Reputation: 20750

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.

Upvotes: 6

Related Questions