Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: check packages existence in local scope #583

Merged
merged 2 commits into from
Aug 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/configs/formatters.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { isPackageExists } from 'local-pkg'
import { GLOB_ASTRO, GLOB_ASTRO_TS, GLOB_CSS, GLOB_GRAPHQL, GLOB_HTML, GLOB_LESS, GLOB_MARKDOWN, GLOB_POSTCSS, GLOB_SCSS, GLOB_XML } from '../globs'
import type { VendoredPrettierOptions } from '../vender/prettier-types'
import { ensurePackages, interopDefault, parserPlain } from '../utils'
import { ensurePackages, interopDefault, isPackageInScope, parserPlain } from '../utils'
import type { OptionsFormatters, StylisticConfig, TypedFlatConfigItem } from '../types'
import { StylisticConfigDefaults } from './stylistic'

Expand All @@ -11,13 +11,13 @@ export async function formatters(
): Promise<TypedFlatConfigItem[]> {
if (options === true) {
options = {
astro: isPackageExists('prettier-plugin-astro'),
astro: isPackageInScope('prettier-plugin-astro'),
css: true,
graphql: true,
html: true,
markdown: true,
slidev: isPackageExists('@slidev/cli'),
xml: isPackageExists('@prettier/plugin-xml'),
xml: isPackageInScope('@prettier/plugin-xml'),
}
}

Expand Down
12 changes: 10 additions & 2 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import process from 'node:process'
import { fileURLToPath } from 'node:url'
import { isPackageExists } from 'local-pkg'
import type { Awaitable, TypedFlatConfigItem } from './types'

const scopeUrl = fileURLToPath(new URL('.', import.meta.url))
const isCwdInScope = isPackageExists('@antfu/eslint-config')

export const parserPlain = {
meta: {
name: 'parser-plain',
Expand Down Expand Up @@ -107,11 +111,15 @@ export async function interopDefault<T>(m: Awaitable<T>): Promise<T extends { de
return (resolved as any).default || resolved
}

export function isPackageInScope(name: string): boolean {
return isPackageExists(name, { paths: [scopeUrl] })
}

export async function ensurePackages(packages: (string | undefined)[]): Promise<void> {
if (process.env.CI || process.stdout.isTTY === false)
if (process.env.CI || process.stdout.isTTY === false || isCwdInScope === false)
return

const nonExistingPackages = packages.filter(i => i && !isPackageExists(i)) as string[]
const nonExistingPackages = packages.filter(i => i && !isPackageInScope(i)) as string[]
if (nonExistingPackages.length === 0)
return

Expand Down