-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BREAKING CHANGE: Removed update method
- Loading branch information
roman.vasilev
committed
Aug 24, 2018
1 parent
ac90db9
commit 68044bb
Showing
10 changed files
with
128 additions
and
207 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import * as ts from 'typescript'; | ||
import * as fs from 'fs'; | ||
import * as path from 'path'; | ||
|
||
type CreateProgramArgument = { | ||
configFile: string; | ||
compilerOptions?: ts.CompilerOptions; | ||
projectDirectory?: string; | ||
}; | ||
|
||
export function createProgram({ configFile, projectDirectory = path.dirname(configFile), compilerOptions = {} }: CreateProgramArgument): ts.Program { | ||
const { config, error } = ts.readConfigFile(configFile, ts.sys.readFile); | ||
if (error !== undefined) { | ||
throw new Error(ts.formatDiagnostics([error], { | ||
getCanonicalFileName: f => f, | ||
getCurrentDirectory: process.cwd, | ||
getNewLine: () => '\n', | ||
})); | ||
} | ||
const parseConfigHost: ts.ParseConfigHost = { | ||
fileExists: (path: string) => { | ||
return fs.existsSync(path); | ||
}, | ||
readDirectory: ts.sys.readDirectory, | ||
readFile: (file) => { | ||
return fs.readFileSync(file, 'utf8'); | ||
}, | ||
useCaseSensitiveFileNames: true, | ||
}; | ||
config.compilerOptions = { ...(config.compilerOptions || {}), ...compilerOptions }; | ||
const parsed = ts.parseJsonConfigFileContent(config, parseConfigHost, path.resolve(projectDirectory), { | ||
noEmit: true, | ||
sourceMap: false, | ||
inlineSources: false, | ||
inlineSourceMap: false, | ||
}); | ||
if (parsed.errors !== undefined) { | ||
// ignore warnings and 'TS18003: No inputs were found in config file ...' | ||
const errors = parsed.errors.filter(d => d.category === ts.DiagnosticCategory.Error && d.code !== 18003); | ||
if (errors.length !== 0) { | ||
throw new Error(ts.formatDiagnostics(errors, { | ||
getCanonicalFileName: f => f, | ||
getCurrentDirectory: process.cwd, | ||
getNewLine: () => '\n', | ||
})); | ||
} | ||
} | ||
const host = ts.createCompilerHost(parsed.options, true); | ||
const program = ts.createProgram(parsed.fileNames, parsed.options, host); | ||
|
||
return program; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import * as ts from 'typescript'; | ||
|
||
export function getSourceFile(program: ts.Program, fileName: string, sourceText?: string): ts.SourceFile { | ||
if (program !== undefined) { | ||
const sourceFile = program.getSourceFile(fileName); | ||
if (sourceFile) { | ||
return sourceFile; | ||
} | ||
} | ||
if (sourceText === undefined) { | ||
throw new Error(`Invalid source file: ${fileName}`); | ||
} | ||
return ts.createSourceFile(fileName, sourceText, ts.ScriptTarget.ES5, true); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.