-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
169 lines (149 loc) · 3.45 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
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
var downloadUrl = require('download')
var gitclone = require('git-clone')
var rm = require('rimraf').sync
/**
* Expose `download`.
*/
module.exports = download
/**
* Download `repo` to `dest` and callback `fn(err)`.
*
* @param {String} repo
* @param {String} dest
* @param {Object} opts
* @param {Function} fn
*/
function download (repo, dest, opts, fn) {
if (typeof opts === 'function') {
fn = opts
opts = null
}
opts = opts || {}
var clone = opts.clone || false
delete opts.clone
repo = normalize(repo)
var url = repo.url || getUrl(repo, clone)
if (clone) {
var cloneOptions = {
checkout: repo.checkout,
shallow: repo.checkout === 'master',
...opts
}
gitclone(url, dest, cloneOptions, function (err) {
if (err === undefined) {
rm(dest + '/.git')
fn()
} else {
fn(err)
}
})
} else {
var downloadOptions = {
extract: true,
strip: 1,
mode: '666',
...opts,
headers: {
accept: 'application/zip',
...(opts.headers || {})
}
}
downloadUrl(url, dest, downloadOptions)
.then(function (data) {
fn()
})
.catch(function (err) {
fn(err)
})
}
}
/**
* Normalize a repo string.
*
* @param {String} repo
* @return {Object}
*/
function normalize (repo) {
var regex = /^(?:(direct):([^#]+)(?:#(.+))?)$/
var match = regex.exec(repo)
if (match) {
var url = match[2]
var directCheckout = match[3] || 'master'
return {
type: 'direct',
url: url,
checkout: directCheckout
}
} else {
regex = /^(?:(github|gitlab|bitbucket|gitee):)?(?:(.+):)?([^/]+)\/([^#]+)(?:#(.+))?$/
match = regex.exec(repo)
var type = match[1] || 'github'
var origin = match[2] || null
var owner = match[3]
var name = match[4]
var checkout = match[5] || 'master'
if (origin == null) {
if (type === 'github') {
origin = 'github.com'
} else if (type === 'gitlab') {
origin = 'gitlab.com'
} else if (type === 'bitbucket') {
origin = 'bitbucket.org'
} else if (type === 'gitee') {
origin = 'gitee.com'
}
}
return {
type: type,
origin: origin,
owner: owner,
name: name,
checkout: checkout
}
}
}
/**
* Adds protocol to url in none specified
*
* @param {String} url
* @return {String}
*/
function addProtocol (origin, clone) {
if (!/^(f|ht)tps?:\/\//i.test(origin)) {
if (clone) {
origin = 'git@' + origin
} else {
origin = 'https://' + origin
}
}
return origin
}
/**
* Return a zip or git url for a given `repo`.
*
* @param {Object} repo
* @return {String}
*/
function getUrl (repo, clone) {
var url
// Get origin with protocol and add trailing slash or colon (for ssh)
var origin = addProtocol(repo.origin, clone)
if (/^git@/i.test(origin)) {
origin = origin + ':'
} else {
origin = origin + '/'
}
// Build url
if (clone) {
url = origin + repo.owner + '/' + repo.name + '.git'
} else {
if (repo.type === 'github') {
url = origin + repo.owner + '/' + repo.name + '/archive/' + repo.checkout + '.zip'
} else if (repo.type === 'gitlab') {
url = origin + repo.owner + '/' + repo.name + '/repository/archive.zip?ref=' + repo.checkout
} else if (repo.type === 'bitbucket') {
url = origin + repo.owner + '/' + repo.name + '/get/' + repo.checkout + '.zip'
}
}
return url
}