-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
71 lines (59 loc) · 2.14 KB
/
index.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
'use strict';
// Used to execute git archive commands such as this:
// git archive --format=tar --remote=ssh://hostname/~username/reponame.git branch:folder | tar xf -
// Get archive from bitbucket -- doesn't work with https
// git archive --format=tar --prefix=alembic/ --remote=ssh://[email protected]/zzzeek/alembic.git master | tar -xf -
// Ideas on how to do similar with github:
// http://stackoverflow.com/questions/9609835/git-export-from-github-remote-repository
// Maybe should do git clone, git checkout, git archive (local). This should support both ssh and https
// and work on bitbucket, stash, github, and any git repo.
var tar = require('tar');
var fs = require('fs');
var uuid = require('uuid');
var Git = require('git-wrapper');
var git = new Git({});
var path = require('path');
module.exports = function(options, callback) {
var gitOptions = {
format: 'tar',
remote: options.source,
output: options.tarfile || path.join(options.tmpDir || '/tmp', uuid.v4()+'.tar'),
prefix: options.prefix || ''
};
// Remove tarfile if savetar option is false, otherwise
// return path to tarfile
function cleanup() {
if (!options.savetar) {
fs.unlinkSync(gitOptions.output);
return;
}
return gitOptions.output;
}
// Download git repository as a tar file
git.exec('archive', gitOptions, [options.branch], function (err, msg) {
if (err) {
// Error occurred downloading from the source
callback && callback(err);
return;
}
if (options.dest) {
// Read in the downloaded tarfile
fs.createReadStream(gitOptions.output)
// Explode the downloaded tarfile
.pipe(tar.Extract({ path: options.dest }))
// Handle extraction errors
.on('error', function (er) {
callback && callback(er);
})
// Handle successful extraction completion
.on('end', function () {
// Return string containing path to downloaded tarfile.
callback && callback(null, cleanup());
});
}
else {
// Return string containing path to downloaded tarfile.
callback && callback(null, cleanup());
}
});
};