symref
Version:
Static code checker for AI code agents (Windsurf, Cline, etc.)
113 lines • 3.96 kB
JavaScript
import { Project, ScriptTarget, ModuleKind, ModuleResolutionKind } from 'ts-morph';
import * as path from 'path';
import * as fs from 'fs';
import * as glob from 'glob';
/**
* TypeScriptプロジェクトの初期化と管理を行うクラス
*/
export class ProjectManager {
/**
* コンストラクタ
* @param options 設定オプション
*/
constructor(options) {
const { basePath, tsConfigPath, includePatterns = ["**/*.ts", "**/*.tsx"], excludePatterns = ["**/node_modules/**"] } = options;
// ベースパスを正規化
this.basePath = path.resolve(basePath);
this.project = this.initializeProject(tsConfigPath, includePatterns, excludePatterns);
}
/**
* プロジェクトを初期化する
* @param tsConfigPath tsconfig.jsonのパス
* @param includePatterns 含めるファイルパターン
* @param excludePatterns 除外するファイルパターン
* @returns 初期化されたプロジェクト
*/
initializeProject(tsConfigPath, includePatterns = [], excludePatterns = []) {
let project;
// tsconfig.jsonが指定されている場合はそれを使用
if (tsConfigPath && fs.existsSync(tsConfigPath)) {
project = new Project({
tsConfigFilePath: tsConfigPath,
compilerOptions: {
skipLibCheck: true,
},
skipAddingFilesFromTsConfig: false,
});
}
else {
// tsconfig.jsonがない場合はデフォルト設定を使用
project = new Project({
compilerOptions: {
target: ScriptTarget.ESNext,
module: ModuleKind.ESNext,
moduleResolution: ModuleResolutionKind.NodeJs,
esModuleInterop: true,
skipLibCheck: true,
},
});
}
// パターンに一致するファイルを追加
const files = glob.sync(includePatterns.length > 1 ? `{${includePatterns.join(',')}}` : includePatterns[0], {
cwd: this.basePath,
ignore: excludePatterns,
absolute: true,
});
// 各ファイルをプロジェクトに追加
files.forEach(file => {
if (!project.getSourceFile(file)) {
project.addSourceFileAtPath(file);
}
});
return project;
}
/**
* プロジェクトインスタンスを取得する
* @returns プロジェクトインスタンス
*/
getProject() {
return this.project;
}
/**
* ベースパスを取得する
* @returns ベースパス
*/
getBasePath() {
return this.basePath;
}
/**
* ファイルをプロジェクトに追加する
* @param filePath ファイルパス
* @returns 追加されたかどうか
*/
addFile(filePath) {
const absolutePath = path.isAbsolute(filePath)
? filePath
: path.resolve(this.basePath, filePath);
if (!fs.existsSync(absolutePath)) {
return false;
}
if (!this.project.getSourceFile(absolutePath)) {
this.project.addSourceFileAtPath(absolutePath);
return true;
}
return false;
}
/**
* ファイルをプロジェクトから削除する
* @param filePath ファイルパス
* @returns 削除されたかどうか
*/
removeFile(filePath) {
const absolutePath = path.isAbsolute(filePath)
? filePath
: path.resolve(this.basePath, filePath);
const sourceFile = this.project.getSourceFile(absolutePath);
if (sourceFile) {
this.project.removeSourceFile(sourceFile);
return true;
}
return false;
}
}
//# sourceMappingURL=ProjectManager.js.map