1//// [tests/cases/compiler/APISample_parseConfig.ts] //// 2 3//// [package.json] 4{ 5 "name": "typescript", 6 "types": "/.ts/typescript.d.ts" 7} 8 9//// [APISample_parseConfig.ts] 10/* 11 * Note: This test is a public API sample. The sample sources can be found 12 * at: https://github.com/Microsoft/TypeScript/wiki/Using-the-Compiler-API#a-minimal-compiler 13 * Please log a "breaking change" issue for any API breaking change affecting this issue 14 */ 15 16declare var process: any; 17declare var console: any; 18declare var os: any; 19 20import ts = require("typescript"); 21 22function printError(error: ts.Diagnostic): void { 23 if (!error) { 24 return; 25 } 26 console.log(`${error.file && error.file.fileName}: ${error.messageText}`); 27} 28 29export function createProgram(rootFiles: string[], compilerOptionsJson: string): ts.Program | undefined { 30 const { config, error } = ts.parseConfigFileTextToJson("tsconfig.json", compilerOptionsJson) 31 if (error) { 32 printError(error); 33 return undefined; 34 } 35 const basePath: string = process.cwd(); 36 const settings = ts.convertCompilerOptionsFromJson(config.config["compilerOptions"], basePath); 37 if (!settings.options) { 38 for (const err of settings.errors) { 39 printError(err); 40 } 41 return undefined; 42 } 43 return ts.createProgram(rootFiles, settings.options); 44} 45 46 47//// [APISample_parseConfig.js] 48"use strict"; 49/* 50 * Note: This test is a public API sample. The sample sources can be found 51 * at: https://github.com/Microsoft/TypeScript/wiki/Using-the-Compiler-API#a-minimal-compiler 52 * Please log a "breaking change" issue for any API breaking change affecting this issue 53 */ 54exports.__esModule = true; 55exports.createProgram = void 0; 56var ts = require("typescript"); 57function printError(error) { 58 if (!error) { 59 return; 60 } 61 console.log("".concat(error.file && error.file.fileName, ": ").concat(error.messageText)); 62} 63function createProgram(rootFiles, compilerOptionsJson) { 64 var _a = ts.parseConfigFileTextToJson("tsconfig.json", compilerOptionsJson), config = _a.config, error = _a.error; 65 if (error) { 66 printError(error); 67 return undefined; 68 } 69 var basePath = process.cwd(); 70 var settings = ts.convertCompilerOptionsFromJson(config.config["compilerOptions"], basePath); 71 if (!settings.options) { 72 for (var _i = 0, _b = settings.errors; _i < _b.length; _i++) { 73 var err = _b[_i]; 74 printError(err); 75 } 76 return undefined; 77 } 78 return ts.createProgram(rootFiles, settings.options); 79} 80exports.createProgram = createProgram; 81