-
Notifications
You must be signed in to change notification settings - Fork 162
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf: use
npm-registry-fetch
instead of pacote
to deduce the pack…
…age size
- Loading branch information
Showing
4 changed files
with
172 additions
and
489 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,13 +2,36 @@ import { createRequire } from 'node:module' | |
import { logger, useNuxt } from '@nuxt/kit' | ||
import { readPackageJSON } from 'pkg-types' | ||
import semver from 'semver' | ||
import { joinURL } from 'ufo' | ||
import { getPackageInfo } from 'local-pkg' | ||
import type { PackageUpdateInfo } from '../types' | ||
|
||
export async function getMainPackageJSON(nuxt = useNuxt()) { | ||
return readPackageJSON(nuxt.options.rootDir) | ||
} | ||
|
||
export interface Packument { | ||
'name': string | ||
/** | ||
* An object where each key is a version, and each value is the manifest for | ||
* that version. | ||
*/ | ||
'versions': Record<string, Omit<Packument, 'versions'>> | ||
/** | ||
* An object mapping dist-tags to version numbers. This is how `foo@latest` | ||
* gets turned into `[email protected]`. | ||
*/ | ||
'dist-tags': { latest: string } & Record<string, string> | ||
/** | ||
* In the full packument, an object mapping version numbers to publication | ||
* times, for the `opts.before` functionality. | ||
*/ | ||
'time': Record<string, string> & { | ||
created: string | ||
modified: string | ||
} | ||
} | ||
|
||
export async function checkForUpdateOf(name: string, current?: string, nuxt = useNuxt()): Promise<PackageUpdateInfo | undefined> { | ||
try { | ||
if (!current) { | ||
|
@@ -22,11 +45,18 @@ export async function checkForUpdateOf(name: string, current?: string, nuxt = us | |
if (!current) | ||
return | ||
|
||
const packument = await import('pacote').then(r => r.default?.packument || r.packument) | ||
const manifest = await packument(name) | ||
const { pickRegistry, json: fetchMeta } = await import('npm-registry-fetch') | ||
const reg = pickRegistry(name) | ||
const manifest = await fetchMeta(joinURL(reg, name), { | ||
headers: { | ||
'user-agent': `npm node/${process.version}`, | ||
'accept': 'application/json', | ||
}, | ||
spec: name, | ||
}) as unknown as Packument | ||
|
||
const latest = manifest['dist-tags'].latest | ||
const needsUpdate = latest !== current && semver.lt(current, latest) | ||
const latest = manifest?.['dist-tags']?.latest | ||
const needsUpdate = !!latest && latest !== current && semver.lt(current, latest) | ||
|
||
return { | ||
name, | ||
|
Oops, something went wrong.