From b263306c403a3d0a54328233d8819f5c305a862b Mon Sep 17 00:00:00 2001 From: Sindre Sorhus Date: Thu, 22 Jul 2021 12:57:47 +0200 Subject: [PATCH] Require Node.js 12.20 and move to ESM --- .github/workflows/main.yml | 5 ++-- index.d.ts | 56 ++++++++++++++------------------------ index.js | 11 ++------ index.test-d.ts | 2 +- license | 2 +- package.json | 19 +++++++------ readme.md | 22 ++++++--------- test.js | 2 +- 8 files changed, 47 insertions(+), 72 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 18531b3..3b8aa86 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -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 diff --git a/index.d.ts b/index.d.ts index 8b3a89a..8149f74 100644 --- a/index.d.ts +++ b/index.d.ts @@ -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; +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; - // 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; diff --git a/index.js b/index.js index 685e574..fa3077f 100644 --- a/index.js +++ b/index.js @@ -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; +} diff --git a/index.test-d.ts b/index.test-d.ts index 2eba693..01f38f9 100644 --- a/index.test-d.ts +++ b/index.test-d.ts @@ -1,5 +1,5 @@ import {expectType} from 'tsd'; -import latestVersion = require('.'); +import latestVersion from './index.js'; expectType>(latestVersion('ava')); expectType>(latestVersion('npm', {version: 'latest-5'})); diff --git a/license b/license index e7af2f7..fa7ceba 100644 --- a/license +++ b/license @@ -1,6 +1,6 @@ MIT License -Copyright (c) Sindre Sorhus (sindresorhus.com) +Copyright (c) Sindre Sorhus (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: diff --git a/package.json b/package.json index 28c4ef2..022a13f 100644 --- a/package.json +++ b/package.json @@ -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": "sindresorhus@gmail.com", - "url": "sindresorhus.com" + "url": "https://sindresorhus.com" }, + "type": "module", + "exports": "./index.js", "engines": { - "node": ">=8" + "node": ">=12.20" }, "scripts": { "test": "xo && ava && tsd" @@ -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" } } diff --git a/readme.md b/readme.md index d49033d..f8036f7 100644 --- a/readme.md +++ b/readme.md @@ -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 - ---
diff --git a/test.js b/test.js index 2c540bd..7b81cd4 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 '.'; +import latestVersion from './index.js'; test('latest version', async t => { t.regex(await latestVersion('ava'), semverRegex());