From 07eca6d3a0c5dd9a440a7b83ff896648a5bc80c4 Mon Sep 17 00:00:00 2001 From: Charlie Gerard Date: Thu, 9 Nov 2023 13:26:38 +1100 Subject: [PATCH 1/4] fetch latest version if the info command is run without one --- lib/commands/info/index.js | 30 +++++++++++------ package-lock.json | 67 ++++++++++++++++++++++++++++++++++++++ package.json | 1 + 3 files changed, 88 insertions(+), 10 deletions(-) diff --git a/lib/commands/info/index.js b/lib/commands/info/index.js index 02b37d1..3f489cd 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' @@ -47,9 +48,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, @@ -92,15 +93,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 e807f94..8b067b1 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 e8ec807..ea38614 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", From 17b15e06df69976bc52fd5de87a769af753ba645 Mon Sep 17 00:00:00 2001 From: Charlie Gerard Date: Tue, 28 Nov 2023 13:27:51 -0800 Subject: [PATCH 2/4] working --- lib/commands/info/index.js | 18 ++-------- package-lock.json | 67 -------------------------------------- package.json | 1 - 3 files changed, 3 insertions(+), 83 deletions(-) diff --git a/lib/commands/info/index.js b/lib/commands/info/index.js index 3f489cd..4c63a86 100644 --- a/lib/commands/info/index.js +++ b/lib/commands/info/index.js @@ -2,7 +2,6 @@ import chalk from 'chalk' import meow from 'meow' -import fetch from 'node-fetch' import ora from 'ora' import { outputFlags, validationFlags } from '../../flags/index.js' @@ -48,9 +47,9 @@ export const info = { * @param {string} description * @param {readonly string[]} argv * @param {ImportMeta} importMeta - * @returns {Promise} + * @returns {void|CommandContext} */ -async function setupCommand (name, description, argv, importMeta) { +function setupCommand (name, description, argv, importMeta) { const flags = { ...outputFlags, ...validationFlags, @@ -95,19 +94,8 @@ async function setupCommand (name, description, argv, importMeta) { let pkgVersion, pkgName if (versionSeparator < 1) { - // 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') - } + pkgVersion = 'latest' } else { pkgName = rawPkgName.slice(0, versionSeparator) pkgVersion = rawPkgName.slice(versionSeparator + 1) diff --git a/package-lock.json b/package-lock.json index 8b067b1..e807f94 100644 --- a/package-lock.json +++ b/package-lock.json @@ -20,7 +20,6 @@ "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", @@ -1894,14 +1893,6 @@ "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", @@ -3569,36 +3560,6 @@ "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", @@ -3720,17 +3681,6 @@ "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", @@ -5651,23 +5601,6 @@ "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 ea38614..e8ec807 100644 --- a/package.json +++ b/package.json @@ -91,7 +91,6 @@ "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", From e04dc92194d4a7b6449826c46729ab4443a2569d Mon Sep 17 00:00:00 2001 From: Charlie Gerard Date: Tue, 28 Nov 2023 13:47:02 -0800 Subject: [PATCH 3/4] small improvement --- lib/commands/info/index.js | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/lib/commands/info/index.js b/lib/commands/info/index.js index 4c63a86..4c0da32 100644 --- a/lib/commands/info/index.js +++ b/lib/commands/info/index.js @@ -21,7 +21,8 @@ export const info = { const input = setupCommand(name, info.description, argv, importMeta) if (input) { - const spinner = ora(`Looking up data for version ${input.pkgVersion} of ${input.pkgName}\n`).start() + const spinnerText = input.pkgVersion === 'latest' ? `Looking up data for the latest version of ${input.pkgName}\n` : `Looking up data for version ${input.pkgVersion} of ${input.pkgName}\n` + const spinner = ora(spinnerText).start() const packageData = await fetchPackageData(input.pkgName, input.pkgVersion, input, spinner) if (packageData) { formatPackageDataOutput(packageData, { name, ...input }, spinner) @@ -92,14 +93,8 @@ function setupCommand (name, description, argv, importMeta) { const versionSeparator = rawPkgName.lastIndexOf('@') - let pkgVersion, pkgName - if (versionSeparator < 1) { - pkgName = rawPkgName - pkgVersion = 'latest' - } else { - pkgName = rawPkgName.slice(0, versionSeparator) - pkgVersion = rawPkgName.slice(versionSeparator + 1) - } + const pkgName = versionSeparator < 1 ? rawPkgName : rawPkgName.slice(0, versionSeparator) + const pkgVersion = versionSeparator < 1 ? 'latest' : rawPkgName.slice(versionSeparator + 1) return { includeAllIssues, From b60f0282b449222c0f4064d5227ea190cd968624 Mon Sep 17 00:00:00 2001 From: Charlie Gerard Date: Tue, 28 Nov 2023 14:23:57 -0800 Subject: [PATCH 4/4] text update --- lib/commands/info/index.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/commands/info/index.js b/lib/commands/info/index.js index 4c0da32..4890350 100644 --- a/lib/commands/info/index.js +++ b/lib/commands/info/index.js @@ -177,7 +177,11 @@ async function fetchPackageData (pkgName, pkgVersion, { includeAllIssues }, spin // Link to issues list const format = new ChalkOrMarkdown(!!outputMarkdown) const url = `https://socket.dev/npm/package/${pkgName}/overview/${pkgVersion}` - console.log('\nDetailed info on socket.dev: ' + format.hyperlink(`${pkgName} v${pkgVersion}`, url, { fallbackToUrl: true })) + if (pkgVersion === 'latest') { + console.log('\nDetailed info on socket.dev: ' + format.hyperlink(`${pkgName}`, url, { fallbackToUrl: true })) + } else { + console.log('\nDetailed info on socket.dev: ' + format.hyperlink(`${pkgName} v${pkgVersion}`, url, { fallbackToUrl: true })) + } if (!outputMarkdown) { console.log(chalk.dim('\nOr rerun', chalk.italic(name), 'using the', chalk.italic('--json'), 'flag to get full JSON output')) }