Skip to content

Commit

Permalink
Document ES Modules usage (#147)
Browse files Browse the repository at this point in the history
  • Loading branch information
jimthedev authored and sindresorhus committed May 3, 2020
1 parent 43e9f39 commit d9d42a2
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 1 deletion.
1 change: 1 addition & 0 deletions .travis.yml
@@ -1,4 +1,5 @@
language: node_js
node_js:
- '14'
- '12'
- '10'
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"
}
5 changes: 4 additions & 1 deletion package.json
Expand Up @@ -63,6 +63,9 @@
"rules": {
"unicorn/no-process-exit": "off",
"node/no-unsupported-features/es-syntax": "off"
}
},
"ignores": [
"estest/index.js"
]
}
}
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);
}
});
}

0 comments on commit d9d42a2

Please sign in to comment.