Skip to content

Commit

Permalink
Require Node.js 12.20 and move to ESM
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Jul 22, 2021
1 parent 5cd4fc2 commit b263306
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 72 deletions.
5 changes: 2 additions & 3 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,12 @@ jobs:
fail-fast: false
matrix:
node-version:
- 16
- 14
- 12
- 10
- 8
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v1
- uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node-version }}
- run: npm install
Expand Down
56 changes: 20 additions & 36 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,42 +1,26 @@
declare namespace latestVersion {
interface Options {
/**
A semver range or [dist-tag](https://docs.npmjs.com/cli/dist-tag).
*/
readonly version?: string;
}
}

declare const latestVersion: {
export interface Options {
/**
Get the latest version of an npm package.
@example
```
import latestVersion = require('latest-version');
A semver range or [dist-tag](https://docs.npmjs.com/cli/dist-tag).
*/
readonly version?: string;
}

(async () => {
console.log(await latestVersion('ava'));
//=> '0.18.0'
/**
Get the latest version of an npm package.
console.log(await latestVersion('@sindresorhus/df'));
//=> '1.0.1'
@example
```
import latestVersion from 'latest-version';
// Also works with semver ranges and dist-tags
console.log(await latestVersion('npm', {version: 'latest-5'}));
//=> '5.5.1'
})();
```
*/
(packageName: string, options?: latestVersion.Options): Promise<string>;
console.log(await latestVersion('ava'));
//=> '0.18.0'
// TODO: Remove this for the next major release, refactor the whole definition to:
// declare function latestVersion(
// packageName: string,
// options?: latestVersion.Options
// ): Promise<string>;
// export = latestVersion;
default: typeof latestVersion;
};
console.log(await latestVersion('@sindresorhus/df'));
//=> '1.0.1'
export = latestVersion;
// Also works with semver ranges and dist-tags
console.log(await latestVersion('npm', {version: 'latest-5'}));
//=> '5.5.1'
```
*/
export default function latestVersion(packageName: string, options?: Options): Promise<string>;
11 changes: 3 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
'use strict';
const packageJson = require('package-json');
import packageJson from 'package-json';

const latestVersion = async (packageName, options) => {
export default async function latestVersion(packageName, options) {
const {version} = await packageJson(packageName.toLowerCase(), options);
return version;
};

module.exports = latestVersion;
// TODO: Remove this for the next major release
module.exports.default = latestVersion;
}
2 changes: 1 addition & 1 deletion index.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {expectType} from 'tsd';
import latestVersion = require('.');
import latestVersion from './index.js';

expectType<Promise<string>>(latestVersion('ava'));
expectType<Promise<string>>(latestVersion('npm', {version: 'latest-5'}));
2 changes: 1 addition & 1 deletion license
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) Sindre Sorhus <[email protected]> (sindresorhus.com)
Copyright (c) Sindre Sorhus <[email protected]> (https://sindresorhus.com)

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

Expand Down
19 changes: 11 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@
"description": "Get the latest version of an npm package",
"license": "MIT",
"repository": "sindresorhus/latest-version",
"funding": "https://github.com/sponsors/sindresorhus",
"author": {
"name": "Sindre Sorhus",
"email": "[email protected]",
"url": "sindresorhus.com"
"url": "https://sindresorhus.com"
},
"type": "module",
"exports": "./index.js",
"engines": {
"node": ">=8"
"node": ">=12.20"
},
"scripts": {
"test": "xo && ava && tsd"
Expand All @@ -30,13 +33,13 @@
"module"
],
"dependencies": {
"package-json": "^6.3.0"
"package-json": "^7.0.0"
},
"devDependencies": {
"ava": "^1.4.1",
"semver": "^6.0.0",
"semver-regex": "^2.0.0",
"tsd": "^0.7.2",
"xo": "^0.24.0"
"ava": "^3.15.0",
"semver": "^7.3.5",
"semver-regex": "^4.0.0",
"tsd": "^0.17.0",
"xo": "^0.42.0"
}
}
22 changes: 8 additions & 14 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,39 +4,33 @@
Fetches the version directly from the registry instead of depending on the massive [npm](https://github.com/npm/npm/blob/8b5e7b6ae5b4cd2d7d62eaf93b1428638b387072/package.json#L37-L85) module like the [latest](https://github.com/bahamas10/node-latest) module does.


## Install

```
$ npm install latest-version
```


## Usage

```js
const latestVersion = require('latest-version');
import latestVersion from 'latest-version';

(async () => {
console.log(await latestVersion('ava'));
//=> '0.18.0'
console.log(await latestVersion('ava'));
//=> '0.18.0'

console.log(await latestVersion('@sindresorhus/df'));
//=> '1.0.1'
console.log(await latestVersion('@sindresorhus/df'));
//=> '1.0.1'

// Also works with semver ranges and dist-tags
console.log(await latestVersion('npm', {version: 'latest-5'}));
//=> '5.5.1'
})();
// Also works with semver ranges and dist-tags
console.log(await latestVersion('npm', {version: 'latest-5'}));
//=> '5.5.1'
```


## Related

- [latest-version-cli](https://github.com/sindresorhus/latest-version-cli) - CLI for this module
- [package-json](https://github.com/sindresorhus/package-json) - Get the package.json of a package from the npm registry


---

<div align="center">
Expand Down
2 changes: 1 addition & 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 '.';
import latestVersion from './index.js';

test('latest version', async t => {
t.regex(await latestVersion('ava'), semverRegex());
Expand Down

0 comments on commit b263306

Please sign in to comment.