Skip to content

Commit

Permalink
feat: support TypeScript code in eval flag (#402)
Browse files Browse the repository at this point in the history
Co-authored-by: Ray Wong <raycnwong@gmail.com>
  • Loading branch information
privatenumber and rayrw committed Nov 21, 2023
1 parent 06c89c9 commit db773c5
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 1 deletion.
47 changes: 46 additions & 1 deletion src/cli.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { cli } from 'cleye';
import {
transformSync as esbuildTransformSync,
} from 'esbuild';
import { version } from '../package.json';
import { run } from './run';
import { watchCommand } from './watch';
Expand Down Expand Up @@ -49,8 +52,50 @@ cli({
console.log(`${'-'.repeat(45)}\n`);

Check warning on line 52 in src/cli.ts

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

Unexpected console statement
}

const interceptFlags = {
eval: {
type: String,
alias: 'e',
},
print: {
type: String,
alias: 'p',
},
};

const { flags: interceptedFlags } = cli({
flags: {
...interceptFlags,
inputType: String,
},
help: false,
ignoreArgv: ignoreAfterArgument(),
});

const argvsToRun = removeArgvFlags({
...tsxFlags,
...interceptFlags,
});

const evalTypes = ['print', 'eval'] as const;
const evalType = evalTypes.find(type => Boolean(interceptedFlags[type]));
if (evalType) {
const { inputType } = interceptedFlags;
const evalCode = interceptedFlags[evalType]!;
const transformed = esbuildTransformSync(
evalCode,
{
loader: 'default',
sourcefile: '/eval.ts',
format: inputType === 'module' ? 'esm' : 'cjs',
},
);

argvsToRun.push(`--${evalType}`, transformed.code);
}

const childProcess = run(
removeArgvFlags(tsxFlags),
argvsToRun,
{
noCache: Boolean(argv.flags.noCache),
tsconfigPath: argv.flags.tsconfig,
Expand Down
34 changes: 34 additions & 0 deletions tests/specs/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,40 @@ export default testSuite(({ describe }) => {
});
});

describe('eval & print', ({ test }) => {
test('TypeScript', async () => {
const tsxProcess = await tsx({
args: ['--eval', 'console.log(require("fs") && module as string)'],
});

expect(tsxProcess.exitCode).toBe(0);
expect(tsxProcess.stdout).toMatch("id: '[eval]'");
expect(tsxProcess.stderr).toBe('');
});

test('--input-type=module is respected', async () => {
const tsxProcess = await tsx({
args: ['--eval', 'console.log(JSON.stringify([typeof require, import.meta.url]))', '--input-type=module'],
});

expect(tsxProcess.exitCode).toBe(0);
const [requireDefined, importMetaUrl] = JSON.parse(tsxProcess.stdout);
expect(requireDefined).toBe('undefined');
expect(importMetaUrl.endsWith('/[eval1]')).toBeTruthy();
expect(tsxProcess.stderr).toBe('');
});

test('--print', async () => {
const tsxProcess = await tsx({
args: ['--print', 'require("fs") && module as string'],
});

expect(tsxProcess.exitCode).toBe(0);
expect(tsxProcess.stdout).toMatch("id: '[eval]'");
expect(tsxProcess.stderr).toBe('');
});
});

test('Node.js test runner', async ({ onTestFinish }) => {
const fixture = await createFixture({
'test.ts': `
Expand Down

0 comments on commit db773c5

Please sign in to comment.