Skip to content

Commit

Permalink
fix: follow Node.js flag handling (#133)
Browse files Browse the repository at this point in the history
  • Loading branch information
privatenumber committed Nov 10, 2022
1 parent 5d8cc04 commit 76a83c2
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 58 deletions.
4 changes: 2 additions & 2 deletions package.json
Expand Up @@ -60,7 +60,7 @@
"@types/semver": "^7.3.12",
"chokidar": "^3.5.3",
"clean-pkg-json": "^1.2.0",
"cleye": "^1.2.1",
"cleye": "^1.3.0",
"cross-spawn": "^7.0.3",
"eslint": "^8.24.0",
"execa": "^6.1.0",
Expand All @@ -74,7 +74,7 @@
"semver": "^7.3.8",
"simple-git-hooks": "^2.8.1",
"strip-ansi": "^7.0.1",
"type-flag": "^2.2.0",
"type-flag": "^3.0.0",
"typescript": "^4.8.4"
},
"eslintConfig": {
Expand Down
18 changes: 9 additions & 9 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

71 changes: 32 additions & 39 deletions src/cli.ts
@@ -1,8 +1,11 @@
import { cli } from 'cleye';
import typeFlag from 'type-flag';
import { version } from '../package.json';
import { run } from './run';
import { watchCommand } from './watch';
import {
removeArgvFlags,
ignoreAfterArgument,
} from './remove-argv-flags';

const tsxFlags = {
noCache: {
Expand All @@ -15,54 +18,44 @@ const tsxFlags = {
},
};

const flags = {
...tsxFlags,
version: {
type: Boolean,
description: 'Show version',
},
help: {
type: Boolean,
alias: 'h',
description: 'Show help',
},
};

cli({
name: 'tsx',
parameters: ['[script path]'],
commands: [
watchCommand,
],
flags,
flags: {
...tsxFlags,
version: {
type: Boolean,
alias: 'v',
description: 'Show version',
},
help: {
type: Boolean,
alias: 'h',
description: 'Show help',
},
},
help: false,
ignoreArgv: ignoreAfterArgument(),
}, (argv) => {
const noArgs = argv._.length === 0;

if (noArgs) {
if (argv.flags.version) {
console.log(version);
return;
}

if (argv.flags.help) {
argv.showHelp({
description: 'Node.js runtime enhanced with esbuild for loading TypeScript & ESM',
});
return;
}
if (argv.flags.version) {
process.stdout.write(`tsx v${version}\nnode `);
} else if (argv.flags.help) {
argv.showHelp({
description: 'Node.js runtime enhanced with esbuild for loading TypeScript & ESM',
});
console.log(`${'-'.repeat(45)}\n`);
}

const args = typeFlag(
noArgs ? flags : tsxFlags,
process.argv.slice(2),
{ ignoreUnknown: true },
)._;

const childProcess = run(args, {
noCache: Boolean(argv.flags.noCache),
tsconfigPath: argv.flags.tsconfig,
});
const childProcess = run(
removeArgvFlags(tsxFlags),
{
noCache: Boolean(argv.flags.noCache),
tsconfigPath: argv.flags.tsconfig,
},
);

const relaySignal = async (signal: NodeJS.Signals) => {
const message = await Promise.race([
Expand Down
35 changes: 35 additions & 0 deletions src/remove-argv-flags.ts
@@ -0,0 +1,35 @@
import {
typeFlag,
type Flags,
type TypeFlagOptions,
} from 'type-flag';

export const ignoreAfterArgument = (): TypeFlagOptions['ignore'] => {
let ignore = false;

return (type) => {
if (ignore) {
return true;
}
const isArgument = type === 'argument';
if (isArgument || type === 'unknown-flag') {
ignore = isArgument;
return true;
}
};
};

export function removeArgvFlags(
tsxFlags: Flags,
argv = process.argv.slice(2),
) {
typeFlag(
tsxFlags,
argv,
{
ignore: ignoreAfterArgument(),
},
);

return argv;
}
10 changes: 3 additions & 7 deletions src/watch/index.ts
Expand Up @@ -3,9 +3,9 @@ import { fileURLToPath } from 'url';
import { constants as osConstants } from 'os';
import path from 'path';
import { command } from 'cleye';
import typeFlag from 'type-flag';
import { watch } from 'chokidar';
import { run } from '../run';
import { removeArgvFlags } from '../remove-argv-flags';
import {
clearScreen,
debounce,
Expand Down Expand Up @@ -42,11 +42,7 @@ export const watchCommand = command({
description: 'Run the script and watch for changes',
},
}, (argv) => {
const args = typeFlag(
flags,
process.argv.slice(3),
{ ignoreUnknown: true },
)._;
const rawArgvs = removeArgvFlags(flags, process.argv.slice(3));

const options = {
noCache: argv.flags.noCache,
Expand Down Expand Up @@ -75,7 +71,7 @@ export const watchCommand = command({
process.stdout.write(clearScreen);
}

runProcess = run(args, options);
runProcess = run(rawArgvs, options);

runProcess.on('message', (data) => {
// Collect run-time dependencies to watch
Expand Down
5 changes: 4 additions & 1 deletion tests/specs/cli.ts
Expand Up @@ -13,7 +13,7 @@ export default testSuite(({ describe }, fixturePath: string) => {
});

expect(tsxProcess.exitCode).toBe(0);
expect(tsxProcess.stdout).toBe(packageJson.version);
expect(tsxProcess.stdout).toBe(`tsx v${packageJson.version}\nnode ${process.version}`);
expect(tsxProcess.stderr).toBe('');
});

Expand All @@ -27,6 +27,7 @@ export default testSuite(({ describe }, fixturePath: string) => {

expect(tsxProcess.exitCode).toBe(0);
expect(tsxProcess.stdout).toMatch('"--version"');
expect(tsxProcess.stdout).not.toMatch(packageJson.version);
expect(tsxProcess.stderr).toBe('');
});
});
Expand All @@ -39,6 +40,7 @@ export default testSuite(({ describe }, fixturePath: string) => {

expect(tsxProcess.exitCode).toBe(0);
expect(tsxProcess.stdout).toMatch('Node.js runtime enhanced with esbuild for loading TypeScript & ESM');
expect(tsxProcess.stdout).toMatch('Usage: node [options] [ script.js ] [arguments]');
expect(tsxProcess.stderr).toBe('');
});

Expand All @@ -52,6 +54,7 @@ export default testSuite(({ describe }, fixturePath: string) => {

expect(tsxProcess.exitCode).toBe(0);
expect(tsxProcess.stdout).toMatch('"--help"');
expect(tsxProcess.stdout).not.toMatch('tsx');
expect(tsxProcess.stderr).toBe('');
});
});
Expand Down

0 comments on commit 76a83c2

Please sign in to comment.