From 46b2114e5597f049ad31a6ca50873ccbe1b60ba3 Mon Sep 17 00:00:00 2001 From: EGOIST <0x142857@gmail.com> Date: Tue, 12 Oct 2021 17:44:08 +0800 Subject: [PATCH] fix: remove `module.exports` statement from esm build --- examples/basic-usage.js | 2 +- examples/command-examples.js | 4 ++-- examples/command-options.js | 2 +- examples/dot-nested-options.js | 4 ++-- examples/help.js | 2 +- examples/ignore-default-value.js | 6 +++--- examples/negated-option.js | 2 +- examples/sub-command.js | 2 +- examples/variadic-arguments.js | 2 +- index-compat.js | 11 +++++++++++ package.json | 5 +++-- scripts/build-deno.ts | 6 ------ src/__test__/index.test.ts | 2 ++ src/index.ts | 11 ----------- 14 files changed, 29 insertions(+), 32 deletions(-) create mode 100644 index-compat.js diff --git a/examples/basic-usage.js b/examples/basic-usage.js index 081396c..a2e4545 100644 --- a/examples/basic-usage.js +++ b/examples/basic-usage.js @@ -1,5 +1,5 @@ require('ts-node/register') -const cli = require('../src/index')() +const cli = require('../src/index').cac() cli.option('--type [type]', 'Choose a project type') diff --git a/examples/command-examples.js b/examples/command-examples.js index 5fa49f8..7c25f34 100644 --- a/examples/command-examples.js +++ b/examples/command-examples.js @@ -1,10 +1,10 @@ require('ts-node/register') -const cli = require('../src/index')() +const cli = require('../src/index').cac() cli .command('build', 'Build project') .example('cli build foo.js') - .example(name => { + .example((name) => { return `${name} build foo.js` }) .option('--type [type]', 'Choose a project type') diff --git a/examples/command-options.js b/examples/command-options.js index 0202e08..0b3606d 100644 --- a/examples/command-options.js +++ b/examples/command-options.js @@ -1,5 +1,5 @@ require('ts-node/register') -const cli = require('../src/index')() +const cli = require('../src/index').cac() cli .command('rm ', 'Remove a dir') diff --git a/examples/dot-nested-options.js b/examples/dot-nested-options.js index 9b8eedd..0bad819 100644 --- a/examples/dot-nested-options.js +++ b/examples/dot-nested-options.js @@ -1,12 +1,12 @@ require('ts-node/register') -const cli = require('../src/index')() +const cli = require('../src/index').cac() cli .command('build', 'desc') .option('--env ', 'Set envs') .option('--foo-bar ', 'Set foo bar') .example('--env.API_SECRET xxx') - .action(options => { + .action((options) => { console.log(options) }) diff --git a/examples/help.js b/examples/help.js index e5ad7fb..a948dfb 100644 --- a/examples/help.js +++ b/examples/help.js @@ -1,5 +1,5 @@ require('ts-node/register') -const cli = require('../src/index')() +const cli = require('../src/index').cac() cli.option('--type [type]', 'Choose a project type', { default: 'node', diff --git a/examples/ignore-default-value.js b/examples/ignore-default-value.js index 96daa0b..9286379 100644 --- a/examples/ignore-default-value.js +++ b/examples/ignore-default-value.js @@ -1,12 +1,12 @@ require('ts-node/register') -const cli = require('../src/index')() +const cli = require('../src/index').cac() cli .command('build', 'Build project', { - ignoreOptionDefaultValue: true + ignoreOptionDefaultValue: true, }) .option('--type [type]', 'Choose a project type', { - default: 'node' + default: 'node', }) const parsed = cli.parse() diff --git a/examples/negated-option.js b/examples/negated-option.js index cb4b485..da726c7 100644 --- a/examples/negated-option.js +++ b/examples/negated-option.js @@ -1,5 +1,5 @@ require('ts-node/register') -const cli = require('../src/index')() +const cli = require('../src/index').cac() cli.option('--no-clear-screen', 'Do not clear screen') diff --git a/examples/sub-command.js b/examples/sub-command.js index 4e9e9a6..32a4715 100644 --- a/examples/sub-command.js +++ b/examples/sub-command.js @@ -1,5 +1,5 @@ require('ts-node/register') -const cli = require('../src/index')() +const cli = require('../src/index').cac() cli .command('deploy [path]', 'Deploy to AWS') diff --git a/examples/variadic-arguments.js b/examples/variadic-arguments.js index 155db1b..0fa5b83 100644 --- a/examples/variadic-arguments.js +++ b/examples/variadic-arguments.js @@ -1,5 +1,5 @@ require('ts-node/register') -const cli = require('../src/index')() +const cli = require('../src/index').cac() cli .command('build [...otherFiles]', 'Build your app') diff --git a/index-compat.js b/index-compat.js new file mode 100644 index 0000000..6b8a78d --- /dev/null +++ b/index-compat.js @@ -0,0 +1,11 @@ +const { cac, CAC, Command } = require('./dist/index') + +// For backwards compatibility +module.exports = cac + +Object.assign(module.exports, { + default: cac, + cac, + CAC, + Command, +}) diff --git a/package.json b/package.json index 3a4751b..f0a6498 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,7 @@ "url": "egoist/cac", "type": "git" }, - "main": "dist/index.js", + "main": "index-compat.js", "module": "dist/index.mjs", "types": "dist/index.d.ts", "exports": { @@ -22,7 +22,8 @@ "!**/__test__/**", "/mod.js", "/mod.ts", - "/deno" + "/deno", + "/index-compat.js" ], "scripts": { "test": "jest", diff --git a/scripts/build-deno.ts b/scripts/build-deno.ts index bd98d5f..6bd8928 100644 --- a/scripts/build-deno.ts +++ b/scripts/build-deno.ts @@ -23,12 +23,6 @@ function node2deno(options: { types: typeof Types }): PluginObj { source.value = `https://cdn.skypack.dev/mri` } }, - - IfStatement(path) { - if (path.getSource().includes('@remove-for-deno')) { - path.remove() - } - }, }, } } diff --git a/src/__test__/index.test.ts b/src/__test__/index.test.ts index 34f79ed..6cb8406 100644 --- a/src/__test__/index.test.ts +++ b/src/__test__/index.test.ts @@ -2,6 +2,8 @@ import path from 'path' import execa from 'execa' import cac from '..' +jest.setTimeout(30000) + function example(file: string) { return path.relative( process.cwd(), diff --git a/src/index.ts b/src/index.ts index 3842352..a45a0f4 100644 --- a/src/index.ts +++ b/src/index.ts @@ -8,14 +8,3 @@ const cac = (name = '') => new CAC(name) export default cac export { cac, CAC, Command } - -if (typeof module !== 'undefined') { - // @remove-for-deno - module.exports = cac - Object.assign(module.exports, { - default: cac, - cac, - CAC, - Command, - }) -}