-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathindex.js
171 lines (142 loc) · 3.8 KB
/
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
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
"use strict";
/* eslint no-process-exit:0 */
var optimist = require("optimist");
var ttys = require("ttys");
var _ = require("lodash");
var help = require("./lib/help");
var commands = require("./lib/commands");
var checkEnv = require("./lib/check-env");
var config = require("./lib/config");
module.exports = safeMain;
// We need to regenerate optimist options if --help or -h is encountered
function getOpts (processArgv) {
return optimist(processArgv)
.usage(help())
// Show version
.boolean("version")
.describe("version", "Show version and exit")
// Help options (converted to help command)
.boolean("h")
.describe("h", "Show help")
.alias("h", "help")
// Update notification control
.boolean("no-notifier")
.describe("no-notifier", "Disable update notifier");
}
// Check for package update
function checkUpdate () {
var pkg = require("./package.json");
require("update-notifier")({
packageName: pkg.name,
packageVersion: pkg.version
}).notify();
}
// Show version and exit
function showVersion () {
var pkg = require("./package.json");
console.log(pkg.version);
process.exit(0);
}
// Transform CLI args to convert --help && -h into help command
function transformHelpArgs (processArgv) {
var args = (processArgv || []).slice();
// Remove "--help" and "-h" from args
var longIndex = args.indexOf("--help");
if (longIndex !== -1) {
args.splice(longIndex, 1);
}
var shortIndex = args.indexOf("-h");
if (shortIndex !== -1) {
args.splice(shortIndex, 1);
}
// Replace `$0 …` by `$0 help …`
args.unshift("help");
return args;
}
// Main execution, after env has been checked
function main (processArgv, conf) {
// CLI input
var opts = getOpts(processArgv);
var argv = opts.argv;
// Update notifier
if (!argv["no-notifier"]) {
checkUpdate();
}
// Convert options "--help" and "-h" into command "help"
if (argv.help) {
processArgv = transformHelpArgs(processArgv);
// Regenerate opts from newly forged process.argv
opts = getOpts(processArgv);
argv = opts.argv;
}
// "--version"
if (argv.version) {
showVersion();
}
var commandName = argv._[0];
// Demand command name
if (!commandName) {
console.error("No command specified");
opts.showHelp();
process.exit(127);
}
// Load command module
var command = commands.load(commandName);
// Demand valid command
if (!command) {
console.error("Unknown command: " + commandName);
opts.showHelp();
process.exit(127);
}
process.on("exit", function () {
ttys.stdin.end();
});
// Configure opts and run command (inside a domain to catch any error)
commands.run(command, opts, conf).then(ok, fail);
function ok () {
process.exit(0);
}
function fail (e) {
if (e.code === "EINTERRUPT") {
process.exit(127);
} else {
console.error("%s", e);
if (process.env.DEBUG) {
throw e;
}
process.exit(1);
}
}
}
// Main execution
// processArgv = CLI args === process.argv.slice(2)
function safeMain (processArgv) {
// Benchmark
if (process.env.TIME) {
var start = Date.now();
console.log("[Github-Todos] Start…");
process.on("exit", function () {
var diff = Date.now() - start;
console.log("[Github-Todos] Execution time: %d ms", diff);
});
}
// Disabled?
if (process.env.NO_GITHUB_TODOS) {
console.log("[Github-Todos] Disabled from environment");
process.exit(0);
return;
}
// Check env then execute
checkEnv()
.then(null, function (err) {
console.error("Error found in your current environment: %s", err);
if (process.env.DEBUG) {
console.error(err.stack);
}
process.exit(2);
})
.then(function () {
return config.list();
})
.then(_.partial(main, processArgv));
}