add code repository basic functions

This commit is contained in:
Simon Larsen 2024-06-10 19:40:27 +01:00
parent 78bdc42534
commit 64ae5eeb89
No known key found for this signature in database
GPG Key ID: 96C5DCA24769DBCA
5 changed files with 107 additions and 22 deletions

View File

@ -1,5 +1,78 @@
import Execute from '../Execute';
import CodeRepositoryFile from './CodeRepositoryFile';
export default class CodeRepositoryUtil {
public static getGitCommitHashForFile(filePath: string): string {
// this is git repository.
public static async getGitCommitHashForFile(
filePath: string
): Promise<string> {
return await Execute.executeCommand(
`git log -1 --pretty=format:"%H" ${filePath}`
);
}
public static async getFilesInDirectory(directoryPath: string): Promise<{
files: Array<CodeRepositoryFile>;
subDirectories: Array<string>;
}> {
const files: Array<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
);
const fileExtension: string = fileName.split('.').pop() || '';
files.push({
filePath,
gitCommitHash,
fileExtension,
fileName,
});
}
return {
files,
subDirectories: subDirectories,
};
}
public static async getFilesInDirectoryRecursive(
directoryPath: string
): Promise<Array<CodeRepositoryFile>> {
const files: Array<CodeRepositoryFile> = [];
const { files: filesInDirectory, subDirectories } =
await this.getFilesInDirectory(directoryPath);
files.push(...filesInDirectory);
for (const subDirectory of subDirectories) {
files.push(
...(await this.getFilesInDirectoryRecursive(subDirectory))
);
}
return files;
}
}

View File

@ -0,0 +1,6 @@
export default interface CodeRepositoryFile {
filePath: string;
gitCommitHash: string;
fileExtension: string;
fileName: string;
}

View File

@ -1,18 +0,0 @@
import { PromiseRejectErrorFunction } from 'Common/Types/FunctionTypes';
import { ExecException, exec } from 'node:child_process';
export default class Exec {
public static exec(command: string): Promise<void> {
return new Promise(
(resolve: VoidFunction, reject: PromiseRejectErrorFunction) => {
exec(`${command}`, (err: ExecException | null) => {
if (err) {
return reject(err);
}
return resolve();
});
}
);
}
}

View File

@ -0,0 +1,24 @@
import { PromiseRejectErrorFunction } from 'Common/Types/FunctionTypes';
import { ExecException, exec } from 'node:child_process';
export default class Execute {
public static executeCommand(command: string): Promise<string> {
return new Promise(
(
resolve: (output: string) => void,
reject: PromiseRejectErrorFunction
) => {
exec(
`${command}`,
(err: ExecException | null, stdout: string) => {
if (err) {
return reject(err);
}
return resolve(stdout);
}
);
}
);
}
}

View File

@ -1,8 +1,8 @@
import Exec from 'CommonServer/Utils/Exec';
import Exec from 'CommonServer/Utils/Execute';
export default class SelfSignedSSL {
public static async generate(path: string, host: string): Promise<void> {
return await Exec.exec(
await Exec.executeCommand(
`mkdir -p ${path} && openssl req -new -x509 -nodes -subj "/C=US/ST=NY/L=NYC/O=Global Security/OU=IT Department/CN=example.com" -out ${path}/${host}.crt -keyout ${path}/${host}.key -days 99999 && chmod -R 777 ${path}`
);
}