Skip to content

Commit

Permalink
fix: check packages existence in local scope (#583)
Browse files Browse the repository at this point in the history
Co-authored-by: Anthony Fu <github@antfu.me>
  • Loading branch information
paescuj and antfu authored Aug 16, 2024
1 parent e5ca299 commit 12fd868
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
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

0 comments on commit 12fd868

Please sign in to comment.