Skip to content

Instantly share code, notes, and snippets.

@rhuanbarreto
Created November 25, 2020 13:55
Show Gist options
  • Save rhuanbarreto/eafdc8b835f7b0a438ea629084b91313 to your computer and use it in GitHub Desktop.
Save rhuanbarreto/eafdc8b835f7b0a438ea629084b91313 to your computer and use it in GitHub Desktop.
Script to Upload Source and Source Maps to Bugsnag
/**
* # Instructions
* Run `BUGSNAG_KEY=vnjekfnvjkfeve APP_VERSION=1.0.0 PUBLIC_URL=https://example.com node upload_sourcemaps.js`
* to upload all sources to Bugsnag. This is very useful if your source is behind a firewall and Bugnsag can't access it
*/
const path = require("path");
const upload = require("bugsnag-sourcemaps").upload;
const walkSync = require("walk-sync");
function BugsnagReactAppUploader(
apiKey,
appVersion,
urlBasePath,
debug = false
) {
this.config = (filename) => {
let baseConfig = {
apiKey,
overwrite: true,
sourceMap: path.resolve(__dirname, `${filename}.map`),
minifiedFile: path.resolve(__dirname, filename),
};
if (appVersion) baseConfig.appVersion = appVersion;
if (urlBasePath)
baseConfig.minifiedUrl =
urlBasePath + "/" + filename.replace("build/", "");
if (debug) console.log({ baseConfig });
return baseConfig;
};
this.uploadFile = (filename) =>
new upload(this.config(filename))
.then(() => {
console.log(`File ${filename} uploaded successfully`);
return true;
})
.catch((err) => {
console.error(`Failed to upload file ${filename}`, { err });
process.exit(1);
});
this.upload = () => {
// Run through the files in build folder
const paths = walkSync("./", { globs: ["build/static/js/*.js"] });
// Do a uploadFile for eachFile
return Promise.all(paths.map((e) => this.uploadFile(e)));
};
}
new BugsnagReactAppUploader(
process.env.BUGSNAG_KEY,
process.env.APP_VERSION,
process.env.PUBLIC_URL
)
.upload()
.then(
(arr) => console.log(`${arr.length} files uploaded successfully`),
() => console.error("Failed to upload files")
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment