From 97e8b2077ae289ee8a6f171bd36e8cd91c3a70ab Mon Sep 17 00:00:00 2001 From: Tommy Date: Thu, 29 Feb 2024 00:32:38 -0600 Subject: [PATCH] Clean up `description` parsing (#256) --- readme.md | 6 ++++-- source/index.js | 20 +++++++++++--------- source/options.js | 14 ++++++++++---- test/options/help.js | 17 +++++++++++++++-- 4 files changed, 40 insertions(+), 17 deletions(-) diff --git a/readme.md b/readme.md index d57ac28..b3b467a 100644 --- a/readme.md +++ b/readme.md @@ -143,7 +143,7 @@ flags: { ##### description -Type: `string | boolean`\ +Type: `string | false`\ Default: The package.json `"description"` property Description to show above the help text. @@ -152,7 +152,7 @@ Set it to `false` to disable it altogether. ##### help -Type: `string | boolean` +Type: `string | false` The help text you want shown. @@ -160,6 +160,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`\ diff --git a/source/index.js b/source/index.js index da32775..cc1be39 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,16 +21,20 @@ 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); - process.exit(typeof code === 'number' ? code : 2); + process.exit(typeof code === 'number' ? code : 2); // Default to code 2 for incorrect usage (#47) }; const showVersion = () => { @@ -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 69d3edf..6531f29 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,19 +64,23 @@ 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, - version: foundPackage?.packageJson.version || 'No version found', + version: pkg.version || 'No version found', autoHelp: true, autoVersion: true, booleanDefault: false, @@ -83,6 +88,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, {