Skip to content

Commit

Permalink
Add fallback in case magicast can't parse/modify the nuxt config
Browse files Browse the repository at this point in the history
  • Loading branch information
andreiborza committed Nov 26, 2024
1 parent 95fcb5e commit 88fe6f6
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 18 deletions.
54 changes: 36 additions & 18 deletions src/nuxt/sdk-setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ import * as Sentry from '@sentry/node';
import chalk from 'chalk';
import fs from 'fs';
// @ts-expect-error - magicast is ESM and TS complains about that. It works though
import { loadFile, generateCode } from 'magicast';
import { loadFile, generateCode, MagicastError } from 'magicast';
// @ts-expect-error - magicast is ESM and TS complains about that. It works though
import { addNuxtModule } from 'magicast/helpers';
import path from 'path';
import {
getConfigBody,
getDefaultNuxtConfig,
getNuxtModuleFallbackTemplate,
getSentryConfigContents,
} from './templates';
import {
Expand Down Expand Up @@ -57,8 +58,6 @@ export async function addSDKModule(
config: string,
options: { org: string; project: string; url: string; selfHosted: boolean },
): Promise<void> {
clack.log.info('Adding Sentry Nuxt Module to Nuxt config.');

try {
const mod = await loadFile(config);

Expand All @@ -69,26 +68,45 @@ export async function addSDKModule(
...(options.selfHosted && { url: options.url }),
},
});
addNuxtModule(mod, '@sentry/nuxt/module', 'sourcemap', { client: 'hidden' });
addNuxtModule(mod, '@sentry/nuxt/module', 'sourcemap', {
client: 'hidden',
});

const { code } = generateCode(mod);

await fs.promises.writeFile(config, code, { encoding: 'utf-8', flag: 'w' });
} catch (e: unknown) {
clack.log.error(
'Error while adding the Sentry Nuxt Module to the Nuxt config.',
);
clack.log.info(
chalk.dim(
typeof e === 'object' && e != null && 'toString' in e
? e.toString()
: typeof e === 'string'
? e
: 'Unknown error',
),

clack.log.success(
`Added Sentry Nuxt Module to ${chalk.cyan(path.basename(config))}.`,
);
Sentry.captureException('Error while setting up the Nuxt SDK');
await abort('Exiting Wizard');
} catch (e: unknown) {
// Cases where users spread options are not covered by magicast,
// so we fall back to showing how to configure the nuxt config
// manually.
if (e instanceof MagicastError) {
clack.log.warn(
`Automatic configuration of ${chalk.cyan(
path.basename(config),
)} failed, please add the following settings:`,
);
// eslint-disable-next-line no-console
console.log(`\n\n${getNuxtModuleFallbackTemplate(options)}\n\n`);
} else {
clack.log.error(
'Error while adding the Sentry Nuxt Module to the Nuxt config.',
);
clack.log.info(
chalk.dim(
typeof e === 'object' && e != null && 'toString' in e
? e.toString()
: typeof e === 'string'
? e
: 'Unknown error',
),
);
Sentry.captureException('Error while setting up the Nuxt SDK');
await abort('Exiting Wizard');
}
}
}

Expand Down
19 changes: 19 additions & 0 deletions src/nuxt/templates.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { getIssueStreamUrl } from '../utils/url';

type SelectedSentryFeatures = {
performance: boolean;
replay: boolean;
Expand All @@ -13,6 +14,24 @@ export default defineNuxtConfig({
`;
}

export function getNuxtModuleFallbackTemplate(options: {
org: string;
project: string;
url: string;
selfHosted: boolean;
}): string {
return ` modules: ["@sentry/nuxt/module"],
sentry: {
sourceMapsUploadOptions: {
org: "${options.org}",
project: "${options.project}",${
options.selfHosted ? `\n url: "${options.url}",` : ''
}
},
},
sourcemap: { client: "hidden" },`;
}

export function getSentryConfigContents(
dsn: string,
config: 'client' | 'server',
Expand Down
23 changes: 23 additions & 0 deletions test/nuxt/templates.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
getDefaultNuxtConfig,
getNuxtModuleFallbackTemplate,
getSentryConfigContents,
} from '../../src/nuxt/templates';

Expand Down Expand Up @@ -202,4 +203,26 @@ describe('Nuxt code templates', () => {
});
});
});

describe('getNuxtModuleFallbackTemplate', () => {
it('generates configuration options for the nuxt config', () => {
const template = getNuxtModuleFallbackTemplate({
org: 'my-org',
project: 'my-project',
url: 'https://sentry.io',
selfHosted: false,
});

expect(template).toMatchInlineSnapshot(`
" modules: ["@sentry/nuxt/module"],
sentry: {
sourceMapsUploadOptions: {
org: "my-org",
project: "my-project",
},
},
sourcemap: { client: "hidden" },"
`);
});
});
});

0 comments on commit 88fe6f6

Please sign in to comment.