From 167d1ecffb0fde0b190ae2fef878a82af6e8b0d6 Mon Sep 17 00:00:00 2001 From: Dimitri Benin Date: Sun, 31 Mar 2019 12:55:37 +0000 Subject: [PATCH] Update dependencies, refactor TypeScript definition to CommonJS compatible export (#117) --- index.d.ts | 402 ++++++++++++++++++++++++++---------------------- index.js | 1 - index.test-d.ts | 5 +- package.json | 8 +- readme.md | 2 + 5 files changed, 230 insertions(+), 188 deletions(-) diff --git a/index.d.ts b/index.d.ts index 7c10b0b..ec1834b 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1,198 +1,238 @@ import {PackageJson} from 'type-fest'; import {Options as MinimistOptions} from 'minimist-options'; -export interface Options { - /** - Define argument flags. +declare namespace meow { + interface Options { + /** + Define argument flags. - The key is the flag name and the value is an object with any of: + The key is the flag name and the value is an object with any of: - - `type`: Type of value. (Possible values: `string` `boolean`) - - `alias`: Usually used to define a short flag alias. - - `default`: Default value when the flag is not specified. + - `type`: Type of value. (Possible values: `string` `boolean`) + - `alias`: Usually used to define a short flag alias. + - `default`: Default value when the flag is not specified. - @example - ``` - flags: { - unicorn: { - type: 'string', - alias: 'u', - default: 'rainbow' + @example + ``` + flags: { + unicorn: { + type: 'string', + alias: 'u', + default: 'rainbow' + } } + ``` + */ + readonly flags?: MinimistOptions; + + /** + Description to show above the help text. Default: The package.json `"description"` property. + + Set it to `false` to disable it altogether. + */ + readonly description?: string | false; + + /** + The help text you want shown. + + The input is reindented and starting/ending newlines are trimmed which means you can use a [template literal](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/template_strings) without having to care about using the correct amount of indent. + + The description will be shown above your help text automatically. + + Set it to `false` to disable it altogether. + */ + readonly help?: string | false; + + /** + Set a custom version output. Default: The package.json `"version"` property. + + Set it to `false` to disable it altogether. + */ + readonly version?: string | false; + + /** + 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. + */ + readonly autoHelp?: boolean; + + /** + Automatically show the version text when the `--version` flag is present. Useful to set this value to `false` when a CLI manages child CLIs with their own version text. + */ + readonly autoVersion?: boolean; + + /** + package.json as an `Object`. Default: Closest package.json upwards. + + _You most likely don't need this option._ + */ + readonly pkg?: {[key: string]: unknown}; + + /** + Custom arguments object. + + @default process.argv.slice(2) + */ + readonly argv?: ReadonlyArray; + + /** + Infer the argument type. + + By default, the argument `5` in `$ foo 5` becomes a string. Enabling this would infer it as a number. + + @default false + */ + readonly inferType?: boolean; + + /** + Value of `boolean` flags not defined in `argv`. If set to `undefined` the flags not defined in `argv` will be excluded from the result. The `default` value set in `boolean` flags take precedence over `booleanDefault`. + + __Caution: Explicitly specifying undefined for `booleanDefault` has different meaning from omitting key itself.__ + + @example + ``` + import meow = require('meow'); + + const cli = meow(` + Usage + $ foo + + Options + --rainbow, -r Include a rainbow + --unicorn, -u Include a unicorn + --no-sparkles Exclude sparkles + + Examples + $ foo + 🌈 unicorns✨🌈 + `, { + booleanDefault: undefined, + flags: { + rainbow: { + type: 'boolean', + default: true, + alias: 'r' + }, + unicorn: { + type: 'boolean', + default: false, + alias: 'u' + }, + cake: { + type: 'boolean', + alias: 'c' + }, + sparkles: { + type: 'boolean', + default: true + } + } + }); + + //{ + // flags: { + // rainbow: true, + // unicorn: false, + // sparkles: true + // }, + // unnormalizedFlags: { + // rainbow: true, + // r: true, + // unicorn: false, + // u: false, + // sparkles: true + // }, + // … + //} + ``` + */ + readonly booleanDefault?: boolean | null | undefined; + + /** + Whether to use [hard-rejection](https://github.com/sindresorhus/hard-rejection) or not. Disabling this can be useful if you need to handle `process.on('unhandledRejection')` yourself. + + @default true + */ + readonly hardRejection?: boolean; } - ``` - */ - readonly flags?: MinimistOptions; - - /** - Description to show above the help text. Default: The package.json `"description"` property. - - Set it to `false` to disable it altogether. - */ - readonly description?: string | false; - - /** - The help text you want shown. - - The input is reindented and starting/ending newlines are trimmed which means you can use a [template literal](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/template_strings) without having to care about using the correct amount of indent. - - The description will be shown above your help text automatically. - - Set it to `false` to disable it altogether. - */ - readonly help?: string | false; - - /** - Set a custom version output. Default: The package.json `"version"` property. - - Set it to `false` to disable it altogether. - */ - readonly version?: string | false; - - /** - 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. - */ - readonly autoHelp?: boolean; - - /** - Automatically show the version text when the `--version` flag is present. Useful to set this value to `false` when a CLI manages child CLIs with their own version text. - */ - readonly autoVersion?: boolean; - - /** - package.json as an `Object`. Default: Closest package.json upwards. - - _You most likely don't need this option._ - */ - readonly pkg?: {[key: string]: unknown}; - - /** - Custom arguments object. - @default process.argv.slice(2) - */ - readonly argv?: ReadonlyArray; - - /** - Infer the argument type. - - By default, the argument `5` in `$ foo 5` becomes a string. Enabling this would infer it as a number. - - @default false - */ - readonly inferType?: boolean; - - /** - Value of `boolean` flags not defined in `argv`. If set to `undefined` the flags not defined in `argv` will be excluded from the result. The `default` value set in `boolean` flags take precedence over `booleanDefault`. + interface Result { + /** + Non-flag arguments. + */ + input: string[]; + + /** + Flags converted to camelCase excluding aliases. + */ + flags: {[name: string]: unknown}; + + /** + Flags converted camelCase including aliases. + */ + unnormalizedFlags: {[name: string]: unknown}; + + /** + The `package.json` object. + */ + pkg: PackageJson; + + /** + The help text used with `--help`. + */ + help: string; + + /** + Show the help text and exit with code. + + @param exitCode - The exit code to use. Default: `2`. + */ + showHelp(exitCode?: number): void; + + /** + Show the version text and exit. + */ + showVersion(): void; + } +} +/** +@param helpMessage - Shortcut for the `help` option. - __Caution: Explicitly specifying undefined for `booleanDefault` has different meaning from omitting key itself.__ +@example +``` +#!/usr/bin/env node +'use strict'; +import meow = require('meow'); +import foo = require('.'); - @example - ``` - const cli = meow(` - Usage - $ foo +const cli = meow(` + Usage + $ foo - Options - --rainbow, -r Include a rainbow - --unicorn, -u Include a unicorn - --no-sparkles Exclude sparkles + Options + --rainbow, -r Include a rainbow - Examples - $ foo - 🌈 unicorns✨🌈 - `, { - booleanDefault: undefined, - flags: { - rainbow: { - type: 'boolean', - default: true, - alias: 'r' - }, - unicorn: { - type: 'boolean', - default: false, - alias: 'u' - }, - cake: { - type: 'boolean', - alias: 'c' - }, - sparkles: { - type: 'boolean', - default: true - } + Examples + $ foo unicorns --rainbow + 🌈 unicorns 🌈 +`, { + flags: { + rainbow: { + type: 'boolean', + alias: 'r' } - }); - - //{ - // flags: { - // rainbow: true, - // unicorn: false, - // sparkles: true - // }, - // unnormalizedFlags: { - // rainbow: true, - // r: true, - // unicorn: false, - // u: false, - // sparkles: true - // }, - // … - //} - ``` - */ - readonly booleanDefault?: boolean | null | undefined; - - /** - Whether to use [hard-rejection](https://github.com/sindresorhus/hard-rejection) or not. Disabling this can be useful if you need to handle `process.on('unhandledRejection')` yourself. - - @default true - */ - readonly hardRejection?: boolean; -} + } +}); -export interface Result { - /** - Non-flag arguments. - */ - input: string[]; - - /** - Flags converted to camelCase excluding aliases. - */ - flags: {[name: string]: unknown}; - - /** - Flags converted camelCase including aliases. - */ - unnormalizedFlags: {[name: string]: unknown}; - - /** - The `package.json` object. - */ - pkg: PackageJson; - - /** - The help text used with `--help`. - */ - help: string; - - /** - Show the help text and exit with code. - - @param exitCode - The exit code to use. Default: `2`. - */ - showHelp(exitCode?: number): void; - - /** - Show the version text and exit. - */ - showVersion(): void; -} +//{ +// input: ['unicorns'], +// flags: {rainbow: true}, +// ... +//} -/** -@param helpMessage - Shortcut for the `help` option. +foo(cli.input[0], cli.flags); +``` */ -export default function meow(helpMessage: string, options?: Options): Result; -export default function meow(options?: Options): Result; +declare function meow(helpMessage: string, options?: meow.Options): meow.Result; +declare function meow(options?: meow.Options): meow.Result; + +export = meow; diff --git a/index.js b/index.js index 1130ee5..5bcaf90 100644 --- a/index.js +++ b/index.js @@ -128,4 +128,3 @@ const meow = (helpText, options) => { }; module.exports = meow; -module.exports.default = meow; diff --git a/index.test-d.ts b/index.test-d.ts index b939a60..419cf9f 100644 --- a/index.test-d.ts +++ b/index.test-d.ts @@ -1,6 +1,7 @@ -import {expectType} from 'tsd-check'; +import {expectType} from 'tsd'; import {PackageJson} from 'type-fest'; -import meow, {Result} from '.'; +import meow = require('.'); +import {Result} from '.'; expectType(meow('Help text')); expectType(meow('Help text', {hardRejection: false})); diff --git a/package.json b/package.json index 22c6399..7295277 100644 --- a/package.json +++ b/package.json @@ -13,7 +13,7 @@ "node": ">=8" }, "scripts": { - "test": "xo && ava && tsd-check" + "test": "xo && ava && tsd" }, "files": [ "index.js", @@ -45,17 +45,17 @@ "hard-rejection": "^2.0.0", "minimist-options": "^4.0.1", "normalize-package-data": "^2.5.0", - "read-pkg-up": "^4.0.0", + "read-pkg-up": "^5.0.0", "redent": "^2.0.0", "trim-newlines": "^2.0.0", "type-fest": "^0.3.0", "yargs-parser": "^13.0.0" }, "devDependencies": { - "ava": "^1.3.1", + "ava": "^1.4.1", "execa": "^1.0.0", "indent-string": "^3.2.0", - "tsd-check": "^0.5.0", + "tsd": "^0.7.1", "xo": "^0.24.0" }, "xo": { diff --git a/readme.md b/readme.md index 246bf78..578fce7 100644 --- a/readme.md +++ b/readme.md @@ -192,6 +192,8 @@ The `default` value set in `boolean` flags take precedence over `booleanDefault` Example: ```js +const meow = require('meow'); + const cli = meow(` Usage $ foo