-
Notifications
You must be signed in to change notification settings - Fork 3
/
cli.js
executable file
·63 lines (55 loc) · 1.15 KB
/
cli.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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/usr/bin/env node
'use strict';
var _ = require('lodash');
var nodetree = require('./');
var nopt = require('nopt');
var pkg = require('./package.json');
var stdin = require('get-stdin');
var knownOpts = {
'all': Boolean,
'directories': Boolean,
'help': Boolean,
'level': Number,
'noreport': Boolean,
'version': Boolean
};
var shortHands = {
'a': ['--all'],
'd': ['--directories'],
'h': ['--help'],
'L': ['--level'],
'v': ['--version']
};
var parsed = nopt(knownOpts, shortHands, process.argv, 2);
var basepath = parsed.argv.remain[0];
var options = _.extend({}, parsed);
options = _.extend(options, { cwd: process.cwd() });
delete options.argv;
function help() {
console.log([
'man nodetree'
].join('\n'));
}
function init(basepath, options) {
if (!basepath) {
// Default to cwd if no path is given.
basepath = '.';
}
return nodetree(basepath, options);
}
if (options.help) {
help();
return;
}
if (options.version) {
console.log(pkg.version);
return;
}
if (process.stdin.isTTY) {
init(basepath, options);
} else {
// Accept basepath from stdin.
// Example:
// $ echo $(pwd) | nodetree
stdin(init);
}