Slide 1

Slide 1 text

Writing CLI tools with Node henvic.github.io 1

Slide 2

Slide 2 text

Read–eval–print loop 2

Slide 3

Slide 3 text

3

Slide 4

Slide 4 text

4 npm cheat sheet http://browsenpm.org/help

Slide 5

Slide 5 text

semver • Semantic Versioning Specification semver.org • MAJOR.MINOR.PATCH • i.e., 1.5.2 5

Slide 6

Slide 6 text

http://browsenpm.org/package.json 
 https://docs.npmjs.com/files/package.json 6

Slide 7

Slide 7 text

Node 0.12 API Stability Index 7

Slide 8

Slide 8 text

Node 0.12 improvements • Promise API (bye bye, callback hell) • child_process.execSync
 - for when async is not necessary 8

Slide 9

Slide 9 text

Environment variables • Global values. • Try other approaches before setting custom ones to modify your app. • i.e., > process.env.PAGER => 'less' 9

Slide 10

Slide 10 text

working directory • process.cwd(); • process.chdir(‘~/your-new-working-dir/’);

Slide 11

Slide 11 text

Name arguments
 the Unix way • --version | -v • --verbose • --help | -h • --force | -f • --your-command | —your-alias • e.g., gh pull-request -s your-reviewer --title “…” 11

Slide 12

Slide 12 text

process.argv 12

Slide 13

Slide 13 text

A filter program 13

Slide 14

Slide 14 text

14

Slide 15

Slide 15 text

stdin, stdout, stderr • process.stdout.write(‘Hello World!\n’); console.log(‘Hello World!’);
 console.assert(state === ‘enabled’);
 console.time(‘checkpoint’); • process.stderr.write();
 console.error(error.message);
 console.warn(‘whatever’);
 DON’T FORGET ABOUT US 15

Slide 16

Slide 16 text

Unix Signals • man sigaction • An asynchronous notification sent to a process. 16

Slide 17

Slide 17 text

No content

Slide 18

Slide 18 text

Exiting • Exit codes: 0 - 255 • 0 when no more async operations are pending • not zero means some sort of error happened • process.exit(code); 18

Slide 19

Slide 19 text

Config files • JSON is not human-friendly • Try something more Unix-like: .yml or .ini 19

Slide 20

Slide 20 text

Keep your config file structure simple • Dictionary approach is the best:
 key: value • Group params to avoid repetition • Don’t nest too much 20

Slide 21

Slide 21 text

Learn the internal APIs • assert • child_process • fs • net • http • path • process • stream 21

Slide 22

Slide 22 text

External Libraries 22 • insight • inquirer • moment • express • istanbul • … • gulp • commander • lodash • async • colors • cordova

Slide 23

Slide 23 text

You need no decoration rainbows :( 23 ANSI escape code is not friendly with filters…

Slide 24

Slide 24 text

Code Quality Tools:
 static analysis, tests, mocking… 24 • jscs • gulp • Travis • coveralls • rewire • istanbul • plato • jshint

Slide 25

Slide 25 text

Final considerations • Slow loading dependencies are common. • Package installation: local, global, etc. • No binaries means node is a hard dependency. • Don’t forget to escape user input. • KISS principle = “Keep it simple, stupid.” 25

Slide 26

Slide 26 text

npm install -g require-time Avoid slow loading dependencies.
 
 Or require() later. http://funmozar.com/baby-cheetah/ $ require-time 5ms async 14ms colors 13ms github 52ms handlebars 125ms inquirer 55ms insight 12ms moment 4ms nopt 2ms open 1ms truncate 120ms update-notifier 1ms userhome 7ms which 1ms wordwrap 26

Slide 27

Slide 27 text

Q&A 27 speakerdeck.com/henvic/writing-cli-tools-with-node