Files
oneuptime/CommonServer/Utils/CodeRepository/CodeRepository.ts
Simon Larsen dfb7f2320c refactor: Update jest.config.json files with testPathIgnorePatterns
The jest.config.json files in the Model, Probe, Common, Copilot, CommonUI, Ingestor, IsolatedVM, TestServer, and CommonServer directories have been updated. The "testPathIgnorePatterns" property has been added to each file, excluding the "node_modules" and "dist" directories from test path matching. This change improves test performance and ensures that unnecessary files are not included in the test coverage.
2024-06-11 14:52:55 +01:00

101 lines
2.9 KiB
TypeScript

import Execute from '../Execute';
import CodeRepositoryFile from './CodeRepositoryFile';
import Dictionary from 'Common/Types/Dictionary';
export default class CodeRepositoryUtil {
public static async getGitCommitHashForFile(data: {
repoPath: string;
filePath: string;
}): Promise<string> {
const { repoPath, filePath } = data;
return await Execute.executeCommand(
`cd ${repoPath} && git log -1 --pretty=format:"%H" "${filePath}"`
);
}
public static async getFilesInDirectory(data: {
directoryPath: string;
repoPath: string;
}): Promise<{
files: Dictionary<CodeRepositoryFile>;
subDirectories: Array<string>;
}> {
const { directoryPath, repoPath } = data;
const files: Dictionary<CodeRepositoryFile> = {};
const output: string = await Execute.executeCommand(
`ls ${directoryPath}`
);
const fileNames: Array<string> = output.split('\n');
const subDirectories: Array<string> = [];
for (const fileName of fileNames) {
if (fileName === '') {
continue;
}
const isDirectory: boolean = (
await Execute.executeCommand(
`file "${directoryPath}/${fileName}"`
)
).includes('directory');
if (isDirectory) {
subDirectories.push(`${directoryPath}/${fileName}`);
continue;
}
const filePath: string = `${directoryPath}/${fileName}`;
const gitCommitHash: string = await this.getGitCommitHashForFile({
filePath,
repoPath,
});
const fileExtension: string = fileName.split('.').pop() || '';
files[filePath] = {
filePath,
gitCommitHash,
fileExtension,
fileName,
};
}
return {
files,
subDirectories: subDirectories,
};
}
public static async getFilesInDirectoryRecursive(data: {
repoPath: string;
directoryPath: string;
}): Promise<Dictionary<CodeRepositoryFile>> {
let files: Dictionary<CodeRepositoryFile> = {};
const { files: filesInDirectory, subDirectories } =
await this.getFilesInDirectory({
directoryPath: data.directoryPath,
repoPath: data.repoPath,
});
files = {
...files,
...filesInDirectory,
};
for (const subDirectory of subDirectories) {
files = {
...files,
...(await this.getFilesInDirectoryRecursive({
repoPath: data.repoPath,
directoryPath: subDirectory,
})),
};
}
return files;
}
}