diff --git a/CommonServer/Utils/CodeRepository/CodeRepository.ts b/CommonServer/Utils/CodeRepository/CodeRepository.ts index 345fb6f6c3..4c21a1272c 100644 --- a/CommonServer/Utils/CodeRepository/CodeRepository.ts +++ b/CommonServer/Utils/CodeRepository/CodeRepository.ts @@ -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 { + return await Execute.executeCommand( + `git log -1 --pretty=format:"%H" ${filePath}` + ); + } + + public static async getFilesInDirectory(directoryPath: string): Promise<{ + files: Array; + subDirectories: Array; + }> { + const files: Array = []; + const output: string = await Execute.executeCommand( + `ls ${directoryPath}` + ); + + const fileNames: Array = output.split('\n'); + + const subDirectories: Array = []; + + 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> { + const files: Array = []; + + const { files: filesInDirectory, subDirectories } = + await this.getFilesInDirectory(directoryPath); + files.push(...filesInDirectory); + + for (const subDirectory of subDirectories) { + files.push( + ...(await this.getFilesInDirectoryRecursive(subDirectory)) + ); + } + + return files; } } diff --git a/CommonServer/Utils/CodeRepository/CodeRepositoryFile.ts b/CommonServer/Utils/CodeRepository/CodeRepositoryFile.ts new file mode 100644 index 0000000000..1f70114530 --- /dev/null +++ b/CommonServer/Utils/CodeRepository/CodeRepositoryFile.ts @@ -0,0 +1,6 @@ +export default interface CodeRepositoryFile { + filePath: string; + gitCommitHash: string; + fileExtension: string; + fileName: string; +} diff --git a/CommonServer/Utils/Exec.ts b/CommonServer/Utils/Exec.ts deleted file mode 100644 index c044aef893..0000000000 --- a/CommonServer/Utils/Exec.ts +++ /dev/null @@ -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 { - return new Promise( - (resolve: VoidFunction, reject: PromiseRejectErrorFunction) => { - exec(`${command}`, (err: ExecException | null) => { - if (err) { - return reject(err); - } - - return resolve(); - }); - } - ); - } -} diff --git a/CommonServer/Utils/Execute.ts b/CommonServer/Utils/Execute.ts new file mode 100644 index 0000000000..dd5f5a8e1d --- /dev/null +++ b/CommonServer/Utils/Execute.ts @@ -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 { + return new Promise( + ( + resolve: (output: string) => void, + reject: PromiseRejectErrorFunction + ) => { + exec( + `${command}`, + (err: ExecException | null, stdout: string) => { + if (err) { + return reject(err); + } + + return resolve(stdout); + } + ); + } + ); + } +} diff --git a/Nginx/Utils/SelfSignedSSL.ts b/Nginx/Utils/SelfSignedSSL.ts index 003758c224..8070943882 100644 --- a/Nginx/Utils/SelfSignedSSL.ts +++ b/Nginx/Utils/SelfSignedSSL.ts @@ -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 { - 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}` ); }