diff --git a/.eslintignore b/.eslintignore new file mode 100644 index 0000000..3621aec --- /dev/null +++ b/.eslintignore @@ -0,0 +1 @@ +estest/index.js \ No newline at end of file diff --git a/.travis.yml b/.travis.yml index f98fed0..f5eabc7 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,5 +1,6 @@ language: node_js node_js: + - '14' - '12' - '10' - '8' diff --git a/estest/index.js b/estest/index.js new file mode 100644 index 0000000..dc1a88c --- /dev/null +++ b/estest/index.js @@ -0,0 +1,25 @@ +import { createRequire } from "module"; + +const meow = createRequire(import.meta.url)("../index.js"); + +meow( + ` +Usage + $ estest + +Options + --rainbow, -r Include a rainbow + +Examples + $ estest unicorns --rainbow + 🌈 unicorns 🌈 +`, + { + flags: { + rainbow: { + type: "boolean", + alias: "r" + } + } + } +); diff --git a/estest/package.json b/estest/package.json new file mode 100644 index 0000000..601be54 --- /dev/null +++ b/estest/package.json @@ -0,0 +1,5 @@ +{ + "name": "estest", + "type": "module", + "version": "1.2.3" +} \ No newline at end of file diff --git a/index.js b/index.js index c151058..9e698cf 100644 --- a/index.js +++ b/index.js @@ -1,6 +1,6 @@ 'use strict'; const path = require('path'); -const buildMinimistOptions = require('minimist-options'); +const buildParserOptions = require('minimist-options'); const yargs = require('yargs-parser'); const camelcaseKeys = require('camelcase-keys'); const decamelizeKeys = require('decamelize-keys'); @@ -40,7 +40,7 @@ const meow = (helpText, options) => { hardRejection(); } - const minimistFlags = options.flags && typeof options.booleanDefault !== 'undefined' ? Object.keys(options.flags).reduce( + const parserFlags = options.flags && typeof options.booleanDefault !== 'undefined' ? Object.keys(options.flags).reduce( (flags, flag) => { if (flags[flag].type === 'boolean' && !Object.prototype.hasOwnProperty.call(flags[flag], 'default')) { flags[flag].default = options.booleanDefault; @@ -51,28 +51,28 @@ const meow = (helpText, options) => { options.flags ) : options.flags; - let minimistOptions = { + let parserOptions = { arguments: options.input, - ...minimistFlags + ...parserFlags }; - minimistOptions = decamelizeKeys(minimistOptions, '-', {exclude: ['stopEarly', '--']}); + parserOptions = decamelizeKeys(parserOptions, '-', {exclude: ['stopEarly', '--']}); if (options.inferType) { - delete minimistOptions.arguments; + delete parserOptions.arguments; } - minimistOptions = buildMinimistOptions(minimistOptions); + parserOptions = buildParserOptions(parserOptions); - if (minimistOptions['--']) { - minimistOptions.configuration = { - ...minimistOptions.configuration, + if (parserOptions['--']) { + parserOptions.configuration = { + ...parserOptions.configuration, 'populate--': true }; } const {pkg} = options; - const argv = yargs(options.argv, minimistOptions); + const argv = yargs(options.argv, parserOptions); let help = redent(trimNewlines((options.help || '').replace(/\t+\n*$/, '')), 2); normalizePackageData(pkg); diff --git a/readme.md b/readme.md index 8071317..79992c0 100644 --- a/readme.md +++ b/readme.md @@ -26,6 +26,8 @@ $ npm install meow $ ./foo-app.js unicorns --rainbow ``` +**CommonJS** + ```js #!/usr/bin/env node 'use strict'; @@ -61,6 +63,44 @@ const cli = meow(` foo(cli.input[0], cli.flags); ``` +**ES Modules** + +```js +#!/usr/bin/env node +import { createRequire } from "module"; +import foo from './lib/index.js'; + +const meow = createRequire(import.meta.url)("meow"); + +const cli = meow(` + Usage + $ foo + + Options + --rainbow, -r Include a rainbow + + Examples + $ foo unicorns --rainbow + 🌈 unicorns 🌈 +`, { + flags: { + rainbow: { + type: 'boolean', + alias: 'r' + } + } +}); +/* +{ + input: ['unicorns'], + flags: {rainbow: true}, + ... +} +*/ + +foo(cli.input[0], cli.flags); +``` + ## API ### meow(helpText, options?) diff --git a/test.js b/test.js index d3e40ec..0060af6 100644 --- a/test.js +++ b/test.js @@ -1,9 +1,12 @@ import test from 'ava'; import indentString from 'indent-string'; import execa from 'execa'; +import path from 'path'; import pkg from './package.json'; import meow from '.'; +const NODE_MAJOR_VERSION = process.versions.node.split('.')[0]; + test('return object', t => { const cli = meow({ argv: ['foo', '--foo-bar', '-u', 'cat', '--', 'unicorn', 'cake'], @@ -307,3 +310,16 @@ test('supports `number` flag type - throws on incorrect default value', t => { }); }); }); + +if (NODE_MAJOR_VERSION >= 14) { + test('supports es modules', async t => { + try { + const {stdout} = await execa('node', ['index.js', '--version'], { + cwd: path.join(__dirname, 'estest') + }); + t.regex(stdout, /1.2.3/); + } catch (error) { + t.is(error, undefined); + } + }); +}