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

feat: support TypeScript code in eval flag #190

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 30 additions & 1 deletion src/cli.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { cli } from 'cleye';
import { transformSync } from '@esbuild-kit/core-utils';
import { version } from '../package.json';
import { run } from './run';
import { watchCommand } from './watch';
Expand Down Expand Up @@ -36,6 +37,11 @@ cli({
alias: 'h',
description: 'Show help',
},
eval: {
type: String,
alias: 'e',
description: 'Evaluate code inside the eval flag',
},
},
help: false,
ignoreArgv: ignoreAfterArgument(),
Expand All @@ -49,8 +55,31 @@ cli({
console.log(`${'-'.repeat(45)}\n`);
}

const argvsToRun = removeArgvFlags(tsxFlags);

if (argv.flags.eval) {
const transformed = transformSync(
argv.flags.eval,
'/tmp/tsx_temporarily_stored_cache_file_for_the_code.ts',
{
loader: 'ts',
tsconfigRaw: {
compilerOptions: {
preserveValueImports: true,
},
},
define: {
require: 'global.require',
},
banner: '',
footer: '',
},
);
argvsToRun[1] = transformed.code;
}

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

describe('eval', ({ test }) => {
test('evaluates TypeScript code inside the eval flag', async () => {
const tsxProcess = await tsx({
args: ['--eval', 'const thing: string = "hi!!!"; console.log(thing);'],
});

expect(tsxProcess.exitCode).toBe(0);
expect(tsxProcess.stdout).toMatch('hi!!!');
expect(tsxProcess.stderr).toBe('');
});

test('works expectedly with --input-type=module', async () => {
const tsxProcess = await tsx({
args: ['--eval', 'console.log(import.meta.url);', '--input-type=module'],
});

expect(tsxProcess.exitCode).toBe(0);
expect(tsxProcess.stdout).toMatch('undefined');
expect(tsxProcess.stderr).toBe('');
});

test('fails to access __dirname with --input-type=module', async () => {
const tsxProcess = await tsx({
args: ['--eval', 'console.log(__dirname);', '--input-type=module'],
});

expect(tsxProcess.exitCode).toBe(1);
expect(tsxProcess.stderr).toMatch('ReferenceError: __dirname is not defined in ES module scope');
});
});

test('Node.js test runner', async () => {
const tsxProcess = await tsx({
args: [
Expand Down