Skip to content

Commit 12fd868

Browse files
paescujantfu
andauthoredAug 16, 2024··
fix: check packages existence in local scope (#583)
Co-authored-by: Anthony Fu <github@antfu.me>
1 parent e5ca299 commit 12fd868

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed
 

‎src/configs/formatters.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { isPackageExists } from 'local-pkg'
22
import { GLOB_ASTRO, GLOB_ASTRO_TS, GLOB_CSS, GLOB_GRAPHQL, GLOB_HTML, GLOB_LESS, GLOB_MARKDOWN, GLOB_POSTCSS, GLOB_SCSS, GLOB_XML } from '../globs'
33
import type { VendoredPrettierOptions } from '../vender/prettier-types'
4-
import { ensurePackages, interopDefault, parserPlain } from '../utils'
4+
import { ensurePackages, interopDefault, isPackageInScope, parserPlain } from '../utils'
55
import type { OptionsFormatters, StylisticConfig, TypedFlatConfigItem } from '../types'
66
import { StylisticConfigDefaults } from './stylistic'
77

@@ -11,13 +11,13 @@ export async function formatters(
1111
): Promise<TypedFlatConfigItem[]> {
1212
if (options === true) {
1313
options = {
14-
astro: isPackageExists('prettier-plugin-astro'),
14+
astro: isPackageInScope('prettier-plugin-astro'),
1515
css: true,
1616
graphql: true,
1717
html: true,
1818
markdown: true,
1919
slidev: isPackageExists('@slidev/cli'),
20-
xml: isPackageExists('@prettier/plugin-xml'),
20+
xml: isPackageInScope('@prettier/plugin-xml'),
2121
}
2222
}
2323

‎src/utils.ts

+10-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
import process from 'node:process'
2+
import { fileURLToPath } from 'node:url'
23
import { isPackageExists } from 'local-pkg'
34
import type { Awaitable, TypedFlatConfigItem } from './types'
45

6+
const scopeUrl = fileURLToPath(new URL('.', import.meta.url))
7+
const isCwdInScope = isPackageExists('@antfu/eslint-config')
8+
59
export const parserPlain = {
610
meta: {
711
name: 'parser-plain',
@@ -107,11 +111,15 @@ export async function interopDefault<T>(m: Awaitable<T>): Promise<T extends { de
107111
return (resolved as any).default || resolved
108112
}
109113

114+
export function isPackageInScope(name: string): boolean {
115+
return isPackageExists(name, { paths: [scopeUrl] })
116+
}
117+
110118
export async function ensurePackages(packages: (string | undefined)[]): Promise<void> {
111-
if (process.env.CI || process.stdout.isTTY === false)
119+
if (process.env.CI || process.stdout.isTTY === false || isCwdInScope === false)
112120
return
113121

114-
const nonExistingPackages = packages.filter(i => i && !isPackageExists(i)) as string[]
122+
const nonExistingPackages = packages.filter(i => i && !isPackageInScope(i)) as string[]
115123
if (nonExistingPackages.length === 0)
116124
return
117125

0 commit comments

Comments
 (0)
Please sign in to comment.