From 200c06de1f850ea0096da00252b0c45babaa8cdb Mon Sep 17 00:00:00 2001 From: Andrew Bradley Date: Thu, 20 Jan 2022 22:05:58 +0000 Subject: [PATCH 1/4] fix --- src/bin.ts | 72 +++++++++++++++++++++++++++--------------------------- 1 file changed, 36 insertions(+), 36 deletions(-) diff --git a/src/bin.ts b/src/bin.ts index 0771f7ab7..2ce7e459a 100644 --- a/src/bin.ts +++ b/src/bin.ts @@ -132,42 +132,42 @@ export function main( if (help) { console.log(` - Usage: ts-node [options] [ -e script | script.ts ] [arguments] - - Options: - - -e, --eval [code] Evaluate code - -p, --print Print result of \`--eval\` - -r, --require [path] Require a node module before execution - -i, --interactive Opens the REPL even if stdin does not appear to be a terminal - - -h, --help Print CLI usage - -v, --version Print module version information - --cwd-mode Use current directory instead of for config resolution - --show-config Print resolved configuration and exit - - -T, --transpile-only Use TypeScript's faster \`transpileModule\` or a third-party transpiler - --swc Use the swc transpiler - -H, --compiler-host Use TypeScript's compiler host API - -I, --ignore [pattern] Override the path patterns to skip compilation - -P, --project [path] Path to TypeScript JSON project file - -C, --compiler [name] Specify a custom TypeScript compiler - --transpiler [name] Specify a third-party, non-typechecking transpiler - -D, --ignore-diagnostics [code] Ignore TypeScript warnings by diagnostic code - -O, --compiler-options [opts] JSON object to merge with compiler options - - --cwd Behave as if invoked within this working directory. - --files Load \`files\`, \`include\` and \`exclude\` from \`tsconfig.json\` on startup - --pretty Use pretty diagnostic formatter (usually enabled by default) - --skip-project Skip reading \`tsconfig.json\` - --skip-ignore Skip \`--ignore\` checks - --emit Emit output files into \`.ts-node\` directory - --scope Scope compiler to files within \`scopeDir\`. Anything outside this directory is ignored. - --scope-dir Directory for \`--scope\` - --prefer-ts-exts Prefer importing TypeScript files over JavaScript files - --log-error Logs TypeScript errors to stderr instead of throwing exceptions - --no-experimental-repl-await Disable top-level await in REPL. Equivalent to node's --no-experimental-repl-await - `); +Usage: ts-node [options] [ -e script | script.ts ] [arguments] + +Options: + + -e, --eval [code] Evaluate code + -p, --print Print result of \`--eval\` + -r, --require [path] Require a node module before execution + -i, --interactive Opens the REPL even if stdin does not appear to be a terminal + + -h, --help Print CLI usage + -v, --version Print module version information + --cwd-mode Use current directory instead of for config resolution + --show-config Print resolved configuration and exit + + -T, --transpile-only Use TypeScript's faster \`transpileModule\` or a third-party transpiler + --swc Use the swc transpiler + -H, --compiler-host Use TypeScript's compiler host API + -I, --ignore [pattern] Override the path patterns to skip compilation + -P, --project [path] Path to TypeScript JSON project file + -C, --compiler [name] Specify a custom TypeScript compiler + --transpiler [name] Specify a third-party, non-typechecking transpiler + -D, --ignore-diagnostics [code] Ignore TypeScript warnings by diagnostic code + -O, --compiler-options [opts] JSON object to merge with compiler options + + --cwd Behave as if invoked within this working directory. + --files Load \`files\`, \`include\` and \`exclude\` from \`tsconfig.json\` on startup + --pretty Use pretty diagnostic formatter (usually enabled by default) + --skip-project Skip reading \`tsconfig.json\` + --skip-ignore Skip \`--ignore\` checks + --emit Emit output files into \`.ts-node\` directory + --scope Scope compiler to files within \`scopeDir\`. Anything outside this directory is ignored. + --scope-dir Directory for \`--scope\` + --prefer-ts-exts Prefer importing TypeScript files over JavaScript files + --log-error Logs TypeScript errors to stderr instead of throwing exceptions + --no-experimental-repl-await Disable top-level await in REPL. Equivalent to node's --no-experimental-repl-await +`); process.exit(0); } From 0ef4ff9170923307105c5671559dadb7eecf2889 Mon Sep 17 00:00:00 2001 From: Andrew Bradley Date: Thu, 20 Jan 2022 22:34:27 +0000 Subject: [PATCH 2/4] fix --- src/bin.ts | 106 ++++++++++++++++++-------------- website/docs/configuration.md | 6 +- website/docs/options.md | 28 +++++---- website/docs/troubleshooting.md | 4 +- website/docs/usage.md | 4 +- 5 files changed, 82 insertions(+), 66 deletions(-) diff --git a/src/bin.ts b/src/bin.ts index 2ce7e459a..b7fedbc3c 100644 --- a/src/bin.ts +++ b/src/bin.ts @@ -40,33 +40,33 @@ export function main( // CLI options. '--help': Boolean, - '--cwd-mode': Boolean, - '--script-mode': Boolean, + '--cwdMode': Boolean, + '--scriptMode': Boolean, '--version': arg.COUNT, - '--show-config': Boolean, + '--showConfig': Boolean, // Project options. '--cwd': String, '--files': Boolean, '--compiler': String, - '--compiler-options': parse, + '--compilerOptions': parse, '--project': String, - '--ignore-diagnostics': [String], + '--ignoreDiagnostics': [String], '--ignore': [String], - '--transpile-only': Boolean, + '--transpileOnly': Boolean, '--transpiler': String, '--swc': Boolean, - '--type-check': Boolean, - '--compiler-host': Boolean, + '--typeCheck': Boolean, + '--compilerHost': Boolean, '--pretty': Boolean, - '--skip-project': Boolean, - '--skip-ignore': Boolean, - '--prefer-ts-exts': Boolean, - '--log-error': Boolean, + '--skipProject': Boolean, + '--skipIgnore': Boolean, + '--preferTsExts': Boolean, + '--logError': Boolean, '--emit': Boolean, '--scope': Boolean, - '--scope-dir': String, - '--no-experimental-repl-await': Boolean, + '--scopeDir': String, + '--noExperimentalReplAwait': Boolean, // Aliases. '-e': '--eval', @@ -76,16 +76,30 @@ export function main( '-h': '--help', '-s': '--script-mode', '-v': '--version', - '-T': '--transpile-only', - '-H': '--compiler-host', + '-T': '--transpileOnly', + '-H': '--compilerHost', '-I': '--ignore', '-P': '--project', '-C': '--compiler', - '-D': '--ignore-diagnostics', - '-O': '--compiler-options', + '-D': '--ignoreDiagnostics', + '-O': '--compilerOptions', '--dir': '--cwd', - '--showConfig': '--show-config', - '--scopeDir': '--scope-dir', + + // Support both tsc-style camelCase and node-style hypen-case for *all* flags + '--cwd-mode': '--cwdMode', + '--script-mode': '--scriptMode', + '--show-config': '--showConfig', + '--compiler-options': '--compilerOptions', + '--ignore-diagnostics': '--ignoreDiagnostics', + '--transpile-only': '--transpileOnly', + '--type-check': '--typeCheck', + '--compiler-host': '--compilerHost', + '--skip-project': '--skipProject', + '--skip-ignore': '--skipIgnore', + '--prefer-ts-exts': '--preferTsExts', + '--log-error': '--logError', + '--scope-dir': '--scopeDir', + '--no-experimental-repl-await': '--noExperimentalReplAwait' }, { argv, @@ -100,34 +114,34 @@ export function main( const { '--cwd': cwdArg, '--help': help = false, - '--script-mode': scriptMode, - '--cwd-mode': cwdMode, + '--scriptMode': scriptMode, + '--cwdMode': cwdMode, '--version': version = 0, - '--show-config': showConfig, + '--showConfig': showConfig, '--require': argsRequire = [], '--eval': code = undefined, '--print': print = false, '--interactive': interactive = false, '--files': files, '--compiler': compiler, - '--compiler-options': compilerOptions, + '--compilerOptions': compilerOptions, '--project': project, - '--ignore-diagnostics': ignoreDiagnostics, + '--ignoreDiagnostics': ignoreDiagnostics, '--ignore': ignore, - '--transpile-only': transpileOnly, - '--type-check': typeCheck, + '--transpileOnly': transpileOnly, + '--typeCheck': typeCheck, '--transpiler': transpiler, '--swc': swc, - '--compiler-host': compilerHost, + '--compilerHost': compilerHost, '--pretty': pretty, - '--skip-project': skipProject, - '--skip-ignore': skipIgnore, - '--prefer-ts-exts': preferTsExts, - '--log-error': logError, + '--skipProject': skipProject, + '--skipIgnore': skipIgnore, + '--preferTsExts': preferTsExts, + '--logError': logError, '--emit': emit, '--scope': scope = undefined, - '--scope-dir': scopeDir = undefined, - '--no-experimental-repl-await': noExperimentalReplAwait, + '--scopeDir': scopeDir = undefined, + '--noExperimentalReplAwait': noExperimentalReplAwait, } = args; if (help) { @@ -143,30 +157,30 @@ Options: -h, --help Print CLI usage -v, --version Print module version information - --cwd-mode Use current directory instead of for config resolution - --show-config Print resolved configuration and exit + --cwdMode Use current directory instead of for config resolution + --showConfig Print resolved configuration and exit - -T, --transpile-only Use TypeScript's faster \`transpileModule\` or a third-party transpiler + -T, --transpileOnly Use TypeScript's faster \`transpileModule\` or a third-party transpiler --swc Use the swc transpiler - -H, --compiler-host Use TypeScript's compiler host API + -H, --compilerHost Use TypeScript's compiler host API -I, --ignore [pattern] Override the path patterns to skip compilation -P, --project [path] Path to TypeScript JSON project file -C, --compiler [name] Specify a custom TypeScript compiler --transpiler [name] Specify a third-party, non-typechecking transpiler - -D, --ignore-diagnostics [code] Ignore TypeScript warnings by diagnostic code - -O, --compiler-options [opts] JSON object to merge with compiler options + -D, --ignoreDiagnostics [code] Ignore TypeScript warnings by diagnostic code + -O, --compilerOptions [opts] JSON object to merge with compiler options --cwd Behave as if invoked within this working directory. --files Load \`files\`, \`include\` and \`exclude\` from \`tsconfig.json\` on startup --pretty Use pretty diagnostic formatter (usually enabled by default) - --skip-project Skip reading \`tsconfig.json\` - --skip-ignore Skip \`--ignore\` checks + --skipProject Skip reading \`tsconfig.json\` + --skipIgnore Skip \`--ignore\` checks --emit Emit output files into \`.ts-node\` directory --scope Scope compiler to files within \`scopeDir\`. Anything outside this directory is ignored. - --scope-dir Directory for \`--scope\` - --prefer-ts-exts Prefer importing TypeScript files over JavaScript files - --log-error Logs TypeScript errors to stderr instead of throwing exceptions - --no-experimental-repl-await Disable top-level await in REPL. Equivalent to node's --no-experimental-repl-await + --scopeDir Directory for \`--scope\` + --preferTsExts Prefer importing TypeScript files over JavaScript files + --logError Logs TypeScript errors to stderr instead of throwing exceptions + --noExperimentalReplAwait Disable top-level await in REPL. Equivalent to node's --no-experimental-repl-await `); process.exit(0); diff --git a/website/docs/configuration.md b/website/docs/configuration.md index 5a1d43bea..130a69eba 100644 --- a/website/docs/configuration.md +++ b/website/docs/configuration.md @@ -19,9 +19,9 @@ Hello, Ronald! 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. -Use `--skip-project` to skip loading the `tsconfig.json`. Use `--project` to explicitly specify the path to a `tsconfig.json`. +Use `--skipProject` to skip loading the `tsconfig.json`. Use `--project` to explicitly specify the path to a `tsconfig.json`. -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()`. +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()`. You can use this sample configuration as a starting point: @@ -74,7 +74,7 @@ With the latest `node` and `typescript`, this is [`@tsconfig/node16`](https://gi Older versions of `typescript` are incompatible with `@tsconfig/node16`. In those cases we will use an older default configuration. -When in doubt, `ts-node --show-config` will log the configuration being used, and `ts-node -vv` will log `node` and `typescript` versions. +When in doubt, `ts-node --showConfig` will log the configuration being used, and `ts-node -vv` will log `node` and `typescript` versions. ## `node` flags diff --git a/website/docs/options.md b/website/docs/options.md index 90878e4f9..38c9247d9 100644 --- a/website/docs/options.md +++ b/website/docs/options.md @@ -4,6 +4,8 @@ title: Options `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). +All command-line flags support both `--camelCase` and `--hyphen-case`. + _Environment variables, where available, are in `ALL_CAPS`_ ## Shell @@ -17,31 +19,31 @@ _Environment variables, where available, are in `ALL_CAPS`_ ## TSConfig - `-P, --project [path]` Path to TypeScript JSON project file
*Environment:* `TS_NODE_PROJECT` -- `--skip-project` Skip project config resolution and loading
*Default:* `false`
*Environment:* `TS_NODE_SKIP_PROJECT` -- `-c, --cwd-mode` Resolve config relative to the current directory instead of the directory of the entrypoint script -- `-O, --compiler-options [opts]` JSON object to merge with compiler options
*Environment:* `TS_NODE_COMPILER_OPTIONS` -- `--show-config` Print resolved `tsconfig.json`, including `ts-node` options, and exit +- `--skipProject` Skip project config resolution and loading
*Default:* `false`
*Environment:* `TS_NODE_SKIP_PROJECT` +- `-c, --cwdMode` Resolve config relative to the current directory instead of the directory of the entrypoint script +- `-O, --compilerOptions [opts]` JSON object to merge with compiler options
*Environment:* `TS_NODE_COMPILER_OPTIONS` +- `--showConfig` Print resolved `tsconfig.json`, including `ts-node` options, and exit ## Typechecking -- `-T, --transpile-only` Use TypeScript's faster `transpileModule`
*Default:* `false`
*Environment:* `TS_NODE_TRANSPILE_ONLY` -- `--type-check` Opposite of `--transpile-only`
*Default:* `true`
*Environment:* `TS_NODE_TYPE_CHECK` -- `-H, --compiler-host` Use TypeScript's compiler host API
*Default:* `false`
*Environment:* `TS_NODE_COMPILER_HOST` +- `-T, --transpileOnly` Use TypeScript's faster `transpileModule`
*Default:* `false`
*Environment:* `TS_NODE_TRANSPILE_ONLY` +- `--typeCheck` Opposite of `--transpileOnly`
*Default:* `true`
*Environment:* `TS_NODE_TYPE_CHECK` +- `-H, --compilerHost` Use TypeScript's compiler host API
*Default:* `false`
*Environment:* `TS_NODE_COMPILER_HOST` - `--files` Load `files`, `include` and `exclude` from `tsconfig.json` on startup
*Default:* `false`
*Environment:* `TS_NODE_FILES` -- `-D, --ignore-diagnostics [code]` Ignore TypeScript warnings by diagnostic code
*Environment:* `TS_NODE_IGNORE_DIAGNOSTICS` +- `-D, --ignoreDiagnostics [code]` Ignore TypeScript warnings by diagnostic code
*Environment:* `TS_NODE_IGNORE_DIAGNOSTICS` ## Transpilation - `-I, --ignore [pattern]` Override the path patterns to skip compilation
*Default:* `/node_modules/`
*Environment:* `TS_NODE_IGNORE` -- `--skip-ignore` Skip ignore checks
*Default:* `false`
*Environment:* `TS_NODE_SKIP_IGNORE` +- `--skipIgnore` Skip ignore checks
*Default:* `false`
*Environment:* `TS_NODE_SKIP_IGNORE` - `-C, --compiler [name]` Specify a custom TypeScript compiler
*Default:* `typescript`
*Environment:* `TS_NODE_COMPILER` -- `--swc` Transpile with [swc](./transpilers.md#swc). Implies `--transpile-only`
*Default:* `false` +- `--swc` Transpile with [swc](./transpilers.md#swc). Implies `--transpileOnly`
*Default:* `false` - `--transpiler [name]` Specify a third-party, non-typechecking transpiler -- `--prefer-ts-exts` Re-order file extensions so that TypeScript imports are preferred
*Default:* `false`
*Environment:* `TS_NODE_PREFER_TS_EXTS` +- `--preferTsExts` Re-order file extensions so that TypeScript imports are preferred
*Default:* `false`
*Environment:* `TS_NODE_PREFER_TS_EXTS` ## Diagnostics -- `--log-error` Logs TypeScript errors to stderr instead of throwing exceptions
*Default:* `false`
*Environment:* `TS_NODE_LOG_ERROR` +- `--logError` Logs TypeScript errors to stderr instead of throwing exceptions
*Default:* `false`
*Environment:* `TS_NODE_LOG_ERROR` - `--pretty` Use pretty diagnostic formatter
*Default:* `false`
*Environment:* `TS_NODE_PRETTY` - `TS_NODE_DEBUG` Enable debug logging
@@ -54,7 +56,7 @@ _Environment variables, where available, are in `ALL_CAPS`_ - `--scopeDir` Directory within which compiler is limited when `scope` is enabled.
*Default:* First of: `tsconfig.json` "rootDir" if specified, directory containing `tsconfig.json`, or cwd if no `tsconfig.json` is loaded.
*Environment:* `TS_NODE_SCOPE_DIR` - `moduleType` Override the module type of certain files, ignoring the `package.json` `"type"` field. See [Module type overrides](./module-type-overrides.md) for details.
*Default:* obeys `package.json` `"type"` and `tsconfig.json` `"module"`
*Can only be specified via `tsconfig.json` or API.* - `TS_NODE_HISTORY` Path to history file for REPL
*Default:* `~/.ts_node_repl_history`
-- `--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)
*Default:* Enabled if TypeScript version is 3.8 or higher and target is ES2018 or higher.
*Environment:* `TS_NODE_EXPERIMENTAL_REPL_AWAIT` set `false` to disable +- `--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)
*Default:* Enabled if TypeScript version is 3.8 or higher and target is ES2018 or higher.
*Environment:* `TS_NODE_EXPERIMENTAL_REPL_AWAIT` set `false` to disable ## API diff --git a/website/docs/troubleshooting.md b/website/docs/troubleshooting.md index 2ee75225d..2e2125c16 100644 --- a/website/docs/troubleshooting.md +++ b/website/docs/troubleshooting.md @@ -5,7 +5,7 @@ title: Troubleshooting ## Understanding configuration ts-node uses sensible default configurations to reduce boilerplate while still respecting `tsconfig.json` if you -have one. If you are unsure which configuration is used, you can log it with `ts-node --show-config`. This is similar to +have one. If you are unsure which configuration is used, you can log it with `ts-node --showConfig`. This is similar to `tsc --showConfig` but includes `"ts-node"` options as well. 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 node v16.1.0 compiler v4.2.2 -$ ts-node --show-config +$ ts-node --showConfig { "compilerOptions": { "target": "es6", diff --git a/website/docs/usage.md b/website/docs/usage.md index ecd6721bc..fb35bf8e5 100644 --- a/website/docs/usage.md +++ b/website/docs/usage.md @@ -20,10 +20,10 @@ ts-node -p -e '"Hello, world!"' # Pipe scripts to execute with TypeScript. echo 'console.log("Hello, world!")' | ts-node -# Equivalent to ts-node --transpile-only +# Equivalent to ts-node --transpileOnly ts-node-transpile-only script.ts -# Equivalent to ts-node --cwd-mode +# Equivalent to ts-node --cwdMode ts-node-cwd script.ts ``` From 9914a0fa26e68262632136c6521d9c698825b2fc Mon Sep 17 00:00:00 2001 From: Andrew Bradley Date: Thu, 20 Jan 2022 23:46:08 +0000 Subject: [PATCH 3/4] lint-fix --- src/bin.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/bin.ts b/src/bin.ts index b7fedbc3c..be8df73bf 100644 --- a/src/bin.ts +++ b/src/bin.ts @@ -99,7 +99,7 @@ export function main( '--prefer-ts-exts': '--preferTsExts', '--log-error': '--logError', '--scope-dir': '--scopeDir', - '--no-experimental-repl-await': '--noExperimentalReplAwait' + '--no-experimental-repl-await': '--noExperimentalReplAwait', }, { argv, From 728d295f2547538b42ac057f0e95eb6d08469d62 Mon Sep 17 00:00:00 2001 From: Andrew Bradley Date: Fri, 21 Jan 2022 00:21:50 +0000 Subject: [PATCH 4/4] fix --- src/bin-cwd.ts | 2 +- src/bin-script-deprecated.ts | 2 +- src/bin-script.ts | 2 +- src/bin-transpile.ts | 2 +- src/bin.ts | 14 ++++++++++++++ 5 files changed, 18 insertions(+), 4 deletions(-) diff --git a/src/bin-cwd.ts b/src/bin-cwd.ts index bd2f4483c..3a6697657 100644 --- a/src/bin-cwd.ts +++ b/src/bin-cwd.ts @@ -2,4 +2,4 @@ import { main } from './bin'; -main(undefined, { '--cwd-mode': true }); +main(undefined, { '--cwdMode': true }); diff --git a/src/bin-script-deprecated.ts b/src/bin-script-deprecated.ts index 07112d85b..a4dcdb91a 100644 --- a/src/bin-script-deprecated.ts +++ b/src/bin-script-deprecated.ts @@ -7,4 +7,4 @@ console.warn( 'Please use ts-node-script instead' ); -main(undefined, { '--script-mode': true }); +main(undefined, { '--scriptMode': true }); diff --git a/src/bin-script.ts b/src/bin-script.ts index 78f9ab320..7c28aa1f2 100644 --- a/src/bin-script.ts +++ b/src/bin-script.ts @@ -2,4 +2,4 @@ import { main } from './bin'; -main(undefined, { '--script-mode': true }); +main(undefined, { '--scriptMode': true }); diff --git a/src/bin-transpile.ts b/src/bin-transpile.ts index 8eb818146..4dce851c9 100644 --- a/src/bin-transpile.ts +++ b/src/bin-transpile.ts @@ -2,4 +2,4 @@ import { main } from './bin'; -main(undefined, { '--transpile-only': true }); +main(undefined, { '--transpileOnly': true }); diff --git a/src/bin.ts b/src/bin.ts index be8df73bf..564ddaba3 100644 --- a/src/bin.ts +++ b/src/bin.ts @@ -28,6 +28,20 @@ export function main( argv: string[] = process.argv.slice(2), entrypointArgs: Record = {} ) { + // HACK: technically, this function is not marked @internal so it's possible + // that libraries in the wild are doing `require('ts-node/dist/bin').main({'--transpile-only': true})` + // We can mark this function @internal in next major release. + // For now, rewrite args to avoid a breaking change. + entrypointArgs = { ...entrypointArgs }; + for (const key of Object.keys(entrypointArgs)) { + entrypointArgs[ + key.replace( + /([a-z])-([a-z])/g, + (_$0, $1, $2: string) => `${$1}${$2.toUpperCase()}` + ) + ] = entrypointArgs[key]; + } + const args = { ...entrypointArgs, ...arg(