Skip to content

Commit 2317262

Browse files
committed
fix(@angular/cli): handle array output from npm view in manifest parser
This commit updates `parseNpmLikeManifest` to correctly handle the output of `npm view` (and compatible commands) when a version range is specified. In such cases, `npm view --json` returns an array of manifests. The parser now returns the last element of the array, which corresponds to the latest version satisfying the range, preventing issues in `ng update` and other commands that rely on version checks.
1 parent 9635082 commit 2317262

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,9 @@ export function parseNpmLikeManifest(stdout: string, logger?: Logger): PackageMa
254254
return null;
255255
}
256256

257-
return JSON.parse(stdout);
257+
const result = JSON.parse(stdout);
258+
259+
return Array.isArray(result) ? result[result.length - 1] : result;
258260
}
259261

260262
/**

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

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* found in the LICENSE file at https://angular.dev/license
77
*/
88

9-
import { parseNpmLikeError, parseYarnClassicError } from './parsers';
9+
import { parseNpmLikeError, parseNpmLikeManifest, parseYarnClassicError } from './parsers';
1010

1111
describe('parsers', () => {
1212
describe('parseNpmLikeError', () => {
@@ -69,6 +69,25 @@ describe('parsers', () => {
6969
});
7070
});
7171

72+
describe('parseNpmLikeManifest', () => {
73+
it('should parse a single manifest', () => {
74+
const stdout = JSON.stringify({ name: 'foo', version: '1.0.0' });
75+
expect(parseNpmLikeManifest(stdout)).toEqual({ name: 'foo', version: '1.0.0' });
76+
});
77+
78+
it('should return the last manifest from an array', () => {
79+
const stdout = JSON.stringify([
80+
{ name: 'foo', version: '1.0.0' },
81+
{ name: 'foo', version: '1.1.0' },
82+
]);
83+
expect(parseNpmLikeManifest(stdout)).toEqual({ name: 'foo', version: '1.1.0' });
84+
});
85+
86+
it('should return null for empty stdout', () => {
87+
expect(parseNpmLikeManifest('')).toBeNull();
88+
});
89+
});
90+
7291
describe('parseYarnClassicError', () => {
7392
it('should parse a 404 from verbose logs', () => {
7493
const stdout =

0 commit comments

Comments
 (0)