diff --git a/cli.js b/cli.js index e035014..46071af 100755 --- a/cli.js +++ b/cli.js @@ -2,7 +2,8 @@ import process from 'node:process'; import meow from 'meow'; import {excludeKeys} from 'filter-obj'; -import packageJson from 'package-json'; +import packageJson, {PackageNotFoundError, VersionNotFoundError} from 'package-json'; +import logSymbols from 'log-symbols'; const cli = meow(` Usage @@ -54,7 +55,19 @@ const options = { ...cli.flags, }; -let package_ = await packageJson(packageName, options); -package_ = excludeKeys(package_, key => key.startsWith('_') || key === 'directories'); +try { + let package_ = await packageJson(packageName, options); + package_ = excludeKeys(package_, key => key.startsWith('_') || key === 'directories'); -console.log(JSON.stringify(package_, undefined, ' ')); + console.log(JSON.stringify(package_, undefined, ' ')); +} catch (error) { + if (error instanceof PackageNotFoundError) { + console.error(`${logSymbols.error} Package \`${packageName}\` does not exist.`); + } else if (error instanceof VersionNotFoundError) { + console.error(`${logSymbols.error} Could not find version \`${version}\` of the package \`${packageName}\`.`); + } else { + throw error; + } + + process.exitCode = 1; +} diff --git a/package.json b/package.json index 650e670..178ca5c 100644 --- a/package.json +++ b/package.json @@ -38,6 +38,7 @@ ], "dependencies": { "filter-obj": "^5.1.0", + "log-symbols": "^6.0.0", "meow": "^13.2.0", "package-json": "^10.0.0" }, diff --git a/test.js b/test.js index b418032..afd4bc7 100644 --- a/test.js +++ b/test.js @@ -1,11 +1,15 @@ import test from 'ava'; import {execa} from 'execa'; -const verify = test.macro(async (t, {args, expected}) => { - const {stdout} = await execa('./cli.js', args.split(' ')); - const json = JSON.parse(stdout); +const verify = test.macro(async (t, {args, expected, error}) => { + const {stdout, stderr} = await execa('./cli.js', args.split(' '), {reject: false, env: {NO_COLOR: '1'}}); - t.like(json, expected); + if (error) { + t.is(stderr, error); + } else { + const json = JSON.parse(stdout); + t.like(json, expected); + } }); test('main', verify, { @@ -26,6 +30,16 @@ test('can specify a version', verify, { }, }); +test('package does not exist', verify, { + args: 'nnnope 1.0.0', + error: '✖ Package `nnnope` does not exist.', +}); + +test('version does not exist', verify, { + args: 'ava 0.0.0', + error: '✖ Could not find version `0.0.0` of the package `ava`.', +}); + test('flags: --full-metadata', verify, { args: 'ava --full-metadata', expected: { @@ -58,17 +72,17 @@ test('flags: --registry-url', verify, { }, }); -test('flags: --no-deprecated', async t => { - const {stdout} = await execa('./cli.js', ['querystring']); - const json = JSON.parse(stdout); - - t.truthy(json.deprecated); - - const {stderr} = await t.throwsAsync( - execa('./cli.js', ['querystring', '0.2', '--no-deprecated']), - ); +test('includes deprecated versions by default', verify, { + args: 'querystring', + expected: { + name: 'querystring', + deprecated: 'The querystring API is considered Legacy. new code should use the URLSearchParams API instead.', + }, +}); - t.regex(stderr, /VersionNotFoundError/); +test('flags: --no-deprecated', verify, { + args: 'querystring 0.2 --no-deprecated', + error: '✖ Could not find version `0.2` of the package `querystring`.', }); test('flags: combined', verify, {