This repository has been archived by the owner on Jan 12, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
index.js
114 lines (104 loc) · 3.41 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
'use strict'
const glob = require('glob')
const parallel = require('run-parallel-limit')
const path = require('path')
const request = require('./lib/request')
const prepareRequest = require('./lib/prepare-request')
const cleanupTempFiles = require('./lib/cleanup-temp-files')
const validateOptions = require('./lib/options').validateOptions
const applyDefaults = require('./lib/options').applyDefaults
const transformOptions = require('./lib/options').transformOptions
const getAppVersion = require('./lib/get-app-version')
const noopLogger = { debug: () => {}, info: () => {}, warn: () => {}, error: () => {} }
/**
* Triggers the sending of a request.
*
* @param {{options: object, formData: object}}
* @returns {Promise<string>}
*/
function sendRequest (options) {
return new Promise((resolve, reject) => request(options.endpoint, () => prepareRequest(options), () => resolve(options), reject, options))
}
/**
* Upload source maps to Bugsnag.
*
* @param {object} options The options
* @param {object} logger A logger object (optional)
* @param {function} callback The node-style callback (optional)
* @returns {Promise<void>}
*/
function upload (options, logger, callback) {
if (typeof logger === 'function') {
callback = logger
logger = noopLogger
}
if (typeof logger === 'undefined') {
logger = noopLogger
}
const promise = (
Promise.resolve(options)
.then(getAppVersion)
.then(applyDefaults)
.then(validateOptions)
.then(opts => {
options = opts
return options
})
.then(options => !options.directory ? uploadOne(options, logger) : uploadMany(options, logger))
)
if (callback) {
promise
.then(() => callback(null))
.catch(err => callback(err, null))
}
return promise
}
/**
* Uploads a single source map.
*
* @param {object} options The options
* @param {object} logger A logger object (optional)
* @returns {Promise<void>}
*/
function uploadOne (options, logger) {
logger.info(`Uploading (${path.basename(options.sourceMap)})`)
return Promise.resolve(options)
.then(transformOptions)
.then(sendRequest)
.catch(err => cleanupTempFiles(options).then(() => Promise.reject(err)))
.then(cleanupTempFiles)
}
/**
* Searches a directory and uploads multiple source maps.
*
* @param {object} options The options
* @param {object} logger A logger object (optional)
* @returns {Promise<void>}
*/
function uploadMany (options, logger) {
return new Promise((resolve, reject) => {
const pattern = path.join(options.directory !== true ? options.directory : options.projectRoot, '**/*.map')
glob(pattern, { ignore: '**/node_modules/**' }, (err, files) => {
if (err) return reject(err)
logger.info(`Found ${files.length} source map(s) to upload`)
const uploads = files.map(f => {
const minifiedUrl = path.relative(options.projectRoot, f.replace(/\.map$/, ''))
const minifiedFile = f.replace(/\.map$/, '')
return cb => {
const opts = Object.assign({}, options, { sourceMap: f, minifiedUrl: minifiedUrl, minifiedFile: minifiedFile, sources: {} })
uploadOne(opts, logger)
.then(() => {
logger.info(`Upload successful (${path.basename(f)})`)
cb()
})
.catch(cb)
}
})
parallel(uploads, 5, (err) => {
if (err) return reject(err)
resolve()
})
})
})
}
exports.upload = upload