Skip to content

Commit

Permalink
fix: enable es modules
Browse files Browse the repository at this point in the history
  • Loading branch information
jimthedev committed May 1, 2020
1 parent 3571757 commit 30bd878
Show file tree
Hide file tree
Showing 7 changed files with 74 additions and 11 deletions.
1 change: 1 addition & 0 deletions .travis.yml
@@ -1,5 +1,6 @@
language: node_js
node_js:
- '14'
- '12'
- '10'
- '8'
26 changes: 26 additions & 0 deletions estest/index.js
@@ -0,0 +1,26 @@
// eslint-disable-next-line import/extensions
import meow from '../index.mjs';

console.log(
meow(
`
Usage
$ estest <input>
Options
--rainbow, -r Include a rainbow
Examples
$ estest unicorns --rainbow
🌈 unicorns 🌈
`,
{
flags: {
rainbow: {
type: 'boolean',
alias: 'r'
}
}
}
)
);
4 changes: 4 additions & 0 deletions estest/package.json
@@ -0,0 +1,4 @@
{
"name": "estest",
"type": "module"
}
22 changes: 11 additions & 11 deletions 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');
Expand Down Expand Up @@ -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;
Expand All @@ -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);
Expand Down
9 changes: 9 additions & 0 deletions index.mjs
@@ -0,0 +1,9 @@
import { createRequire} from 'module';

const require = createRequire(import.meta.url);

console.log('meta', import.meta.url);

const meow = require('./index.js');

export default meow;
7 changes: 7 additions & 0 deletions package.json
Expand Up @@ -17,6 +17,7 @@
"test": "xo && ava && tsd"
},
"files": [
"index.mjs",
"index.js",
"index.d.ts"
],
Expand Down Expand Up @@ -63,5 +64,11 @@
"rules": {
"unicorn/no-process-exit": "off"
}
},
"exports": {
"node": {
"import": "./index.mjs",
"require": "./index.js"
}
}
}
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'], {
cwd: path.join(__dirname, 'estest')
});
t.regex(stdout, /rainbow: false/);
} catch (error) {
t.is(error, undefined);
}
});
}

0 comments on commit 30bd878

Please sign in to comment.