let host = ts.createCompilerHost(config.options);
host.readFile = (filePath: string) => {
return tree.read(filePath)?.toString();
};
host.fileExists = (filePath) => {
return tree.exists(filePath);
};
host.directoryExists = (filePath: string) => {
if (filePath.includes('node_modules')) {
return false;
}
try {
let dir = tree.getDir(filePath);
return !!dir.subdirs.length || !!dir.subfiles.length;
} catch (error) {
return false;
}
};
host.getCanonicalFileName = (filePath) => filePath;
host.getCurrentDirectory = () => '/';
let program = ts.createProgram({ rootNames: config.fileNames, options: config.options, host: host });
//读取文件
let list = program
.getSourceFiles()
.filter((sf) => !sf.isDeclarationFile)
.filter((sf) => !sf.fileName.includes('node_modules'));
list;
let sf = program.getSourceFile('test.ts')!;
let statement = sf?.statements[1]!;
console.log(ts.SyntaxKind[statement.kind]);