-
Notifications
You must be signed in to change notification settings - Fork 0
/
cli.js
executable file
·128 lines (107 loc) · 2.97 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#!/usr/bin/env node
'use strict';
var replHere = require('./');
var helpVersion = require('help-version')(usage()),
minimist = require('minimist'),
textTable = require('text-table'),
pairs = require('object-pairs'),
chalk = require('chalk'),
tableHeader = require('table-header'),
stringLength = require('string-length'),
replHistory = require('repl.history'),
home = require('home-dir'),
findRoot = require('find-root'),
cmpby = require('cmpby'),
mapKeys = require('map-keys'),
camelCase = require('camel-case');
var Repl = require('repl'),
Path = require('path');
function usage() {
return [
'Usage: repl-here [OPTION]...',
'',
'Options:',
' -v, --verbose Print name table.',
' -l, --load-main Load module at current working directory.',
' -i MODULE, --ignore=MODULE Ignore module by name.'
].join('\n');
}
var opts = mapKeys(minimist(process.argv.slice(2), {
boolean: ['verbose', 'load-main'],
string: ['ignore'],
alias: {
verbose: 'v',
'load-main': 'l',
ignore: 'i'
},
unknown: function () {
helpVersion.help(1);
}
}), camelCase);
var loadHistory = function (repl) {
var historyFile = home('.node_history');
replHistory(repl, historyFile);
return repl;
};
var renderNameTable = function (names) {
var table = pairs(names).sort(cmpby(function (row) {
return row[0];
}));
if (!table.length) {
return '';
}
var types = {
failed: {
color: 'red',
message: 'failed to load'
},
ignored: {
color: 'yellow',
message: 'ignored'
}
};
table.forEach(function (row) {
if (typeof row[1] == 'object') {
var type = types[row[1].type];
row[1] = chalk[type.color]('(' + type.message + ')');
}
});
tableHeader.add(table, ['MODULE', 'VARIABLE'], { stringLength: stringLength });
table.push(table[1]);
return textTable(table);
};
(function main(opts) {
var repl = loadHistory(Repl.start({
prompts: '> ',
useGlobal: true
}));
var names = {};
var packageName = function (path) {
return Path.basename(Path.dirname(path)) == 'node_modules'
? Path.basename(path) // node_modules/file.js
: Path.basename(findRoot(path)); // node_modules/module/src/index.js
};
(typeof opts.ignore == 'string' ? [opts.ignore] : opts.ignore || [])
.forEach(function (name) {
names[name] = { type: 'ignored' };
});
replHere(repl, process.cwd(), opts)
.on('load', function (name, path) {
names[packageName(path)] = name;
})
.on('fail', function (name, path) {
if (opts.verbose) {
names[packageName(path)] = { type: 'failed' };
}
else {
console.error('\rModule failed to load: ' + name);
}
})
.on('end', function () {
if (opts.verbose) {
var table = renderNameTable(names);
console.log('\r' + (table || chalk.dim('(No modules found.)')));
}
repl.displayPrompt();
});
}(opts));