forked from bgentry/ember-cli-deploy-rollbar-sourcemap
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
180 lines (151 loc) · 5.67 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
170
171
172
173
174
175
176
177
178
179
180
'use strict';
var RSVP = require('rsvp');
var fs = require('fs');
var path = require('path');
var request = require('request-promise');
var zlib = require('zlib');
var BasePlugin = require('ember-cli-deploy-plugin');
module.exports = {
name: require('./package').name,
createDeployPlugin: function(options) {
var DeployPlugin = BasePlugin.extend({
name: options.name,
defaultConfig: Object.freeze({
distDir: function(context) {
return context.distDir || '';
},
distFiles: function(context) {
return context.distFiles || [];
},
gzippedFiles: function(context) {
return context.gzippedFiles || [];
},
revisionKey: function(context) {
if (context.revisionData) {
return context.revisionData.revisionKey;
} else {
return process.env.SOURCE_VERSION || '';
}
},
environment: function(context) {
var HoneybadgerConfig = context.config["honeybadger-sourcemap"].honeybadgerConfig;
var buildConfig = context.config.build;
var environment = HoneybadgerConfig ? HoneybadgerConfig.environment : false;
return environment || buildConfig.environment || 'production';
},
additionalFiles: [],
}),
requiredConfig: Object.freeze(['apiKey', 'publicUrl']),
upload: function() {
var log = this.log.bind(this);
var distDir = this.readConfig('distDir');
var distFiles = this.readConfig('distFiles');
var apiKey = this.readConfig('apiKey');
var revisionKey = this.readConfig('revisionKey');
log('Uploading sourcemaps to Honeybadger', { verbose: true });
var publicUrl = this.readConfig('publicUrl');
var promiseArray = [];
var jsMapPairs = fetchJSMapPairs(distFiles, publicUrl, distDir);
for (var i = 0; i < jsMapPairs.length; i++) {
var mapFilePath = jsMapPairs[i].mapFile;
var jsUrl = jsMapPairs[i].jsFile;
var minifiedFilePath = jsMapPairs[i].minifiedFile;
var formData = {
api_key: apiKey,
minified_url: jsUrl,
source_map: this._readSourceMap(mapFilePath),
revision: revisionKey,
minified_file: this._readSourceMap(minifiedFilePath),
};
log(`Uploading sourcemap to Honeybadger: version=${revisionKey} minified_url=${jsUrl}`, { verbose: true });
var promise = request({
uri: 'https://api.honeybadger.io/v1/source_maps',
method: 'POST',
formData: formData
});
promiseArray.push(promise);
}
return RSVP.all(promiseArray)
.then(function() {
log('Finished uploading sourcemaps', { verbose: true });
});
},
didDeploy: function(context) {
var didDeployHook = this.readConfig('didDeploy');
if (didDeployHook) {
return didDeployHook.call(this, context);
}
var apiKey = this.readConfig('apiKey');
var environment = this.readConfig('environment');
var revision = this.readConfig('revisionKey');
var username = this.readConfig('username');
var formData = {
api_key: apiKey,
"deploy[environment]": environment,
"deploy[revision]": revision,
};
if (username) {
formData.local_username = username;
}
return request({
uri: 'https://api.honeybadger.io/v1/deploys',
method: 'POST',
formData: formData
});
},
_readSourceMap(mapFilePath) {
var relativeMapFilePath = mapFilePath.replace(this.readConfig('distDir') + '/', '');
if (this.readConfig('gzippedFiles').indexOf(relativeMapFilePath) !== -1) {
// When the source map is gzipped, we need to eagerly load it into a buffer
// so that the actual content length is known.
return {
value: zlib.unzipSync(fs.readFileSync(mapFilePath)),
options: {
filename: path.basename(mapFilePath),
}
};
} else {
return fs.createReadStream(mapFilePath);
}
}
});
return new DeployPlugin();
},
};
function fetchJSMapPairs(distFiles, publicUrl, deployDistPath) {
var jsFiles = indexByBaseFilename(fetchFilePaths(distFiles, '', 'js'));
return fetchFilePaths(distFiles, '', 'map').map(function(mapFile) {
var baseFileName = mapFile.replace(/\.map$/, '');
// depending on the version of ember-cli, baseFileName may either
// be in the pattern /assets/some-app-{fingerprint} or just /assets/some-app
var baseHyphenTokens = baseFileName.split('-');
if (!jsFiles[baseFileName] && baseHyphenTokens.length > 1) {
baseFileName = baseHyphenTokens.slice(0, -1).join('-');
}
if (!jsFiles[baseFileName]) {
throw new Error("[ember-cli-deploy-honeybadger-sourcemap] jsFile not found: " + baseFileName + " (jsFiles: " + Object.keys(jsFiles).join(',') + ")");
}
return {
mapFile: deployDistPath + mapFile,
jsFile: publicUrl + jsFiles[baseFileName],
minifiedFile: deployDistPath + jsFiles[baseFileName],
};
});
}
function indexByBaseFilename(files) {
return files.reduce(function(result, file) {
result[getBaseFilename(file)] = file;
return result;
}, {});
}
function getBaseFilename(file) {
return file.replace(/(-[0-9a-f]+)?\.(js|map)$/, '');
}
function fetchFilePaths(distFiles, basePath, type) {
return distFiles.filter(function(filePath) {
return new RegExp('assets/.*\\.' + type + '$').test(filePath);
})
.map(function(filePath) {
return basePath + '/' + filePath;
});
}