From b621c60438a54a808602739d72831d8610908cf1 Mon Sep 17 00:00:00 2001 From: tommy-mitchell Date: Wed, 28 Feb 2024 15:18:32 -0600 Subject: [PATCH 1/4] clean up `description` parsing --- source/index.js | 20 +++++++++++--------- source/options.js | 12 +++++++++--- test/options/help.js | 17 +++++++++++++++-- 3 files changed, 35 insertions(+), 14 deletions(-) diff --git a/source/index.js b/source/index.js index 7881e24..cfcac6c 100644 --- a/source/index.js +++ b/source/index.js @@ -3,13 +3,11 @@ import parseArguments from 'yargs-parser'; import camelCaseKeys from 'camelcase-keys'; import {trimNewlines} from 'trim-newlines'; import redent from 'redent'; -import normalizePackageData from 'normalize-package-data'; import {buildOptions} from './options.js'; import {buildParserOptions} from './parser.js'; import {validate, checkUnknownFlags, checkMissingRequiredFlags} from './validate.js'; -const buildResult = (options, parserOptions) => { - const {pkg: package_} = options; +const buildResult = ({pkg: packageJson, ...options}, parserOptions) => { const argv = parseArguments(options.argv, parserOptions); let help = ''; @@ -23,12 +21,16 @@ const buildResult = (options, parserOptions) => { help = `\n${help}`; } - normalizePackageData(package_); + if (options.description !== false) { + let {description} = options; - let description = options.description ?? package_.description; + if (description) { + description = help ? redent(`\n${description}\n`, options.helpIndent) : `\n${description}`; + help = `${description}${help}`; + } + } - description &&= help ? redent(`\n${description}\n`, options.helpIndent) : `\n${description}`; - help = `${description || ''}${help}\n`; + help += '\n'; const showHelp = code => { console.log(help); @@ -36,7 +38,7 @@ const buildResult = (options, parserOptions) => { }; const showVersion = () => { - console.log(typeof options.version === 'string' ? options.version : package_.version); + console.log(typeof options.version === 'string' ? options.version : packageJson.version); process.exit(0); }; @@ -76,7 +78,7 @@ const buildResult = (options, parserOptions) => { input, flags, unnormalizedFlags, - pkg: package_, + pkg: packageJson, help, showHelp, showVersion, diff --git a/source/options.js b/source/options.js index d46d317..2961e27 100644 --- a/source/options.js +++ b/source/options.js @@ -2,6 +2,7 @@ import process from 'node:process'; import {dirname} from 'node:path'; import {fileURLToPath} from 'node:url'; import {readPackageUpSync} from 'read-package-up'; +import normalizePackageData from 'normalize-package-data'; import {decamelizeFlagKey, joinFlagKeys} from './utils.js'; const validateOptions = options => { @@ -63,17 +64,21 @@ export const buildOptions = (helpText, options) => { throw new TypeError('The `importMeta` option is required. Its value must be `import.meta`.'); } - const foundPackage = readPackageUpSync({ + const foundPackage = options.pkg ?? readPackageUpSync({ cwd: dirname(fileURLToPath(options.importMeta.url)), normalize: false, - }); + })?.packageJson; + + // eslint-disable-next-line unicorn/prevent-abbreviations + const pkg = foundPackage ?? {}; + normalizePackageData(pkg); const parsedOptions = { - pkg: foundPackage ? foundPackage.packageJson : {}, argv: process.argv.slice(2), flags: {}, inferType: false, input: 'string', + description: pkg.description ?? false, help: helpText, autoHelp: true, autoVersion: true, @@ -82,6 +87,7 @@ export const buildOptions = (helpText, options) => { allowParentFlags: true, helpIndent: 2, ...options, + pkg, }; validateOptions(parsedOptions); diff --git a/test/options/help.js b/test/options/help.js index 1a1ad15..f708d2b 100644 --- a/test/options/help.js +++ b/test/options/help.js @@ -25,12 +25,25 @@ test('support help shortcut', verifyHelp, { unicorn cat `], - expected: indentString('\nCLI app helper\n\nunicorn\ncat\n', 2), + expected: indentString(stripIndent` + + CLI app helper + + unicorn + cat + `, 2), }); test('spawn cli and show help screen', verifyCli, { args: '--help', - expected: indentString('\nCustom description\n\nUsage\n foo \n\n', 2), + expected: indentString(stripIndent` + + Custom description + + Usage + foo + + `, 2), }); test('spawn cli and disabled autoHelp', verifyCli, { From 713122b8788aff2df129058852c7d85ba6d21919 Mon Sep 17 00:00:00 2001 From: tommy-mitchell Date: Wed, 28 Feb 2024 19:53:16 -0600 Subject: [PATCH 2/4] add `false` to readme types --- readme.md | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/readme.md b/readme.md index 7f51362..db2dcaa 100644 --- a/readme.md +++ b/readme.md @@ -142,7 +142,7 @@ flags: { ##### description -Type: `string | boolean`\ +Type: `string | false`\ Default: The package.json `"description"` property Description to show above the help text. @@ -151,7 +151,7 @@ Set it to `false` to disable it altogether. ##### help -Type: `string | boolean` +Type: `string | false` The help text you want shown. @@ -159,6 +159,8 @@ The input is reindented and starting/ending newlines are trimmed which means you The description will be shown above your help text automatically. +Set it to `false` to disable it altogether. + ##### version Type: `string | boolean`\ From ecf5dbae031a50bd34783d95717fe9a9da882829 Mon Sep 17 00:00:00 2001 From: tommy-mitchell Date: Wed, 28 Feb 2024 19:54:09 -0600 Subject: [PATCH 3/4] add `showHelp` note --- source/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/index.js b/source/index.js index cfcac6c..d5379b4 100644 --- a/source/index.js +++ b/source/index.js @@ -34,7 +34,7 @@ const buildResult = ({pkg: packageJson, ...options}, parserOptions) => { const showHelp = code => { console.log(help); - process.exit(typeof code === 'number' ? code : 2); + process.exit(typeof code === 'number' ? code : 2); // Default to code 2 for incorrect usage (#47) }; const showVersion = () => { From 8a95e8d7b69928616b0196810487967cdcf942b1 Mon Sep 17 00:00:00 2001 From: Tommy Date: Wed, 28 Feb 2024 22:11:44 -0600 Subject: [PATCH 4/4] Update options.js --- source/options.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/options.js b/source/options.js index a4c5311..6531f29 100644 --- a/source/options.js +++ b/source/options.js @@ -80,7 +80,7 @@ export const buildOptions = (helpText, options) => { input: 'string', description: pkg.description ?? false, help: helpText, - version: foundPackage?.packageJson.version || 'No version found', + version: pkg.version || 'No version found', autoHelp: true, autoVersion: true, booleanDefault: false,