<commit_before>import path from 'path';
import CommonBugsnagPlugin from './helpers/CommonBugsnagPlugin';
import { upload } from 'bugsnag-sourcemaps';

class BugsnagSourceMapPlugin extends CommonBugsnagPlugin {
  constructor({
    apiKey = null,
    publicPath = null,
    appVersion = null,
    overwrite = false,
  }) {
    super();
    this.options = {
      apiKey,
      publicPath,
      appVersion,
      overwrite,
    };
    this.validateOptions();
  }

  apply(compiler) {
    compiler.plugin('after-emit', this.handle.bind(this));
  }

  getSourceMaps(compilation) {
    const sourceMaps = [];

    const stats = compilation.getStats().toJson();
    const outputPath = path.resolve(
      compilation.compiler.options.context,
      compilation.compiler.options.output.path
    );
    let publicPath = this.options.publicPath || stats.publicPath;
    publicPath += /\/$/.test(publicPath) ? '' : '/';

    stats.chunks.forEach(chunk => {
      const [ file ] = chunk.files.filter(file => /\.js$/.test(file));
      const [ sourceMap ] = chunk.files.filter(file => /\.js\.map$/.test(file));

      if (sourceMap) {
        sourceMaps.push({
          url: publicPath + file,
          file: path.resolve(outputPath, file),
          sourceMap: path.resolve(outputPath, sourceMap),
        });
      }
    });

    return sourceMaps;
  }

  uploadSourceMaps(options, sourceMaps) {
    return Promise.all(
      sourceMaps.map(({ url, file, sourceMap }) => (
        upload({
          ...options,
          minifiedUrl: url,
          minifiedFile: file,
          sourceMap: sourceMap,
        })
      ))
    );
  }

  getUploadOptions(compilation) {
    const { apiKey, appVersion, overwrite } = this.options;
    const uploadOptions = { apiKey, appVersion, overwrite };
    if (appVersion) {
      return Promise.resolve(uploadOptions);
    } else {
      return this.getProjectDetails(compilation).then(details => {
        uploadOptions.appVersion = details.version;
        return uploadOptions;
      });
    }
  }

  handle(compilation, callback) {
    const sourceMaps = this.getSourceMaps(compilation);
    this.getUploadOptions(compilation)
      .then(options => this.uploadSourceMaps(options, sourceMaps))
      .catch(this.handleErrors(compilation, callback))
      .then(() => callback());
  }
}

export default BugsnagSourceMapPlugin;<commit_msg>propogate endpoint option to bugsnag plugin<commit_after>import path from 'path';
import CommonBugsnagPlugin from './helpers/CommonBugsnagPlugin';
import { upload } from 'bugsnag-sourcemaps';

class BugsnagSourceMapPlugin extends CommonBugsnagPlugin {
  constructor({
    apiKey = null,
    publicPath = null,
    appVersion = null,
    overwrite = false,
    endpoint = 'https://upload.bugsnag.com',
  }) {
    super();
    this.options = {
      apiKey,
      publicPath,
      appVersion,
      overwrite,
      endpoint,
    };
    this.validateOptions();
  }

  apply(compiler) {
    compiler.plugin('after-emit', this.handle.bind(this));
  }

  getSourceMaps(compilation) {
    const sourceMaps = [];

    const stats = compilation.getStats().toJson();
    const outputPath = path.resolve(
      compilation.compiler.options.context,
      compilation.compiler.options.output.path
    );
    let publicPath = this.options.publicPath || stats.publicPath;
    publicPath += /\/$/.test(publicPath) ? '' : '/';

    stats.chunks.forEach(chunk => {
      const [ file ] = chunk.files.filter(file => /\.js$/.test(file));
      const [ sourceMap ] = chunk.files.filter(file => /\.js\.map$/.test(file));

      if (sourceMap) {
        sourceMaps.push({
          url: publicPath + file,
          file: path.resolve(outputPath, file),
          sourceMap: path.resolve(outputPath, sourceMap),
        });
      }
    });

    return sourceMaps;
  }

  uploadSourceMaps(options, sourceMaps) {
    return Promise.all(
      sourceMaps.map(({ url, file, sourceMap }) => (
        upload({
          ...options,
          minifiedUrl: url,
          minifiedFile: file,
          sourceMap: sourceMap,
        })
      ))
    );
  }

  getUploadOptions(compilation) {
    const { apiKey, appVersion, overwrite, endpoint } = this.options;
    const uploadOptions = { apiKey, appVersion, overwrite, endpoint };
    if (appVersion) {
      return Promise.resolve(uploadOptions);
    } else {
      return this.getProjectDetails(compilation).then(details => {
        uploadOptions.appVersion = details.version;
        return uploadOptions;
      });
    }
  }

  handle(compilation, callback) {
    const sourceMaps = this.getSourceMaps(compilation);
    this.getUploadOptions(compilation)
      .then(options => this.uploadSourceMaps(options, sourceMaps))
      .catch(this.handleErrors(compilation, callback))
      .then(() => callback());
  }
}

export default BugsnagSourceMapPlugin;
{'fraction_non_alphanumeric': 0.1311069882498454, 'fraction_numerical': 0.0, 'mean_word_length': 2.709480122324159, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 17, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7531848', 'n_tokens_mistral': 1461, 'n_tokens_neox': 1431, 'n_words': 267}