Use Nexe with Commander.js Sub Commands

Nexe is a command-line utility that compiles your Node.js application into a single executable file. And Commander.js is the most popular command-line interfaces solution for Node.js. But unfortunately, they don't play well with each other when it comes to sub commands.

To accomplish what's not possible, I started tricky works between Nexe and Commander.js.

But again unfortunately, it required both library to be modified (for Nexe actually it was bug fix).

The fix to Nexe is now merged and published to npm, but the pull request to Commander.js has not yet been merged (and I doubt whether they will ever merge it, as it's not directly relevant to its own functionality).

Luckily you get my fork, and we are going to play with Nexe and Commander.js sub commands now.

npm install nexe -g
npm install vilic/commander.js --save

Nexe will analyze the files to packed based on string literals inside require call. But Commander.js will try to find sub commands dynamically. So the first thing we'll need to do, as what's suggested in README of Nexe, is to add require of each sub command to someplace that will never be executed (pay attention to your uglify task and make sure it's not removing it).

For example:

function commands() {
    require('./command-foo');
    require('./command-bar');
}

Great, expecting more? But that's it.

Enjoy.