mirror of
https://github.com/OneUptime/oneuptime
synced 2024-11-21 14:49:07 +00:00
add code repository basic functions
This commit is contained in:
parent
78bdc42534
commit
64ae5eeb89
@ -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;
|
||||
}
|
||||
}
|
||||
|
6
CommonServer/Utils/CodeRepository/CodeRepositoryFile.ts
Normal file
6
CommonServer/Utils/CodeRepository/CodeRepositoryFile.ts
Normal file
@ -0,0 +1,6 @@
|
||||
export default interface CodeRepositoryFile {
|
||||
filePath: string;
|
||||
gitCommitHash: string;
|
||||
fileExtension: string;
|
||||
fileName: string;
|
||||
}
|
@ -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();
|
||||
});
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
24
CommonServer/Utils/Execute.ts
Normal file
24
CommonServer/Utils/Execute.ts
Normal 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);
|
||||
}
|
||||
);
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
@ -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}`
|
||||
);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user