diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index d50ada6..3a9f814 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -10,9 +10,9 @@ jobs: fail-fast: false matrix: node-version: + - 19 - 18 - 16 - - 14 steps: - uses: actions/checkout@v3 - uses: actions/setup-node@v3 diff --git a/index.d.ts b/index.d.ts index 24fea92..b7c240b 100644 --- a/index.d.ts +++ b/index.d.ts @@ -404,7 +404,7 @@ const cli = meow(` // ... //} -foo(cli.input[0], cli.flags); +foo(cli.input.at(0), cli.flags); ``` */ export default function meow(helpMessage: string, options?: Options): Result; diff --git a/package.json b/package.json index 9827c69..c622716 100644 --- a/package.json +++ b/package.json @@ -16,7 +16,7 @@ "default": "./source/index.js" }, "engines": { - "node": ">=14.16" + "node": ">=16.10" }, "scripts": { "test": "xo && ava && tsd" diff --git a/readme.md b/readme.md index c8a8076..d82246f 100644 --- a/readme.md +++ b/readme.md @@ -60,7 +60,7 @@ const cli = meow(` } */ -foo(cli.input[0], cli.flags); +foo(cli.input.at(0), cli.flags); ``` ## API diff --git a/source/index.js b/source/index.js index 56664b1..3bc1865 100644 --- a/source/index.js +++ b/source/index.js @@ -12,10 +12,16 @@ import {validate, checkUnknownFlags, checkMissingRequiredFlags} from './validate const buildResult = (options, parserOptions) => { const {pkg: package_} = options; const argv = parseArguments(options.argv, parserOptions); - let help = trimNewlines((options.help || '').replace(/\t+\n*$/, '')); + let help = ''; - if (help.includes('\n')) { - help = redent(help, 2); + if (options.help) { + help = trimNewlines((options.help || '').replace(/\t+\n*$/, '')); + + if (help.includes('\n')) { + help = redent(help, 2); + } + + help = `\n${help}`; } normalizePackageData(package_); @@ -25,12 +31,8 @@ const buildResult = (options, parserOptions) => { ({description} = package_); } - // Change to `&&=` when targeting Node 15+ - if (description) { - description = help ? `\n ${description}\n` : `\n${description}`; - } - - help = (description || '') + (help ? `\n${help}\n` : '\n'); + description &&= help ? `\n ${description}\n` : `\n${description}`; + help = `${description || ''}${help}\n`; const showHelp = code => { console.log(help); @@ -95,7 +97,7 @@ const meow = (helpText, options = {}) => { const parserOptions = buildParserOptions(parsedOptions); const result = buildResult(parsedOptions, parserOptions); - process.title = result.pkg.bin ? Object.keys(result.pkg.bin)[0] : result.pkg.name; + process.title = result.pkg.bin ? Object.keys(result.pkg.bin).at(0) : result.pkg.name; return result; }; diff --git a/source/options.js b/source/options.js index 7fb167e..2706d71 100644 --- a/source/options.js +++ b/source/options.js @@ -12,11 +12,11 @@ const validateOptions = options => { message: flagKeys => `Flag keys may not contain '-'. Invalid flags: ${joinFlagKeys(flagKeys, '')}`, }, aliasIsSet: { - filter: ([, flag]) => flag.alias !== undefined, + filter: ([, flag]) => Object.hasOwn(flag, 'alias'), message: flagKeys => `The option \`alias\` has been renamed to \`shortFlag\`. The following flags need to be updated: ${joinFlagKeys(flagKeys)}`, }, choicesNotAnArray: { - filter: ([, flag]) => flag.choices !== undefined && !Array.isArray(flag.choices), + filter: ([, flag]) => Object.hasOwn(flag, 'choices') && !Array.isArray(flag.choices), message: flagKeys => `The option \`choices\` must be an array. Invalid flags: ${joinFlagKeys(flagKeys)}`, }, choicesNotMatchFlagType: { diff --git a/source/parser.js b/source/parser.js index 6ddf4f5..8daa714 100644 --- a/source/parser.js +++ b/source/parser.js @@ -16,7 +16,7 @@ const buildParserFlags = ({flags, booleanDefault}) => { if ( typeof booleanDefault !== 'undefined' && flag.type === 'boolean' - && !Object.prototype.hasOwnProperty.call(flag, 'default') + && !Object.hasOwn(flag, 'default') ) { flag.default = flag.isMultiple ? [booleanDefault] : booleanDefault; } diff --git a/test/test.js b/test/test.js index 898ed92..4d8c4ec 100644 --- a/test/test.js +++ b/test/test.js @@ -8,7 +8,7 @@ import meow from '../source/index.js'; import {spawnFixture, __dirname} from './_utils.js'; const importMeta = import.meta; -const NODE_MAJOR_VERSION = process.versions.node.split('.')[0]; +const NODE_MAJOR_VERSION = process.versions.node.split('.').at(0); test('return object', t => { const cli = meow({ @@ -25,7 +25,7 @@ test('return object', t => { }, }); - t.is(cli.input[0], 'foo'); + t.is(cli.input.at(0), 'foo'); t.true(cli.flags.fooBar); t.is(cli.flags.meow, 'dog'); t.is(cli.flags.unicorn, 'cat'); @@ -92,19 +92,19 @@ test('single character flag casing should be preserved', t => { }); test('type inference', t => { - t.is(meow({importMeta, argv: ['5']}).input[0], '5'); - t.is(meow({importMeta, argv: ['5']}, {input: 'string'}).input[0], '5'); + t.is(meow({importMeta, argv: ['5']}).input.at(0), '5'); + t.is(meow({importMeta, argv: ['5']}, {input: 'string'}).input.at(0), '5'); t.is(meow({ importMeta, argv: ['5'], inferType: true, - }).input[0], 5); + }).input.at(0), 5); t.is(meow({ importMeta, argv: ['5'], inferType: true, flags: {foo: 'string'}, - }).input[0], 5); + }).input.at(0), 5); t.is(meow({ importMeta, argv: ['5'], @@ -112,12 +112,12 @@ test('type inference', t => { flags: { foo: 'string', }, - }).input[0], 5); + }).input.at(0), 5); t.is(meow({ importMeta, argv: ['5'], input: 'number', - }).input[0], 5); + }).input.at(0), 5); }); test('booleanDefault: undefined, filter out unset boolean args', t => { @@ -241,9 +241,9 @@ test('grouped flags work', t => { }); test('disable autoVersion/autoHelp if `cli.input.length > 0`', t => { - t.is(meow({importMeta, argv: ['bar', '--version']}).input[0], 'bar'); - t.is(meow({importMeta, argv: ['bar', '--help']}).input[0], 'bar'); - t.is(meow({importMeta, argv: ['bar', '--version', '--help']}).input[0], 'bar'); + t.is(meow({importMeta, argv: ['bar', '--version']}).input.at(0), 'bar'); + t.is(meow({importMeta, argv: ['bar', '--help']}).input.at(0), 'bar'); + t.is(meow({importMeta, argv: ['bar', '--version', '--help']}).input.at(0), 'bar'); }); test('supports `number` flag type', t => { diff --git a/tsconfig.json b/tsconfig.json index f26f355..a97b4b7 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -6,9 +6,9 @@ "compilerOptions": { "strict": true, "jsx": "react", - "target": "ES2020", // Node.js 14 + "target": "ES2021", // Node.js 16 "lib": [ - "ES2020" + "ES2021" ], "module": "ES2020", "moduleResolution": "node",