Skip to content

Commit 07eca6d

Browse files
committed
fetch latest version if the info command is run without one
1 parent d4b792e commit 07eca6d

File tree

3 files changed

+88
-10
lines changed

3 files changed

+88
-10
lines changed

lib/commands/info/index.js

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import chalk from 'chalk'
44
import meow from 'meow'
5+
import fetch from 'node-fetch'
56
import ora from 'ora'
67

78
import { outputFlags, validationFlags } from '../../flags/index.js'
@@ -47,9 +48,9 @@ export const info = {
4748
* @param {string} description
4849
* @param {readonly string[]} argv
4950
* @param {ImportMeta} importMeta
50-
* @returns {void|CommandContext}
51+
* @returns {Promise<void|CommandContext>}
5152
*/
52-
function setupCommand (name, description, argv, importMeta) {
53+
async function setupCommand (name, description, argv, importMeta) {
5354
const flags = {
5455
...outputFlags,
5556
...validationFlags,
@@ -92,15 +93,24 @@ function setupCommand (name, description, argv, importMeta) {
9293

9394
const versionSeparator = rawPkgName.lastIndexOf('@')
9495

96+
let pkgVersion, pkgName
9597
if (versionSeparator < 1) {
96-
throw new InputError('Need to specify a full package identifier, like eg: [email protected]')
97-
}
98-
99-
const pkgName = rawPkgName.slice(0, versionSeparator)
100-
const pkgVersion = rawPkgName.slice(versionSeparator + 1)
101-
102-
if (!pkgVersion) {
103-
throw new InputError('Need to specify a version, like eg: [email protected]')
98+
// Get the latest version
99+
try {
100+
pkgName = rawPkgName
101+
const response = await fetch(`https://registry.npmjs.org/${rawPkgName}/latest`)
102+
/** @type any */
103+
const packageDetails = await response.json() || null
104+
105+
if (packageDetails?.version) {
106+
pkgVersion = packageDetails.version
107+
}
108+
} catch (e) {
109+
throw new Error('Issue fetching package version')
110+
}
111+
} else {
112+
pkgName = rawPkgName.slice(0, versionSeparator)
113+
pkgVersion = rawPkgName.slice(versionSeparator + 1)
104114
}
105115

106116
return {

package-lock.json

Lines changed: 67 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@
9191
"is-interactive": "^2.0.0",
9292
"is-unicode-supported": "^1.3.0",
9393
"meow": "^12.0.1",
94+
"node-fetch": "^3.3.2",
9495
"ora": "^6.1.2",
9596
"pony-cause": "^2.1.8",
9697
"prompts": "^2.4.2",

0 commit comments

Comments
 (0)