Skip to content

Commit db86ea3

Browse files
committed
Add getProjects helper
1 parent d9f1c34 commit db86ea3

File tree

3 files changed

+30
-15
lines changed

3 files changed

+30
-15
lines changed

packages/bundler-plugin-core/src/build-plugin-manager.ts

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,12 @@ import {
1919
safeFlushTelemetry,
2020
} from "./sentry/telemetry";
2121
import { Options, SentrySDKBuildFlags } from "./types";
22-
import { arrayify, getTurborepoEnvPassthroughWarning, stripQueryAndHashFromPath } from "./utils";
22+
import {
23+
arrayify,
24+
getProjects,
25+
getTurborepoEnvPassthroughWarning,
26+
stripQueryAndHashFromPath,
27+
} from "./utils";
2328
import { glob } from "glob";
2429
import { defaultRewriteSourcesHook, prepareBundleForDebugIdUpload } from "./debug-id-upload";
2530

@@ -95,7 +100,7 @@ function createCliInstance(options: NormalizedOptions): SentryCli {
95100
authToken: options.authToken,
96101
org: options.org,
97102
// Default to the first project if multiple projects are specified
98-
project: Array.isArray(options.project) ? options.project[0] : options.project,
103+
project: getProjects(options.project)?.[0],
99104
silent: options.silent,
100105
url: options.url,
101106
vcsRemote: options.release.vcsRemote,
@@ -361,12 +366,8 @@ export function createSentryBuildPluginManager(
361366
if (typeof options.moduleMetadata === "function") {
362367
const args = {
363368
org: options.org,
364-
project: Array.isArray(options.project) ? options.project[0] : options.project,
365-
projects: Array.isArray(options.project)
366-
? options.project
367-
: options.project
368-
? [options.project]
369-
: undefined,
369+
project: getProjects(options.project)?.[0],
370+
projects: getProjects(options.project),
370371
release: options.release.name,
371372
};
372373
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
@@ -492,7 +493,7 @@ export function createSentryBuildPluginManager(
492493
dist: options.release.dist,
493494
// @ts-expect-error - projects is not a valid option for uploadSourceMaps but is implemented in the CLI
494495
// Remove once https://github.com/getsentry/sentry-cli/pull/2856 is released
495-
projects: Array.isArray(options.project) ? options.project : [options.project],
496+
projects: getProjects(options.project),
496497
// We want this promise to throw if the sourcemaps fail to upload so that we know about it.
497498
// see: https://github.com/getsentry/sentry-cli/pull/2605
498499
live: "rejectOnError",
@@ -639,7 +640,7 @@ export function createSentryBuildPluginManager(
639640
ignore: ignorePaths,
640641
// @ts-expect-error - projects is not a valid option for uploadSourceMaps but is implemented in the CLI
641642
// Remove once https://github.com/getsentry/sentry-cli/pull/2856 is released
642-
projects: Array.isArray(options.project) ? options.project : [options.project],
643+
projects: getProjects(options.project),
643644
live: "rejectOnError",
644645
});
645646
});
@@ -752,9 +753,7 @@ export function createSentryBuildPluginManager(
752753
],
753754
// @ts-expect-error - projects is not a valid option for uploadSourceMaps but is implemented in the CLI
754755
// Remove once https://github.com/getsentry/sentry-cli/pull/2856 is released
755-
projects: Array.isArray(options.project)
756-
? options.project
757-
: [options.project],
756+
projects: getProjects(options.project),
758757
live: "rejectOnError",
759758
}
760759
);
@@ -863,7 +862,7 @@ function canUploadSourceMaps(
863862
);
864863
return false;
865864
}
866-
if (!options.project || (Array.isArray(options.project) && options.project.length === 0)) {
865+
if (!getProjects(options.project)?.[0]) {
867866
logger.warn(
868867
"No project provided. Will not upload source maps. Please set the `project` option to your Sentry project slug." +
869868
getTurborepoEnvPassthroughWarning("SENTRY_PROJECT")

packages/bundler-plugin-core/src/sentry/telemetry.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { NormalizedOptions, SENTRY_SAAS_URL } from "../options-mapping";
55
import { Scope } from "@sentry/core";
66
import { createStackParser, nodeStackLineParser } from "@sentry/utils";
77
import { makeOptionallyEnabledNodeTransport } from "./transports";
8+
import { getProjects } from "../utils";
89

910
const SENTRY_SAAS_HOSTNAME = "sentry.io";
1011

@@ -129,7 +130,7 @@ export async function allowedToSendTelemetry(options: NormalizedOptions): Promis
129130
url,
130131
authToken,
131132
org,
132-
project: Array.isArray(project) ? project[0] : project,
133+
project: getProjects(project)?.[0],
133134
vcsRemote: release.vcsRemote,
134135
silent,
135136
headers,

packages/bundler-plugin-core/src/utils.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,3 +393,18 @@ export function getTurborepoEnvPassthroughWarning(envVarName: string): string {
393393
? `\nYou seem to be using Turborepo, did you forget to put ${envVarName} in \`passThroughEnv\`? https://turbo.build/repo/docs/reference/configuration#passthroughenv`
394394
: "";
395395
}
396+
397+
/**
398+
* Gets the projects from the project option. This might be a single project or an array of projects.
399+
*/
400+
export function getProjects(project: string | string[] | undefined): string[] | undefined {
401+
if (Array.isArray(project)) {
402+
return project;
403+
}
404+
405+
if (project) {
406+
return [project];
407+
}
408+
409+
return undefined;
410+
}

0 commit comments

Comments
 (0)