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

Commit 9bb9448

Browse files
authored
feat(options): Allow include to be an object (#299)
This mirrors the change in getsentry/sentry-cli#1001, which allows for path-specific options to be passed as part of the `include` option value. Specifically, this: - Fixes normalization of the `include` option to account for the new possibility of it being an object, and of the `ignore` option within that object - Adds tests for the normalization - Updates the README to reflect the new possibility - Fixes a bug in the normalization function `toArray()` so that falsy (but defined) values aren't ignored. We don't actually ever use it in a way which would make this an issue, but it seemed silly to leave the bug in place when the fix was so simple.
1 parent 830cb17 commit 9bb9448

File tree

3 files changed

+55
-6
lines changed

3 files changed

+55
-6
lines changed

README.md

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ Also, check the [example](example) directory.
6060

6161
| Option | Type | Required | Description |
6262
| ------------------ | ----------------------------------------------------------------------------------- | -------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
63-
| include | `string`/`array` | required | One or more paths that Sentry CLI should scan recursively for sources. It will upload all `.map` files and match associated `.js` files. |
63+
| include | `string`/`array`/`object` | required | One or more paths that Sentry CLI should scan recursively for sources. It will upload all `.map` files and match associated `.js` files. Can be given as an object with path-specific options. See [table below](#include) for details. |
6464
| org | `string` | optional | The slug of the Sentry organization associated with the app. |
6565
| project | `string` | optional | The slug of the Sentry project associated with the app. |
6666
| authToken | `string` | optional | The authentication token to use for all communication with Sentry. Can be obtained from https://sentry.io/settings/account/api/auth-tokens/. Required scopes: `project:releases` (and `org:read` if `setCommits` option is used). |
@@ -89,6 +89,21 @@ Also, check the [example](example) directory.
8989
| setCommits | `Object` | optional | Adds commits to Sentry. See [table below](#setCommits) for details. |
9090
| deploy | `Object` | optional | Creates a new release deployment in Sentry. See [table below](#deploy) for details. |
9191

92+
#### <a name="include"></a>options.include:
93+
94+
| Option | Type | Required | Description |
95+
| ------------------ | ---------------- | -------- | ---------------------------------------------- |
96+
| paths | `array` | required | One or more paths to scan for files to upload. |
97+
| ignoreFile | `string` | optional | See above. |
98+
| ignore | `string`/`array` | optional | See above. |
99+
| ext | `array` | optional | See above. |
100+
| urlPrefix | `string` | optional | See above. |
101+
| urlSuffix | `string` | optional | See above. |
102+
| stripPrefix | `array` | optional | See above. |
103+
| stripCommonPrefix | `boolean` | optional | See above. |
104+
| sourceMapReference | `boolean` | optional | See above. |
105+
| rewrite | `boolean` | optional | See above. |
106+
92107
#### <a name="setCommits"></a>options.setCommits:
93108

94109
| Option | Type | Required | Description |

src/__tests__/index.spec.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,26 @@ describe('constructor', () => {
6767
expect(sentryCliPlugin.options.include).toEqual(['foo']);
6868
expect(sentryCliPlugin.options.ignore).toEqual(['bar']);
6969
});
70+
71+
test('keeps object `ignore` option', () => {
72+
const sentryCliPlugin = new SentryCliPlugin({
73+
include: { paths: ['foo'], urlPrefix: '~/bar/' },
74+
});
75+
expect(sentryCliPlugin.options.include).toEqual({
76+
paths: ['foo'],
77+
urlPrefix: '~/bar/',
78+
});
79+
});
80+
81+
test('sanitizes `ignore` array option in object `ignore` option', () => {
82+
const sentryCliPlugin = new SentryCliPlugin({
83+
include: { paths: ['foo'], ignore: 'bar' },
84+
});
85+
expect(sentryCliPlugin.options.include).toEqual({
86+
paths: ['foo'],
87+
ignore: ['bar'],
88+
});
89+
});
7090
});
7191

7292
describe('CLI configuration', () => {

src/index.js

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,18 @@ function getLoaderName(entry) {
5151
}
5252

5353
/**
54-
* Ensures that the passed value is in an array or an array itself.
54+
* Wraps the given value in an array if it is not already an array itself.
55+
* Ignores `undefined` and `null`, returning them as is.
5556
*
56-
* @param {any} value Either an array or a value that should be wrapped
57-
* @returns {array} The array
57+
* @param {any} value Either an array or a value that should be wrapped in an array
58+
* @returns {array} The resulting array, or the original value if it's null/undefined
5859
*/
5960
function toArray(value) {
60-
return !value || Array.isArray(value) ? value : [value];
61+
if (Array.isArray(value) || value === null || value === undefined) {
62+
return value;
63+
}
64+
65+
return [value];
6166
}
6267

6368
/** Backwards compatible version of `compiler.plugin.afterEmit.tapAsync()`. */
@@ -78,7 +83,16 @@ class SentryCliPlugin {
7883

7984
this.options = Object.assign({}, defaults, options);
8085

81-
if (options.include) this.options.include = toArray(options.include);
86+
// the webpack plugin has looser type requirements than `@sentry/cli` -
87+
// ensure `include` and `ignore` options are in the right format
88+
if (options.include) {
89+
if (typeof options.include === 'string') {
90+
this.options.include = toArray(options.include);
91+
}
92+
if (typeof options.include === 'object') {
93+
this.options.include.ignore = toArray(options.include.ignore);
94+
}
95+
}
8296
if (options.ignore) this.options.ignore = toArray(options.ignore);
8397

8498
this.cli = this.getSentryCli();

0 commit comments

Comments
 (0)