diff --git a/readme.md b/readme.md index db2dcaa..09fabd6 100644 --- a/readme.md +++ b/readme.md @@ -163,7 +163,7 @@ Set it to `false` to disable it altogether. ##### version -Type: `string | boolean`\ +Type: `string`\ Default: The package.json `"version"` property Set a custom version output. @@ -215,7 +215,7 @@ By default, the argument `5` in `$ foo 5` becomes a string. Enabling this would ##### booleanDefault -Type: `boolean | null | undefined`\ +Type: `boolean | undefined`\ Default: `false` Value of `boolean` flags not defined in `argv`. diff --git a/source/index.d.ts b/source/index.d.ts index 5fa3b34..b8f194f 100644 --- a/source/index.d.ts +++ b/source/index.d.ts @@ -180,10 +180,8 @@ export type Options = { /** Set a custom version output. Default: The package.json `"version"` property. - - Set it to `false` to disable it altogether. */ - readonly version?: string | false; + readonly version?: string; /** Automatically show the help text when the `--help` flag is present. Useful to set this value to `false` when a CLI manages child CLIs with their own help text. @@ -291,8 +289,7 @@ export type Options = { //} ``` */ - // eslint-disable-next-line @typescript-eslint/ban-types - readonly booleanDefault?: boolean | null | undefined; + readonly booleanDefault?: boolean | undefined; // TODO: Remove this in meow 14. /** diff --git a/source/index.js b/source/index.js index d5379b4..cc1be39 100644 --- a/source/index.js +++ b/source/index.js @@ -38,7 +38,7 @@ const buildResult = ({pkg: packageJson, ...options}, parserOptions) => { }; const showVersion = () => { - console.log(typeof options.version === 'string' ? options.version : packageJson.version); + console.log(options.version); process.exit(0); }; diff --git a/source/options.js b/source/options.js index 2961e27..a4c5311 100644 --- a/source/options.js +++ b/source/options.js @@ -80,6 +80,7 @@ export const buildOptions = (helpText, options) => { input: 'string', description: pkg.description ?? false, help: helpText, + version: foundPackage?.packageJson.version || 'No version found', autoHelp: true, autoVersion: true, booleanDefault: false, diff --git a/source/parser.js b/source/parser.js index 6685c3e..7dc7e68 100644 --- a/source/parser.js +++ b/source/parser.js @@ -13,11 +13,7 @@ const buildParserFlags = ({flags, booleanDefault}) => { delete flag.shortFlag; } - if ( - booleanDefault !== undefined - && flag.type === 'boolean' - && !Object.hasOwn(flag, 'default') - ) { + if (booleanDefault !== undefined && flag.type === 'boolean' && !Object.hasOwn(flag, 'default')) { flag.default = flag.isMultiple ? [booleanDefault] : booleanDefault; } diff --git a/test-d/build.test-d.ts b/test-d/build.test-d.ts index 29ec7de..ce26a8c 100644 --- a/test-d/build.test-d.ts +++ b/test-d/build.test-d.ts @@ -40,14 +40,12 @@ expectType>(meow({importMeta, description: false})); expectType>(meow({importMeta, help: 'foo'})); expectType>(meow({importMeta, help: false})); expectType>(meow({importMeta, version: 'foo'})); -expectType>(meow({importMeta, version: false})); expectType>(meow({importMeta, autoHelp: false})); expectType>(meow({importMeta, autoVersion: false})); expectType>(meow({importMeta, pkg: {foo: 'bar'}})); expectType>(meow({importMeta, argv: ['foo', 'bar']})); expectType>(meow({importMeta, inferType: true})); expectType>(meow({importMeta, booleanDefault: true})); -expectType>(meow({importMeta, booleanDefault: null})); expectType>(meow({importMeta, booleanDefault: undefined})); expectType>(meow({importMeta})); diff --git a/test-d/index.test-d.ts b/test-d/index.test-d.ts index aa5bf91..9d06253 100644 --- a/test-d/index.test-d.ts +++ b/test-d/index.test-d.ts @@ -40,14 +40,12 @@ expectType>(meow({importMeta, description: false})); expectType>(meow({importMeta, help: 'foo'})); expectType>(meow({importMeta, help: false})); expectType>(meow({importMeta, version: 'foo'})); -expectType>(meow({importMeta, version: false})); expectType>(meow({importMeta, autoHelp: false})); expectType>(meow({importMeta, autoVersion: false})); expectType>(meow({importMeta, pkg: {foo: 'bar'}})); expectType>(meow({importMeta, argv: ['foo', 'bar']})); expectType>(meow({importMeta, inferType: true})); expectType>(meow({importMeta, booleanDefault: true})); -expectType>(meow({importMeta, booleanDefault: null})); expectType>(meow({importMeta, booleanDefault: undefined})); expectType>(meow({importMeta, hardRejection: false})); diff --git a/test/flags/_utils.js b/test/flags/_utils.js index dde13de..69f20c9 100644 --- a/test/flags/_utils.js +++ b/test/flags/_utils.js @@ -2,10 +2,16 @@ import test from 'ava'; import meow from '../../source/index.js'; -export const _verifyFlags = importMeta => test.macro(async (t, {flags = {}, args, expected, error}) => { +export const _verifyFlags = importMeta => test.macro(async (t, {flags = {}, args, expected, error, ...meowOptions}) => { const assertions = await t.try(async tt => { const arguments_ = args?.split(' ') ?? []; - const meowOptions = {importMeta, argv: arguments_, flags}; + + meowOptions = { + ...meowOptions, + importMeta, + argv: arguments_, + flags, + }; tt.log('arguments:', arguments_); diff --git a/test/flags/boolean-default.js b/test/flags/boolean-default.js index 70747b4..ac54256 100644 --- a/test/flags/boolean-default.js +++ b/test/flags/boolean-default.js @@ -1,55 +1,58 @@ import test from 'ava'; -import meow from '../../source/index.js'; +import {_verifyFlags} from './_utils.js'; -const importMeta = import.meta; +const verifyFlags = _verifyFlags(import.meta); -test('undefined - filter out unset boolean args', t => { - const cli = meow({ - importMeta, - argv: ['--foo'], - booleanDefault: undefined, - flags: { - foo: { - type: 'boolean', - }, - bar: { - type: 'boolean', - }, - baz: { - type: 'boolean', - default: false, - }, +test('undefined - filter out unset boolean args', verifyFlags, { + booleanDefault: undefined, + flags: { + foo: { + type: 'boolean', }, - }); - - t.like(cli.flags, { + bar: { + type: 'boolean', + }, + baz: { + type: 'boolean', + default: false, + }, + }, + args: '--foo', + expected: { foo: true, bar: undefined, baz: false, - }); + }, }); -test('boolean args are false by default', t => { - const cli = meow({ - importMeta, - argv: ['--foo'], - flags: { - foo: { - type: 'boolean', - }, - bar: { - type: 'boolean', - default: true, - }, - baz: { - type: 'boolean', - }, +test('boolean args are false by default', verifyFlags, { + flags: { + foo: { + type: 'boolean', }, - }); - - t.like(cli.flags, { + bar: { + type: 'boolean', + default: true, + }, + baz: { + type: 'boolean', + }, + }, + args: '--foo', + expected: { foo: true, bar: true, baz: false, - }); + }, +}); + +test('throws if default is null', verifyFlags, { + booleanDefault: null, + flags: { + foo: { + type: 'boolean', + }, + }, + args: '--foo', + error: 'Expected "foo" default value to be of type "boolean", got "null"', }); diff --git a/test/options/version.js b/test/options/version.js index 29b5f28..576a6a6 100644 --- a/test/options/version.js +++ b/test/options/version.js @@ -38,10 +38,16 @@ test('custom version', verifyVersion, { test('version = false has no effect', verifyVersion, { args: '--version', execaOptions: {env: {VERSION: 'false'}}, - expected: '1.0.0', + expected: 'false', }); test('manual showVersion', verifyVersion, { args: '--show-version', expected: '1.0.0', }); + +test('no version fallback message', verifyVersion, { + fixture: 'with-package-json/default/fixture.js', + args: '--version', + expected: 'No version found', +});