Skip to content

Commit

Permalink
chore: add utils
Browse files Browse the repository at this point in the history
  • Loading branch information
Mister-Hope committed Jan 31, 2024
1 parent b59d315 commit 38d7130
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
14 changes: 14 additions & 0 deletions tools/helper/src/node/utils/getInstalledStatus.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { createRequire } from 'node:module'

export const getInstalledStatus = (
pkg: string,
currentUrl: string,
): boolean => {
try {
pkg && createRequire(currentUrl).resolve(pkg)

return true
} catch (error) {
return false
}
}
1 change: 1 addition & 0 deletions tools/helper/src/node/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './getInstalledStatus.js'
export * from './getRealPath.js'
export * from './logger.js'
export * from './packageManager.js'
22 changes: 22 additions & 0 deletions tools/helper/tests/node/getInstalledStatus.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { describe, expect, it } from 'vitest'
import { getInstalledStatus } from '../../src/node/utils/getInstalledStatus.js'

describe('getInstalledStatus', () => {
it('should return true with deps', () => {
expect(getInstalledStatus('vue', import.meta.url)).toEqual(true)
})

it('should return true with built-in', () => {
expect(getInstalledStatus('path', import.meta.url)).toEqual(true)
expect(getInstalledStatus('node:path', import.meta.url)).toEqual(true)
})

it('should return true with built-in', () => {
expect(getInstalledStatus('path', import.meta.url)).toEqual(true)
expect(getInstalledStatus('node:path', import.meta.url)).toEqual(true)
})

it('should return false with not existed', () => {
expect(getInstalledStatus('not-existed', import.meta.url)).toEqual(false)
})
})

0 comments on commit 38d7130

Please sign in to comment.