Skip to content

Commit

Permalink
Implement #1598: Support both camelCase and hyphen-case for all CLI f…
Browse files Browse the repository at this point in the history
…lags; update documentation to prefer camelCase (#1599)

* fix

* fix

* lint-fix

* fix
  • Loading branch information
cspotcode authored Jan 21, 2022
1 parent aff9bb9 commit 652fc95
Show file tree
Hide file tree
Showing 9 changed files with 124 additions and 94 deletions.
2 changes: 1 addition & 1 deletion src/bin-cwd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

import { main } from './bin';

main(undefined, { '--cwd-mode': true });
main(undefined, { '--cwdMode': true });
2 changes: 1 addition & 1 deletion src/bin-script-deprecated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@ console.warn(
'Please use ts-node-script instead'
);

main(undefined, { '--script-mode': true });
main(undefined, { '--scriptMode': true });
2 changes: 1 addition & 1 deletion src/bin-script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

import { main } from './bin';

main(undefined, { '--script-mode': true });
main(undefined, { '--scriptMode': true });
2 changes: 1 addition & 1 deletion src/bin-transpile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@

import { main } from './bin';

main(undefined, { '--transpile-only': true });
main(undefined, { '--transpileOnly': true });
168 changes: 98 additions & 70 deletions src/bin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,20 @@ export function main(
argv: string[] = process.argv.slice(2),
entrypointArgs: Record<string, any> = {}
) {
// 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(
Expand All @@ -40,33 +54,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',
Expand All @@ -76,16 +90,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,
Expand All @@ -100,74 +128,74 @@ 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) {
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 <script.ts> 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
--cwdMode Use current directory instead of <script.ts> for config resolution
--showConfig Print resolved configuration and exit
-T, --transpileOnly Use TypeScript's faster \`transpileModule\` or a third-party transpiler
--swc Use the swc transpiler
-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, --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)
--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.
--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);
}
Expand Down
6 changes: 3 additions & 3 deletions website/docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down Expand Up @@ -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

Expand Down
28 changes: 15 additions & 13 deletions website/docs/options.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -17,31 +19,31 @@ _Environment variables, where available, are in `ALL_CAPS`_
## TSConfig

- `-P, --project [path]` Path to TypeScript JSON project file <br/>*Environment:* `TS_NODE_PROJECT`
- `--skip-project` Skip project config resolution and loading <br/>*Default:* `false` <br/>*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 <br/>*Environment:* `TS_NODE_COMPILER_OPTIONS`
- `--show-config` Print resolved `tsconfig.json`, including `ts-node` options, and exit
- `--skipProject` Skip project config resolution and loading <br/>*Default:* `false` <br/>*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 <br/>*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` <br/>*Default:* `false` <br/>*Environment:* `TS_NODE_TRANSPILE_ONLY`
- `--type-check` Opposite of `--transpile-only` <br/>*Default:* `true`<br/>*Environment:* `TS_NODE_TYPE_CHECK`
- `-H, --compiler-host` Use TypeScript's compiler host API <br/>*Default:* `false` <br/>*Environment:* `TS_NODE_COMPILER_HOST`
- `-T, --transpileOnly` Use TypeScript's faster `transpileModule` <br/>*Default:* `false` <br/>*Environment:* `TS_NODE_TRANSPILE_ONLY`
- `--typeCheck` Opposite of `--transpileOnly` <br/>*Default:* `true`<br/>*Environment:* `TS_NODE_TYPE_CHECK`
- `-H, --compilerHost` Use TypeScript's compiler host API <br/>*Default:* `false` <br/>*Environment:* `TS_NODE_COMPILER_HOST`
- `--files` Load `files`, `include` and `exclude` from `tsconfig.json` on startup <br/>*Default:* `false` <br/>*Environment:* `TS_NODE_FILES`
- `-D, --ignore-diagnostics [code]` Ignore TypeScript warnings by diagnostic code <br/>*Environment:* `TS_NODE_IGNORE_DIAGNOSTICS`
- `-D, --ignoreDiagnostics [code]` Ignore TypeScript warnings by diagnostic code <br/>*Environment:* `TS_NODE_IGNORE_DIAGNOSTICS`

## Transpilation

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

## Diagnostics

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

Expand All @@ -54,7 +56,7 @@ _Environment variables, where available, are in `ALL_CAPS`_
- `--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`
- `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.*
- `TS_NODE_HISTORY` Path to history file for REPL <br/>*Default:* `~/.ts_node_repl_history`<br/>
- `--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
- `--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

## API

Expand Down
4 changes: 2 additions & 2 deletions website/docs/troubleshooting.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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",
Expand Down
4 changes: 2 additions & 2 deletions website/docs/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```

Expand Down

0 comments on commit 652fc95

Please sign in to comment.