Skip to content

Commit a28a89b

Browse files
authored
Add cjs to list of expected node script extensions (#1449)
* Add cjs to list of expected node script extensions * Extend file extension tests * Lint, and simplify code
1 parent f6190b0 commit a28a89b

File tree

8 files changed

+29
-16
lines changed

8 files changed

+29
-16
lines changed

index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1317,7 +1317,7 @@ class Command extends EventEmitter {
13171317
_executeSubCommand(subcommand, args) {
13181318
args = args.slice();
13191319
let launchWithNode = false; // Use node for source targets so do not need to get permissions correct, and on Windows.
1320-
const sourceExt = ['.js', '.ts', '.tsx', '.mjs'];
1320+
const sourceExt = ['.js', '.ts', '.tsx', '.mjs', '.cjs'];
13211321

13221322
// Not checking for help first. Unlikely to have mandatory and executable, and can't robustly test for help flags in external command.
13231323
this._checkForMissingMandatoryOptions();

tests/command.executableSubcommand.lookup.test.js

+16-4
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,23 @@ testOrSkipOnWindows('when subcommand file is double symlink then lookup succeeds
8383

8484
test('when subcommand suffix is .ts then lookup succeeds', async() => {
8585
// We support looking for ts files for ts-node in particular, but don't need to test ts-node itself.
86-
// The program and the subcommand `pm-install.ts` are both plain JavaScript code.
87-
const binLinkTs = path.join(__dirname, 'fixtures-ts', 'pm.ts');
86+
// The subcommand is both plain JavaScript code for this test.
87+
const binLinkTs = path.join(__dirname, 'fixtures-extensions', 'pm.js');
8888
// childProcess.execFile('node', ['-r', 'ts-node/register', binLinkTs, 'install'], function(_error, stdout, stderr) {
89-
const { stdout } = await execFileAsync('node', [binLinkTs, 'install']);
90-
expect(stdout).toBe('install\n');
89+
const { stdout } = await execFileAsync('node', [binLinkTs, 'try-ts']);
90+
expect(stdout).toBe('found .ts\n');
91+
});
92+
93+
test('when subcommand suffix is .cjs then lookup succeeds', async() => {
94+
const binLinkTs = path.join(__dirname, 'fixtures-extensions', 'pm.js');
95+
const { stdout } = await execFileAsync('node', [binLinkTs, 'try-cjs']);
96+
expect(stdout).toBe('found .cjs\n');
97+
});
98+
99+
test('when subcommand suffix is .mjs then lookup succeeds', async() => {
100+
const binLinkTs = path.join(__dirname, 'fixtures-extensions', 'pm.js');
101+
const { stdout } = await execFileAsync('node', [binLinkTs, 'try-mjs']);
102+
expect(stdout).toBe('found .mjs\n');
91103
});
92104

93105
test('when subsubcommand then lookup sub-sub-command', async() => {
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
console.log('found .cjs');
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
console.log('found .mjs');
+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
console.log('found .ts');

tests/fixtures-extensions/pm.js

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#!/usr/bin/env node
2+
3+
const program = require('../../');
4+
5+
program
6+
.command('try-ts', 'test file extension lookup')
7+
.command('try-cjs', 'test file extension lookup')
8+
.command('try-mjs', 'test file extension lookup')
9+
.parse(process.argv);

tests/fixtures-ts/pm-install.ts

-3
This file was deleted.

tests/fixtures-ts/pm.ts

-8
This file was deleted.

0 commit comments

Comments
 (0)