uncategorized

hexo-code-reading

Hexo Code Reading

hexoコマンドの実体

node_modules/.bin/hexo の中身

1
2
3
4
5
#!/usr/bin/env node

'use strict';


require('hexo-cli')();

hexo-cli というライブラリを読み込んで実行しているだけ

hexo-cliのエントリポイント

node_modules/hexo/node_modules/hexo-cli/lib/index.js でそれらしき処理

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
function runHexoCommand(){
var cmd = args._.shift();

if (cmd){
var c = hexo.extend.console.get(cmd);
if (!c) cmd = 'help';
} else if (args.v || args.version){
cmd = 'version';
} else {
cmd = 'help';
}

// Listen to Ctrl+C (SIGINT) signal
process.on('SIGINT', function(){
log.info(goodbye());

// Stop watching
if (hexo) hexo.unwatch();

// Exit
exit().then(function(){
process.exit();
});
});

return hexo.call(cmd, args);
}
  • 2 space indentを採用
  • コマンドライン引数は args という値で抽象化されている

minimist というライブラリに任せているようだ

1
2
var minimist = require('minimist');
var args = minimist(process.argv.slice(2));

JavaScriptでCLIツール書くの面倒くさそう