-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
100 lines (84 loc) · 2.88 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
'use strict';
var through = require('through2');
var path = require('path');
var File = require('vinyl');
var log = require('fancy-log');
var PluginError = require('plugin-error');
var requestRetry = require('requestretry');
var PLUGIN_NAME = 'gulp-rollbar';
var API_URL = 'https://api.rollbar.com/api/1/sourcemap';
/**
* Upload the sourcemap file to Rollbar on build (pre-deploy).
*
* @param {Object} options Rollbar settings such as `accessToken`, `version`, etc.
* @see https://rollbar.com/docs/source-maps/
*/
function rollbar(options) {
options = options || {};
if (!options.accessToken) {
throw new PluginError(PLUGIN_NAME, 'Missing `accessToken`!');
}
if (!options.version) {
throw new PluginError(PLUGIN_NAME, 'Missing `version`!');
}
if (!options.sourceMappingURLPrefix) {
options.sourceMappingURLPrefix = '';
}
function postSourcemap(file, encoding, callback) {
/*jshint validthis:true, camelcase:false*/
if (file.isNull() || !file.sourceMap) {
return callback(null, file);
}
if (file.isStream()) {
return callback(new Error(PLUGIN_NAME + '-write: Streaming not supported'));
}
var sourceMap = file.sourceMap;
// fix paths if Windows style paths
sourceMap.file = unixStylePath(file.relative);
sourceMap.sources = sourceMap.sources.map(function (filePath) {
return unixStylePath(filePath);
});
var formData = {
access_token: options.accessToken,
version: options.version,
minified_url: [options.sourceMappingURLPrefix, sourceMap.file].join('/'),
source_map: {
value: new Buffer(JSON.stringify(sourceMap)),
options: {
filename: [sourceMap.file, 'map'].join('.'),
contentType: 'application/octet-stream'
}
}
};
function retryStrategyWithLog(err, response) {
// Uses default strategy but use custom strategy to trigger logs.
if (err || response.statusCode != 200) {
log('Retrying failed rollbar sourcemap upload: ' + err);
}
return requestRetry.RetryStrategies.HTTPOrNetworkError(err, response);
}
requestRetry({
url: API_URL,
method: 'POST',
formData: formData,
maxAttempts: 10,
retryStrategy: retryStrategyWithLog
}, function (err, httpResponse, body) {
if (err) {
throw new PluginError(PLUGIN_NAME, err, {showStack: true});
}
if (httpResponse.statusCode === 200) {
log(formData.source_map.options.filename + ' uploaded successfully to Rollbar');
} else {
var message = JSON.parse(body).message;
log('Failed to upload sourcemap file to Rollbar. Server responded', httpResponse.statusCode, 'with error `' + message + '`');
}
callback(null, file);
});
}
return through.obj(postSourcemap);
}
function unixStylePath(filePath) {
return filePath.split(path.sep).join('/');
}
module.exports = rollbar;