• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// @module: commonjs
2// @skipLibCheck: true
3// @noImplicitAny:true
4// @strictNullChecks:true
5
6// @filename: node_modules/typescript/package.json
7{
8    "name": "typescript",
9    "types": "/.ts/typescript.d.ts"
10}
11
12// @filename: APISample_parseConfig.ts
13/*
14 * Note: This test is a public API sample. The sample sources can be found
15 *       at: https://github.com/Microsoft/TypeScript/wiki/Using-the-Compiler-API#a-minimal-compiler
16 *       Please log a "breaking change" issue for any API breaking change affecting this issue
17 */
18
19declare var process: any;
20declare var console: any;
21declare var os: any;
22
23import ts = require("typescript");
24
25function printError(error: ts.Diagnostic): void {
26    if (!error) {
27        return;
28    }
29    console.log(`${error.file && error.file.fileName}: ${error.messageText}`);
30}
31
32export function createProgram(rootFiles: string[], compilerOptionsJson: string): ts.Program | undefined {
33    const { config, error } = ts.parseConfigFileTextToJson("tsconfig.json", compilerOptionsJson)
34    if (error) {
35        printError(error);
36        return undefined;
37    }
38    const basePath: string = process.cwd();
39    const settings = ts.convertCompilerOptionsFromJson(config.config["compilerOptions"], basePath);
40    if (!settings.options) {
41        for (const err of settings.errors) {
42            printError(err);
43        }
44        return undefined;
45    }
46    return ts.createProgram(rootFiles, settings.options);
47}
48