From c4d607932a2dc6e43071beb408c0e4cdb504e3f4 Mon Sep 17 00:00:00 2001 From: jimthedev Date: Mon, 27 Apr 2020 11:30:40 -0500 Subject: [PATCH] fix: enable es modules --- .travis.yml | 1 + estest/index.js | 26 ++++++++++++++++++++++++++ estest/package.json | 4 ++++ index.js | 7 +++++-- test.js | 12 ++++++++++++ 5 files changed, 48 insertions(+), 2 deletions(-) create mode 100644 estest/index.js create mode 100644 estest/package.json 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..2a5960a 100644 --- a/test.js +++ b/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 '.'; @@ -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); + } +});