Skip to content

Commit 1feaf05

Browse files
committed
feat(plugin-js-packages): add Yarn v2 outdated, improve naming
1 parent 50d496e commit 1feaf05

File tree

5 files changed

+80
-21
lines changed

5 files changed

+80
-21
lines changed

packages/plugin-js-packages/README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,12 @@ This plugin checks for known vulnerabilities and outdated dependencies.
1010
It supports the following package managers:
1111

1212
- [NPM](https://docs.npmjs.com/)
13-
- [Yarn v1](https://classic.yarnpkg.com/docs/) & [Yarn v2+](https://yarnpkg.com/getting-started)
13+
- [Yarn v1](https://classic.yarnpkg.com/docs/)[Yarn v2+](https://yarnpkg.com/getting-started)
1414
- [PNPM](https://pnpm.io/pnpm-cli)
1515

16+
> ![NOTE]
17+
> As of now, in order to check outdated dependencies for Yarn v2+, you need to install [`yarn-plugin-outdated`](https://github.com/mskelton/yarn-plugin-outdated).
18+
1619
## Getting started
1720

1821
1. If you haven't already, install [@code-pushup/cli](../cli/README.md) and create a configuration file.

packages/plugin-js-packages/src/lib/runner/outdated/constants.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
import { IssueSeverity } from '@code-pushup/models';
22
import { PackageManager } from '../../config';
33
import { OutdatedResult, VersionType } from './types';
4-
import { npmToOutdatedResult, yarnv1ToOutdatedResult } from './unify-type';
4+
import {
5+
npmToOutdatedResult,
6+
yarnv1ToOutdatedResult,
7+
yarnv2ToOutdatedResult,
8+
} from './unify-type';
59

610
export const outdatedSeverity: Record<VersionType, IssueSeverity> = {
711
major: 'error',
@@ -12,7 +16,7 @@ export const outdatedSeverity: Record<VersionType, IssueSeverity> = {
1216
export const outdatedArgs: Record<PackageManager, string[]> = {
1317
npm: ['--json', '--long'],
1418
'yarn-classic': ['--json'],
15-
'yarn-modern': [],
19+
'yarn-modern': ['--json'],
1620
pnpm: [],
1721
};
1822

@@ -22,7 +26,6 @@ export const normalizeOutdatedMapper: Record<
2226
> = {
2327
npm: npmToOutdatedResult,
2428
'yarn-classic': yarnv1ToOutdatedResult,
25-
// eslint-disable-next-line @typescript-eslint/naming-convention
26-
'yarn-modern': _ => [],
29+
'yarn-modern': yarnv2ToOutdatedResult,
2730
pnpm: _ => [],
2831
};

packages/plugin-js-packages/src/lib/runner/outdated/types.ts

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
// Subset of NPM outdated JSON type
21
export const versionType = ['major', 'minor', 'patch'] as const;
32
export type VersionType = (typeof versionType)[number];
43
export type PackageVersion = Record<VersionType, number>;
@@ -7,18 +6,28 @@ export type DependencyGroupLong =
76
| 'devDependencies'
87
| 'optionalDependencies';
98

10-
export type VersionOverview = {
9+
// Unified Outdated result type
10+
export type OutdatedResult = {
11+
name: string;
12+
current: string;
13+
latest: string;
14+
type: DependencyGroupLong;
15+
url?: string;
16+
}[];
17+
18+
// Subset of NPM outdated JSON type
19+
export type NpmVersionOverview = {
1120
current?: string;
1221
latest: string;
1322
type: DependencyGroupLong;
1423
homepage?: string;
1524
};
1625

17-
export type NormalizedVersionOverview = Omit<VersionOverview, 'current'> & {
26+
export type NpmNormalizedOverview = Omit<NpmVersionOverview, 'current'> & {
1827
current: string;
1928
};
20-
export type NormalizedOutdatedEntries = [string, NormalizedVersionOverview][];
21-
export type NpmOutdatedResultJson = Record<string, VersionOverview>;
29+
30+
export type NpmOutdatedResultJson = Record<string, NpmVersionOverview>;
2231

2332
// Subset of Yarn v1 outdated JSON type
2433
export type Yarnv1VersionOverview = [
@@ -41,11 +50,12 @@ type Yarnv1Table = {
4150

4251
export type Yarnv1OutdatedResultJson = [Yarnv1Info, Yarnv1Table];
4352

44-
// Unified Outdated result type
45-
export type OutdatedResult = {
46-
name: string;
53+
// Subset of Yarn v2 outdated JSON type
54+
export type Yarnv2VersionOverview = {
4755
current: string;
4856
latest: string;
57+
name: string;
4958
type: DependencyGroupLong;
50-
url?: string;
51-
}[];
59+
};
60+
61+
export type Yarnv2OutdatedResultJson = Yarnv2VersionOverview[];

packages/plugin-js-packages/src/lib/runner/outdated/unify-type.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import { fromJsonLines, objectToEntries } from '@code-pushup/utils';
22
import {
3-
NormalizedVersionOverview,
3+
NpmNormalizedOverview,
44
NpmOutdatedResultJson,
55
OutdatedResult,
66
Yarnv1OutdatedResultJson,
7+
Yarnv2OutdatedResultJson,
78
} from './types';
89

910
export function npmToOutdatedResult(output: string): OutdatedResult {
@@ -12,7 +13,7 @@ export function npmToOutdatedResult(output: string): OutdatedResult {
1213
// https://stackoverflow.com/questions/42267101/npm-outdated-command-shows-missing-in-current-version
1314
return objectToEntries(npmOutdated)
1415
.filter(
15-
(entry): entry is [string, NormalizedVersionOverview] =>
16+
(entry): entry is [string, NpmNormalizedOverview] =>
1617
entry[1].current != null,
1718
)
1819
.map(([name, overview]) => ({
@@ -36,3 +37,14 @@ export function yarnv1ToOutdatedResult(output: string): OutdatedResult {
3637
url,
3738
}));
3839
}
40+
41+
export function yarnv2ToOutdatedResult(output: string): OutdatedResult {
42+
const npmOutdated = JSON.parse(output) as Yarnv2OutdatedResultJson;
43+
44+
return npmOutdated.map(({ name, current, latest, type }) => ({
45+
name,
46+
current,
47+
latest,
48+
type,
49+
}));
50+
}

packages/plugin-js-packages/src/lib/runner/outdated/unify-type.unit.test.ts

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
import { describe, expect, it } from 'vitest';
22
import { toJsonLines } from '@code-pushup/utils';
3-
import { OutdatedResult } from './types';
4-
import { npmToOutdatedResult, yarnv1ToOutdatedResult } from './unify-type';
3+
import { OutdatedResult, Yarnv2OutdatedResultJson } from './types';
4+
import {
5+
npmToOutdatedResult,
6+
yarnv1ToOutdatedResult,
7+
yarnv2ToOutdatedResult,
8+
} from './unify-type';
59

6-
describe('npmOutdatedToOutdatedResult', () => {
10+
describe('npmToOutdatedResult', () => {
711
it('should transform NPM outdated to unified outdated result', () => {
812
expect(
913
npmToOutdatedResult(
@@ -59,7 +63,7 @@ describe('npmOutdatedToOutdatedResult', () => {
5963
});
6064
});
6165

62-
describe('yarnv1OutdatedToOutdatedResult', () => {
66+
describe('yarnv1ToOutdatedResult', () => {
6367
const yarnInfo = { type: 'info', data: 'Colours' };
6468
it('should transform Yarn v1 outdated to unified outdated result', () => {
6569
const table = {
@@ -90,3 +94,30 @@ describe('yarnv1OutdatedToOutdatedResult', () => {
9094
expect(yarnv1ToOutdatedResult(toJsonLines([yarnInfo, table]))).toEqual([]);
9195
});
9296
});
97+
98+
describe('yarnv2ToOutdatedResult', () => {
99+
it('should transform Yarn v2 outdated to unified outdated result', () => {
100+
const outdated = [
101+
{
102+
name: 'nx',
103+
current: '16.8.1',
104+
latest: '17.0.0',
105+
type: 'dependencies',
106+
},
107+
{
108+
name: 'vite',
109+
current: '4.3.1',
110+
latest: '5.1.4',
111+
type: 'devDependencies',
112+
},
113+
] satisfies Yarnv2OutdatedResultJson;
114+
115+
expect(yarnv2ToOutdatedResult(JSON.stringify(outdated))).toStrictEqual(
116+
outdated,
117+
);
118+
});
119+
120+
it('should transform no dependencies to empty array', () => {
121+
expect(yarnv2ToOutdatedResult('[]')).toEqual([]);
122+
});
123+
});

0 commit comments

Comments
 (0)