Skip to content

Commit c680c26

Browse files
committedMar 10, 2025
feat: support overrides field in pnpm-workspace.yaml
1 parent c45ea7f commit c680c26

File tree

5 files changed

+64
-36
lines changed

5 files changed

+64
-36
lines changed
 

‎src/commands/check/render.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,8 @@ export function renderChanges(
8888
.join(', ')
8989
: c.dim('no change')
9090

91-
const displayName = pkg.name?.startsWith('catalog:')
92-
? c.dim('catalog:') + c.yellow(pkg.name.slice('catalog:'.length))
91+
const displayName = pkg.name?.startsWith('pnpm-catalog:')
92+
? c.dim('pnpm-catalog:') + c.yellow(pkg.name.slice('pnpm-catalog:'.length))
9393
: pkg.name
9494
? c.cyan(pkg.name)
9595
: c.red('›') + c.dim(` ${filepath || ''}`.trimEnd())

‎src/io/pnpmWorkspaces.ts

+23-10
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@ export async function loadPnpmWorkspace(
1717

1818
const catalogs: PnpmWorkspaceMeta[] = []
1919

20-
function createCatalogFromKeyValue(catalogName: string, map: Record<string, string>): PnpmWorkspaceMeta {
20+
function createPnpmWorkspaceEntry(name: string, map: Record<string, string>): PnpmWorkspaceMeta {
2121
const deps: RawDep[] = Object.entries(map)
22-
.map(([name, version]) => parseDependency(name, version, 'pnpm:catalog', shouldUpdate))
22+
.map(([pkg, version]) => parseDependency(pkg, version, 'pnpm-workspace', shouldUpdate))
2323

2424
return {
25-
name: `catalog:${catalogName}`,
25+
name,
2626
private: true,
2727
version: '',
2828
type: 'pnpm-workspace.yaml',
@@ -37,34 +37,47 @@ export async function loadPnpmWorkspace(
3737

3838
if (raw.catalog) {
3939
catalogs.push(
40-
createCatalogFromKeyValue('default', raw.catalog),
40+
createPnpmWorkspaceEntry('pnpm-catalog:default', raw.catalog),
4141
)
4242
}
4343

4444
if (raw.catalogs) {
4545
for (const key of Object.keys(raw.catalogs)) {
4646
catalogs.push(
47-
createCatalogFromKeyValue(key, raw.catalogs[key]),
47+
createPnpmWorkspaceEntry(`pnpm-catalog:${key}`, raw.catalogs[key]),
4848
)
4949
}
5050
}
5151

52+
if (raw.overrides) {
53+
catalogs.push(
54+
createPnpmWorkspaceEntry('pnpm-workspace:overrides', raw.overrides),
55+
)
56+
}
57+
5258
return catalogs
5359
}
5460

5561
export async function writePnpmWorkspace(
5662
pkg: PnpmWorkspaceMeta,
5763
_options: CommonOptions,
5864
) {
59-
const versions = dumpDependencies(pkg.resolved, 'pnpm:catalog')
65+
const versions = dumpDependencies(pkg.resolved, 'pnpm-workspace')
6066

6167
if (!Object.keys(versions).length)
6268
return
6369

64-
const catalogName = pkg.name.replace('catalog:', '')
65-
66-
for (const [key, targetVersion] of Object.entries(versions)) {
67-
pkg.context.setPackage(catalogName, key, targetVersion)
70+
if (pkg.name.startsWith('pnpm-catalog:')) {
71+
const catalogName = pkg.name.replace('pnpm-catalog:', '')
72+
for (const [key, targetVersion] of Object.entries(versions)) {
73+
pkg.context.setPackage(catalogName, key, targetVersion)
74+
}
75+
}
76+
else {
77+
const paths = pkg.name.replace('pnpm-workspace:', '').split(/\./g)
78+
for (const [key, targetVersion] of Object.entries(versions)) {
79+
pkg.context.setPath([...paths, key], targetVersion)
80+
}
6881
}
6982

7083
if (pkg.context.hasChanged()) {

‎src/types.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export type DepType =
1414
| 'pnpm.overrides'
1515
| 'resolutions'
1616
| 'overrides'
17-
| 'pnpm:catalog'
17+
| 'pnpm-workspace'
1818

1919
export const DependenciesTypeShortMap = {
2020
'packageManager': 'package-manager',
@@ -25,7 +25,7 @@ export const DependenciesTypeShortMap = {
2525
'resolutions': 'resolutions',
2626
'overrides': 'overrides',
2727
'pnpm.overrides': 'pnpm-overrides',
28-
'pnpm:catalog': 'catalog',
28+
'pnpm-workspace': 'pnpm-workspace',
2929
}
3030

3131
export interface RawDep {

‎test/fixtures/pnpm-catalog/pnpm-workspace.yaml

+3
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,6 @@ catalogs:
1313
react18:
1414
react: ^18.2.0
1515
react-dom: ^18.2.0
16+
17+
overrides:
18+
vue: ~3.3.0

‎test/pnpmCatalog.test.ts

+34-22
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,10 @@ it('pnpm catalog', async () => {
3333

3434
expect(result.packages.map(p => p.name)).toMatchInlineSnapshot(`
3535
[
36-
"catalog:default",
37-
"catalog:react17",
38-
"catalog:react18",
36+
"pnpm-catalog:default",
37+
"pnpm-catalog:react17",
38+
"pnpm-catalog:react18",
39+
"pnpm-workspace:overrides",
3940
"@taze/monorepo-example",
4041
]
4142
`)
@@ -48,7 +49,7 @@ it('pnpm catalog', async () => {
4849
).toMatchInlineSnapshot(`
4950
[
5051
{
51-
"name": "catalog:default",
52+
"name": "pnpm-catalog:default",
5253
"packages": [
5354
[
5455
"react",
@@ -61,7 +62,7 @@ it('pnpm catalog', async () => {
6162
],
6263
},
6364
{
64-
"name": "catalog:react17",
65+
"name": "pnpm-catalog:react17",
6566
"packages": [
6667
[
6768
"react",
@@ -74,7 +75,7 @@ it('pnpm catalog', async () => {
7475
],
7576
},
7677
{
77-
"name": "catalog:react18",
78+
"name": "pnpm-catalog:react18",
7879
"packages": [
7980
[
8081
"react",
@@ -86,6 +87,15 @@ it('pnpm catalog', async () => {
8687
],
8788
],
8889
},
90+
{
91+
"name": "pnpm-workspace:overrides",
92+
"packages": [
93+
[
94+
"vue",
95+
"~3.3.0",
96+
],
97+
],
98+
},
8999
{
90100
"name": "@taze/monorepo-example",
91101
"packages": [
@@ -133,12 +143,12 @@ describe('pnpm catalog update w/ yaml anchors and aliases', () => {
133143
const context = parsePnpmWorkspaceYaml(workspaceYamlContents)
134144
const pkg: PnpmWorkspaceMeta = {
135145
type: 'pnpm-workspace.yaml',
136-
name: 'catalog:default',
146+
name: 'pnpm-catalog:default',
137147
resolved: [
138148
// testing purpose
139-
{ name: 'react', targetVersion: '^18.3.1', source: 'pnpm:catalog', update: true, currentVersion: '^18.2.0', diff: 'minor' } as any,
149+
{ name: 'react', targetVersion: '^18.3.1', source: 'pnpm-workspace', update: true, currentVersion: '^18.2.0', diff: 'minor' } as any,
140150
// testing purpose
141-
{ name: 'react-dom', targetVersion: '^18.3.1', source: 'pnpm:catalog', update: true, currentVersion: '^18.2.0', diff: 'minor' } as any,
151+
{ name: 'react-dom', targetVersion: '^18.3.1', source: 'pnpm-workspace', update: true, currentVersion: '^18.2.0', diff: 'minor' } as any,
142152
],
143153
raw: context.toJSON(),
144154
context,
@@ -151,10 +161,11 @@ describe('pnpm catalog update w/ yaml anchors and aliases', () => {
151161
await pnpmWorkspaces.writePnpmWorkspace(pkg, {})
152162

153163
expect(output).toMatchInlineSnapshot(`
154-
"catalog:
155-
react: &foo ^18.3.1
156-
react-dom: *foo
157-
"`)
164+
"catalog:
165+
react: &foo ^18.3.1
166+
react-dom: *foo
167+
"
168+
`)
158169
})
159170

160171
it('should preserve yaml anchors and aliases with single string value, when anchor is defined in a separate field', async () => {
@@ -168,12 +179,12 @@ describe('pnpm catalog update w/ yaml anchors and aliases', () => {
168179
`
169180
const context = parsePnpmWorkspaceYaml(workspaceYamlContents)
170181
const pkg: PnpmWorkspaceMeta = {
171-
name: 'catalog:default',
182+
name: 'pnpm-catalog:default',
172183
resolved: [
173184
// testing purpose
174-
{ name: 'react', targetVersion: '^18.3.1', source: 'pnpm:catalog', update: true, currentVersion: '^18.2.0', diff: 'minor' } as any,
185+
{ name: 'react', targetVersion: '^18.3.1', source: 'pnpm-workspace', update: true, currentVersion: '^18.2.0', diff: 'minor' } as any,
175186
// testing purpose
176-
{ name: 'react-dom', targetVersion: '^18.3.1', source: 'pnpm:catalog', update: true, currentVersion: '^18.2.0', diff: 'minor' } as any,
187+
{ name: 'react-dom', targetVersion: '^18.3.1', source: 'pnpm-workspace', update: true, currentVersion: '^18.2.0', diff: 'minor' } as any,
177188
],
178189
raw: context.toJSON(),
179190
context,
@@ -186,12 +197,13 @@ describe('pnpm catalog update w/ yaml anchors and aliases', () => {
186197
}
187198
await pnpmWorkspaces.writePnpmWorkspace(pkg, {})
188199
expect(output).toMatchInlineSnapshot(`
189-
"defines:
190-
- &react ^18.3.1
200+
"defines:
201+
- &react ^18.3.1
191202
192-
catalog:
193-
react: *react
194-
react-dom: *react
195-
"`)
203+
catalog:
204+
react: *react
205+
react-dom: *react
206+
"
207+
`)
196208
})
197209
})

0 commit comments

Comments
 (0)
Please sign in to comment.