Skip to content

Commit

Permalink
Require Node.js 14
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed Oct 11, 2022
1 parent 6c57fc8 commit 01cf2a5
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 24 deletions.
14 changes: 7 additions & 7 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {
import type {
CamelCasedProperties,
PackageJson,
} from 'type-fest';
Expand All @@ -15,21 +15,21 @@ Callback function to determine if a flag is required during runtime.
*/
export type IsRequiredPredicate = (flags: Readonly<AnyFlags>, input: readonly string[]) => boolean;

export interface Flag<Type extends FlagType, Default, IsMultiple = false> {
export type Flag<Type extends FlagType, Default, IsMultiple = false> = {
readonly type?: Type;
readonly alias?: string;
readonly default?: Default;
readonly isRequired?: boolean | IsRequiredPredicate;
readonly isMultiple?: IsMultiple;
}
};

type StringFlag = Flag<'string', string> | Flag<'string', string[], true>;
type BooleanFlag = Flag<'boolean', boolean> | Flag<'boolean', boolean[], true>;
type NumberFlag = Flag<'number', number> | Flag<'number', number[], true>;
type AnyFlag = StringFlag | BooleanFlag | NumberFlag;
type AnyFlags = Record<string, AnyFlag>;

export interface Options<Flags extends AnyFlags> {
export type Options<Flags extends AnyFlags> = {
/**
Pass in [`import.meta`](https://nodejs.org/dist/latest/docs/api/esm.html#esm_import_meta). This is used to find the correct package.json file.
*/
Expand Down Expand Up @@ -216,7 +216,7 @@ export interface Options<Flags extends AnyFlags> {
@default true
*/
readonly allowUnknownFlags?: boolean;
}
};

type TypedFlag<Flag extends AnyFlag> =
Flag extends {type: 'number'}
Expand All @@ -240,7 +240,7 @@ export type TypedFlags<Flags extends AnyFlags> = {
: PossiblyOptionalFlag<Flags[F], TypedFlag<Flags[F]>>
};

export interface Result<Flags extends AnyFlags> {
export type Result<Flags extends AnyFlags> = {
/**
Non-flag arguments.
*/
Expand Down Expand Up @@ -277,7 +277,7 @@ export interface Result<Flags extends AnyFlags> {
Show the version text and exit.
*/
showVersion: () => void;
}
};
/**
@param helpMessage - Shortcut for the `help` option.
Expand Down
6 changes: 3 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ const buildParserFlags = ({flags, booleanDefault}) => {

if (flag.isMultiple) {
flag.type = flag.type ? `${flag.type}-array` : 'array';
flag.default = flag.default || [];
flag.default = flag.default ?? [];
delete flag.isMultiple;
}

Expand All @@ -107,7 +107,7 @@ const meow = (helpText, options = {}) => {
helpText = '';
}

if (!(options.importMeta && options.importMeta.url)) {
if (!options.importMeta?.url) {
throw new TypeError('The `importMeta` option is required. Its value must be `import.meta`.');
}

Expand Down Expand Up @@ -176,7 +176,7 @@ const meow = (helpText, options = {}) => {

const {pkg: package_} = options;
const argv = parseArguments(options.argv, parserOptions);
let help = redent(trimNewlines((options.help || '').replace(/\t+\n*$/, '')), 2);
let help = redent(trimNewlines((options.help ?? '').replace(/\t+\n*$/, '')), 2);

normalizePackageData(package_);

Expand Down
4 changes: 2 additions & 2 deletions index.test-d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {expectAssignable, expectError, expectType} from 'tsd';
import {PackageJson} from 'type-fest';
import meow, {Result, AnyFlag} from './index.js';
import type {PackageJson} from 'type-fest';
import meow, {type Result, type AnyFlag} from './index.js';

const importMeta = import.meta;

Expand Down
23 changes: 13 additions & 10 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@
"url": "https://sindresorhus.com"
},
"type": "module",
"exports": "./index.js",
"exports": {
"types": "./index.d.ts",
"default": "./index.js"
},
"engines": {
"node": "^12.20.0 || ^14.13.1 || >=16.0.0"
"node": ">=14.16"
},
"scripts": {
"test": "xo && ava && tsd"
Expand Down Expand Up @@ -43,25 +46,25 @@
],
"dependencies": {
"@types/minimist": "^1.2.2",
"camelcase-keys": "^7.0.0",
"decamelize": "^5.0.0",
"camelcase-keys": "^8.0.2",
"decamelize": "^6.0.0",
"decamelize-keys": "^1.1.0",
"hard-rejection": "^2.1.0",
"minimist-options": "4.1.0",
"normalize-package-data": "^3.0.2",
"read-pkg-up": "^8.0.0",
"normalize-package-data": "^4.0.1",
"read-pkg-up": "^9.1.0",
"redent": "^4.0.0",
"trim-newlines": "^4.0.2",
"type-fest": "^3.1.0",
"yargs-parser": "^20.2.9"
"yargs-parser": "^21.1.1"
},
"devDependencies": {
"ava": "^3.15.0",
"ava": "^4.3.3",
"execa": "^6.1.0",
"indent-string": "^5.0.0",
"read-pkg": "^7.1.0",
"tsd": "^0.20.0",
"xo": "^0.48.0"
"tsd": "^0.24.1",
"xo": "^0.52.4"
},
"xo": {
"rules": {
Expand Down
4 changes: 2 additions & 2 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,13 +101,13 @@ test('spawn cli and test input flag', async t => {
t.is(stdout, 'bar');
});

test.serial('pkg.bin as a string should work', t => {
test.serial.failing('pkg.bin as a string should work', t => {
meow({
importMeta,
pkg: {
importMeta,
name: 'browser-sync',
bin: 'bin/browser-sync.js',
bin: './bin/browser-sync.js',
},
});

Expand Down

0 comments on commit 01cf2a5

Please # to comment.