|
1 |
| -// issuer.endsWith(path.posix.sep) || issuer.endsWith(path.win32.sep) |
2 |
| -import findUp from 'next/dist/compiled/find-up' |
3 |
| -import * as path from 'path' |
4 | 1 | import { promises as fs } from 'fs'
|
| 2 | +import findUp from 'next/dist/compiled/find-up' |
5 | 3 | import JSON5 from 'next/dist/compiled/json5'
|
| 4 | +import * as path from 'path' |
6 | 5 | import { resolveRequest } from './resolve-request'
|
7 | 6 |
|
8 |
| -export async function getPackageVersion({ |
| 7 | +type PackageJsonDependencies = { |
| 8 | + dependencies: Record<string, string> |
| 9 | + devDependencies: Record<string, string> |
| 10 | +} |
| 11 | + |
| 12 | +let cachedDeps: PackageJsonDependencies |
| 13 | + |
| 14 | +async function getDependencies({ |
9 | 15 | cwd,
|
10 |
| - name, |
11 | 16 | }: {
|
12 | 17 | cwd: string
|
13 |
| - name: string |
14 |
| -}): Promise<string | null> { |
| 18 | +}): Promise<PackageJsonDependencies> { |
| 19 | + if (cachedDeps) { |
| 20 | + return cachedDeps |
| 21 | + } |
| 22 | + |
15 | 23 | const configurationPath: string | undefined = await findUp('package.json', {
|
16 | 24 | cwd,
|
17 | 25 | })
|
18 | 26 | if (!configurationPath) {
|
19 |
| - return null |
| 27 | + return (cachedDeps = { dependencies: {}, devDependencies: {} }) |
20 | 28 | }
|
21 | 29 |
|
22 | 30 | const content = await fs.readFile(configurationPath, 'utf-8')
|
23 | 31 | const packageJson: any = JSON5.parse(content)
|
24 | 32 |
|
25 | 33 | const { dependencies = {}, devDependencies = {} } = packageJson || {}
|
| 34 | + return (cachedDeps = { dependencies, devDependencies }) |
| 35 | +} |
| 36 | + |
| 37 | +export async function getPackageVersion({ |
| 38 | + cwd, |
| 39 | + name, |
| 40 | +}: { |
| 41 | + cwd: string |
| 42 | + name: string |
| 43 | +}): Promise<string | null> { |
| 44 | + const { dependencies, devDependencies } = await getDependencies({ cwd }) |
26 | 45 | if (!(dependencies[name] || devDependencies[name])) {
|
27 | 46 | return null
|
28 | 47 | }
|
|
0 commit comments