Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable configuration of the cache directory #154

Merged
merged 1 commit into from
Jul 21, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ ts-node --compiler ntypescript --project src --ignoreWarnings 2304 hello-world.t
* **--fast, -F** Use TypeScript's `transpileModule` mode (no type checking, but faster compilation) (also `process.env.TS_NODE_FAST`)
* **--lazy, -L** Lazily defer TypeScript initialization until first `.ts` file
* **--no-cache** Skip hitting the compiled JavaScript cache
* **--cache-directory** Configure the TypeScript cache directory

### Programmatic Usage

Expand Down
29 changes: 10 additions & 19 deletions src/_bin.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { join, resolve } from 'path'
import { start } from 'repl'
import { inspect } from 'util'
import arrify = require('arrify')
import extend = require('xtend')
import Module = require('module')
import minimist = require('minimist')
import chalk = require('chalk')
Expand All @@ -14,6 +16,7 @@ interface Argv {
fast?: boolean
lazy?: boolean
cache?: boolean
cacheDirectory?: string
version?: boolean
help?: boolean
compiler?: string
Expand All @@ -24,7 +27,7 @@ interface Argv {
_: string[]
}

const strings = ['eval', 'print', 'compiler', 'project', 'ignoreWarnings']
const strings = ['eval', 'print', 'compiler', 'project', 'ignoreWarnings', 'cacheDirectory']
const booleans = ['help', 'fast', 'lazy', 'version', 'disableWarnings', 'cache']

const aliases: { [key: string]: string[] } = {
Expand All @@ -36,6 +39,7 @@ const aliases: { [key: string]: string[] } = {
print: ['p'],
project: ['P'],
compiler: ['C'],
cacheDirectory: ['cache-directory'],
ignoreWarnings: ['I', 'ignore-warnings'],
disableWarnings: ['D', 'disable-warnings'],
compilerOptions: ['O', 'compiler-options']
Expand Down Expand Up @@ -116,13 +120,14 @@ Options:
-e, --eval [code] Evaluate code
-p, --print [code] Evaluate code and print result
-C, --compiler [name] Specify a custom TypeScript compiler
-I, --ignoreWarnings [codes] Ignore TypeScript warnings by diagnostic code
-I, --ignoreWarnings [code] Ignore TypeScript warnings by diagnostic code
-D, --disableWarnings Ignore every TypeScript warning
-P, --project [path] Path to TypeScript project (or \`false\`)
-O, --compilerOptions [opts] JSON compiler options to merge with compilation
-L, --lazy Lazily load TypeScript compilation
-F, --fast Run TypeScript compilation in transpile mode
--no-cache Disable the TypeScript cache
--cache-directory Configure the TypeScript cache directory
`)

process.exit(0)
Expand All @@ -149,19 +154,12 @@ const isEval = isEvalScript || stop === process.argv.length
const isPrinted = argv.print != null

// Register the TypeScript compiler instance.
const service = register({
const service = register(extend(argv, {
getFile: isEval ? getFileEval : getFile,
getVersion: isEval ? getVersionEval : getVersion,
fileExists: isEval ? fileExistsEval : fileExists,
fast: argv.fast,
lazy: argv.lazy,
cache: argv.cache,
compiler: argv.compiler,
ignoreWarnings: list(argv.ignoreWarnings),
project: argv.project,
disableWarnings: argv.disableWarnings,
compilerOptions: argv.compilerOptions
})
ignoreWarnings: arrify(argv.ignoreWarnings)
}))

// TypeScript files must always end with `.ts`.
const EVAL_FILENAME = '[eval].ts'
Expand Down Expand Up @@ -356,13 +354,6 @@ function replEval (code: string, context: any, filename: string, callback: (err?
callback(err, result)
}

/**
* Split a string of values into an array.
*/
function list (value: string | string[]) {
return String(value).split(/ *, */)
}

/**
* Get the file text, checking for eval first.
*/
Expand Down
6 changes: 3 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import crypto = require('crypto')
import { BaseError } from 'make-error'
import * as TS from 'typescript'

const TMP_DIR = join(tmpdir(), 'ts-node')

const pkg = require('../package.json')
const oldHandlers: { [key: string]: any } = {}

Expand Down Expand Up @@ -62,6 +60,7 @@ export interface Options {
fast?: boolean
lazy?: boolean
cache?: boolean
cacheDirectory?: string
compiler?: string
project?: string
ignoreWarnings?: Array<number | string>
Expand Down Expand Up @@ -98,6 +97,7 @@ const DEFAULT_OPTIONS: Options = {
getVersion,
fileExists,
cache: process.env.TS_NODE_CACHE,
cacheDirectory: process.env.TS_NODE_CACHE_DIRECTORY || join(tmpdir(), 'ts-node'),
disableWarnings: process.env.TS_NODE_DISABLE_WARNINGS,
compiler: process.env.TS_NODE_COMPILER,
project: process.env.TS_NODE_PROJECT,
Expand Down Expand Up @@ -148,7 +148,7 @@ export function register (opts?: Options): () => Register {
const ts: typeof TS = require(options.compiler)
const config = readConfig(options, cwd, ts)
const configDiagnostics = formatDiagnostics(config.errors, options, cwd, ts)
const cachedir = join(TMP_DIR, getCompilerDigest(ts, options, config))
const cachedir = join(resolve(cwd, options.cacheDirectory), getCompilerDigest(ts, options, config))

// Make sure the temp cache directory exists.
mkdirp.sync(cachedir)
Expand Down