Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
261 lines (235 loc) · 6.95 KB
/
Copy pathindex.js
File metadata and controls
261 lines (235 loc) · 6.95 KB
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
const octokit = require('@octokit/rest')()
const fs = require('fs')
const mime = require('mime')
const path = require('path')
const glob = require('glob-fs')({ gitignore: true });
var NightlyDeploy = {
release: {},
uploadedAssets: [],
filteredAssets: [],
config: {
owner: null,
repo: null,
branch: null,
tag: null,
assets: [],
dir: null,
token: ''
},
// Initialize NightlyDeploy.
init(config) {
this.config = config
// Makes sure of asstes that will be uploaded.
this.filteredAssets = this.config.assets.filter(asset => {
let assetUrl = path.join(this.config.dir, asset)
return fs.existsSync(assetUrl)
})
if (this.filteredAssets.length === 0) {
console.log('There are no assets to upload...')
return
}
this.authenticate()
this.getRelease()
},
// Authenticating user token.
authenticate () {
if (!this.config.token) {
throw new Error("Token is not provided")
}
console.log('Authenticating...')
octokit.authenticate({
type: 'token',
token: this.config.token
})
},
// Tries to check whether release is existing or not.
// If it exists, delete it. Otherwise create new one.
getRelease() {
console.log('Getting relesae info...')
octokit.repos.getReleaseByTag({
owner: this.config.owner,
repo: this.config.repo,
tag: this.config.tag
}).then(result => {
// Release is already created.
console.log(this.config.tag + ' is existing, so it will be deleted')
this.release = result.data
this.deleteRelease(result.data.id)
}).catch(e => {
console.log('Unable to get release info...')
if (e.code === 404) {
// Create the release as it does not exist.
this.createRelease('nightly builds', 'nightly builds')
}
else {
throw new Error('Unhandled response for getReleaseByTag: ' + e)
}
})
},
// Deletes release with releaseId, then creates new one.
deleteRelease(releaseId) {
console.log('Deleting release...')
octokit.repos.deleteRelease({
owner: this.config.owner,
repo: this.config.repo,
release_id: releaseId
}).then(result => {
console.log('Release is deleted successfully...')
// Use previous name and body.
let name = this.release.name
let body = this.release.body
// Free release object.
this.release = null
this.createRelease(name, body)
}).catch(e => {
throw new Error('Unhandled response for deleteRelease: ' + e)
})
},
// Creates release with name and body using provided configs, then it calls
// uploadAllAssets.
createRelease(name, body) {
console.log('Creating a new release...')
octokit.repos.createRelease({
owner: this.config.owner,
repo: this.config.repo,
tag_name: this.config.tag,
name: name,
body: body,
target_commitish: this.config.branch,
draft: false,
prerelease: true
}).then(result => {
console.log('Release is created successfully...')
this.release = result.data
this.uploadedAssets = []
this.uploadAllAssets()
}).catch(e => {
throw new Error('Unhandled response for createRelease: ' + e)
})
},
// Gets a list for assets of a release to avoid conflicts while uploading
// new assets by deleting them, then it calls uploadAllAssets.
getAssets(releaseId) {
console.log('Getting assets...')
octokit.repos.getAssets({
owner: this.config.owner,
repo: this.config.repo,
release_id: releaseId,
per_page: 100
}).then(result => {
this.uploadedAssets = result.data.map(asset => {
return { name: asset.name, id: asset.id }
})
this.uploadAsset(0)
}).catch(function(e) {
throw new Error('Unhandled response for getAssets: ' + e)
})
},
// Uploads asset for a release if it's not already uploaded, Otherwise
// calls deleteAsset.
uploadAsset(assetIndex) {
if (assetIndex >= this.filteredAssets.length) {
console.log('Assets uploaded successfully...')
return
}
let asset = this.filteredAssets[assetIndex]
console.log('Uploading ' + asset)
// Check if it's uploaded.
let assetId = this.getAssetId(assetIndex)
if (assetId != -1) {
console.log(asset + ' is existing, so it will be deleted')
// Asset exists, so we need to delete it first.
this.deleteAsset(assetId, assetIndex)
return
}
let assetUrl = path.join(this.config.dir, asset)
octokit.repos.uploadAsset({
url: this.release.upload_url,
file: fs.readFileSync(assetUrl),
contentType: mime.getType(assetUrl),
contentLength: fs.statSync(assetUrl).size,
name: asset,
}).then(result => {
console.log('Uploaded successfully...')
this.uploadAsset(assetIndex + 1)
}).catch(function(e) {
throw new Error('Unhandled response for uploadAsset: ' + e)
})
},
// Deletes old asset with assetId, then it calls uploadAsset to upload new
// one.
deleteAsset(assetId, assetIndex) {
console.log('Deleting ' + this.filteredAssets[assetIndex])
octokit.repos.deleteAsset({
owner: this.config.owner,
repo: this.config.repo,
asset_id: assetId
}).then(result => {
console.log('Deleted successfully...')
this.deleteAssetId(assetId)
this.uploadAsset(assetIndex)
}).catch(function(e) {
throw new Error('Unhandled response for deleteAsset: ' + e)
})
},
// Returns id for asset to be used in deleting it.
getAssetId(index) {
let newAsset = this.filteredAssets[index]
let result = this.uploadedAssets.find(asset => asset.name === newAsset)
if (result && result.id) {
return result.id
}
return -1
},
// Removes asset from uploadedAssets list.
deleteAssetId(assetId) {
this.uploadedAssets =
this.uploadedAssets.filter(asset => asset.id !== assetId)
}
}
// Handles errors.
process.on('unhandledRejection', error => {
console.log('Failed to deploy')
console.log('unhandledRejection', error)
process.exit(1)
})
function getAssetNames(files = []) {
let result = []
files.forEach(pattern => {
result =
result.concat(glob.readdirSync(pattern).map(file => path.basename(file)))
})
return result
}
const assets = [
'./dist/*.deb',
'./dist/*.rpm',
'./dist/*.zip',
'./dist/*.dmg',
'./dist/*.exe'
]
const repoSlug = process.env.TRAVIS_REPO_SLUG
if (repoSlug != 'abahmed/Deer') {
console.log('Deployment is only done for abahmed/Deer')
process.exit()
}
const isPullRequest = process.env.TRAVIS_PULL_REQUEST !== false
if (isPullRequest) {
console.log('Deployment is not done for Pull Requests')
process.exit()
}
const branch = process.env.TRAVIS_BRANCH
if (branch === 'develop') {
NightlyDeploy.init({
owner: 'abahmed',
repo: 'release-test',
branch: branch,
tag: 'nightly',
assets: getAssetNames(assets),
dir:'./dist',
token: process.env.GH_TOKEN
})
}
else {
console.log('No deployments for ' + branch)
}