-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathscrepto.js
executable file
·233 lines (182 loc) · 6.68 KB
/
screpto.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
#!/usr/bin/env node
'use strict';
var program = require('commander');
var fs = require('fs');
var promptly = require('promptly');
var path = require('path');
var request = require('request');
var errcode = require('err-code');
var chalk = require('chalk');
var async = require('async');
var minimatch = require('minimatch');
var find = require('mout/array/find');
var pick = require('mout/object/pick');
var mixIn = require('mout/object/mixIn');
var rpad = require('mout/string/rpad');
var fail = require('./util/fail');
var exec = require('./util/exec');
var pkg = require('./package.json');
var config = require('./config.json');
var executed;
program
.version(pkg.version);
program
.arguments('<script> <patterns...>')
.description('Runs script for each repository that matched a pattern.')
.option('-c, --config <name>', 'the config to be used (e.g.: myself, myorg)')
.action(screpto);
// Ugly hack, * has a special meaning in commander
process.argv.forEach(function (arg, index) {
if (arg === '*') {
process.argv[index] = '?';
}
});
program.parse(process.argv);
if (!executed) {
program.help();
}
// ------------------------------------
function screpto(script, patterns, options) {
executed = true;
setupConfig(options.config);
// List repos
listRepos(patterns, function (err, repos) {
if (err) {
return fail(err);
}
if (!repos.length) {
process.stdout.write('No repositories matched the patterns\n');
process.exit();
}
// Proceed?
confirmWithUser(repos, function () {
// For each repo, fetch and script it
repos.forEach(function (repo) {
const isEmpty = fetchRepo(repo);
!isEmpty && scriptRepo(repo, script);
});
});
});
}
// ------------------------------------
function setupConfig(name) {
if (!name) {
name = Object.keys(config)[0];
if (!name) {
fail(new Error('Please fill the config.json file'));
}
}
config = config[name];
if (!config) {
fail(new Error('Unknown config name ' + name));
}
process.stdout.write(chalk.bold(chalk.cyan('>') + ' Using ' + name + ' configuration\n'));
}
function listRepos(patterns, callback) {
var url = 'https://api.github.com/' + config.type + 's/' + config.name + '/repos';
var allRepos = [];
process.stdout.write(chalk.bold(chalk.cyan('>') + ' Fetching repositories from GitHub\n'));
async.doWhilst(function (callback) {
process.stdout.write(url + '\n');
request(url, {
headers: {
'User-Agent': 'screpto nodejs',
'Accept': 'application/vnd.github.v3+json',
'Authorization': 'token ' + config.access_token
}
}, function (err, response, body) {
var repos;
var pages;
var nextPage;
if (err) {
return callback(err);
}
if (response.statusCode !== 200) {
return callback(errcode('GitHub response code: ' + response.statusCode, 'EREQFAIL', {
details: JSON.stringify(JSON.parse(body), null, 2)
} ));
}
body = JSON.parse(body);
// Process repos
repos = body
.filter(function (repo) {
const matched = patterns.some((pattern) => minimatch(repo.name, pattern.replace(/^\?$/, '*')));
return !repo.archived && matched;
})
.map(function (repo) {
return pick(repo, ['name', 'html_url', 'ssh_url']);
});
allRepos.push.apply(allRepos, repos);
// Check if we got a next page
if (!response.headers.link) {
url = null;
} else {
pages = response.headers.link.split(',').map(function (link) {
var match = link.trim().match(/^<([^>]+?)>; rel="([^"]+?)"$/);
if (!match) {
return callback(new Error('Unable to parse Link header', 'EPARSELINK', {
details: response.headers.link
}));
}
return {
url: match[1],
type: match[2]
};
});
nextPage = find(pages, function (link) {
return link.type == 'next';
});
url = nextPage ? nextPage.url : null;
}
callback();
});
}, function () {
return !!url;
}, function (err) {
if (err) {
return callback(err);
}
allRepos.forEach(function (repo) {
repo.dir = path.join(config.dir || 'repos/' + config.name, repo.name);
});
return callback(null, allRepos);
});
}
function confirmWithUser(repos, callback) {
process.stdout.write(chalk.bold(chalk.cyan('>') + ' The script will run on these repositories:\n'));
repos.forEach(function (repo) {
process.stdout.write(rpad(repo.name, 25) + ' ' + repo.html_url + '\n');
});
process.stdout.write('\n');
promptly.confirm('Shall I proceed? [y/n]', function (err, res) {
if (!res) {
process.exit(1);
}
callback();
});
}
function fetchRepo(repo) {
process.stdout.write(chalk.bold(chalk.cyan('>') + ' Fetching ' + repo.name + '\n'));
if (!fs.existsSync(repo.dir)) {
exec('git', ['clone', repo.ssh_url, repo.dir], { stdio: 'inherit' });
} else {
exec('git', ['fetch', '--prune'], { cwd: repo.dir, stdio: 'inherit' });
}
// Check if repo is empty.
const findResult = exec('find', ['.git/objects', '-type', 'f'], { cwd: repo.dir });
if (!findResult.stdout.length) {
return true;
}
exec('git', ['fetch', '--tags', '--force'], { cwd: repo.dir, stdio: 'inherit' });
const { stdout: remoteHead } = exec('git', ['symbolic-ref', 'refs/remotes/origin/HEAD'], { cwd: repo.dir });
const defaultBranch = Buffer(remoteHead).toString().replace('refs/remotes/origin/', '').trim();
exec('git', ['checkout', defaultBranch], { cwd: repo.dir, stdio: 'inherit' });
exec('git', ['reset', '--hard', 'refs/remotes/origin/HEAD'], { cwd: repo.dir, stdio: 'inherit' });
}
function scriptRepo(repo, script) {
process.stdout.write(chalk.bold(chalk.cyan('>') + ' Running script on ' + repo.name + '\n'));
exec(path.resolve(script), {
env: mixIn({ REPO_DIR: path.resolve(repo.dir) }, process.env),
stdio: 'inherit'
});
}