Skip to content

Commit

Permalink
feat(nextjs): Detect typescript usage and emit files accordingly (#580)
Browse files Browse the repository at this point in the history
Co-authored-by: Luca Forstner <[email protected]>
  • Loading branch information
michael-harvey and lforst authored Jun 17, 2024
1 parent e65b18f commit f133174
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Unreleased

- feat(nextjs): Detect typescript usage and emit files accordingly (#580)
- fix(step-wizards): Show correct URL when prompting DSN (#577)
- feat(electron): Update code examples for v5 (#591)

Expand Down
38 changes: 29 additions & 9 deletions src/nextjs/nextjs-wizard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ export async function runNextjsWizardWithTelemetry(
telemetryEnabled: options.telemetryEnabled,
});

const typeScriptDetected = isUsingTypeScript();

await confirmContinueIfNoOrDirtyGitRepo();

const packageJson = await getPackageDotJson();
Expand Down Expand Up @@ -236,15 +238,19 @@ export async function runNextjsWizardWithTelemetry(
: undefined;

if (!globalErrorPageFile) {
const newGlobalErrorFileName = `global-error.${
typeScriptDetected ? 'tsx' : 'jsx'
}`;

await fs.promises.writeFile(
path.join(process.cwd(), ...appDirLocation, 'global-error.jsx'),
path.join(process.cwd(), ...appDirLocation, newGlobalErrorFileName),
getSentryDefaultGlobalErrorPage(),
{ encoding: 'utf8', flag: 'w' },
);

clack.log.success(
`Created ${chalk.cyan(
path.join(...appDirLocation, 'global-error.jsx'),
path.join(...appDirLocation, newGlobalErrorFileName),
)}.`,
);
} else {
Expand Down Expand Up @@ -417,11 +423,14 @@ async function createOrMergeNextJsFiles(
}

if (instrumentationHookLocation === 'does-not-exist') {
const newInstrumentationFileName = `instrumentation.${
typeScriptDetected ? 'ts' : 'js'
}`;
const srcFolderExists = fs.existsSync(path.join(process.cwd(), 'src'));

const instrumentationHookPath = srcFolderExists
? path.join(process.cwd(), 'src', 'instrumentation.ts')
: path.join(process.cwd(), 'instrumentation.ts');
? path.join(process.cwd(), 'src', newInstrumentationFileName)
: path.join(process.cwd(), newInstrumentationFileName);

const successfullyCreated = await createNewConfigFile(
instrumentationHookPath,
Expand All @@ -430,7 +439,7 @@ async function createOrMergeNextJsFiles(

if (!successfullyCreated) {
await showCopyPasteInstructions(
'instrumentation.ts',
newInstrumentationFileName,
getInstrumentationHookCopyPasteSnippet(
srcFolderExists ? 'src' : 'root',
),
Expand Down Expand Up @@ -630,6 +639,8 @@ async function createExamplePage(
const maybeAppDirPath = path.join(process.cwd(), 'app');
const maybeSrcAppDirPath = path.join(srcDir, 'app');

const typeScriptDetected = isUsingTypeScript();

let pagesLocation =
fs.existsSync(maybePagesDirPath) &&
fs.lstatSync(maybePagesDirPath).isDirectory()
Expand Down Expand Up @@ -676,20 +687,22 @@ async function createExamplePage(
},
);

const newPageFileName = `page.${typeScriptDetected ? 'tsx' : 'jsx'}`;

await fs.promises.writeFile(
path.join(
process.cwd(),
...appLocation,
'sentry-example-page',
'page.jsx',
newPageFileName,
),
examplePageContents,
{ encoding: 'utf8', flag: 'w' },
);

clack.log.success(
`Created ${chalk.cyan(
path.join(...appLocation, 'sentry-example-page', 'page.jsx'),
path.join(...appLocation, 'sentry-example-page', newPageFileName),
)}.`,
);

Expand All @@ -700,21 +713,28 @@ async function createExamplePage(
},
);

const newRouteFileName = `route.${typeScriptDetected ? 'ts' : 'js'}`;

await fs.promises.writeFile(
path.join(
process.cwd(),
...appLocation,
'api',
'sentry-example-api',
'route.js',
newRouteFileName,
),
getSentryExampleAppDirApiRoute(),
{ encoding: 'utf8', flag: 'w' },
);

clack.log.success(
`Created ${chalk.cyan(
path.join(...appLocation, 'api', 'sentry-example-api', 'route.js'),
path.join(
...appLocation,
'api',
'sentry-example-api',
newRouteFileName,
),
)}.`,
);
} else if (pagesLocation) {
Expand Down

0 comments on commit f133174

Please sign in to comment.