Skip to content

Commit

Permalink
Add cjs to list of expected node script extensions (#1449)
Browse files Browse the repository at this point in the history
* Add cjs to list of expected node script extensions

* Extend file extension tests

* Lint, and simplify code
  • Loading branch information
shadowspawn committed Jan 30, 2021
1 parent f6190b0 commit a28a89b
Show file tree
Hide file tree
Showing 8 changed files with 29 additions and 16 deletions.
2 changes: 1 addition & 1 deletion index.js
Expand Up @@ -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();
Expand Down
20 changes: 16 additions & 4 deletions tests/command.executableSubcommand.lookup.test.js
Expand Up @@ -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() => {
Expand Down
1 change: 1 addition & 0 deletions tests/fixtures-extensions/pm-try-cjs.cjs
@@ -0,0 +1 @@
console.log('found .cjs');
1 change: 1 addition & 0 deletions tests/fixtures-extensions/pm-try-mjs.mjs
@@ -0,0 +1 @@
console.log('found .mjs');
1 change: 1 addition & 0 deletions tests/fixtures-extensions/pm-try-ts.ts
@@ -0,0 +1 @@
console.log('found .ts');
9 changes: 9 additions & 0 deletions 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);
3 changes: 0 additions & 3 deletions tests/fixtures-ts/pm-install.ts

This file was deleted.

8 changes: 0 additions & 8 deletions tests/fixtures-ts/pm.ts

This file was deleted.

0 comments on commit a28a89b

Please sign in to comment.