From 000d96a1345ba3794b33f4590baeb2780e8d54ba Mon Sep 17 00:00:00 2001 From: Lukas Stracke Date: Fri, 3 Nov 2023 09:40:46 +0100 Subject: [PATCH] fix(sourcemaps): Re-read package.json after CLI install (#489) --- CHANGELOG.md | 1 + src/remix/sdk-setup.ts | 11 +++-------- src/sourcemaps/tools/sentry-cli.ts | 9 +++------ src/utils/clack-utils.ts | 6 ++++++ 4 files changed, 13 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f0d4921c..2f4ec5e1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## Unreleased +- fix(sourcemaps): Re-read package.json after CLI install (#489) - fix(nextjs): Set created test route handler to always be dynamic (#486) ## 3.16.1 diff --git a/src/remix/sdk-setup.ts b/src/remix/sdk-setup.ts index ac1fdd89..aa01fede 100644 --- a/src/remix/sdk-setup.ts +++ b/src/remix/sdk-setup.ts @@ -21,6 +21,7 @@ import { getInitCallInsertionIndex, hasSentryContent } from './utils'; import { instrumentRootRouteV1 } from './codemods/root-v1'; import { instrumentRootRouteV2 } from './codemods/root-v2'; import { instrumentHandleError } from './codemods/handle-error'; +import { getPackageDotJson } from '../utils/clack-utils'; export type PartialRemixConfig = { unstable_dev?: boolean; @@ -166,13 +167,7 @@ export async function updateBuildScript(args: { url?: string; isHydrogen: boolean; }): Promise { - /* eslint-disable @typescript-eslint/no-unsafe-member-access */ - // Add sourcemaps option to build script - const packageJsonPath = path.join(process.cwd(), 'package.json'); - const packageJsonString = ( - await fs.promises.readFile(packageJsonPath) - ).toString(); - const packageJson = JSON.parse(packageJsonString); + const packageJson = await getPackageDotJson(); if (!packageJson.scripts) { packageJson.scripts = {}; @@ -200,7 +195,7 @@ export async function updateBuildScript(args: { } await fs.promises.writeFile( - packageJsonPath, + path.join(process.cwd(), 'package.json'), JSON.stringify(packageJson, null, 2), ); diff --git a/src/sourcemaps/tools/sentry-cli.ts b/src/sourcemaps/tools/sentry-cli.ts index b8307f2c..2d06bb17 100644 --- a/src/sourcemaps/tools/sentry-cli.ts +++ b/src/sourcemaps/tools/sentry-cli.ts @@ -78,11 +78,7 @@ export async function configureSentryCLI( await configureSourcemapGenerationFlow(); - await createAndAddNpmScript( - packageDotJson, - options, - relativePosixArtifactPath, - ); + await createAndAddNpmScript(options, relativePosixArtifactPath); if (await askShouldAddToBuildCommand()) { await traceStep('sentry-cli-add-to-build-cmd', () => @@ -135,7 +131,6 @@ export async function setupNpmScriptInCI(): Promise { } async function createAndAddNpmScript( - packageDotJson: PackageDotJson, options: SourceMapUploadToolConfigurationOptions, relativePosixArtifactPath: string, ): Promise { @@ -149,6 +144,8 @@ async function createAndAddNpmScript( options.projectSlug } ${relativePosixArtifactPath}`; + const packageDotJson = await getPackageDotJson(); + packageDotJson.scripts = packageDotJson.scripts || {}; packageDotJson.scripts[SENTRY_NPM_SCRIPT_NAME] = sentryCliNpmScript; diff --git a/src/utils/clack-utils.ts b/src/utils/clack-utils.ts index 0041b05f..e92d5730 100644 --- a/src/utils/clack-utils.ts +++ b/src/utils/clack-utils.ts @@ -340,6 +340,12 @@ Or setup using ${chalk.cyan( }); } +/** + * Installs or updates a package with the user's package manager. + * + * IMPORTANT: This function modifies the `package.json`! Be sure to re-read + * it if you make additional modifications to it after calling this function! + */ export async function installPackage({ packageName, alreadyInstalled,