-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fetch latest version if the info command is run without one
- Loading branch information
1 parent
edf65a6
commit 66c287d
Showing
3 changed files
with
89 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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<void|CommandContext>} | ||
*/ | ||
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: [email protected]') | ||
} | ||
|
||
const pkgName = rawPkgName.slice(0, versionSeparator) | ||
const pkgVersion = rawPkgName.slice(versionSeparator + 1) | ||
|
||
if (!pkgVersion) { | ||
throw new InputError('Need to specify a version, like eg: [email protected]') | ||
// 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 { | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters