Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion packages/angular/cli/src/commands/version/version-info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,13 @@ export async function gatherVersionInfo(context: CommandContext): Promise<Versio

const angularCoreVersion = packages['@angular/core'];

let packageManagerVersion: string | undefined;
try {
packageManagerVersion = await context.packageManager.getVersion();
} catch {
// Ignore errors if the package manager is not available.
}
Comment thread
alan-agius4 marked this conversation as resolved.

return {
cli: {
version: VERSION.full,
Expand All @@ -122,7 +129,7 @@ export async function gatherVersionInfo(context: CommandContext): Promise<Versio
},
packageManager: {
name: context.packageManager.name,
version: await context.packageManager.getVersion(),
version: packageManagerVersion,
},
},
packages,
Expand Down
64 changes: 64 additions & 0 deletions packages/angular/cli/src/commands/version/version-info_spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/**
* @license
* Copyright Google LLC All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.dev/license
*/

import assert from 'node:assert';
import { mkdtemp, rm } from 'node:fs/promises';
import { join } from 'node:path';
import type { CommandContext } from '../../command-builder/definitions';
import { PackageManager } from '../../package-managers/package-manager';
import { SUPPORTED_PACKAGE_MANAGERS } from '../../package-managers/package-manager-descriptor';
import { MockHost } from '../../package-managers/testing/mock-host';
import { gatherVersionInfo } from './version-info';

describe('gatherVersionInfo', () => {
let tempRoot: string;

beforeEach(async () => {
const baseTmpDir = process.env['TEST_TMPDIR'];
assert(baseTmpDir, 'TEST_TMPDIR is not set');
tempRoot = await mkdtemp(join(baseTmpDir, 'angular-cli-version-info-test-'));
});

afterEach(async () => {
await rm(tempRoot, { recursive: true, force: true });
});

function createContext(packageManager: PackageManager): CommandContext {
return { root: tempRoot, packageManager } as unknown as CommandContext;
}

it('should report the package manager version when it is available', async () => {
const packageManager = new PackageManager(
new MockHost(),
tempRoot,
SUPPORTED_PACKAGE_MANAGERS['npm'],
{ version: '10.9.0' },
);

const info = await gatherVersionInfo(createContext(packageManager));

expect(info.system.packageManager).toEqual({ name: 'npm', version: '10.9.0' });
});

it('should not fail when the package manager is not installed', async () => {
const packageManager = new PackageManager(
new MockHost(),
tempRoot,
SUPPORTED_PACKAGE_MANAGERS['bun'],
{
initializationError: new Error(
`The project is configured to use 'bun', but it is not installed.`,
),
},
);

const info = await gatherVersionInfo(createContext(packageManager));

expect(info.system.packageManager).toEqual({ name: 'bun', version: undefined });
});
});
Loading