Skip to content

Instantly share code, notes, and snippets.

@nuc
Last active March 27, 2019 06:12
Show Gist options
  • Star 8 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nuc/ed956a040b89764c9c3944f4e5a52e15 to your computer and use it in GitHub Desktop.
Save nuc/ed956a040b89764c9c3944f4e5a52e15 to your computer and use it in GitHub Desktop.
Upload sourcemaps to Sentry
/* eslint-disable no-sync */
const request = require('superagent')
const path = require('path')
const fs = require('fs')
const ACCOUNT = 'account'
const PROJECT = 'project-name'
const TOKEN = 'your-token'
const ASSET_URL = 'https://example.com/assets'
const SOURCEMAPS_PATH = './temp'
const LOCAL_ASSET_PATH = './dist/static'
const currentVersion = () => {
const pf = path.resolve(__dirname, './package.json')
const { version } = JSON.parse(fs.readFileSync(pf, 'utf8'))
return version
}
const getSourceMapFileName = () => {
const pf = path.resolve(__dirname, `${LOCAL_ASSET_PATH}/stats.json`)
const {
assetsByChunkName: { main }
} = JSON.parse(fs.readFileSync(pf, 'utf8'))
const jsSourceMaps = main.filter(filename => /\.js\.map/.test(filename))
return jsSourceMaps[0]
}
const uploadSourceMap = (filename, version) => {
console.log(`Uploading sourcemap: ${filename} for ${version}`)
const sourceMapPath = path.resolve(
__dirname, `${SOURCEMAPS_PATH}/${filename}`
)
request
.post(
`https://app.getsentry.com/api/0/projects/${ACCOUNT}/${PROJECT}/releases/${version}/files/` //eslint-disable-line max-len
)
.set({
'Accept': 'application/json',
'Authorization': `Bearer ${TOKEN}`,
'Content-Type': 'application/json'
})
.attach('file', sourceMapPath)
.field('name', `${ASSET_URL}/${filename}`)
.end((err, res) => {
if (err) {
console.error(`Error uploading sourcemap: ${err}`)
} else {
switch (res.statusCode) {
case 201: // created
console.log('Sourcemap uploaded!')
break
default:
console.error(`Error uploading sourcemap: ${res.statusCode}`)
break
}
}
})
}
const createRelease = (version, filename) => {
console.log(`Creating release '${version}'...`)
request
.post(
`https://app.getsentry.com/api/0/projects/${ACCOUNT}/${PROJECT}/releases/` //eslint-disable-line max-len
)
.set({
'Accept': 'application/json',
'Authorization': `Bearer ${TOKEN}`,
'Content-Type': 'application/json'
})
.send({ version })
.end((err, res) => {
if (err) {
console.error(`Error creating release ${err}`)
} else {
switch (res.statusCode) {
case 400: // already exists
case 200: // all good
console.log('Release already exists.')
uploadSourceMap(filename, version)
break
case 201: // created
console.log('Release created')
uploadSourceMap(filename, version)
break
default:
console.error(`Error creating release: ${res.statusCode}`)
break
}
}
})
}
const version = currentVersion()
const filename = getSourceMapFileName()
createRelease(version, filename)
const StatsWriterPlugin = require('webpack-stats-plugin').StatsWriterPlugin
module.exports = {
devtool: 'source-map',
output: {
path: path.join(__dirname, 'dist/static'),
filename: '[name].[hash].js',
sourceMapFilename: '../temp/[file].map',
publicPath: '/assets/'
},
plugins: [
new StatsWriterPlugin({
filename: 'stats.json' // Default
}),
]
}
@jzabala
Copy link

jzabala commented Jun 30, 2018

Thanks.

@ZhaZhengRefn
Copy link

Thanks 2.
It helps a lot.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment