Skip to content

Commit 84d82eb

Browse files
committed
refactor(@angular/cli): improve error handling for package installation in ng add
This change enhances the error handling in the 'ng add' command's 'installPackageTask'. It now specifically catches 'PackageManagerError' exceptions and surfaces the standard output or standard error from the underlying package manager process. This provides users with more actionable information when a package installation fails. (cherry picked from commit 158d715)
1 parent 122ed27 commit 84d82eb

File tree

2 files changed

+42
-29
lines changed

2 files changed

+42
-29
lines changed

packages/angular/cli/src/commands/add/cli.ts

Lines changed: 41 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import {
2626
import {
2727
NgAddSaveDependency,
2828
PackageManager,
29+
PackageManagerError,
2930
PackageManifest,
3031
PackageMetadata,
3132
createPackageManager,
@@ -576,36 +577,47 @@ export default class AddCommandModule
576577
// Only show if installation will actually occur
577578
task.title = 'Installing package';
578579

579-
if (context.savePackage === false) {
580-
task.title += ' in temporary location';
581-
582-
// Temporary packages are located in a different directory
583-
// Hence we need to resolve them using the temp path
584-
const { workingDirectory } = await packageManager.acquireTempPackage(
585-
packageIdentifier.toString(),
586-
{
587-
registry,
588-
},
589-
);
590-
591-
const tempRequire = createRequire(workingDirectory + '/');
592-
assert(context.collectionName, 'Collection name should always be available');
593-
const resolvedCollectionPath = tempRequire.resolve(
594-
join(context.collectionName, 'package.json'),
595-
);
580+
try {
581+
if (context.savePackage === false) {
582+
task.title += ' in temporary location';
583+
584+
// Temporary packages are located in a different directory
585+
// Hence we need to resolve them using the temp path
586+
const { workingDirectory } = await packageManager.acquireTempPackage(
587+
packageIdentifier.toString(),
588+
{
589+
registry,
590+
},
591+
);
592+
593+
const tempRequire = createRequire(workingDirectory + '/');
594+
assert(context.collectionName, 'Collection name should always be available');
595+
const resolvedCollectionPath = tempRequire.resolve(
596+
join(context.collectionName, 'package.json'),
597+
);
598+
599+
context.collectionName = dirname(resolvedCollectionPath);
600+
} else {
601+
await packageManager.add(
602+
packageIdentifier.toString(),
603+
'none',
604+
savePackage !== 'dependencies',
605+
false,
606+
true,
607+
{
608+
registry,
609+
},
610+
);
611+
}
612+
} catch (e) {
613+
if (e instanceof PackageManagerError) {
614+
const output = e.stderr || e.stdout;
615+
if (output) {
616+
throw new CommandError(`Package installation failed: ${e.message}\nOutput: ${output}`);
617+
}
618+
}
596619

597-
context.collectionName = dirname(resolvedCollectionPath);
598-
} else {
599-
await packageManager.add(
600-
packageIdentifier.toString(),
601-
'none',
602-
savePackage !== 'dependencies',
603-
false,
604-
true,
605-
{
606-
registry,
607-
},
608-
);
620+
throw e;
609621
}
610622
}
611623

packages/angular/cli/src/package-managers/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,6 @@
99
export { createPackageManager } from './factory';
1010
export type { PackageManagerName } from './package-manager-descriptor';
1111
export { PackageManager } from './package-manager';
12+
export { PackageManagerError } from './error';
1213
export type * from './package-metadata';
1314
export type { InstalledPackage } from './package-tree';

0 commit comments

Comments
 (0)