feat: adding in all the functions needed for isomorphic git feature

This commit is contained in:
blam
2020-12-21 15:38:53 +01:00
parent e2beaf5940
commit 70ceab3db4
3 changed files with 91 additions and 8 deletions
+1
View File
@@ -45,6 +45,7 @@
"fs-extra": "^9.0.1",
"git-url-parse": "^11.4.0",
"helmet": "^4.0.0",
"isomorphic-git": "^1.8.0",
"knex": "^0.21.6",
"lodash": "^4.17.15",
"logform": "^2.1.1",
+84 -6
View File
@@ -16,14 +16,19 @@
import git from 'isomorphic-git';
import http from 'isomorphic-git/http/node';
import fs from 'fs-extra';
import { Logger } from 'winston';
class SCM {
constructor(
private readonly config: { username: string; password: string },
private readonly logger: Logger,
private readonly config: {
username: string;
password: string;
logger?: Logger;
},
) {}
async clone({ url, dir }) {
async clone({ url, dir }: { url: string; dir: string }) {
this.config.logger?.info(`Cloning repo {dir=${dir},url=${url}}`);
return git.clone({
fs,
http,
@@ -35,7 +40,7 @@ class SCM {
const total = event.total
? `${Math.round((event.loaded / event.total) * 100)}%`
: event.loaded;
this.logger.info(`status={${event.phase},total={${total}}}`);
this.config.logger?.info(`status={${event.phase},total={${total}}}`);
},
headers: {
'user-agent': 'git/@isomorphic-git',
@@ -46,6 +51,79 @@ class SCM {
}),
});
}
async init({ dir }: { dir: string }) {
this.config.logger?.info(`Init git repository {dir=${dir}}`);
return git.init({
fs,
dir,
});
}
async add({ dir, filepath }: { dir: string; filepath: string }) {
this.config.logger?.info(`Adding file {dir=${dir},filepath=${filepath}}`);
return git.add({ fs, dir, filepath });
}
async commit({
dir,
message,
author,
committer,
}: {
dir: string;
message: string;
author: { name: string; email: string };
committer: { name: string; email: string };
}) {
this.config.logger?.info(
`Committing file to repo {dir=${dir},message=${message}}`,
);
return git.commit({ fs, dir, message, author, committer });
}
async addRemote({
dir,
url,
remoteName,
}: {
dir: string;
remoteName: string;
url: string;
}) {
this.config.logger?.info(
`Creating new remote {dir=${dir},remoteName=${remoteName},url=${url}}`,
);
return git.addRemote({ fs, dir, remote: remoteName, url });
}
async push({ dir, remoteName }: { dir: string; remoteName: string }) {
this.config.logger?.info(
`Pushing directory to remote {dir=${dir},remoteName=${remoteName}}`,
);
git.push({
fs,
dir,
http,
onProgress: event => {
const total = event.total
? `${Math.round((event.loaded / event.total) * 100)}%`
: event.loaded;
this.config.logger?.info(`status={${event.phase},total={${total}}}`);
},
headers: {
'user-agent': 'git/@isomorphic-git',
},
remote: remoteName,
onAuth: () => ({
username: this.config.username,
password: this.config.password,
}),
});
}
}
export const fromAuth = ({
@@ -55,7 +133,7 @@ export const fromAuth = ({
}: {
username: string;
password: string;
logger: Logger;
logger?: Logger;
}) => {
return new SCM({ username, password });
return new SCM({ username, password, logger });
};