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 Apr 27, 2020
1 parent 3571757 commit c4d6079
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 2 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 unicorn/import-index,import/extensions,import/no-useless-path-segments
import meow from '../index.js';

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"
}
7 changes: 5 additions & 2 deletions index.js
Expand Up @@ -12,7 +12,10 @@ const normalizePackageData = require('normalize-package-data');

// Prevent caching of this module so module.parent is always accurate
delete require.cache[__filename];
const parentDir = path.dirname(module.parent.filename);

// In CJS module.parent is available but not in ES modules
const parentFilename = module.parent ? module.parent.filename : Object.keys(require.cache)[0];
const parentDirectory = path.dirname(parentFilename);

const meow = (helpText, options) => {
if (typeof helpText !== 'string') {
Expand All @@ -22,7 +25,7 @@ const meow = (helpText, options) => {

options = {
pkg: readPkgUp.sync({
cwd: parentDir,
cwd: parentDirectory,
normalize: false
}).packageJson || {},
argv: process.argv.slice(2),
Expand Down
12 changes: 12 additions & 0 deletions test.js
@@ -1,6 +1,7 @@
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 '.';

Expand Down Expand Up @@ -307,3 +308,14 @@ test('supports `number` flag type - throws on incorrect default value', t => {
});
});
});

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 c4d6079

Please sign in to comment.