1/* 2 * Copyright (c) 2022-2024 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16import * as ts from 'typescript'; 17import * as fs from 'node:fs'; 18import * as path from 'node:path'; 19 20import { logTscDiagnostics } from '../utils/LogTscDiagnostics'; 21import { LogLevel, Logger } from '../../utils/logger/Logger'; 22 23export function defaultCompilerOptions(): ts.CompilerOptions { 24 return { 25 target: ts.ScriptTarget.Latest, 26 module: ts.ModuleKind.CommonJS, 27 allowJs: true, 28 checkJs: true 29 }; 30} 31 32export function compile( 33 rootFileNames: readonly string[], 34 compilerOptions?: ts.CompilerOptions, 35 compilerHost?: ts.CompilerHost 36): ts.Program { 37 const program = ts.createProgram(rootFileNames, compilerOptions ?? defaultCompilerOptions(), compilerHost); 38 const diagnostics = ts.getPreEmitDiagnostics(program); 39 40 logTscDiagnostics(diagnostics, LogLevel.ERROR); 41 42 return program; 43} 44 45export function parseConfigFile(tsconfig: string): ts.ParsedCommandLine | undefined { 46 const host: ts.ParseConfigFileHost = ts.sys as ts.System & ts.ParseConfigFileHost; 47 48 const diagnostics: ts.Diagnostic[] = []; 49 50 let parsedConfigFile: ts.ParsedCommandLine | undefined; 51 52 try { 53 const oldUnrecoverableDiagnostic = host.onUnRecoverableConfigFileDiagnostic; 54 host.onUnRecoverableConfigFileDiagnostic = (diagnostic: ts.Diagnostic): void => { 55 diagnostics.push(diagnostic); 56 }; 57 parsedConfigFile = ts.getParsedCommandLineOfConfigFile(tsconfig, {}, host); 58 host.onUnRecoverableConfigFileDiagnostic = oldUnrecoverableDiagnostic; 59 60 if (parsedConfigFile) { 61 diagnostics.push(...ts.getConfigFileParsingDiagnostics(parsedConfigFile)); 62 } 63 64 if (diagnostics.length > 0) { 65 // Log all diagnostic messages and exit program. 66 Logger.error('Failed to read config file.'); 67 logTscDiagnostics(diagnostics, LogLevel.ERROR); 68 process.exit(-1); 69 } 70 } catch (error) { 71 Logger.error('Failed to read config file: ' + error); 72 process.exit(-1); 73 } 74 75 return parsedConfigFile; 76} 77 78export function getSourceFilesFromDir(dir: string): string[] { 79 const resultFiles: string[] = []; 80 81 for (const entity of fs.readdirSync(dir)) { 82 const entityName = path.join(dir, entity); 83 84 if (fs.statSync(entityName).isFile()) { 85 const extension = path.extname(entityName); 86 if (extension === ts.Extension.Ts || extension === ts.Extension.Tsx) { 87 resultFiles.push(entityName); 88 } 89 } 90 91 if (fs.statSync(entityName).isDirectory()) { 92 resultFiles.push(...getSourceFilesFromDir(entityName)); 93 continue; 94 } 95 } 96 97 return resultFiles; 98} 99