nodejs command line utility
mkdir samplecli
cd samplecli
npm init
npm install owncli --save
touch index.js
var config = {
tasks: __dirname + '/tasks',
mainCommand : 'sample'
};
require('owncli').init(config, function(err, cli){
cli.execute();
});
mkdir tasks
cd tasks
touch test.js
module.exports = {
commands: {
one: {
description: 'do one thing',
execute: require('./test-one'),
options: {
significance: {abbr: 's', description: 'significance of version',required: true}
}
}
two: {
description: 'do two things',
execute: require('./test-two'),
options: {
verbose: {abbr: 'v', description: 'set verbosity'},
save: {abbr: 's', description: 'save the settings', required: true},
env: {abbr: 'e', description: 'set environment', required: true, values: ['dev', 'prod']}
}
}
}
};
touch test-one.js
var runner = require('owncli').runner;
module.exports = function(options){
runner(options)
.echo("Here we go!")
.exec("cat index.js")
.get('version').from('package.json').as('oldVersion')
.semver('oldVersion').as('newVersion')
.set('version').in('package.json').to('newVersion')
.echo("Done!")
.go(function (err, results) {
console.log('options', options);
console.log(err || results || '');
});
};
touch test-two.js
module.exports = function(options){
console.log("three");
};
node index //=> outputs help
node index test //=> outputs help for the test command
node index test one -s patch //call test-one command
WINDOWS: SET DEBUG=owncli & node index.js
*nix: DEBUG=owncli & node index.js