Skip to content

Commit 654c01d

Browse files
committed
fix(pnpm): enforce catalog usage based on smart detection
1 parent 50ceae6 commit 654c01d

File tree

2 files changed

+35
-8
lines changed

2 files changed

+35
-8
lines changed

src/configs/pnpm.ts

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,20 @@
1-
import type { OptionsIsInEditor, TypedFlatConfigItem } from '../types'
1+
import type { OptionsPnpm, TypedFlatConfigItem } from '../types'
2+
import fs from 'node:fs/promises'
3+
import { findUp } from 'find-up-simple'
24

35
import { interopDefault } from '../utils'
46

7+
async function detectCatalogUsage(): Promise<boolean> {
8+
const workspaceFile = await findUp('pnpm-workspace.yaml')
9+
if (!workspaceFile)
10+
return false
11+
12+
const yaml = await fs.readFile(workspaceFile, 'utf-8')
13+
return yaml.includes('catalog:') || yaml.includes('catalogs:')
14+
}
15+
516
export async function pnpm(
6-
options: OptionsIsInEditor,
17+
options: OptionsPnpm,
718
): Promise<TypedFlatConfigItem[]> {
819
const [
920
pluginPnpm,
@@ -15,6 +26,11 @@ export async function pnpm(
1526
interopDefault(import('jsonc-eslint-parser')),
1627
])
1728

29+
const {
30+
catalogs = detectCatalogUsage(),
31+
isInEditor = false,
32+
} = options
33+
1834
return [
1935
{
2036
files: [
@@ -29,17 +45,21 @@ export async function pnpm(
2945
pnpm: pluginPnpm,
3046
},
3147
rules: {
32-
'pnpm/json-enforce-catalog': [
33-
'error',
34-
{ autofix: !options.isInEditor },
35-
],
48+
...(catalogs
49+
? {
50+
'pnpm/json-enforce-catalog': [
51+
'error',
52+
{ autofix: !isInEditor },
53+
],
54+
}
55+
: {}),
3656
'pnpm/json-prefer-workspace-settings': [
3757
'error',
38-
{ autofix: !options.isInEditor },
58+
{ autofix: !isInEditor },
3959
],
4060
'pnpm/json-valid-catalog': [
4161
'error',
42-
{ autofix: !options.isInEditor },
62+
{ autofix: !isInEditor },
4363
],
4464
},
4565
},

src/types.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,13 @@ export interface OptionsIsInEditor {
257257
isInEditor?: boolean
258258
}
259259

260+
export interface OptionsPnpm extends OptionsIsInEditor {
261+
/**
262+
* Requires catalogs usage
263+
*/
264+
catalogs?: boolean
265+
}
266+
260267
export interface OptionsUnoCSS extends OptionsOverrides {
261268
/**
262269
* Enable attributify support.

0 commit comments

Comments
 (0)