This repository has been archived by the owner on Jan 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathrollbar-source-map.js
145 lines (123 loc) · 3.88 KB
/
rollbar-source-map.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
const { promisifyAll } = require('bluebird');
const request = require('request-promise-native');
const fs = promisifyAll(require('fs'));
const glob = require('glob');
const gulp = require('gulp');
const gutil = require('gulp-util');
const { env, getRevision, getConfigFor } = require('./utils');
const BUILD_HASH_WILDCARD = '[hash]';
function injectHashIntoPath(path, hash) {
return path.replace(BUILD_HASH_WILDCARD, hash).replace('%s', hash); // deprecated
}
async function uploadSourceMap(config, rev, callback) {
const sourceMap = fs.createReadStream(config.sourceMapPath);
// curl https://api.rollbar.com/api/1/sourcemap \
// -F access_token=aaaabbbbccccddddeeeeffff00001111 \
// -F version=version_string_here \
// -F minified_url=http://example.com/static/js/example.min.js \
// -F source_map=@static/js/example.min.map
gutil.log(gutil.colors.yellow(env()), 'Uploading source map ...');
try {
const response = await request.post(
{
url: 'https://api.rollbar.com/api/1/sourcemap',
formData: {
access_token: config.accessToken,
version: rev,
minified_url: config.minifiedUrl,
source_map: sourceMap,
},
},
callback,
);
let parsed;
try {
parsed = JSON.parse(response);
} catch (e) {
gutil.log(gutil.colors.red('Cannot parse reponse'));
}
if (parsed && !parsed.err && parsed.result) {
gutil.log(gutil.colors.yellow(env()), 'Source map uploaded.');
gutil.log('File:', config.sourceMapPath);
gutil.log('URL:', config.minifiedUrl);
gutil.log(gutil.colors.green('rev:', rev));
}
} catch (e) {
gutil.log(gutil.colors.red('Uploading failed'), e);
}
}
// Finds corresponding hashed source map file name
function findByHash(config, hash) {
const re = new RegExp(injectHashIntoPath(config.sourceMapPath, hash));
return glob
.sync('./**')
.filter(file => re.test(file))
.map(sourceMapPath => {
// strip relative path characters
const sourceMapPathMatch = sourceMapPath.match(re);
if (sourceMapPathMatch) {
const minifiedUrl = sourceMapPathMatch[0].replace(
re,
injectHashIntoPath(config.minifiedUrl, hash),
);
return {
sourceMapPath,
minifiedUrl,
};
}
})
.filter(Boolean);
}
const resolveConfig = (hashes, config) =>
hashes
.map(hash =>
(config.files || [config]).reduce(
(result, fileConfig) => result.concat(findByHash(fileConfig, hash)),
[],
),
)
.filter(paths => paths.length > 0);
async function uploadAppVersions(config, rev) {
// Detect last build version hashes
const buildLogFiles = glob.sync('./build*.log');
if (buildLogFiles < 1) {
gutil.log('ERROR: No build logs found');
return;
}
const hashes = [
// 'hashone', 'hashtwo', ...
];
// Read build logs and fill up array of hashes
await Promise.all(
buildLogFiles.map(async file => {
try {
const buffer = await fs.readFileAsync(file);
const buildInfo = JSON.parse(buffer);
if (buildInfo) {
hashes.push(buildInfo.hash);
}
} catch (e) {
gutil.log('ERROR: Cannot read', file, e);
}
}),
);
const resolvedSourceMapConfigs = resolveConfig(hashes, config);
// Upload source maps one by one
if (buildLogFiles.length !== resolvedSourceMapConfigs.length) {
gutil.log('ERROR: Could not find source maps for all builds');
} else {
const flattened = resolvedSourceMapConfigs.reduce(
(result, configs) => [...result, ...configs],
[],
);
await Promise.all(
flattened.map(async sourceMapConfig =>
uploadSourceMap(Object.assign({}, config, sourceMapConfig), rev),
),
);
}
}
gulp.task('rollbar-source-map', async () => {
const rev = await getRevision();
await uploadAppVersions(getConfigFor('rollbar'), rev);
});