diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 346585c..a768beb 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -10,6 +10,7 @@ jobs: fail-fast: false matrix: node-version: + - 21 - 20 - 18 steps: diff --git a/index.d.ts b/index.d.ts index b9be850..d4d90a7 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1,9 +1,8 @@ -export type Options = { - /** - A semver range or [dist-tag](https://docs.npmjs.com/cli/dist-tag). - */ - readonly version?: string; -}; +import type {Options as PackageJsonOptions} from 'package-json'; + +export {PackageNotFoundError, VersionNotFoundError} from 'package-json'; + +export type Options = Pick; /** Get the latest version of an npm package. @@ -13,14 +12,14 @@ Get the latest version of an npm package. import latestVersion from 'latest-version'; console.log(await latestVersion('ava')); -//=> '0.18.0' +//=> '6.1.1' console.log(await latestVersion('@sindresorhus/df')); -//=> '1.0.1' +//=> '4.0.0' // Also works with semver ranges and dist-tags console.log(await latestVersion('npm', {version: 'latest-5'})); -//=> '5.5.1' +//=> '5.10.0' ``` */ export default function latestVersion(packageName: string, options?: Options): Promise; diff --git a/index.js b/index.js index fa3077f..bb690b8 100644 --- a/index.js +++ b/index.js @@ -1,5 +1,7 @@ import packageJson from 'package-json'; +export {PackageNotFoundError, VersionNotFoundError} from 'package-json'; + export default async function latestVersion(packageName, options) { const {version} = await packageJson(packageName.toLowerCase(), options); return version; diff --git a/index.test-d.ts b/index.test-d.ts index 01f38f9..d639c18 100644 --- a/index.test-d.ts +++ b/index.test-d.ts @@ -3,3 +3,5 @@ import latestVersion from './index.js'; expectType>(latestVersion('ava')); expectType>(latestVersion('npm', {version: 'latest-5'})); +expectType>(latestVersion('npm', {registryUrl: 'https://registry.yarnpkg.com'})); +expectType>(latestVersion('npm', {omitDeprecated: false})); diff --git a/package.json b/package.json index 3df8ce8..4a9b8a0 100644 --- a/package.json +++ b/package.json @@ -37,13 +37,13 @@ "module" ], "dependencies": { - "package-json": "^9.0.0" + "package-json": "^10.0.0" }, "devDependencies": { - "ava": "^6.1.0", - "semver": "^7.5.4", + "ava": "^6.1.1", + "semver": "^7.6.0", "semver-regex": "^4.0.5", - "tsd": "^0.30.4", - "xo": "^0.56.0" + "tsd": "^0.30.7", + "xo": "^0.57.0" } } diff --git a/readme.md b/readme.md index 2a1ae12..dfd4a6d 100644 --- a/readme.md +++ b/readme.md @@ -16,16 +16,18 @@ npm install latest-version import latestVersion from 'latest-version'; console.log(await latestVersion('ava')); -//=> '0.18.0' +//=> '6.1.1' console.log(await latestVersion('@sindresorhus/df')); -//=> '1.0.1' +//=> '4.0.0' // Also works with semver ranges and dist-tags console.log(await latestVersion('npm', {version: 'latest-5'})); -//=> '5.5.1' +//=> '5.10.0' ``` +This package exposes the [`version`](https://github.com/sindresorhus/package-json#version), [`registryUrl`](https://github.com/sindresorhus/package-json#registryurl), and [`omitDeprecated`](https://github.com/sindresorhus/package-json#omitdeprecated) options from [`package-json`](https://github.com/sindresorhus/package-json#options), as well as the [`PackageNotFoundError`](https://github.com/sindresorhus/package-json#packagenotfounderror) and [`VersionNotFoundError`](https://github.com/sindresorhus/package-json#versionnotfounderror) errors. + ## Related - [latest-version-cli](https://github.com/sindresorhus/latest-version-cli) - CLI for this module diff --git a/test.js b/test.js index 7b81cd4..f5a5111 100644 --- a/test.js +++ b/test.js @@ -1,7 +1,7 @@ import test from 'ava'; import semver from 'semver'; import semverRegex from 'semver-regex'; -import latestVersion from './index.js'; +import latestVersion, {PackageNotFoundError, VersionNotFoundError} from './index.js'; test('latest version', async t => { t.regex(await latestVersion('ava'), semverRegex()); @@ -18,3 +18,16 @@ test('latest version with dist-tag', async t => { test('latest version scoped', async t => { t.regex(await latestVersion('@sindresorhus/df'), semverRegex()); }); + +test('registry url', async t => { + t.regex(await latestVersion('npm', {registryUrl: 'https://registry.yarnpkg.com/'}), semverRegex()); +}); + +test('include deprecated', async t => { + t.regex(await latestVersion('querystring', {version: '0.2', omitDeprecated: false}), semverRegex()); +}); + +test('throws if not found', async t => { + await t.throwsAsync(latestVersion('nnnope'), {instanceOf: PackageNotFoundError}); + await t.throwsAsync(latestVersion('npm', {version: '0.0.0'}), {instanceOf: VersionNotFoundError}); +});