UNPKG

2.84 kBJavaScriptView Raw
1var Download = require('download');
2var gitclone = require('git-clone');
3var rm = require('rimraf').sync;
4
5/**
6 * Expose `download`.
7 */
8
9module.exports = download;
10
11/**
12 * Download `repo` to `dest` and callback `fn(err)`.
13 *
14 * @param {String} repo
15 * @param {String} dest
16 * @param {Function} fn
17 */
18
19function 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 * Normalize a repo string.
50 *
51 * @param {String} string
52 * @return {Object}
53 */
54
55function normalize(repo) {
56 var regex = /^((github|gitlab|bitbucket):)?([^/]*)\/([^#]*)(#(.*))?$/;
57 var match = regex.exec(repo);
58 var type = match[2] || "github";
59 var owner = match[3];
60 var name = match[4];
61 var checkout = match[6] || "master";
62
63 return {
64 type: type,
65 owner: owner,
66 name: name,
67 checkout: checkout
68 };
69}
70
71/**
72 * Return a zip or git url for a given `repo`.
73 *
74 * @param {Object} repo
75 * @return {String}
76 */
77
78function getUrl(repo, clone) {
79 var url;
80
81 if (repo.type === 'github')
82 url = github(repo, clone);
83 else if (repo.type === 'gitlab')
84 url = gitlab(repo, clone);
85 else if (repo.type === 'bitbucket')
86 url = bitbucket(repo, clone);
87 else
88 url = github(repo, clone);
89
90 return url;
91}
92
93/**
94 * Return a GitHub url for a given `repo` object.
95 *
96 * @param {Object} repo
97 * @return {String}
98 */
99
100function github(repo, clone) {
101 var url;
102
103 if (clone)
104 url = 'git@github.com:' + repo.owner + '/' + repo.name + '.git';
105 else
106 url = 'https://github.com/' + repo.owner + '/' + repo.name + '/archive/' + repo.checkout + '.zip';
107
108 return url;
109}
110
111/**
112 * Return a GitLab url for a given `repo` object.
113 *
114 * @param {Object} repo
115 * @return {String}
116 */
117
118function gitlab(repo, clone) {
119 var url;
120
121 if (clone)
122 url = 'git@gitlab.com:' + repo.owner + '/' + repo.name + '.git';
123 else
124 url = 'https://gitlab.com/' + repo.owner + '/' + repo.name + '/repository/archive.zip?ref=' + repo.checkout;
125
126 return url;
127}
128
129/**
130 * Return a Bitbucket url for a given `repo` object.
131 *
132 * @param {Object} repo
133 * @return {String}
134 */
135
136function bitbucket(repo, clone) {
137 var url;
138
139 if (clone)
140 url = 'git@bitbucket.org:' + repo.owner + '/' + repo.name + '.git';
141 else
142 url = 'https://bitbucket.org/' + repo.owner + '/' + repo.name + '/get/' + repo.checkout + '.zip';
143
144 return url;
145}