Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Document ES Modules usage #147

Merged
merged 4 commits into from May 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions .eslintignore
@@ -0,0 +1 @@
estest/index.js
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We must ignore this file due to eslint/eslint#13133

1 change: 1 addition & 0 deletions .travis.yml
@@ -1,5 +1,6 @@
language: node_js
node_js:
- '14'
- '12'
- '10'
- '8'
24 changes: 24 additions & 0 deletions estest/index.js
@@ -0,0 +1,24 @@
import {createRequire} from 'module';

const meow = createRequire(import.meta.url)('../index.js');

meow(`
Usage
$ estest <input>

Options
--rainbow, -r Include a rainbow

Examples
$ estest unicorns --rainbow
🌈 unicorns 🌈
`,
{
flags: {
rainbow: {
type: 'boolean',
alias: 'r'
}
}
}
);
5 changes: 5 additions & 0 deletions estest/package.json
@@ -0,0 +1,5 @@
{
"name": "estest",
"type": "module",
"version": "1.2.3"
}
40 changes: 40 additions & 0 deletions readme.md
Expand Up @@ -26,6 +26,8 @@ $ npm install meow
$ ./foo-app.js unicorns --rainbow
```

**CommonJS**

```js
#!/usr/bin/env node
'use strict';
Expand Down Expand Up @@ -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 <input>

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?)
Expand Down
16 changes: 16 additions & 0 deletions 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'],
Expand Down Expand Up @@ -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);
}
});
}