Skip to content

Commit

Permalink
chore: update sentryEnvironment
Browse files Browse the repository at this point in the history
  • Loading branch information
bapmrl committed Dec 20, 2023
1 parent 5adf04a commit f3bcd0e
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 31 deletions.
30 changes: 23 additions & 7 deletions packages/init/src/lib/setupSentry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,38 @@ import semver from "semver";

import { SENTRY_EXPRESS_DSN } from "../constants";
import { checkIsSentryEnabled } from "./checkIsSentryEnabled";
import { version } from "../../package.json";
import { name as pkgName, version as pkgVersion } from "../../package.json";

export function setupSentry(): void {
if (!checkIsSentryEnabled()) {
return;
}

const isStableVersion =
semver.prerelease(version) === null && semver.lte("0.1.0", version);
const parsedVersion = semver.parse(pkgVersion);
if (parsedVersion === null) {
throw new Error(
`Package \`${pkgName}\` has an invalid version \`${pkgVersion}\` in its manifest.`,
);
}

let environment;
if (parsedVersion.prerelease.length === 0) {
environment = import.meta.env.MODE || "production";
} else if (
parsedVersion.prerelease[0] === "alpha" ||
parsedVersion.prerelease[0] === "beta"
) {
environment = parsedVersion.prerelease[0];
} else {
throw new Error(
`Invalid package specifier: \`${pkgName}@${parsedVersion.version}\`. The first prerelease component \`${parsedVersion.prerelease[0]}\` must be either \`alpha\` or \`beta\`.`,
);
}

Sentry.init({
dsn: SENTRY_EXPRESS_DSN,
release: version,
environment: isStableVersion
? import.meta.env.MODE || "production"
: "alpha",
release: parsedVersion.version,
environment,
// Increase the default truncation length of 250 to 2500 (x10)
// to have enough details in Sentry
maxValueLength: 2_500,
Expand Down
39 changes: 22 additions & 17 deletions packages/slice-machine/next.config.mjs
Original file line number Diff line number Diff line change
@@ -1,30 +1,35 @@
import * as fs from "node:fs";
import * as path from "node:path";
import * as url from "node:url";
import { withSentryConfig } from "@sentry/nextjs";
import { createVanillaExtractPlugin } from "@vanilla-extract/next-plugin";
import semver from "semver";

const __dirname = path.dirname(url.fileURLToPath(import.meta.url));
import pkg from "./package.json" assert { type: "json" };

/** @type {{ version: string }} */
const pkg = JSON.parse(
fs.readFileSync(path.resolve(__dirname, "package.json"), "utf8"),
);

/** @type string */
const parsedVersion = semver.parse(pkg.version);
if (parsedVersion === null) {
throw new Error(
`Package \`${pkg.name}\` has an invalid version \`${pkg.version}\` in its manifest.`,
);
}

const RELEASE_NUMBER = pkg.version;
const isStableVersion =
/^\d+\.\d+\.\d+$/.test(RELEASE_NUMBER) && semver.lte("0.1.0", RELEASE_NUMBER);
let sentryEnvironment;
if (parsedVersion.prerelease.length === 0) {
sentryEnvironment = process.env.NODE_ENV;
} else if (
parsedVersion.prerelease[0] === "alpha" ||
parsedVersion.prerelease[0] === "beta"
) {
sentryEnvironment = parsedVersion.prerelease[0];
} else {
throw new Error(
`Invalid package specifier: \`${pkg.name}@${parsedVersion.version}\`. The first prerelease component \`${parsedVersion.prerelease[0]}\` must be either \`alpha\` or \`beta\`.`,
);
}

/** @type {import("next").NextConfig} */
let nextConfig = {
experimental: { newNextLinkBehavior: true },
swcMinify: true,
publicRuntimeConfig: {
sentryEnvironment: isStableVersion ? process.env.NODE_ENV : "alpha",
},
publicRuntimeConfig: { sentryEnvironment },
};

if (process.env.NODE_ENV !== "development") {
Expand All @@ -50,7 +55,7 @@ if (process.env.NODE_ENV !== "development") {
// The Sentry webpack plugin always ignores some files when uploading sourcemaps
// We actually need them (because of the static export?) to get the complete trace in Sentry
ignore: [],
release: RELEASE_NUMBER,
release: parsedVersion.version,

configFile: "sentry-next.properties",
};
Expand Down
29 changes: 22 additions & 7 deletions packages/start-slicemachine/src/lib/setupSentry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,31 @@ export const setupSentry = async (
// User is not logged in, it doesn't matter
}

const isStableVersion =
semver.prerelease(sliceMachineVersion) === null &&
semver.lte("0.1.0", sliceMachineVersion);
const parsedSliceMachineVersion = semver.parse(sliceMachineVersion);
if (parsedSliceMachineVersion === null) {
throw new Error(
`Package \`slice-machine-ui\` has an invalid version \`${sliceMachineVersion}\` in its manifest.`,
);
}

let environment;
if (parsedSliceMachineVersion.prerelease.length === 0) {
environment = import.meta.env.MODE || "production";
} else if (
parsedSliceMachineVersion.prerelease[0] === "alpha" ||
parsedSliceMachineVersion.prerelease[0] === "beta"
) {
environment = parsedSliceMachineVersion.prerelease[0];
} else {
throw new Error(
`Invalid package specifier: \`slice-machine-ui@${parsedSliceMachineVersion.version}\`. The first prerelease component \`${parsedSliceMachineVersion.prerelease[0]}\` must be either \`alpha\` or \`beta\`.`,
);
}

Sentry.init({
dsn: SENTRY_EXPRESS_DSN,
release: sliceMachineVersion,
environment: isStableVersion
? import.meta.env.MODE || "production"
: "alpha",
release: parsedSliceMachineVersion.version,
environment,
// Increase the default truncation length of 250 to 2500 (x10)
// to have enough details in Sentry
maxValueLength: 2_500,
Expand Down

0 comments on commit f3bcd0e

Please sign in to comment.