Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add cjs to list of expected node script extensions #1449

Merged
merged 3 commits into from Jan 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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.