Skip to content

Commit

Permalink
Remove support for ts-node cache output (#701)
Browse files Browse the repository at this point in the history
  • Loading branch information
blakeembrey committed Oct 7, 2018
1 parent df1ac1d commit b61c745
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 102 deletions.
14 changes: 2 additions & 12 deletions src/bin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ interface Argv {
typeCheck?: boolean
transpileOnly?: boolean
files?: boolean
cache?: boolean
cacheDirectory?: string
compiler?: string
ignore?: string | string[]
project?: string
Expand All @@ -38,8 +36,8 @@ interface Argv {

const argv = minimist<Argv>(process.argv.slice(2), {
stopEarly: true,
string: ['eval', 'print', 'compiler', 'project', 'ignoreDiagnostics', 'require', 'cacheDirectory', 'ignore'],
boolean: ['help', 'transpileOnly', 'typeCheck', 'version', 'files', 'cache', 'pretty', 'skipProject', 'skipIgnore'],
string: ['eval', 'print', 'compiler', 'project', 'ignoreDiagnostics', 'require', 'ignore'],
boolean: ['help', 'transpileOnly', 'typeCheck', 'version', 'files', 'pretty', 'skipProject', 'skipIgnore'],
alias: {
eval: ['e'],
print: ['p'],
Expand All @@ -48,7 +46,6 @@ const argv = minimist<Argv>(process.argv.slice(2), {
version: ['v'],
typeCheck: ['type-check'],
transpileOnly: ['T', 'transpile-only'],
cacheDirectory: ['cache-directory'],
ignore: ['I'],
project: ['P'],
skipIgnore: ['skip-ignore'],
Expand All @@ -58,12 +55,10 @@ const argv = minimist<Argv>(process.argv.slice(2), {
compilerOptions: ['O', 'compiler-options']
},
default: {
cache: DEFAULTS.cache,
files: DEFAULTS.files,
pretty: DEFAULTS.pretty,
typeCheck: DEFAULTS.typeCheck,
transpileOnly: DEFAULTS.transpileOnly,
cacheDirectory: DEFAULTS.cacheDirectory,
ignore: DEFAULTS.ignore,
project: DEFAULTS.project,
skipIgnore: DEFAULTS.skipIgnore,
Expand All @@ -87,7 +82,6 @@ Options:
-v, --version Print module version information
-T, --transpile-only Use TypeScript's faster \`transpileModule\`
--cache-directory Configure the output file cache directory
-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
Expand All @@ -96,7 +90,6 @@ Options:
--files Load files from \`tsconfig.json\` on startup
--pretty Use pretty diagnostic formatter
--no-cache Disable the local TypeScript Node cache
--skip-project Skip reading \`tsconfig.json\`
--skip-ignore Skip \`--ignore\` checks
`)
Expand All @@ -115,8 +108,6 @@ const service = register({
pretty: argv.pretty,
typeCheck: argv.typeCheck,
transpileOnly: argv.transpileOnly,
cache: argv.cache,
cacheDirectory: argv.cacheDirectory,
ignore: argv.ignore,
project: argv.project,
skipIgnore: argv.skipIgnore,
Expand All @@ -133,7 +124,6 @@ if (argv.version) {
console.log(`ts-node v${VERSION}`)
console.log(`node ${process.version}`)
console.log(`typescript v${service.ts.version}`)
console.log(`cache ${JSON.stringify(service.cachedir)}`)
process.exit(0)
}

Expand Down
99 changes: 9 additions & 90 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@ export interface Options {
typeCheck?: boolean | null
transpileOnly?: boolean | null
files?: boolean | null
cache?: boolean | null
cacheDirectory?: string
compiler?: string
ignore?: string | string[]
project?: string
Expand Down Expand Up @@ -79,9 +77,7 @@ export interface TypeInfo {
*/
export const DEFAULTS: Options = {
files: yn(process.env['TS_NODE_FILES']),
cache: yn(process.env['TS_NODE_CACHE'], { default: true }),
pretty: yn(process.env['TS_NODE_PRETTY']),
cacheDirectory: process.env['TS_NODE_CACHE_DIRECTORY'],
compiler: process.env['TS_NODE_COMPILER'],
compilerOptions: parse(process.env['TS_NODE_COMPILER_OPTIONS']),
ignore: split(process.env['TS_NODE_IGNORE']),
Expand Down Expand Up @@ -150,7 +146,6 @@ export class TSError extends BaseError {
export interface Register {
cwd: string
extensions: string[]
cachedir: string
ts: typeof ts
compile (code: string, fileName: string, lineOffset?: number): string
getTypeInfo (code: string, fileName: string, position: number): TypeInfo
Expand All @@ -170,7 +165,6 @@ function getTmpDir (): string {
*/
export function register (opts: Options = {}): Register {
const options = Object.assign({}, DEFAULTS, opts)
const cacheDirectory = options.cacheDirectory || getTmpDir()
const originalJsHandler = require.extensions['.js']

const ignoreDiagnostics = arrify(options.ignoreDiagnostics).concat([
Expand Down Expand Up @@ -210,18 +204,6 @@ export function register (opts: Options = {}): Register {
const extensions = ['.ts', '.tsx']
const fileNames = options.files ? config.fileNames : []

const cachedir = join(
resolve(cwd, cacheDirectory),
getCompilerDigest({
version: ts.version,
options: config.options,
fileNames,
typeCheck,
ignoreDiagnostics,
compiler
})
)

const diagnosticHost: ts.FormatDiagnosticsHost = {
getNewLine: () => EOL,
getCurrentDirectory: () => cwd,
Expand Down Expand Up @@ -372,8 +354,15 @@ export function register (opts: Options = {}): Register {
}
}

const compile = readThrough(cachedir, options.cache === true, memoryCache, getOutput, getExtension)
const register: Register = { cwd, compile, getTypeInfo, extensions, cachedir, ts }
// Create a simple TypeScript compiler proxy.
function compile (code: string, fileName: string, lineOffset?: number) {
const [value, sourceMap] = getOutput(code, fileName, lineOffset)
const output = updateOutput(value, fileName, sourceMap, getExtension)
memoryCache.outputs[fileName] = output
return output
}

const register: Register = { cwd, compile, getTypeInfo, extensions, ts }

// Register the extensions.
extensions.forEach(extension => {
Expand Down Expand Up @@ -494,57 +483,6 @@ function readConfig (
*/
type SourceOutput = [string, string]

/**
* Wrap the function with caching.
*/
function readThrough (
cachedir: string,
shouldCache: boolean,
memoryCache: MemoryCache,
compile: (code: string, fileName: string, lineOffset?: number) => SourceOutput,
getExtension: (fileName: string) => string
) {
if (shouldCache === false) {
return function (code: string, fileName: string, lineOffset?: number) {
debug('readThrough', fileName)

const [value, sourceMap] = compile(code, fileName, lineOffset)
const output = updateOutput(value, fileName, sourceMap, getExtension)

memoryCache.outputs[fileName] = output

return output
}
}

// Make sure the cache directory exists before continuing.
mkdirp.sync(cachedir)

return function (code: string, fileName: string, lineOffset?: number) {
debug('readThrough', fileName)

const cachePath = join(cachedir, getCacheName(code, fileName))
const extension = getExtension(fileName)
const outputPath = `${cachePath}${extension}`

try {
const output = readFileSync(outputPath, 'utf8')
if (isValidCacheContent(output)) {
memoryCache.outputs[fileName] = output
return output
}
} catch (err) {/* Ignore. */}

const [value, sourceMap] = compile(code, fileName, lineOffset)
const output = updateOutput(value, fileName, sourceMap, getExtension)

memoryCache.outputs[fileName] = output
writeFileSync(outputPath, output)

return output
}
}

/**
* Update the output remapping the source map.
*/
Expand All @@ -567,25 +505,6 @@ function updateSourceMap (sourceMapText: string, fileName: string) {
return JSON.stringify(sourceMap)
}

/**
* Get the file name for the cache entry.
*/
function getCacheName (sourceCode: string, fileName: string) {
return crypto.createHash('sha256')
.update(extname(fileName), 'utf8')
.update('\x00', 'utf8')
.update(sourceCode, 'utf8')
.digest('hex')
}

/**
* Ensure the given cached content is valid by sniffing for a base64 encoded '}'
* at the end of the content, which should exist if there is a valid sourceMap present.
*/
function isValidCacheContent (contents: string) {
return /(?:9|0=|Q==)$/.test(contents.slice(-3))
}

/**
* Create a hash of the current configuration.
*/
Expand Down

0 comments on commit b61c745

Please sign in to comment.