diff --git a/index.js b/index.js index f1cf9eaa1..b53035c78 100644 --- a/index.js +++ b/index.js @@ -1317,7 +1317,7 @@ class Command extends EventEmitter { _executeSubCommand(subcommand, args) { args = args.slice(); let launchWithNode = false; // Use node for source targets so do not need to get permissions correct, and on Windows. - const sourceExt = ['.js', '.ts', '.tsx', '.mjs']; + const sourceExt = ['.js', '.ts', '.tsx', '.mjs', '.cjs']; // Not checking for help first. Unlikely to have mandatory and executable, and can't robustly test for help flags in external command. this._checkForMissingMandatoryOptions(); diff --git a/tests/command.executableSubcommand.lookup.test.js b/tests/command.executableSubcommand.lookup.test.js index 32fcc389b..419d0a537 100644 --- a/tests/command.executableSubcommand.lookup.test.js +++ b/tests/command.executableSubcommand.lookup.test.js @@ -83,11 +83,23 @@ testOrSkipOnWindows('when subcommand file is double symlink then lookup succeeds test('when subcommand suffix is .ts then lookup succeeds', async() => { // We support looking for ts files for ts-node in particular, but don't need to test ts-node itself. - // The program and the subcommand `pm-install.ts` are both plain JavaScript code. - const binLinkTs = path.join(__dirname, 'fixtures-ts', 'pm.ts'); + // The subcommand is both plain JavaScript code for this test. + const binLinkTs = path.join(__dirname, 'fixtures-extensions', 'pm.js'); // childProcess.execFile('node', ['-r', 'ts-node/register', binLinkTs, 'install'], function(_error, stdout, stderr) { - const { stdout } = await execFileAsync('node', [binLinkTs, 'install']); - expect(stdout).toBe('install\n'); + const { stdout } = await execFileAsync('node', [binLinkTs, 'try-ts']); + expect(stdout).toBe('found .ts\n'); +}); + +test('when subcommand suffix is .cjs then lookup succeeds', async() => { + const binLinkTs = path.join(__dirname, 'fixtures-extensions', 'pm.js'); + const { stdout } = await execFileAsync('node', [binLinkTs, 'try-cjs']); + expect(stdout).toBe('found .cjs\n'); +}); + +test('when subcommand suffix is .mjs then lookup succeeds', async() => { + const binLinkTs = path.join(__dirname, 'fixtures-extensions', 'pm.js'); + const { stdout } = await execFileAsync('node', [binLinkTs, 'try-mjs']); + expect(stdout).toBe('found .mjs\n'); }); test('when subsubcommand then lookup sub-sub-command', async() => { diff --git a/tests/fixtures-extensions/pm-try-cjs.cjs b/tests/fixtures-extensions/pm-try-cjs.cjs new file mode 100644 index 000000000..dc14394be --- /dev/null +++ b/tests/fixtures-extensions/pm-try-cjs.cjs @@ -0,0 +1 @@ +console.log('found .cjs'); diff --git a/tests/fixtures-extensions/pm-try-mjs.mjs b/tests/fixtures-extensions/pm-try-mjs.mjs new file mode 100644 index 000000000..262cc527e --- /dev/null +++ b/tests/fixtures-extensions/pm-try-mjs.mjs @@ -0,0 +1 @@ +console.log('found .mjs'); diff --git a/tests/fixtures-extensions/pm-try-ts.ts b/tests/fixtures-extensions/pm-try-ts.ts new file mode 100644 index 000000000..b28c8fcc3 --- /dev/null +++ b/tests/fixtures-extensions/pm-try-ts.ts @@ -0,0 +1 @@ +console.log('found .ts'); diff --git a/tests/fixtures-extensions/pm.js b/tests/fixtures-extensions/pm.js new file mode 100644 index 000000000..8aff56027 --- /dev/null +++ b/tests/fixtures-extensions/pm.js @@ -0,0 +1,9 @@ +#!/usr/bin/env node + +const program = require('../../'); + +program + .command('try-ts', 'test file extension lookup') + .command('try-cjs', 'test file extension lookup') + .command('try-mjs', 'test file extension lookup') + .parse(process.argv); diff --git a/tests/fixtures-ts/pm-install.ts b/tests/fixtures-ts/pm-install.ts deleted file mode 100644 index a99408b45..000000000 --- a/tests/fixtures-ts/pm-install.ts +++ /dev/null @@ -1,3 +0,0 @@ -#!/usr/bin/env node - -console.log('install'); diff --git a/tests/fixtures-ts/pm.ts b/tests/fixtures-ts/pm.ts deleted file mode 100644 index 41f8fc002..000000000 --- a/tests/fixtures-ts/pm.ts +++ /dev/null @@ -1,8 +0,0 @@ -#!/usr/bin/env node - -var program = require('../../'); - -program - .version('0.0.1') - .command('install [name]', 'install one or more packages') - .parse(process.argv);