Skip to content

Commit

Permalink
Target Node 16 (#235)
Browse files Browse the repository at this point in the history
  • Loading branch information
tommy-mitchell authored Mar 30, 2023
1 parent a94476a commit 24d5e9c
Show file tree
Hide file tree
Showing 9 changed files with 32 additions and 30 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ const cli = meow(`
// ...
//}
foo(cli.input[0], cli.flags);
foo(cli.input.at(0), cli.flags);
```
*/
export default function meow<Flags extends AnyFlags>(helpMessage: string, options?: Options<Flags>): Result<Flags>;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"default": "./source/index.js"
},
"engines": {
"node": ">=14.16"
"node": ">=16.10"
},
"scripts": {
"test": "xo && ava && tsd"
Expand Down
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ const cli = meow(`
}
*/

foo(cli.input[0], cli.flags);
foo(cli.input.at(0), cli.flags);
```

## API
Expand Down
22 changes: 12 additions & 10 deletions source/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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_);
Expand All @@ -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);
Expand Down Expand Up @@ -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;
};
Expand Down
4 changes: 2 additions & 2 deletions source/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down
2 changes: 1 addition & 1 deletion source/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
22 changes: 11 additions & 11 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -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');
Expand Down Expand Up @@ -92,32 +92,32 @@ 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'],
inferType: true,
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 => {
Expand Down Expand Up @@ -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 => {
Expand Down
4 changes: 2 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 24d5e9c

Please sign in to comment.