Skip to content

Commit 652fc95

Browse files
authored
Implement #1598: Support both camelCase and hyphen-case for all CLI flags; update documentation to prefer camelCase (#1599)
* fix * fix * lint-fix * fix
1 parent aff9bb9 commit 652fc95

File tree

9 files changed

+124
-94
lines changed

9 files changed

+124
-94
lines changed

src/bin-cwd.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22

33
import { main } from './bin';
44

5-
main(undefined, { '--cwd-mode': true });
5+
main(undefined, { '--cwdMode': true });

src/bin-script-deprecated.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@ console.warn(
77
'Please use ts-node-script instead'
88
);
99

10-
main(undefined, { '--script-mode': true });
10+
main(undefined, { '--scriptMode': true });

src/bin-script.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22

33
import { main } from './bin';
44

5-
main(undefined, { '--script-mode': true });
5+
main(undefined, { '--scriptMode': true });

src/bin-transpile.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22

33
import { main } from './bin';
44

5-
main(undefined, { '--transpile-only': true });
5+
main(undefined, { '--transpileOnly': true });

src/bin.ts

Lines changed: 98 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,20 @@ export function main(
2828
argv: string[] = process.argv.slice(2),
2929
entrypointArgs: Record<string, any> = {}
3030
) {
31+
// HACK: technically, this function is not marked @internal so it's possible
32+
// that libraries in the wild are doing `require('ts-node/dist/bin').main({'--transpile-only': true})`
33+
// We can mark this function @internal in next major release.
34+
// For now, rewrite args to avoid a breaking change.
35+
entrypointArgs = { ...entrypointArgs };
36+
for (const key of Object.keys(entrypointArgs)) {
37+
entrypointArgs[
38+
key.replace(
39+
/([a-z])-([a-z])/g,
40+
(_$0, $1, $2: string) => `${$1}${$2.toUpperCase()}`
41+
)
42+
] = entrypointArgs[key];
43+
}
44+
3145
const args = {
3246
...entrypointArgs,
3347
...arg(
@@ -40,33 +54,33 @@ export function main(
4054

4155
// CLI options.
4256
'--help': Boolean,
43-
'--cwd-mode': Boolean,
44-
'--script-mode': Boolean,
57+
'--cwdMode': Boolean,
58+
'--scriptMode': Boolean,
4559
'--version': arg.COUNT,
46-
'--show-config': Boolean,
60+
'--showConfig': Boolean,
4761

4862
// Project options.
4963
'--cwd': String,
5064
'--files': Boolean,
5165
'--compiler': String,
52-
'--compiler-options': parse,
66+
'--compilerOptions': parse,
5367
'--project': String,
54-
'--ignore-diagnostics': [String],
68+
'--ignoreDiagnostics': [String],
5569
'--ignore': [String],
56-
'--transpile-only': Boolean,
70+
'--transpileOnly': Boolean,
5771
'--transpiler': String,
5872
'--swc': Boolean,
59-
'--type-check': Boolean,
60-
'--compiler-host': Boolean,
73+
'--typeCheck': Boolean,
74+
'--compilerHost': Boolean,
6175
'--pretty': Boolean,
62-
'--skip-project': Boolean,
63-
'--skip-ignore': Boolean,
64-
'--prefer-ts-exts': Boolean,
65-
'--log-error': Boolean,
76+
'--skipProject': Boolean,
77+
'--skipIgnore': Boolean,
78+
'--preferTsExts': Boolean,
79+
'--logError': Boolean,
6680
'--emit': Boolean,
6781
'--scope': Boolean,
68-
'--scope-dir': String,
69-
'--no-experimental-repl-await': Boolean,
82+
'--scopeDir': String,
83+
'--noExperimentalReplAwait': Boolean,
7084

7185
// Aliases.
7286
'-e': '--eval',
@@ -76,16 +90,30 @@ export function main(
7690
'-h': '--help',
7791
'-s': '--script-mode',
7892
'-v': '--version',
79-
'-T': '--transpile-only',
80-
'-H': '--compiler-host',
93+
'-T': '--transpileOnly',
94+
'-H': '--compilerHost',
8195
'-I': '--ignore',
8296
'-P': '--project',
8397
'-C': '--compiler',
84-
'-D': '--ignore-diagnostics',
85-
'-O': '--compiler-options',
98+
'-D': '--ignoreDiagnostics',
99+
'-O': '--compilerOptions',
86100
'--dir': '--cwd',
87-
'--showConfig': '--show-config',
88-
'--scopeDir': '--scope-dir',
101+
102+
// Support both tsc-style camelCase and node-style hypen-case for *all* flags
103+
'--cwd-mode': '--cwdMode',
104+
'--script-mode': '--scriptMode',
105+
'--show-config': '--showConfig',
106+
'--compiler-options': '--compilerOptions',
107+
'--ignore-diagnostics': '--ignoreDiagnostics',
108+
'--transpile-only': '--transpileOnly',
109+
'--type-check': '--typeCheck',
110+
'--compiler-host': '--compilerHost',
111+
'--skip-project': '--skipProject',
112+
'--skip-ignore': '--skipIgnore',
113+
'--prefer-ts-exts': '--preferTsExts',
114+
'--log-error': '--logError',
115+
'--scope-dir': '--scopeDir',
116+
'--no-experimental-repl-await': '--noExperimentalReplAwait',
89117
},
90118
{
91119
argv,
@@ -100,74 +128,74 @@ export function main(
100128
const {
101129
'--cwd': cwdArg,
102130
'--help': help = false,
103-
'--script-mode': scriptMode,
104-
'--cwd-mode': cwdMode,
131+
'--scriptMode': scriptMode,
132+
'--cwdMode': cwdMode,
105133
'--version': version = 0,
106-
'--show-config': showConfig,
134+
'--showConfig': showConfig,
107135
'--require': argsRequire = [],
108136
'--eval': code = undefined,
109137
'--print': print = false,
110138
'--interactive': interactive = false,
111139
'--files': files,
112140
'--compiler': compiler,
113-
'--compiler-options': compilerOptions,
141+
'--compilerOptions': compilerOptions,
114142
'--project': project,
115-
'--ignore-diagnostics': ignoreDiagnostics,
143+
'--ignoreDiagnostics': ignoreDiagnostics,
116144
'--ignore': ignore,
117-
'--transpile-only': transpileOnly,
118-
'--type-check': typeCheck,
145+
'--transpileOnly': transpileOnly,
146+
'--typeCheck': typeCheck,
119147
'--transpiler': transpiler,
120148
'--swc': swc,
121-
'--compiler-host': compilerHost,
149+
'--compilerHost': compilerHost,
122150
'--pretty': pretty,
123-
'--skip-project': skipProject,
124-
'--skip-ignore': skipIgnore,
125-
'--prefer-ts-exts': preferTsExts,
126-
'--log-error': logError,
151+
'--skipProject': skipProject,
152+
'--skipIgnore': skipIgnore,
153+
'--preferTsExts': preferTsExts,
154+
'--logError': logError,
127155
'--emit': emit,
128156
'--scope': scope = undefined,
129-
'--scope-dir': scopeDir = undefined,
130-
'--no-experimental-repl-await': noExperimentalReplAwait,
157+
'--scopeDir': scopeDir = undefined,
158+
'--noExperimentalReplAwait': noExperimentalReplAwait,
131159
} = args;
132160

133161
if (help) {
134162
console.log(`
135-
Usage: ts-node [options] [ -e script | script.ts ] [arguments]
136-
137-
Options:
138-
139-
-e, --eval [code] Evaluate code
140-
-p, --print Print result of \`--eval\`
141-
-r, --require [path] Require a node module before execution
142-
-i, --interactive Opens the REPL even if stdin does not appear to be a terminal
143-
144-
-h, --help Print CLI usage
145-
-v, --version Print module version information
146-
--cwd-mode Use current directory instead of <script.ts> for config resolution
147-
--show-config Print resolved configuration and exit
148-
149-
-T, --transpile-only Use TypeScript's faster \`transpileModule\` or a third-party transpiler
150-
--swc Use the swc transpiler
151-
-H, --compiler-host Use TypeScript's compiler host API
152-
-I, --ignore [pattern] Override the path patterns to skip compilation
153-
-P, --project [path] Path to TypeScript JSON project file
154-
-C, --compiler [name] Specify a custom TypeScript compiler
155-
--transpiler [name] Specify a third-party, non-typechecking transpiler
156-
-D, --ignore-diagnostics [code] Ignore TypeScript warnings by diagnostic code
157-
-O, --compiler-options [opts] JSON object to merge with compiler options
158-
159-
--cwd Behave as if invoked within this working directory.
160-
--files Load \`files\`, \`include\` and \`exclude\` from \`tsconfig.json\` on startup
161-
--pretty Use pretty diagnostic formatter (usually enabled by default)
162-
--skip-project Skip reading \`tsconfig.json\`
163-
--skip-ignore Skip \`--ignore\` checks
164-
--emit Emit output files into \`.ts-node\` directory
165-
--scope Scope compiler to files within \`scopeDir\`. Anything outside this directory is ignored.
166-
--scope-dir Directory for \`--scope\`
167-
--prefer-ts-exts Prefer importing TypeScript files over JavaScript files
168-
--log-error Logs TypeScript errors to stderr instead of throwing exceptions
169-
--no-experimental-repl-await Disable top-level await in REPL. Equivalent to node's --no-experimental-repl-await
170-
`);
163+
Usage: ts-node [options] [ -e script | script.ts ] [arguments]
164+
165+
Options:
166+
167+
-e, --eval [code] Evaluate code
168+
-p, --print Print result of \`--eval\`
169+
-r, --require [path] Require a node module before execution
170+
-i, --interactive Opens the REPL even if stdin does not appear to be a terminal
171+
172+
-h, --help Print CLI usage
173+
-v, --version Print module version information
174+
--cwdMode Use current directory instead of <script.ts> for config resolution
175+
--showConfig Print resolved configuration and exit
176+
177+
-T, --transpileOnly Use TypeScript's faster \`transpileModule\` or a third-party transpiler
178+
--swc Use the swc transpiler
179+
-H, --compilerHost Use TypeScript's compiler host API
180+
-I, --ignore [pattern] Override the path patterns to skip compilation
181+
-P, --project [path] Path to TypeScript JSON project file
182+
-C, --compiler [name] Specify a custom TypeScript compiler
183+
--transpiler [name] Specify a third-party, non-typechecking transpiler
184+
-D, --ignoreDiagnostics [code] Ignore TypeScript warnings by diagnostic code
185+
-O, --compilerOptions [opts] JSON object to merge with compiler options
186+
187+
--cwd Behave as if invoked within this working directory.
188+
--files Load \`files\`, \`include\` and \`exclude\` from \`tsconfig.json\` on startup
189+
--pretty Use pretty diagnostic formatter (usually enabled by default)
190+
--skipProject Skip reading \`tsconfig.json\`
191+
--skipIgnore Skip \`--ignore\` checks
192+
--emit Emit output files into \`.ts-node\` directory
193+
--scope Scope compiler to files within \`scopeDir\`. Anything outside this directory is ignored.
194+
--scopeDir Directory for \`--scope\`
195+
--preferTsExts Prefer importing TypeScript files over JavaScript files
196+
--logError Logs TypeScript errors to stderr instead of throwing exceptions
197+
--noExperimentalReplAwait Disable top-level await in REPL. Equivalent to node's --no-experimental-repl-await
198+
`);
171199

172200
process.exit(0);
173201
}

website/docs/configuration.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ Hello, Ronald!
1919

2020
ts-node automatically finds and loads `tsconfig.json`. Most ts-node options can be specified in a `"ts-node"` object using their programmatic, camelCase names. We recommend this because it works even when you cannot pass CLI flags, such as `node --require ts-node/register` and when using shebangs.
2121

22-
Use `--skip-project` to skip loading the `tsconfig.json`. Use `--project` to explicitly specify the path to a `tsconfig.json`.
22+
Use `--skipProject` to skip loading the `tsconfig.json`. Use `--project` to explicitly specify the path to a `tsconfig.json`.
2323

24-
When searching, it is resolved using [the same search behavior as `tsc`](https://www.typescriptlang.org/docs/handbook/tsconfig-json.html). By default, this search is performed relative to the entrypoint script. In `--cwd-mode` or if no entrypoint is specified -- for example when using the REPL -- the search is performed relative to `--cwd` / `process.cwd()`.
24+
When searching, it is resolved using [the same search behavior as `tsc`](https://www.typescriptlang.org/docs/handbook/tsconfig-json.html). By default, this search is performed relative to the entrypoint script. In `--cwdMode` or if no entrypoint is specified -- for example when using the REPL -- the search is performed relative to `--cwd` / `process.cwd()`.
2525

2626
You can use this sample configuration as a starting point:
2727

@@ -74,7 +74,7 @@ With the latest `node` and `typescript`, this is [`@tsconfig/node16`](https://gi
7474

7575
Older versions of `typescript` are incompatible with `@tsconfig/node16`. In those cases we will use an older default configuration.
7676

77-
When in doubt, `ts-node --show-config` will log the configuration being used, and `ts-node -vv` will log `node` and `typescript` versions.
77+
When in doubt, `ts-node --showConfig` will log the configuration being used, and `ts-node -vv` will log `node` and `typescript` versions.
7878

7979
## `node` flags
8080

website/docs/options.md

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ title: Options
44

55
`ts-node` supports `--print` (`-p`), `--eval` (`-e`), `--require` (`-r`) and `--interactive` (`-i`) similar to the [node.js CLI options](https://nodejs.org/api/cli.html).
66

7+
All command-line flags support both `--camelCase` and `--hyphen-case`.
8+
79
_Environment variables, where available, are in `ALL_CAPS`_
810

911
## Shell
@@ -17,31 +19,31 @@ _Environment variables, where available, are in `ALL_CAPS`_
1719
## TSConfig
1820

1921
- `-P, --project [path]` Path to TypeScript JSON project file <br/>*Environment:* `TS_NODE_PROJECT`
20-
- `--skip-project` Skip project config resolution and loading <br/>*Default:* `false` <br/>*Environment:* `TS_NODE_SKIP_PROJECT`
21-
- `-c, --cwd-mode` Resolve config relative to the current directory instead of the directory of the entrypoint script
22-
- `-O, --compiler-options [opts]` JSON object to merge with compiler options <br/>*Environment:* `TS_NODE_COMPILER_OPTIONS`
23-
- `--show-config` Print resolved `tsconfig.json`, including `ts-node` options, and exit
22+
- `--skipProject` Skip project config resolution and loading <br/>*Default:* `false` <br/>*Environment:* `TS_NODE_SKIP_PROJECT`
23+
- `-c, --cwdMode` Resolve config relative to the current directory instead of the directory of the entrypoint script
24+
- `-O, --compilerOptions [opts]` JSON object to merge with compiler options <br/>*Environment:* `TS_NODE_COMPILER_OPTIONS`
25+
- `--showConfig` Print resolved `tsconfig.json`, including `ts-node` options, and exit
2426

2527
## Typechecking
2628

27-
- `-T, --transpile-only` Use TypeScript's faster `transpileModule` <br/>*Default:* `false` <br/>*Environment:* `TS_NODE_TRANSPILE_ONLY`
28-
- `--type-check` Opposite of `--transpile-only` <br/>*Default:* `true`<br/>*Environment:* `TS_NODE_TYPE_CHECK`
29-
- `-H, --compiler-host` Use TypeScript's compiler host API <br/>*Default:* `false` <br/>*Environment:* `TS_NODE_COMPILER_HOST`
29+
- `-T, --transpileOnly` Use TypeScript's faster `transpileModule` <br/>*Default:* `false` <br/>*Environment:* `TS_NODE_TRANSPILE_ONLY`
30+
- `--typeCheck` Opposite of `--transpileOnly` <br/>*Default:* `true`<br/>*Environment:* `TS_NODE_TYPE_CHECK`
31+
- `-H, --compilerHost` Use TypeScript's compiler host API <br/>*Default:* `false` <br/>*Environment:* `TS_NODE_COMPILER_HOST`
3032
- `--files` Load `files`, `include` and `exclude` from `tsconfig.json` on startup <br/>*Default:* `false` <br/>*Environment:* `TS_NODE_FILES`
31-
- `-D, --ignore-diagnostics [code]` Ignore TypeScript warnings by diagnostic code <br/>*Environment:* `TS_NODE_IGNORE_DIAGNOSTICS`
33+
- `-D, --ignoreDiagnostics [code]` Ignore TypeScript warnings by diagnostic code <br/>*Environment:* `TS_NODE_IGNORE_DIAGNOSTICS`
3234

3335
## Transpilation
3436

3537
- `-I, --ignore [pattern]` Override the path patterns to skip compilation <br/>*Default:* `/node_modules/` <br/>*Environment:* `TS_NODE_IGNORE`
36-
- `--skip-ignore` Skip ignore checks <br/>*Default:* `false` <br/>*Environment:* `TS_NODE_SKIP_IGNORE`
38+
- `--skipIgnore` Skip ignore checks <br/>*Default:* `false` <br/>*Environment:* `TS_NODE_SKIP_IGNORE`
3739
- `-C, --compiler [name]` Specify a custom TypeScript compiler <br/>*Default:* `typescript` <br/>*Environment:* `TS_NODE_COMPILER`
38-
- `--swc` Transpile with [swc](./transpilers.md#swc). Implies `--transpile-only` <br/>*Default:* `false`
40+
- `--swc` Transpile with [swc](./transpilers.md#swc). Implies `--transpileOnly` <br/>*Default:* `false`
3941
- `--transpiler [name]` Specify a third-party, non-typechecking transpiler
40-
- `--prefer-ts-exts` Re-order file extensions so that TypeScript imports are preferred <br/>*Default:* `false` <br/>*Environment:* `TS_NODE_PREFER_TS_EXTS`
42+
- `--preferTsExts` Re-order file extensions so that TypeScript imports are preferred <br/>*Default:* `false` <br/>*Environment:* `TS_NODE_PREFER_TS_EXTS`
4143

4244
## Diagnostics
4345

44-
- `--log-error` Logs TypeScript errors to stderr instead of throwing exceptions <br/>*Default:* `false` <br/>*Environment:* `TS_NODE_LOG_ERROR`
46+
- `--logError` Logs TypeScript errors to stderr instead of throwing exceptions <br/>*Default:* `false` <br/>*Environment:* `TS_NODE_LOG_ERROR`
4547
- `--pretty` Use pretty diagnostic formatter <br/>*Default:* `false` <br/>*Environment:* `TS_NODE_PRETTY`
4648
- `TS_NODE_DEBUG` Enable debug logging<br/>
4749

@@ -54,7 +56,7 @@ _Environment variables, where available, are in `ALL_CAPS`_
5456
- `--scopeDir` Directory within which compiler is limited when `scope` is enabled. <br/>*Default:* First of: `tsconfig.json` "rootDir" if specified, directory containing `tsconfig.json`, or cwd if no `tsconfig.json` is loaded.<br/>*Environment:* `TS_NODE_SCOPE_DIR`
5557
- `moduleType` Override the module type of certain files, ignoring the `package.json` `"type"` field. See [Module type overrides](./module-type-overrides.md) for details.<br/>*Default:* obeys `package.json` `"type"` and `tsconfig.json` `"module"` <br/>*Can only be specified via `tsconfig.json` or API.*
5658
- `TS_NODE_HISTORY` Path to history file for REPL <br/>*Default:* `~/.ts_node_repl_history`<br/>
57-
- `--no-experimental-repl-await` Disable top-level await in REPL. Equivalent to node's [`--no-experimental-repl-await`](https://nodejs.org/api/cli.html#cli_no_experimental_repl_await)<br/>*Default:* Enabled if TypeScript version is 3.8 or higher and target is ES2018 or higher.<br/>*Environment:* `TS_NODE_EXPERIMENTAL_REPL_AWAIT` set `false` to disable
59+
- `--noExperimentalReplAwait` Disable top-level await in REPL. Equivalent to node's [`--no-experimental-repl-await`](https://nodejs.org/api/cli.html#cli_no_experimental_repl_await)<br/>*Default:* Enabled if TypeScript version is 3.8 or higher and target is ES2018 or higher.<br/>*Environment:* `TS_NODE_EXPERIMENTAL_REPL_AWAIT` set `false` to disable
5860

5961
## API
6062

website/docs/troubleshooting.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ title: Troubleshooting
55
## Understanding configuration
66

77
ts-node uses sensible default configurations to reduce boilerplate while still respecting `tsconfig.json` if you
8-
have one. If you are unsure which configuration is used, you can log it with `ts-node --show-config`. This is similar to
8+
have one. If you are unsure which configuration is used, you can log it with `ts-node --showConfig`. This is similar to
99
`tsc --showConfig` but includes `"ts-node"` options as well.
1010

1111
ts-node also respects your locally-installed `typescript` version, but global installations fallback to the globally-installed
@@ -17,7 +17,7 @@ ts-node v10.0.0
1717
node v16.1.0
1818
compiler v4.2.2
1919

20-
$ ts-node --show-config
20+
$ ts-node --showConfig
2121
{
2222
"compilerOptions": {
2323
"target": "es6",

website/docs/usage.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@ ts-node -p -e '"Hello, world!"'
2020
# Pipe scripts to execute with TypeScript.
2121
echo 'console.log("Hello, world!")' | ts-node
2222

23-
# Equivalent to ts-node --transpile-only
23+
# Equivalent to ts-node --transpileOnly
2424
ts-node-transpile-only script.ts
2525

26-
# Equivalent to ts-node --cwd-mode
26+
# Equivalent to ts-node --cwdMode
2727
ts-node-cwd script.ts
2828
```
2929

0 commit comments

Comments
 (0)