Skip to content

Commit

Permalink
add insertion/file creation/fallback flow
Browse files Browse the repository at this point in the history
  • Loading branch information
Lms24 committed Sep 13, 2023
1 parent b6a859c commit fa4a016
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 6 deletions.
52 changes: 47 additions & 5 deletions src/sourcemaps/tools/tsc.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
import * as fs from 'fs';
import * as path from 'path';

import * as recast from 'recast';

import * as Sentry from '@sentry/node';

// @ts-ignore - clack is ESM and TS complains about that. It works though
import * as clack from '@clack/prompts';

import {
askForToolConfigPath,
createNewConfigFile,
makeCodeSnippet,
showCopyPasteInstructions,
} from '../../utils/clack-utils';
import {
findFile,
getOrSetObjectProperty,
parseJsonC,
printJsonC,
Expand All @@ -33,13 +42,43 @@ const getCodeSnippet = (colors: boolean) =>
);

export async function configureTscSourcemapGenerationFlow(): Promise<void> {
await showCopyPasteInstructions(
'tsconfig.json',
getCodeSnippet(true),
'This ensures that source maps are generated correctly',
);
const tsConfigPath =
findFile(path.join(process.cwd(), 'tsconfig'), ['.json']) ??
(await askForToolConfigPath('TypeScript', 'tsconfig.json'));

let successfullyAdded = false;
if (tsConfigPath) {
successfullyAdded = await enableSourcemaps(tsConfigPath);
} else {
successfullyAdded = await createNewConfigFile(
path.join(process.cwd(), 'tsconfig.json'),
getCodeSnippet(false),
);
Sentry.setTag('created-new-config', successfullyAdded ? 'success' : 'fail');
}

if (successfullyAdded) {
Sentry.setTag('ast-mod', 'success');
clack.log.info(
`We recommend checking the ${
tsConfigPath ? 'modified' : 'added'
} file after the wizard finished to ensure it works with your build setup.`,
);
} else {
Sentry.setTag('ast-mod', 'fail');
await showCopyPasteInstructions(
'tsconfig.json',
getCodeSnippet(true),
'This ensures that source maps are generated correctly',
);
}
}

/**
* Modifies tsconfig.json (@param tsConfigPath) to enable source maps generation.
*
* Exported only for testing
*/
export async function enableSourcemaps(tsConfigPath: string): Promise<boolean> {
try {
const tsConfig = await fs.promises.readFile(tsConfigPath, 'utf-8');
Expand All @@ -48,6 +87,7 @@ export async function enableSourcemaps(tsConfigPath: string): Promise<boolean> {

if (!jsonObject || !ast) {
// this will only happen if the input file isn't valid JSON-C
Sentry.setTag('ast-mod-fail-reason', 'original-file-invalid');
return false;
}

Expand All @@ -61,6 +101,7 @@ export async function enableSourcemaps(tsConfigPath: string): Promise<boolean> {

if (!compilerOptionsObj || compilerOptionsObj.type !== 'ObjectExpression') {
// a valid compilerOptions prop should always be an object expression
Sentry.setTag('ast-mod-fail-reason', 'original-file-invalid');
return false;
}

Expand Down Expand Up @@ -89,6 +130,7 @@ export async function enableSourcemaps(tsConfigPath: string): Promise<boolean> {
return true;
} catch (e) {
debug(e);
Sentry.setTag('ast-mod-fail-reason', 'insertion-fail');
return false;
}
}
2 changes: 1 addition & 1 deletion src/utils/clack-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -826,7 +826,7 @@ export async function askForToolConfigPath(
clack.confirm({
message: `Do you have a ${toolName} config file (e.g. ${chalk.cyan(
configFileName,
)}?`,
)})?`,
initialValue: true,
}),
);
Expand Down

0 comments on commit fa4a016

Please sign in to comment.