Skip to content

Commit

Permalink
Allow empty files to be read by TypeScript (#581)
Browse files Browse the repository at this point in the history
  • Loading branch information
blakeembrey committed Apr 29, 2018
1 parent 3085eaa commit 56256e5
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 7 deletions.
9 changes: 9 additions & 0 deletions src/index.spec.ts
Expand Up @@ -106,6 +106,15 @@ describe('ts-node', function () {
)
})

it('should import empty files', function (done) {
exec(`${BIN_EXEC} -e "import './tests/empty'"`, function (err, stdout) {
expect(err).to.equal(null)
expect(stdout).to.equal('')

return done()
})
})

it('should throw errors', function (done) {
exec(`${BIN_EXEC} -e "import * as m from './tests/module';console.log(m.example(123))"`, function (err) {
if (err === null) {
Expand Down
15 changes: 8 additions & 7 deletions src/index.ts
Expand Up @@ -71,7 +71,7 @@ export interface Options {
* Track the project information.
*/
interface MemoryCache {
contents: { [path: string]: string }
contents: { [path: string]: string | undefined }
versions: { [path: string]: number | undefined }
outputs: { [path: string]: string }
}
Expand Down Expand Up @@ -290,16 +290,17 @@ export function register (opts: Options = {}): Register {
return version === undefined ? undefined as any as string : String(version)
},
getScriptSnapshot (fileName: string) {
if (!memoryCache.contents[fileName]) {
const contents = readFile(fileName)
if (!contents) return
memoryCache.contents[fileName] = contents
// Read contents into TypeScript memory cache.
if (!Object.prototype.hasOwnProperty.call(memoryCache.contents, fileName)) {
memoryCache.contents[fileName] = readFile(fileName)
}

return ts.ScriptSnapshot.fromString(memoryCache.contents[fileName])
const contents = memoryCache.contents[fileName]
if (contents === undefined) return
return ts.ScriptSnapshot.fromString(contents)
},
fileExists: debugFn('fileExists', fileExists),
readFile: debugFn('getFile', readFile),
readFile: debugFn('readFile', readFile),
readDirectory: debugFn('readDirectory', ts.sys.readDirectory),
getDirectories: debugFn('getDirectories', ts.sys.getDirectories),
directoryExists: debugFn('directoryExists', ts.sys.directoryExists),
Expand Down
Empty file added tests/empty.ts
Empty file.

0 comments on commit 56256e5

Please sign in to comment.