$ node adder.js 4 9
4 + 9 = 13
$ adder 4 9
4 + 9 = 13
Command Line Executable Javascript
$ touch adder.js
$ chmod +x adder.js
#!/usr/bin/env node
console.log('I added those 2 numbers in my head... I promise');
$ ./adder.js 50 4
I added those 2 numbers in my head... I promise
$ ln adder.js /usr/local/bin/adder
$ adder 50 4
I added those 2 numbers in my head... I promise
$ touch adder.js
$ chmod +x adder.js
$ ln adder.js usr/local/bin/adder
$ adder 17 24
17+24=41
#!/usr/bin/env node
var a1 = Number(process.argv[2]);
var a2 = Number(process.argv[3]);
var sum = a1 + a2;
console.log(a1 + "+" + a2 + "=" + sum);
Make Your Own Node Package
$ npm init
name: (directory-name)
version: (1.0.0)
entry point: (index.js)
blah:
blah:
blah:
{
"name": "slide-test",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\""
},
"author": "",
"license": "ISC",
///////////////////////////////////////
"bin": {
"commandname":"path/to/file"
},
"preferGlobal": true
///////////////////////////////////////
}
#!/usr/bin/env node
console.log("hello, world");
$ npm link
$ npm init
$ touch adder.js
$ npm link
$ adder 17 24
17+24=41
adder.js:
#!/usr/bin/env node
var a1 = Number(process.argv[2]);
var a2 = Number(process.argv[3]);
var sum = a1 + a2;
console.log(a1 + "+" + a2 + "=" + sum);
package.json:
"bin": {
"commandname":"path/to/file"
},
"preferGlobal": true
$ npm adduser
OR
$ npm login
$ npm publish
$ npm version [patch]
Making command line parsing FUN!
program
.option('-c, --captain',
'Specify that this person is a captain.')
.option('-n, --name [name]',
'Specify the name of the person.',
'Jack Sparrow')
.parse(process.argv);
console.log(program.name);
program
.usage('<directory> [options]');
program
.command('angular <someVar>')
.description('')
.action(function callback(someVar, options) {
console.log('do some things');
});
program
.command('angular', 'this generator does some stuff!')
.command('angular-fullstack', 'this generator does more stuff!');
program
.command('yeoman')
.alias('yo')
.action(someCallback);
$ griffin --help
Usage: griffin [options] [command]
Commands:
compliment [options] generate a random compliment
calculate|calc [options] <base> do some pretty complicated math
Options:
-h, --help output usage information