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

fix: follow Node.js flag handling #133

Merged
merged 9 commits into from
Nov 10, 2022
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
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a breaking change.

When pass args to some.ts :

< 3.12.0 :

  tsx some.ts -- dev

>= 3.12.0 :

  tsx some.ts dev

follow Semantic Versioning Specification , I think should bump to 4.0.0 , not 3.12.0

{
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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