Skip to content

Commit

Permalink
feat: Improve error logging for package installation (#635)
Browse files Browse the repository at this point in the history
  • Loading branch information
lforst authored Aug 1, 2024
1 parent 420042a commit 5febecc
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 12 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

- feat(nextjs): Support all `next.config` file types (#630)
- fix(nextjs): Update instrumentation and example creation logic for app or pages usage (#629)
- Improve error logging for package installation (#635)


## 3.25.2

Expand Down
29 changes: 26 additions & 3 deletions src/utils/clack-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import { traceStep } from '../telemetry';
import {
detectPackageManger,
PackageManager,
installPackageWithPackageManager,
packageManagers,
} from './package-manager';
import { debug } from './debug';
Expand Down Expand Up @@ -383,15 +382,39 @@ export async function installPackage({
);

try {
await installPackageWithPackageManager(packageManager, packageName);
await new Promise<void>((resolve, reject) => {
childProcess.exec(
`${packageManager.installCommand} ${packageName} ${packageManager.flags}`,
(err, stdout, stderr) => {
if (err) {
// Write a log file so we can better troubleshoot issues
fs.writeFileSync(
path.join(
process.cwd(),
`sentry-wizard-installation-error-${Date.now()}.log`,
),
JSON.stringify({
stdout,
stderr,
}),
{ encoding: 'utf8' },
);

reject(err);
} else {
resolve();
}
},
);
});
} catch (e) {
sdkInstallSpinner.stop('Installation failed.');
clack.log.error(
`${chalk.red(
'Encountered the following error during installation:',
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
)}\n\n${e}\n\n${chalk.dim(
'If you think this issue is caused by the Sentry wizard, let us know here:\nhttps://github.com/getsentry/sentry-wizard/issues',
"The wizard has created a `sentry-wizard-installation-error-*.log` file. If you think this issue is caused by the Sentry wizard, create an issue on GitHub and include the log file's content:\nhttps://github.com/getsentry/sentry-wizard/issues",
)}`,
);
await abort();
Expand Down
14 changes: 5 additions & 9 deletions src/utils/package-manager.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
/* eslint-disable @typescript-eslint/typedef */
import { exec } from 'child_process';
import * as fs from 'fs';
import * as path from 'path';
import { promisify } from 'util';

import * as Sentry from '@sentry/node';
import { traceStep } from '../telemetry';
Expand All @@ -15,6 +13,7 @@ export interface PackageManager {
buildCommand: string;
/* The command that the package manager uses to run a script from package.json */
runScriptCommand: string;
flags: string;
}

export const BUN: PackageManager = {
Expand All @@ -24,6 +23,7 @@ export const BUN: PackageManager = {
installCommand: 'bun add',
buildCommand: 'bun run build',
runScriptCommand: 'bun run',
flags: '',
};
export const YARN: PackageManager = {
name: 'yarn',
Expand All @@ -32,6 +32,7 @@ export const YARN: PackageManager = {
installCommand: 'yarn add',
buildCommand: 'yarn build',
runScriptCommand: 'yarn',
flags: '--ignore-workspace-root-check',
};
export const PNPM: PackageManager = {
name: 'pnpm',
Expand All @@ -40,6 +41,7 @@ export const PNPM: PackageManager = {
installCommand: 'pnpm add',
buildCommand: 'pnpm build',
runScriptCommand: 'pnpm',
flags: '--ignore-workspace-root-check',
};
export const NPM: PackageManager = {
name: 'npm',
Expand All @@ -48,6 +50,7 @@ export const NPM: PackageManager = {
installCommand: 'npm add',
buildCommand: 'npm run build',
runScriptCommand: 'npm run',
flags: '',
};

export const packageManagers = [BUN, YARN, PNPM, NPM];
Expand All @@ -64,10 +67,3 @@ export function detectPackageManger(): PackageManager | null {
return null;
});
}

export async function installPackageWithPackageManager(
packageManager: PackageManager,
packageName: string,
): Promise<void> {
await promisify(exec)(`${packageManager.installCommand} ${packageName}`);
}

0 comments on commit 5febecc

Please sign in to comment.