forked from jspm/github
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgithub.js
377 lines (315 loc) · 9.89 KB
/
github.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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
var exec = require('child_process').exec;
var fs = require('fs');
var path = require('path');
var mkdirp = require('mkdirp');
var rimraf = require('rimraf');
var request = require('request');
var Promise = require('rsvp').Promise;
var asp = require('rsvp').denodeify;
var tar = require('tar');
var zlib = require('zlib');
var execOpt;
var username, password;
var remoteString;
var apiRemoteString;
var GithubLocation = function(options) {
username = options.username;
password = options.password;
execOpt = {
cwd: options.tmpDir,
timeout: options.timeout * 1000,
killSignal: 'SIGKILL'
};
this.remote = options.remote;
remoteString = 'https://' + (username ? (username + ':' + password + '@') : '') + 'github.com/';
apiRemoteString = 'https://' + (username ? (username + ':' + password + '@') : '') + 'api.github.com/';
}
function clearDir(dir) {
return asp(fs.exists)(dir)
.then(function(exists) {
if (exists)
return asp(rimraf)(dir);
});
}
function prepDir(dir) {
return clearDir(dir)
.then(function() {
return asp(mkdirp)(dir);
});
}
function checkReleases(repo, version) {
return asp(request)({
uri: apiRemoteString + 'repos' + repo + '/releases',
headers: {
'User-Agent': 'jspm'
},
strictSSL: false,
followRedirect: false
})
.then(function(res) {
try {
return JSON.parse(res.body);
}
catch(e) {
throw 'Unable to parse GitHub API response';
}
})
.then(function(releases) {
// run through releases list to see if we have this version tag
for (var i = 0; i < releases.length; i++) {
var tagName = releases[i].tag_name.trim();
if (tagName == version) {
var firstAsset = releases[i].assets[0];
if (!firstAsset)
return false;
var assetType;
if (firstAsset.name.substr(firstAsset.name.length - 7, 7) == '.tar.gz' || firstAsset.name.substr(firstAsset.name.length - 4, 4) == '.tgz')
assetType = 'tar';
else if (firstAsset.name.substr(firstAsset.name.length - 4, 4) == '.zip')
assetType = 'zip';
else
return false;
return { url: firstAsset.url, type: assetType };
}
}
return false;
});
}
// check if the given directory contains one directory only
// so that when we unzip, we should use the inner directory as
// the directory
function checkStripDir(dir) {
return asp(fs.readdir)(dir)
.then(function(files) {
if (files.length > 1)
return dir;
var dirPath = path.resolve(dir, files[0]);
return asp(fs.stat)(dirPath);
})
.then(function(stat) {
if (stat.isDirectory())
return dirPath;
return dir;
});
}
GithubLocation.prototype = {
parse: function(name) {
var parts = name.split('/');
var packageName = parts.splice(0, 2).join('/');
return {
package: packageName,
path: parts.join('/')
};
},
configure: function(config) {
config.name = 'github';
config.remote = 'https://github.jspm.io';
return Promise.resolve(config);
},
// return values
// { versions: { versionhash } }
// { redirect: 'newrepo' }
// { notfound: true }
lookup: function(repo) {
return new Promise(function(resolve, reject) {
var versions, cancel, passed = 0;
// request the repo to check that it isn't a redirect
request({
uri: apiRemoteString + 'repos/' + repo,
headers: {
'User-Agent': 'jspm'
},
strictSSL: false,
followRedirect: false
})
.on('response', function(res) {
if (cancel)
return;
// redirect
if (res.statusCode == 301) {
cancel = true;
return resolve({ redirect: res.headers.location.split('/').splice(3) });
}
//not found
if (res.statusCode == 404) {
cancel = true;
return resolve({ notfound: true });
}
//other error
else if (res.statusCode != 200) {
cancel = true;
return reject('Invalid status code ' + res.statusCode);
}
passed++;
if (passed == 2)
return resolve({ versions: versions });
})
.on('error', function(err) {
cancel = true;
reject(err);
});
exec('git ls-remote ' + remoteString + repo + '.git refs/tags/* refs/heads/*', execOpt, function(err, stdout, stderr) {
if (cancel)
return;
if (err) {
if ((err + '').indexOf('Repository not found') == -1) {
cancel = true;
reject(stderr);
}
return;
}
versions = {};
var refs = stdout.split('\n');
for (var i = 0; i < refs.length; i++) {
if (!refs[i])
continue;
var hash = refs[i].substr(0, refs[i].indexOf('\t'));
var refName = refs[i].substr(hash.length + 1);
if (refName.substr(0, 11) == 'refs/heads/')
versions[refName.substr(11)] = hash;
else if (refName.substr(0, 10) == 'refs/tags/') {
if (refName.substr(refName.length - 3, 3) == '^{}')
versions[refName.substr(10, refName.length - 13)] = hash;
else
versions[refName.substr(10)] = hash;
}
}
passed++;
if (passed == 2)
resolve({ versions: versions });
});
});
},
// optional hook, allows for quicker dependency resolution
// since download doesn't block dependencies
getPackageConfig: function(repo, version, hash) {
// NB ensure this works for private repos
var reqOptions = {
uri: 'https://raw.githubusercontent.com/' + repo + '/' + hash + '/package.json',
strictSSL: false
};
if (username)
reqOptions.auth = {
user: username,
pass: password
};
return asp(request)(reqOptions).then(function(res) {
if (res.statusCode == 404) {
// it is quite valid for a repo not to have a package.json
return {};
}
if (res.statusCode != 200)
throw 'Unable to check repo package.json for release';
var packageJSON;
try {
packageJSON = JSON.parse(res.body);
}
catch(e) {
throw 'Error parsing package.json';
}
return packageJSON;
});
},
// always an exact version
// assumed that this is run after getVersions so the repo exists
download: function(repo, version, hash, outDir) {
return checkReleases(repo, version)
.then(function(release) {
if (!release)
return true;
// Download from the release archive
return new Promise(function(resolve, reject) {
var inPipe;
if (release.type == 'tar') {
inPipe = zlib.createGunzip()
.pipe(tar.Extract({ path: outDir, strip: 1 }))
.on('end', function() {
resolve();
})
.on('error', reject);
}
else if (release.type == 'zip') {
var tmpDir = path.resolve(execOpt.cwd, 'release-' + repo.replace('/', '#') + '-' + version);
var tmpFile = tmpDir + '.' + type;
if (process.platform.match(/^win/))
return errback('No unzip support for windows yet due to https://github.com/nearinfinity/node-unzip/issues/33. Please post a jspm-cli issue.');
inPipe = fs.createWriteStream(tmpFile)
.on('finish', function() {
Promise.resolve()
.then(function() {
return asp(exec)('unzip -o ' + tmpFile + ' -d ' + tmpDir + ' && chmod -R +w ' + tmpDir, execOpt)
})
.then(function() {
return checkStripDir(tmpDir);
})
.then(function(repoDir) {
return prepDir(outDir);
})
.then(function() {
return asp(fs.rename)(repoDir, outDir);
})
.then(function() {
if (repoDir != tmpDir)
return asp(fs.rmdir)(tmpDir);
})
.then(function() {
return asp(fs.unlink)(tmpFile);
})
.then(resolve, reject);
})
.on('error', reject);
}
else {
throw 'Github release found, but no archive present.';
}
// now that the inPipe is ready, do the request
request({
uri: archiveURL,
headers: {
'accept': 'application/octet-stream',
'user-agent': 'jspm'
},
strictSSL: false
}).on('response', function(archiveRes) {
if (archiveRes.statusCode != 200)
return reject('Bad response code ' + archiveRes.statusCode + '\n' + JSON.sringify(archiveRes.headers));
if (archiveRes.headers['content-length'] > 100000000)
return reject('Response too large.');
archiveRes.pause();
archiveRes.pipe(inPipe);
archiveRes.on('error', reject);
archiveRes.resume();
})
.on('error', reject);
});
})
.then(function(git) {
if (!git)
return;
// Download from the git archive
return new Promise(function(resolve, reject) {
request({
uri: remoteString + repo + '/archive/' + version + '.tar.gz',
headers: { 'accept': 'application/octet-stream' },
strictSSL: false
})
.on('response', function(pkgRes) {
if (pkgRes.statusCode != 200)
return reject('Bad response code ' + pkgRes.statusCode);
if (pkgRes.headers['content-length'] > 10000000)
return reject('Response too large.');
pkgRes.pause();
var gzip = zlib.createGunzip();
pkgRes
.pipe(gzip)
.pipe(tar.Extract({ path: outDir, strip: 1 }))
.on('error', reject)
.on('end', resolve);
pkgRes.resume();
})
.on('error', reject);
});
});
}
};
module.exports = GithubLocation;