forked from naholyr/github-todos
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
135 lines (110 loc) · 3.01 KB
/
index.js
File metadata and controls
135 lines (110 loc) · 3.01 KB
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
"use strict";
/* eslint no-process-exit:0 */
var optimist = require("optimist");
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())
// 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();
}
// 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;
}
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);
}
// Configure opts and run command (inside a domain to catch any error)
commands.run(command, opts, conf, function (e) {
console.error(e.message);
if (process.env.DEBUG) {
throw e;
}
process.exit(1);
});
}
// Main execution
// processArgv = CLI args === process.argv.slice(2)
function safeMain (processArgv) {
if (process.env.NO_GITHUB_TODOS) {
console.log("[Github-Todos] Disabled from environment");
process.exit(0);
return;
}
// Check env then execute
checkEnv(function (err) {
if (err) {
console.error("Error found in your current environment:");
console.error(err.message);
if (process.env.DEBUG) {
throw err;
}
process.exit(2);
}
config.list(function (err, conf) {
if (err) {
conf = {};
}
main(processArgv, conf);
});
});
}