1 | var Download = require('download');
|
2 | var gitclone = require('git-clone');
|
3 | var rm = require('rimraf').sync;
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 | module.exports = download;
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 | function download(repo, dest, opts, fn) {
|
20 | if (typeof opts === 'function') {
|
21 | fn = opts;
|
22 | opts = null;
|
23 | }
|
24 | opts = opts || {};
|
25 | var clone = opts.clone || false;
|
26 |
|
27 | repo = normalize(repo);
|
28 | var url = getUrl(repo, clone);
|
29 |
|
30 | if (clone) {
|
31 | gitclone(url, dest, { checkout: repo.checkout }, function(err) {
|
32 | if (err === undefined) {
|
33 | rm(dest + "/.git");
|
34 | fn();
|
35 | }
|
36 | else {
|
37 | fn(err);
|
38 | }
|
39 | });
|
40 | }
|
41 | else {
|
42 | new Download({ mode: '666', extract: true, strip: 1 }).get(url).dest(dest).run(function(err, files) {
|
43 | err === null ? fn() : fn(err);
|
44 | });
|
45 | }
|
46 | }
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 | function normalize(repo) {
|
56 | var type = 'github';
|
57 | if (repo.indexOf('github:') === 0) {
|
58 | type = 'github';
|
59 | repo = repo.substring(7);
|
60 | }
|
61 | else if (repo.indexOf('bitbucket:') === 0) {
|
62 | type = 'bitbucket';
|
63 | repo = repo.substring(10);
|
64 | }
|
65 |
|
66 | var owner = repo.split('/')[0];
|
67 | var name = repo.split('/')[1];
|
68 | var checkout = 'master';
|
69 |
|
70 | if (~name.indexOf('#')) {
|
71 | checkout = name.split('#')[1];
|
72 | name = name.split('#')[0];
|
73 | }
|
74 |
|
75 | return {
|
76 | type: type,
|
77 | owner: owner,
|
78 | name: name,
|
79 | checkout: checkout
|
80 | };
|
81 | }
|
82 |
|
83 |
|
84 |
|
85 |
|
86 |
|
87 |
|
88 |
|
89 |
|
90 | function getUrl(repo, clone) {
|
91 | var url;
|
92 |
|
93 | if (repo.type === 'github')
|
94 | url = github(repo, clone);
|
95 | else if (repo.type === 'bitbucket')
|
96 | url = bitbucket(repo, clone);
|
97 | else
|
98 | url = github(repo, clone);
|
99 |
|
100 | return url;
|
101 | }
|
102 |
|
103 |
|
104 |
|
105 |
|
106 |
|
107 |
|
108 |
|
109 |
|
110 | function github(repo, clone) {
|
111 | var url;
|
112 |
|
113 | if (clone)
|
114 | url = 'git@github.com:' + repo.owner + '/' + repo.name + '.git';
|
115 | else
|
116 | url = 'https://github.com/' + repo.owner + '/' + repo.name + '/archive/' + repo.checkout + '.zip';
|
117 |
|
118 | return url;
|
119 | }
|
120 |
|
121 |
|
122 |
|
123 |
|
124 |
|
125 |
|
126 |
|
127 |
|
128 | function bitbucket(repo, clone) {
|
129 | var url;
|
130 |
|
131 | if (clone)
|
132 | url = 'git@bitbucket.org:' + repo.owner + '/' + repo.name + '.git';
|
133 | else
|
134 | url = 'https://bitbucket.org/' + repo.owner + '/' + repo.name + '/get/' + repo.checkout + '.zip';
|
135 |
|
136 | return url;
|
137 | }
|