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..4a7ca88 --- /dev/null +++ b/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 + +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..d48c222 --- /dev/null +++ b/estest/package.json @@ -0,0 +1,4 @@ +{ + "name": "estest", + "type": "module" +} \ No newline at end of file diff --git a/index.js b/index.js index c151058..21c6ba9 100644 --- a/index.js +++ b/index.js @@ -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') { @@ -22,7 +25,7 @@ const meow = (helpText, options) => { options = { pkg: readPkgUp.sync({ - cwd: parentDir, + cwd: parentDirectory, normalize: false }).packageJson || {}, argv: process.argv.slice(2), diff --git a/test.js b/test.js index d3e40ec..3a7efd5 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'], { + cwd: path.join(__dirname, 'estest') + }); + t.regex(stdout, /rainbow: false/); + } catch (error) { + t.is(error, undefined); + } + }); +}