Skip to content

Commit

Permalink
Cleanup bin script entrypoint; accepts a args object so we don't need…
Browse files Browse the repository at this point in the history
… to slice process.argv (#1141)
  • Loading branch information
cspotcode committed Nov 1, 2020
1 parent 5f813b1 commit c1e6e3e
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 54 deletions.
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(['--script-mode', ...process.argv.slice(2)])
main(undefined, { '--script-mode': 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(['--script-mode', ...process.argv.slice(2)])
main(undefined, { '--script-mode': 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(['--transpile-only', ...process.argv.slice(2)])
main(undefined, { '--transpile-only': true })
105 changes: 54 additions & 51 deletions src/bin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,56 +31,59 @@ class EvalState {
/**
* Main `bin` functionality.
*/
export function main (argv: string[]) {
const args = arg({
// Node.js-like options.
'--eval': String,
'--interactive': Boolean,
'--print': Boolean,
'--require': [String],

// CLI options.
'--help': Boolean,
'--script-mode': Boolean,
'--version': arg.COUNT,

// Project options.
'--dir': String,
'--files': Boolean,
'--compiler': String,
'--compiler-options': parse,
'--project': String,
'--ignore-diagnostics': [String],
'--ignore': [String],
'--transpile-only': Boolean,
'--type-check': Boolean,
'--compiler-host': Boolean,
'--pretty': Boolean,
'--skip-project': Boolean,
'--skip-ignore': Boolean,
'--prefer-ts-exts': Boolean,
'--log-error': Boolean,
'--emit': Boolean,

// Aliases.
'-e': '--eval',
'-i': '--interactive',
'-p': '--print',
'-r': '--require',
'-h': '--help',
'-s': '--script-mode',
'-v': '--version',
'-T': '--transpile-only',
'-H': '--compiler-host',
'-I': '--ignore',
'-P': '--project',
'-C': '--compiler',
'-D': '--ignore-diagnostics',
'-O': '--compiler-options'
}, {
argv,
stopAtPositional: true
})
export function main (argv: string[] = process.argv.slice(2), entrypointArgs: Record<string, any> = {}) {
const args = {
...entrypointArgs,
...arg({
// Node.js-like options.
'--eval': String,
'--interactive': Boolean,
'--print': Boolean,
'--require': [String],

// CLI options.
'--help': Boolean,
'--script-mode': Boolean,
'--version': arg.COUNT,

// Project options.
'--dir': String,
'--files': Boolean,
'--compiler': String,
'--compiler-options': parse,
'--project': String,
'--ignore-diagnostics': [String],
'--ignore': [String],
'--transpile-only': Boolean,
'--type-check': Boolean,
'--compiler-host': Boolean,
'--pretty': Boolean,
'--skip-project': Boolean,
'--skip-ignore': Boolean,
'--prefer-ts-exts': Boolean,
'--log-error': Boolean,
'--emit': Boolean,

// Aliases.
'-e': '--eval',
'-i': '--interactive',
'-p': '--print',
'-r': '--require',
'-h': '--help',
'-s': '--script-mode',
'-v': '--version',
'-T': '--transpile-only',
'-H': '--compiler-host',
'-I': '--ignore',
'-P': '--project',
'-C': '--compiler',
'-D': '--ignore-diagnostics',
'-O': '--compiler-options'
}, {
argv,
stopAtPositional: true
})
}

// Only setting defaults for CLI-specific flags
// Anything passed to `register()` can be `undefined`; `create()` will apply
Expand Down Expand Up @@ -509,5 +512,5 @@ function hasOwnProperty (object: any, property: string): boolean {
}

if (require.main === module) {
main(process.argv.slice(2))
main()
}

0 comments on commit c1e6e3e

Please sign in to comment.