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

feat: isPackageExists support version check #12

Closed
wants to merge 2 commits into from
Closed
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
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@ import {
} from 'local-pkg'

isPackageExists('local-pkg') // true

isPackageExists('local-pkg', {
version: '> 0.5.1'
}) // true

isPackageExists('local-pkg', {
version: '0.0.0 || 0.1.1'
}) // false

isPackageExists('foo') // false

await getPackageInfo('local-pkg')
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,16 @@
},
"dependencies": {
"mlly": "^1.4.2",
"pkg-types": "^1.0.3"
"pkg-types": "^1.0.3",
"semver": "^7.5.4"
},
"devDependencies": {
"@antfu/eslint-config": "^1.0.0-beta.18",
"@antfu/ni": "^0.21.8",
"@antfu/utils": "^0.7.6",
"@types/chai": "^4.3.6",
"@types/node": "^20.8.0",
"@types/semver": "^7.5.3",
"bumpp": "^9.2.0",
"chai": "^4.3.10",
"eslint": "^8.50.0",
Expand Down
9 changes: 6 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 15 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import process from 'node:process'
import { interopDefault, resolvePathSync } from 'mlly'
import type { PackageJson } from 'pkg-types'
import { findUp } from 'find-up'
import { satisfies } from 'semver'

export interface PackageInfo {
name: string
Expand All @@ -24,6 +25,13 @@ export interface PackageResolvingOptions {
platform?: 'posix' | 'win32' | 'auto'
}

export interface PackageExistsOptions extends PackageResolvingOptions {
/**
* match package version with `semver`
*/
version?: string
}

function _resolve(path: string, options: PackageResolvingOptions = {}) {
if (options.platform === 'auto' || !options.platform)
options.platform = process.platform === 'win32' ? 'win32' : 'posix'
Expand Down Expand Up @@ -52,7 +60,13 @@ export async function importModule<T = any>(path: string): Promise<T> {
return i
}

export function isPackageExists(name: string, options: PackageResolvingOptions = {}) {
export function isPackageExists(name: string, options: PackageExistsOptions = {}) {
if (options.version) {
const info = getPackageInfoSync(name, options)
if (!info?.version)
return false
return satisfies(info.version, options.version)
}
return !!resolvePackage(name, options)
}

Expand Down
7 changes: 7 additions & 0 deletions test/cjs.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ async function run() {
expect(resolveModule('@antfu/utils')).to.contain(join('node_modules', '@antfu', 'utils'))

expect(isPackageExists('unbuild')).to.eq(true)
expect(isPackageExists('unbuild', {
version: '1.0.0',
})).to.eq(false)

expect(isPackageExists('unbuild', {
version: '> 1 || < 3',
})).to.eq(true)
expect(isPackageExists('hi')).to.eq(false)
expect(isPackageExists('esno')).to.eq(true)

Expand Down
7 changes: 7 additions & 0 deletions test/esm.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ async function run() {
expect(resolveModule('@antfu/utils')).to.contain(join('node_modules', '@antfu', 'utils'))

expect(isPackageExists('unbuild')).to.eq(true)
expect(isPackageExists('unbuild', {
version: '1.0.0',
})).to.eq(false)

expect(isPackageExists('unbuild', {
version: '> 1 || < 3',
})).to.eq(true)
expect(isPackageExists('hi')).to.eq(false)
expect(isPackageExists('esno')).to.eq(true)

Expand Down
9 changes: 9 additions & 0 deletions test/source.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,15 @@ it('test by source', async () => {
expect(resolveModule('@antfu/utils')).to.contain(join('node_modules', '@antfu', 'utils'))

expect(isPackageExists('unbuild')).to.eq(true)

expect(isPackageExists('unbuild', {
version: '1.0.0',
})).to.eq(false)

expect(isPackageExists('unbuild', {
version: '> 1 || < 3',
})).to.eq(true)

expect(isPackageExists('hi')).to.eql(false)
expect(isPackageExists('esno')).to.eq(true)

Expand Down