Skip to content
This repository has been archived by the owner on Nov 20, 2023. It is now read-only.

Commit

Permalink
feat: Add runOnce flag to allow for skipping multiple uploads with th…
Browse files Browse the repository at this point in the history
…e same config (#270)
  • Loading branch information
kamilogorek authored Apr 9, 2021
1 parent b33bcf7 commit 610f6cd
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 7 deletions.
7 changes: 7 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,13 @@ export interface SentryCliPluginOptions {
*/
finalize?: boolean;

/**
* Determines whether plugin should be applied not more than once during whole webpack run.
* Useful when the process is performing multiple builds using the same config.
* Defaults to `false`.
*/
runOnce?: boolean;

/**
* Attempts a dry run (useful for dev environments).
*/
Expand Down
32 changes: 26 additions & 6 deletions src/__tests__/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,17 @@ const mockCli = {

const SentryCliMock = jest.fn((configFile, options) => mockCli);
const SentryCli = jest.mock('@sentry/cli', () => SentryCliMock);
const SentryCliPlugin = require('../..');

afterEach(() => {
jest.clearAllMocks();
});
let SentryCliPlugin = require('../..');

const defaults = {
debug: false,
finalize: true,
rewrite: true,
};

beforeEach(() => {
jest.clearAllMocks();
});

describe('constructor', () => {
test('uses defaults without options', () => {
const sentryCliPlugin = new SentryCliPlugin();
Expand Down Expand Up @@ -287,6 +286,27 @@ describe('afterEmitHook', () => {
done();
});
});

test('does not skip the release if `runOnce` option is not set and its called more than once', () => {
const sentryCliPlugin = new SentryCliPlugin();
sentryCliPlugin.apply(compiler);
expect(compiler.hooks.afterEmit.tapAsync).toHaveBeenCalledTimes(1);
sentryCliPlugin.apply(compiler);
expect(compiler.hooks.afterEmit.tapAsync).toHaveBeenCalledTimes(2);
});

test('skips the release if `runOnce` option is set and its called more than once', () => {
// Reset the state of a module to verify `runOnce` behavior
jest.resetModules();
SentryCliPlugin = require('../..');
const sentryCliPlugin = new SentryCliPlugin({
runOnce: true,
});
sentryCliPlugin.apply(compiler);
expect(compiler.hooks.afterEmit.tapAsync).toHaveBeenCalledTimes(1);
sentryCliPlugin.apply(compiler);
expect(compiler.hooks.afterEmit.tapAsync).toHaveBeenCalledTimes(1);
});
});

describe('module rule overrides', () => {
Expand Down
16 changes: 15 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ function attachAfterEmitHook(compiler, callback) {
class SentryCliPlugin {
constructor(options = {}) {
const defaults = {
debug: false,
finalize: true,
rewrite: true,
};
Expand Down Expand Up @@ -452,6 +451,21 @@ class SentryCliPlugin {

/** Webpack lifecycle hook to update compiler options. */
apply(compiler) {
/**
* Determines whether plugin should be applied not more than once during whole webpack run.
* Useful when the process is performing multiple builds using the same config.
* It cannot be stored on the instance, as every run is creating a new one.
*/
if (this.options.runOnce && module.alreadyRun) {
if (this.options.debug) {
this.outputDebug(
'`runOnce` option set and plugin already ran. Skipping release.'
);
}
return;
}
module.alreadyRun = true;

const compilerOptions = compiler.options || {};
ensure(compilerOptions, 'module', Object);

Expand Down

0 comments on commit 610f6cd

Please sign in to comment.