Платформа Node.js становится все более популярной. Для нее уже создано много библиотек и инструментов. Рассказ о том, какие из них и для чего мы используем.
14. npm search
Поиск модуля
$ npm search logging
NAME DESCRIPTION
ain Syslog logging for node.js
ain-tcp Syslog logging for node.js, with syslog/TCP
beaver CLI tool for piping a log's tail over TCP
bolt-logger simple logging for bolt
book flexible node.js logging library
book-email email transport for book logging framework
book-file file transport for book logging framework
book-git git middleware for book logging framework
14
14
15. npm show
Информация о модуле
$ npm show bem
{ name: 'bem',
description: 'BEM Tools',
'dist-tags':
{ latest: '0.4.2',
stable: '0.2.5',
unstable: '0.5.10' },
maintainers:
[ 'arikon <peimei@ya.ru>',
'veged <veged@mail.ru>',
'fedor.indutny <fedor.indutny@gmail.com>',
'scf <scf2k@yandex.ru>',
'afelix <skryzhanovsky@gmail.com>' ],
author: 'Sergey Berezhnoy <veged@mail.ru> (http://github.com/
veged)',
...
15
15
16. npm install
Установка модуля
$ npm install bem
npm http GET https://registry.npmjs.org/bem
npm http GET https://registry.npmjs.org/coa
npm http GET https://registry.npmjs.org/q
npm http GET https://registry.npmjs.org/inherit
npm http GET https://registry.npmjs.org/borschik
npm http 304 https://registry.npmjs.org/ometajs
bem@0.4.2 ../node_modules/bem
!"" q-wrap@0.0.1
!"" inherit@1.0.4
!"" qq@0.3.3
!"" coa@0.3.5
!"" q@0.7.2 (event-queue@0.2.0)
#"" borschik@0.0.10 (q@0.8.5, q-fs@0.1.27, cssp@1.0.5)
16
16
69. Стили тестов
QUnit
function ok(expr, msg) {
if (!expr) throw new Error(msg);
}
suite('Array');
test('#indexOf()', function(){
var arr = [1,2,3];
ok(arr.indexOf(4) == -1);
});
69
69
70. Mocha
— Тестирование асинхронного кода
— Разные виды репортов
— Ваша любимая библиотека ассертов
— Несколько стилей описания тестов
— Test coverage
70
70
79. Почему Promises?
— Не требует препроцессинга
— Читабельный код
— Единообразие
— Позволяет писать плоский код
— Внутри все та же асинхронность
Но...
79
79
82. Консольные утилиты
— Команды, опции, аргументы
— Помощь
— Автокомплит
— API для использования из программ
Все это есть в COA!
82
82
83. require('coa').Cmd()
.name(process.argv[1])
.title('Test COA util')
.helpful()
.opt()
.name('version') // name for use in API
.title('Version') // title for use in text messages
.short('v') // short key: -v
.long('version') // long key: --version
.flag() // for options without value
.only() // act as command
.act(function(opts) { // add action for option
return "COA util 2.3.1";
})
.end() // end option chain and return to main command
83
83
84. .cmd() // subcommand declaration
.name('greet').title('Test greet command').helpful()
.opt()
.name('name').title('who to greet, required')
.short('n').long('name')
.val(function(v) { // validator function
return v || 'Unknown'
})
.req() // make option required
.end() // end option chain and return to command
.act(function(opts, args){
console.log('Hello, ' + opts.name + '!!!');
})
.end() // end subcommand chain
.completable() // make possible generate completion script
.act(function(){ // default handler when run with no params
console.log('run with --help to see help')
})
.run(process.argv.slice(2)); // parse and run on process.argv
84
84
85. $ node coa.js
run with --help to see help
$ node coa.js --help
Test COA util
Usage:
coa.js COMMAND [OPTIONS] [ARGS]
coa.js [OPTIONS] [ARGS]
Commands:
greet : Test greet command
completion : Shell completion
Options:
-h, --help : Help
-v, --version : Version
$ node coa.js -v
COA util 2.3.1
85
85