Skip to content

Commit

Permalink
Update package-json, expose some options (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
tommy-mitchell committed Feb 27, 2024
1 parent cb09280 commit 69ff072
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 18 deletions.
1 change: 1 addition & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ jobs:
fail-fast: false
matrix:
node-version:
- 21
- 20
- 18
steps:
Expand Down
17 changes: 8 additions & 9 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -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<PackageJsonOptions, 'version' | 'registryUrl' | 'omitDeprecated'>;

/**
Get the latest version of an npm package.
Expand All @@ -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<string>;
2 changes: 2 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
2 changes: 2 additions & 0 deletions index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ import latestVersion from './index.js';

expectType<Promise<string>>(latestVersion('ava'));
expectType<Promise<string>>(latestVersion('npm', {version: 'latest-5'}));
expectType<Promise<string>>(latestVersion('npm', {registryUrl: 'https://registry.yarnpkg.com'}));
expectType<Promise<string>>(latestVersion('npm', {omitDeprecated: false}));
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
8 changes: 5 additions & 3 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 14 additions & 1 deletion test.js
Original file line number Diff line number Diff line change
@@ -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());
Expand All @@ -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});
});

0 comments on commit 69ff072

Please sign in to comment.