-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
354 lines (314 loc) · 10 KB
/
app.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
(function () {
var spawn = require('child_process').spawn;
var path = require('path');
var fs = require('fs');
var parse = require('parse-link-header');
var cmd = require('commander');
var request = require('request');
var async = require('async');
var chalk = require('chalk');
var _ = require('lodash');
var Configstore = require('configstore');
var pkg = require('./package.json');
var token_url = 'https://github.com/settings/tokens';
var baseAPIUrl = 'https://api.github.com';
// TODO: Use more decent CLI parsing
cmd
.version(pkg.version)
.usage('[options] [user|organization]')
.option('-u, --update', 'Update (git pull) cloned repositories of user/organization')
.option('--no-fork', 'Ignore forked repositories')
.option('-v, --verbose', 'Print git messages')
.option('-t, --token <token>', 'Save Github personal API token')
.parse(process.argv);
if ( !cmd.token && (!cmd.args || cmd.args.length !== 1) ) {
cmd.outputHelp();
process.exit(1);
}
var conf = new Configstore(pkg.name, {token: ''});
if (cmd.token) {
conf.set('token', cmd.token);
console.log(chalk.green.bold('* Saved token'));
if (!cmd.args || cmd.args.length !== 1)
process.exit(0);
}
var options = {
headers: {
'Accept': 'application/vnd.github.v3+json',
'User-Agent': 'lekkas/repo-fetch'
}
};
if (!conf.get('token')) {
console.log(chalk.yellow.bold('* No personal API token found. Your requests may get rate limited'));
console.log(chalk.yellow.bold('* Create a token at <' + token_url + '> and save it using the -t option\n'));
} else {
options.headers.Authorization = 'token ' + conf.get('token');
}
var baseRequest = request.defaults(options);
var activeChild;
/*
* Signal handler for Ctrl-C (SIGINT)
* Aborts current operation (clone or update) on github repository
*/
process.on('SIGINT', function() {
if (activeChild) {
process.stdout.write(chalk.red(' - Aborting operation\n'));
activeChild.kill('SIGKILL');
activeChild = null;
}
});
/*
* Get next repository page. When github repositories of a user/org
* are more than a certain threshold, Github paginates the repository
* list.
*
* @param repoPageURL The URL of a repository page. Normally this will be the
* first one, i.e. https://api.github.com/users/:user/repos . The function
* will then recursively append all next repository pages, if they exist, to
* the repoPages list.
* @param repoPages A list containing all repository page URL's
* @callback Callback to call when all repository pages have been retrieved
*/
function getNextRepoPage(repoPageURL, repoPages, callback) {
baseRequest.get(repoPageURL, function(error, resp, body) {
if (error || resp.statusCode != 200) {
console.log(chalk.red.bold('Status Code: '+ resp.statusCode));
console.log(chalk.red.bold(body));
process.exit(3);
}
if (!resp.headers.link)
return callback(null, repoPages);
var parsed = parse(resp.headers.link);
if (parsed.next && parsed.next.url) {
var nextRepoPageURL = parsed.next.url;
repoPages.push(nextRepoPageURL);
/*
* Prevent the stack from overflowing while we are recursing,
* though this is unlikely to happen, since we don't expect
* a large number of github repository pagination
* results. Just playing with node here.
*/
async.nextTick(function() {
getNextRepoPage(nextRepoPageURL, repoPages, callback);
});
} else {
callback(null, repoPages);
}
});
}
/*
* Get full name, clone url and size of repository.
*
* @param repoPageURL The URL of repository page
* @param repos List of repository info objects:
* {
* full_name: 'user/repo',
* clone_url: 'URL',
* size: 0
* }
* @param callback Callback to signal that repository info objects of for
* current page
* have been added to the list
*/
function getRepos(repoPageURL, repos, callback) {
baseRequest.get(repoPageURL, function(error, resp, body) {
if (error || resp.statusCode != 200) {
console.log(chalk.red.bold('Status Code: '+ resp.statusCode));
console.log(chalk.red.bold(body));
process.exit(3);
}
body = JSON.parse(body);
var currentPageRepos = body
.filter(function(repo) {
return cmd.fork || (!cmd.fork && !repo.fork);
})
.map(function(repo) {
return {
full_name: repo.full_name,
clone_url: repo.clone_url,
size: repo.size
};
});
currentPageRepos.forEach(function(repo) {
repos.push(repo);
});
callback();
});
}
/*
* Clone git repository, given a clone url.
*
* @param repoInfo The repository info object
* {
* full_name: 'user/repo',
* clone_url: 'URL',
* size: 0
* }
*
* cloneRepo() will clone repo at location 'clone_url' into folder 'full_name'
*/
function cloneRepo(repoInfo) {
return function(callback) {
var opts = {
stdio: [
'pipe', // pipe child's stdin to parent
cmd.verbose ? 1 : 'ignore', // use parent's stdout
cmd.verbose ? 2 : 'ignore' // use parent's stderr
]
};
var args = [
'clone',
repoInfo.clone_url,
repoInfo.full_name
];
process.stdout.write(chalk.yellow.bold(' * Cloning '+repoInfo.full_name));
var child = spawn('git', args, opts);
activeChild = child;
child.on('close', function(code, signal) {
if (code) {
process.stdout.write(chalk.red.bold(' - git clone failed\n'));
} else if (!signal) {
process.stdout.write(chalk.green.bold(' - OK\n'));
}
callback(null);
});
child.on('error', function(err) {
if (err) {
console.log(chalk.red.bold(' - ' + err.message));
}
});
};
}
/*
* Update git repository
*
* @param repoInfo The repository info object
* {
* full_name: 'user/repo',
* clone_url: 'URL',
* size: 0
* }
*/
function updateRepo(repoInfo) {
return function(callback) {
var dir = path.join(process.cwd(), repoInfo.full_name);
var opts = {
cwd: dir,
stdio: [
'pipe', // pipe child's stdin to parent
cmd.verbose ? 1 : 'ignore', // use parent's stdout
cmd.verbose ? 2 : 'ignore' // use parent's stderr
]
};
var args = [
'pull'
];
process.stdout.write(chalk.yellow.bold(' * Updating '+repoInfo.full_name));
var child = spawn('git', args, opts);
child.full_name = repoInfo.full_name;
activeChild = child;
child.on('close', function(code, signal) {
if (code) {
process.stdout.write(chalk.red.bold(' - git pull failed\n'));
} else if (!signal) {
process.stdout.write(chalk.green.bold(' - OK\n'));
}
callback(null);
});
child.on('error', function(err) {
if (err) {
console.log(chalk.red.bold(' - ' + err.message));
}
callback(null);
});
};
}
// Populate 'repoPageList' with list of repository pages
async.waterfall([
/*
* Waterfall 1: Get URLs of all repository pages
*/
function(callback) {
var repoPageList = [];
var repoPage = baseAPIUrl + '/users/' + cmd.args[0] + '/repos';
repoPageList.push(repoPage);
console.log(chalk.yellow('* Fetching repository pages'));
getNextRepoPage(repoPage, repoPageList, callback);
},
/*
* Waterfall 2: Get repository clone URLs
*/
function(repoPageList, callback) {
var repoList = [];
// TODO: Is nesting 'async' module calls considered an antipattern?
process.stdout.write(chalk.yellow('* Fetching clone URLs'));
async.each(repoPageList, function (repoPage, cb) {
getRepos(repoPage, repoList, cb);
},
function (err) {
if (err) {
console.log(chalk.red.bold('Error: '+err));
process.exit(2);
}
process.stdout.write(chalk.green(' - found ' + repoList.length +
' repositories\n'));
callback(null, repoList);
});
},
/*
* Waterfall 3: Create task queue
*/
function(repoList, callback) {
var cloneTasks = [];
var updateTasks = [];
var cloneList = [];
var taskQueue = [];
if (repoList.length === 0) {
return callback(new Error(chalk.yellow.bold('* No repositories found')));
}
async.each(repoList, function(repo, cb) {
var dir = path.join(process.cwd(), repo.full_name);
fs.readdir(dir, function(err, files) {
// Directory does not exist
if (err) {
taskQueue.push(cloneRepo(repo));
} else {
/*
* If directory is empty then enqueue a clone task
* for the respective repository
*/
if (files.length === 0) {
taskQueue.push(cloneRepo(repo));
} else {
// Directory is not empty, so enqueue update task
if (cmd.update) {
taskQueue.push(updateRepo(repo));
}
}
}
cb(null);
});
},
function(err) {
callback(null, taskQueue);
});
},
/*
* Waterfall 4: Run clone & update tasks
*/
function(taskQueue, callback) {
async.series(taskQueue, function(err) {
if (err) {
console.log(err.message);
}
callback(null);
});
},
], function (err) {
if (err) {
console.log(err.message);
} else {
console.log(chalk.blue.bold('* Done!'));
}
});
}).call(this);