-
Notifications
You must be signed in to change notification settings - Fork 33
/
init.js
208 lines (172 loc) · 6.38 KB
/
init.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
"use strict";
/* eslint no-process-exit:0 */
var fs = require("fs");
var path = require("path");
var async = require("async");
var _ = require("lodash");
var git = require("../git");
var github = require("../github");
var config = require("../config");
var templates = path.join(__dirname, "..", "..", "doc", "hooks");
var prePushCommand = fs.readFileSync(path.join(templates, "command.txt"), {encoding: "utf8"}).trim();
var prePushTxt = fs.readFileSync(path.join(templates, "pre-push.txt"), {encoding: "utf8"});
var prePushScript = prePushTxt.replace(/\{\s*command\s*\}/g, prePushCommand);
exports.config = function (opts) {
return opts
.boolean("u")
.alias("u", "uninstall")
.describe("u", "Uninstall Github-Todos hook")
.boolean("f")
.alias("f", "force")
.describe("f", "Force-delete hook on uninstall, or force-add command to existing hook on install")
.boolean("connect")
.default("connect", true)
.describe("connect", "Check or create Github authentication credentials before generating hook");
};
exports.run = function (argv) {
var ops = {
"path": _.partial(git.dir, "hooks/pre-push")
};
if (argv.connect) {
console.log("[Github-Todos] To disable checking credentials on 'init', add option '--no-connect'");
ops.connect = function (cb) {
config.list(function (err, conf) {
if (err) {
throw err;
}
github.connect(conf, cb);
});
};
}
async.auto(ops, function (err, res) {
if (err) {
throw err;
}
var file = res.path;
var found = fs.existsSync(file);
var content = found && fs.readFileSync(file, {encoding: "utf8"});
var isUnmodifiedScript = content && (content.trim() === prePushScript.trim());
var commandFound = content && content.indexOf(prePushCommand) !== -1;
if (argv.uninstall) {
if (isUnmodifiedScript || argv.force) {
removeHook(file);
} else if (content && commandFound) {
removeFromHook(file, content);
} else {
cannotUninstall(file, content);
}
} else {
if (commandFound && !argv.force) {
commandAlreadyInHook(file);
} else if (found) {
addToHook(file, content, commandFound);
} else {
createHook(file);
}
}
if (!argv.uninstall) {
try {
fs.chmodSync(file, 493); // 0755
} catch (e) {
console.error("[Github-Todos] WARNING: Failed to `chmod 755 " + file + "`. Not that this file *must* be executable, you may want to fix this manually.");
}
console.log("");
console.log("[Github-Todos] Hook installed");
config.get("repo", "local", function (err, repo) {
if (err) {
console.error("[Github-Todos] %s", err);
console.error("[Github-Todos] Failed to fetch 'repo' option");
guessRepo();
} else if (!repo) {
console.log("[Github-Todos] Option 'repo' is not set");
guessRepo();
} else {
console.log("[Github-Todos] Option 'repo' is already set. Using '" + repo + "'");
console.log("[Github-Todos] OK");
}
});
} else {
console.log("[Github-Todos] OK");
}
});
};
function guessRepo () {
console.log("[Github-Todos] Now guessing initial configuration from remote 'origin'…");
git.run("config --local remote.origin.url", function (err, url) {
if (err) {
console.error("%s", err);
return finishedConfig("could not fetch remote 'origin' url");
}
var match = url.match(/github\.com[:\/]([^\/]+\/[^\/]+?)(?:\.git)?$/);
if (!match) {
return finishedConfig("could not guess repo from url '" + url + "'");
} else {
var repo = match[1];
config.set("repo", repo, "local", function (err) {
if (err) {
console.error("[Github-Todos] %s", err);
return finishedConfig("could not save configuration, please run 'github-todos config repo \"" + repo + "\"'");
}
finishedConfig(null, repo);
});
}
});
}
function finishedConfig (errMessage, repo) {
if (errMessage) {
console.error("");
console.error("[Github-Todos] Initial configuration failed: %s", errMessage);
console.error("[Github-Todos] Run 'github-todos config repo \"<GITHUB USER OR ORG>/<REPOSITORY>\"' to enable hook");
} else {
console.log("");
console.log("[Github-Todos] Will use repository '%s'", repo);
console.log("[Github-Todos] Run 'github-todos config' to check configuration, you may want to customize 'repo' option");
console.log("[Github-Todos] OK");
}
}
function createHook (file) {
console.log("[Github-Todos] Hook file not found, create new one…");
fs.writeFileSync(file, prePushScript);
}
function addToHook (file, content, unsafe) {
if (unsafe) {
console.error("[Github-Todos] Hook file found, github-todos command found.");
console.error("[Github-Todos] Execution forced by option --force: add github-todos command on top anyway…");
} else {
console.log("[Github-Todos] Hook file found, add github-todos command on top…");
}
var lines = content.split("\n");
lines.splice(1, 0, "\n" + prePushCommand + "\n");
content = lines.join("\n");
fs.writeFileSync(file, content);
}
function removeFromHook (file, content) {
console.log("[Github-Todos] Hook file found, removing github-todos command…");
content = content.split("\n").map(function (line, index) {
if (line.indexOf(prePushCommand) !== -1) {
console.log("[Github-Todos] Remove line " + (index + 1) + ": " + line);
return "";
}
return line;
}).join("\n");
fs.writeFileSync(file, content);
}
function removeHook (file) {
console.log("[Github-Todos] Hook file found, unmodified, remove hook…");
fs.unlinkSync(file);
}
function commandAlreadyInHook (file) {
console.error("[Github-Todos] Hook file found, github-todos command found.");
console.error("[Github-Todos] Use option --force to add command to hook anyway.");
console.error("[Github-Todos] You may want to insert command '" + prePushCommand + "' manually: edit '" + file + "'");
process.exit(5);
}
function cannotUninstall (file, hasContent) {
if (hasContent) {
console.error("[Github-Todos] Hook file found but command not found, cannot uninstall.");
} else {
console.error("[Github-Todos] Hook file not found, cannot uninstall.");
}
console.error("[Github-Todos] You may want to uninstall manually: edit '" + file + "'");
process.exit(5);
}