-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathupload.js
More file actions
104 lines (90 loc) · 2.75 KB
/
upload.js
File metadata and controls
104 lines (90 loc) · 2.75 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
const tiny = require('tiny-json-http')
const { getViewerData } = require('webpack-bundle-analyzer/lib/analyzer')
const { isPlainObject, isEmpty, cloneDeep } = require('lodash')
const omitDeep = require('omit-deep')
const os = require('os')
const logger = require('./logger')
class Upload {
constructor (config) {
this.config = config
}
process (statsJson, outputPath) {
logger('processing upload')
if (statsJson.errors.length) {
logger('halting upload due to stats errors')
statsJson.errors.forEach((e) => logger(`stats error: ${e}`))
return Promise.resolve()
}
// Ensure we're not capturing the source
statsJson = omitDeep(statsJson, ['source'])
logger('filtering out source from stats json')
const payload = {
packer: 'webpack@' + statsJson.version,
commit: this.config.commit,
committed_at: parseInt(this.config.committedAt),
branch: this.config.branch,
author: this.config.author,
message: this.config.message,
prior_commit: this.config.priorCommit,
stats: statsJson,
uploader_hostname: os.hostname(),
bundle: getBundleData(
cloneDeep(statsJson),
outputPath,
this.config.excludeAssets
)
}
return generateUploadUrl(
this.config.host,
this.config.projectToken,
this.config.commit
)
.then(response => {
logger(`upload url generated`)
payload.project_id = response.project_id
return uploadToS3(response.upload_url, payload)
})
.then(() => {
logger('stats uploaded')
})
.catch((error) => {
logger(`stats failed to upload: ${error.message}`)
logger(`this could be because your project token is not properly set`)
if (this.config.failBuild) {
logger('re-throwing failure because `fail_build` set to true')
throw error
}
})
}
}
function getBundleData (statJson, outputPath, excludeAssets = null) {
let data
logger('retrieving javascript bundle data')
try {
data = getViewerData(statJson, outputPath, { excludeAssets })
} catch (err) {
logger(`could not analyze webpack bundle (${err})`)
data = null
}
if (isPlainObject(data) && isEmpty(data)) {
logger('could not find any javascript bundles')
data = null
}
return data
}
function generateUploadUrl (host, projectToken, commitHash) {
logger('generating upload url')
return tiny.post({
url: `${host}/generate-upload-url`,
headers: { 'Accept': 'application/json' },
data: {
project_token: projectToken,
commit_hash: commitHash
}
}).then(response => response.body)
}
function uploadToS3 (url, data) {
logger('uploading to s3')
return tiny.put({ url, data })
}
module.exports = Upload