diff --git a/lib/commands/info/index.js b/lib/commands/info/index.js index abee2416..97025522 100644 --- a/lib/commands/info/index.js +++ b/lib/commands/info/index.js @@ -2,6 +2,7 @@ import chalk from 'chalk' import meow from 'meow' +import fetch from 'node-fetch' import ora from 'ora' import { outputFlags, validationFlags } from '../../flags/index.js' @@ -19,7 +20,7 @@ export const info = { async run (argv, importMeta, { parentName }) { const name = parentName + ' info' - const input = setupCommand(name, info.description, argv, importMeta) + const input = await setupCommand(name, info.description, argv, importMeta) const packageData = input && await fetchPackageData(input.pkgName, input.pkgVersion, input) if (packageData) { @@ -45,9 +46,9 @@ export const info = { * @param {string} description * @param {readonly string[]} argv * @param {ImportMeta} importMeta - * @returns {void|CommandContext} + * @returns {Promise} */ -function setupCommand (name, description, argv, importMeta) { +async function setupCommand (name, description, argv, importMeta) { const flags = { ...outputFlags, ...validationFlags, @@ -90,15 +91,24 @@ function setupCommand (name, description, argv, importMeta) { const versionSeparator = rawPkgName.lastIndexOf('@') + let pkgVersion, pkgName if (versionSeparator < 1) { - throw new InputError('Need to specify a full package identifier, like eg: webtorrent@1.0.0') - } - - const pkgName = rawPkgName.slice(0, versionSeparator) - const pkgVersion = rawPkgName.slice(versionSeparator + 1) - - if (!pkgVersion) { - throw new InputError('Need to specify a version, like eg: webtorrent@1.0.0') + // Get the latest version + try { + pkgName = rawPkgName + const response = await fetch(`https://registry.npmjs.org/${rawPkgName}/latest`) + /** @type any */ + const packageDetails = await response.json() || null + + if (packageDetails?.version) { + pkgVersion = packageDetails.version + } + } catch (e) { + throw new Error('Issue fetching package version') + } + } else { + pkgName = rawPkgName.slice(0, versionSeparator) + pkgVersion = rawPkgName.slice(versionSeparator + 1) } return { diff --git a/package-lock.json b/package-lock.json index 317c36cc..e572af32 100644 --- a/package-lock.json +++ b/package-lock.json @@ -20,6 +20,7 @@ "is-interactive": "^2.0.0", "is-unicode-supported": "^1.3.0", "meow": "^12.0.1", + "node-fetch": "^3.3.2", "ora": "^6.1.2", "pony-cause": "^2.1.8", "prompts": "^2.4.2", @@ -1893,6 +1894,14 @@ "url": "https://github.com/sponsors/sindresorhus" } }, + "node_modules/data-uri-to-buffer": { + "version": "4.0.1", + "resolved": "https://registry.npmjs.org/data-uri-to-buffer/-/data-uri-to-buffer-4.0.1.tgz", + "integrity": "sha512-0R9ikRb668HB7QDxT1vkpuUBtqc53YyAwMwGeUFKRojY/NWKvdZ+9UYtRfGmhqNbRkTSVpMbmyhXipFFv2cb/A==", + "engines": { + "node": ">= 12" + } + }, "node_modules/debug": { "version": "4.3.4", "resolved": "https://registry.npmjs.org/debug/-/debug-4.3.4.tgz", @@ -3560,6 +3569,36 @@ "reusify": "^1.0.4" } }, + "node_modules/fetch-blob": { + "version": "3.2.0", + "resolved": "https://registry.npmjs.org/fetch-blob/-/fetch-blob-3.2.0.tgz", + "integrity": "sha512-7yAQpD2UMJzLi1Dqv7qFYnPbaPx7ZfFK6PiIxQ4PfkGPyNyl2Ugx+a/umUonmKqjhM4DnfbMvdX6otXq83soQQ==", + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/jimmywarting" + }, + { + "type": "paypal", + "url": "https://paypal.me/jimmywarting" + } + ], + "dependencies": { + "node-domexception": "^1.0.0", + "web-streams-polyfill": "^3.0.3" + }, + "engines": { + "node": "^12.20 || >= 14.13" + } + }, + "node_modules/fetch-blob/node_modules/web-streams-polyfill": { + "version": "3.2.1", + "resolved": "https://registry.npmjs.org/web-streams-polyfill/-/web-streams-polyfill-3.2.1.tgz", + "integrity": "sha512-e0MO3wdXWKrLbL0DgGnUV7WHVuw9OUvL4hjgnPkIeEvESk74gAITi5G606JtZPp39cd8HA9VQzCIvA49LpPN5Q==", + "engines": { + "node": ">= 8" + } + }, "node_modules/file-entry-cache": { "version": "6.0.1", "resolved": "https://registry.npmjs.org/file-entry-cache/-/file-entry-cache-6.0.1.tgz", @@ -3681,6 +3720,17 @@ "node": ">= 14.17" } }, + "node_modules/formdata-polyfill": { + "version": "4.0.10", + "resolved": "https://registry.npmjs.org/formdata-polyfill/-/formdata-polyfill-4.0.10.tgz", + "integrity": "sha512-buewHzMvYL29jdeQTVILecSaZKnt/RJWjoZCF5OW60Z67/GmSLBkOFM7qh1PI3zFNtJbaZL5eQu1vLfazOwj4g==", + "dependencies": { + "fetch-blob": "^3.1.2" + }, + "engines": { + "node": ">=12.20.0" + } + }, "node_modules/fs.realpath": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz", @@ -5601,6 +5651,23 @@ "node": ">=10.5.0" } }, + "node_modules/node-fetch": { + "version": "3.3.2", + "resolved": "https://registry.npmjs.org/node-fetch/-/node-fetch-3.3.2.tgz", + "integrity": "sha512-dRB78srN/l6gqWulah9SrxeYnxeddIG30+GOqK/9OlLVyLg3HPnr6SqOWTWOXKRwC2eGYCkZ59NNuSgvSrpgOA==", + "dependencies": { + "data-uri-to-buffer": "^4.0.0", + "fetch-blob": "^3.1.4", + "formdata-polyfill": "^4.0.10" + }, + "engines": { + "node": "^12.20.0 || ^14.13.1 || >=16.0.0" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/node-fetch" + } + }, "node_modules/node-source-walk": { "version": "4.3.0", "resolved": "https://registry.npmjs.org/node-source-walk/-/node-source-walk-4.3.0.tgz", diff --git a/package.json b/package.json index 0b1c8092..a01ce1ba 100644 --- a/package.json +++ b/package.json @@ -91,6 +91,7 @@ "is-interactive": "^2.0.0", "is-unicode-supported": "^1.3.0", "meow": "^12.0.1", + "node-fetch": "^3.3.2", "ora": "^6.1.2", "pony-cause": "^2.1.8", "prompts": "^2.4.2",