Skip to content

Commit

Permalink
Rename --cwd flag to --dir
Browse files Browse the repository at this point in the history
  • Loading branch information
blakeembrey committed Nov 10, 2019
1 parent be8e2e1 commit 98c0c9b
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 13 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ _Environment variable denoted in parentheses._
* `-C, --compiler [name]` Specify a custom TypeScript compiler (`TS_NODE_COMPILER`, default: `typescript`)
* `-D, --ignore-diagnostics [code]` Ignore TypeScript warnings by diagnostic code (`TS_NODE_IGNORE_DIAGNOSTICS`)
* `-O, --compiler-options [opts]` JSON object to merge with compiler options (`TS_NODE_COMPILER_OPTIONS`)
* `--cwd` Specify working directory for config resolution (`TS_NODE_CWD`, default: `process.cwd()`)
* `--dir` Specify working directory for config resolution (`TS_NODE_CWD`, default: `process.cwd()`)
* `--scope` Scope compiler to files within `cwd` (`TS_NODE_SCOPE`, default: `false`)
* `--files` Load files from `tsconfig.json` on startup (`TS_NODE_FILES`, default: `false`)
* `--pretty` Use pretty diagnostic formatter (`TS_NODE_PRETTY`, default: `false`)
Expand Down
15 changes: 10 additions & 5 deletions src/bin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export function main (argv: string[]) {
'--version': arg.COUNT,

// Project options.
'--cwd': String,
'--dir': String,
'--files': Boolean,
'--compiler': String,
'--compiler-options': parse,
Expand Down Expand Up @@ -79,7 +79,7 @@ export function main (argv: string[]) {
})

const {
'--cwd': cwd = DEFAULTS.cwd || process.cwd(),
'--dir': dir = DEFAULTS.dir,
'--help': help = false,
'--script-mode': scriptMode = false,
'--version': version = 0,
Expand Down Expand Up @@ -139,12 +139,13 @@ export function main (argv: string[]) {
process.exit(0)
}

const cwd = dir || process.cwd()
const scriptPath = args._.length ? resolve(cwd, args._[0]) : undefined
const state = new EvalState(scriptPath || join(cwd, EVAL_FILENAME))

// Register the TypeScript compiler instance.
const service = register({
cwd: getCwd(cwd, scriptMode, scriptPath),
dir: getCwd(dir, scriptMode, scriptPath),
files,
pretty,
transpileOnly,
Expand Down Expand Up @@ -222,17 +223,21 @@ export function main (argv: string[]) {
/**
* Get project path from args.
*/
function getCwd (cwd: string, scriptMode?: boolean, scriptPath?: string) {
function getCwd (dir?: string, scriptMode?: boolean, scriptPath?: string) {
// Validate `--script-mode` usage is correct.
if (scriptMode) {
if (!scriptPath) {
throw new TypeError('Script mode must be used with a script name, e.g. `ts-node -s <script.ts>`')
}

if (dir) {
throw new TypeError('Script mode cannot be combined with `--dir`')
}

return dirname(scriptPath)
}

return cwd
return dir
}

/**
Expand Down
4 changes: 2 additions & 2 deletions src/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -388,8 +388,8 @@ describe('ts-node', function () {
registered.enabled(false)

const compilers = [
register({ cwd: join(TEST_DIR, 'scope/a'), scope: true }),
register({ cwd: join(TEST_DIR, 'scope/b'), scope: true })
register({ dir: join(TEST_DIR, 'scope/a'), scope: true }),
register({ dir: join(TEST_DIR, 'scope/b'), scope: true })
]

compilers.forEach(c => {
Expand Down
9 changes: 4 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ export const VERSION = require('../package.json').version
* Options for creating a new TypeScript compiler instance.
*/
export interface CreateOptions {
cwd?: string
dir?: string
scope?: boolean | null
pretty?: boolean | null
typeCheck?: boolean | null
Expand Down Expand Up @@ -123,7 +123,7 @@ export interface TypeInfo {
* Default register options.
*/
export const DEFAULTS: RegisterOptions = {
cwd: process.env.TS_NODE_CWD,
dir: process.env.TS_NODE_DIR,
scope: yn(process.env.TS_NODE_SCOPE),
files: yn(process.env['TS_NODE_FILES']),
pretty: yn(process.env['TS_NODE_PRETTY']),
Expand Down Expand Up @@ -195,7 +195,6 @@ export class TSError extends BaseError {
* Return type for registering `ts-node`.
*/
export interface Register {
cwd: string
ts: TSCommon
config: _ts.ParsedCommandLine
enabled (enabled?: boolean): boolean
Expand Down Expand Up @@ -258,7 +257,7 @@ export function create (options: CreateOptions = {}): Register {
).map(str => new RegExp(str))

// Require the TypeScript compiler and configuration.
const cwd = options.cwd ? resolve(options.cwd) : process.cwd()
const cwd = options.dir ? resolve(options.dir) : process.cwd()
const isScoped = options.scope ? (fileName: string) => relative(cwd, fileName).charAt(0) !== '.' : () => true
const typeCheck = options.typeCheck === true || options.transpileOnly !== true
const compiler = require.resolve(options.compiler || 'typescript', { paths: [cwd, __dirname] })
Expand Down Expand Up @@ -465,7 +464,7 @@ export function create (options: CreateOptions = {}): Register {
const enabled = (enabled?: boolean) => enabled === undefined ? active : (active = !!enabled)
const ignored = (fileName: string) => !active || !isScoped(fileName) || shouldIgnore(fileName, ignore)

return { cwd, ts, config, compile, getTypeInfo, ignored, enabled }
return { ts, config, compile, getTypeInfo, ignored, enabled }
}

/**
Expand Down

0 comments on commit 98c0c9b

Please sign in to comment.