smartgit/ts/smartgit.classes.gitrepo.ts

115 lines
2.9 KiB
TypeScript
Raw Normal View History

2019-06-18 13:17:50 +00:00
import * as plugins from './smartgit.plugins';
2016-11-21 23:07:36 +00:00
import { Smartgit } from './smartgit.classes.smartgit';
2016-11-21 23:07:36 +00:00
/**
* class GitRepo allows access to git directories from node
*/
export class GitRepo {
2019-06-18 13:17:50 +00:00
// STATIC
/**
* creates a new GitRepo Instance after cloning a project
*/
public static async fromCloningIntoDir(
smartgitRefArg: Smartgit,
fromArg: string,
toArg: string
): Promise<GitRepo> {
2019-06-18 13:41:03 +00:00
const dirArg = plugins.path.resolve(toArg);
await plugins.isomorphicGit.clone({
dir: toArg,
fs: smartgitRefArg.envDeps.fs,
http: smartgitRefArg.envDeps.http,
url: fromArg,
2019-06-18 13:41:03 +00:00
});
return new GitRepo(smartgitRefArg, toArg);
2019-06-18 13:41:03 +00:00
}
public static async fromCreatingRepoInDir(
smartgitRefArg: Smartgit,
dirArg: string
): Promise<GitRepo> {
2019-06-18 13:41:03 +00:00
dirArg = plugins.path.resolve(dirArg);
await plugins.isomorphicGit.init({
dir: dirArg,
fs: smartgitRefArg.envDeps.fs,
});
return new GitRepo(smartgitRefArg, dirArg);
2019-06-18 13:41:03 +00:00
}
public static async fromOpeningRepoDir(smartgitRefArg: Smartgit, dirArg: string) {
2019-06-18 13:41:03 +00:00
dirArg = plugins.path.resolve(dirArg);
return new GitRepo(smartgitRefArg, dirArg);
2019-06-18 13:41:03 +00:00
}
// INSTANCE
public smartgitRef: Smartgit;
public repoDir: string;
2019-06-18 13:41:03 +00:00
constructor(smartgitRefArg: Smartgit, repoDirArg: string) {
this.smartgitRef = smartgitRefArg;
this.repoDir = repoDirArg;
2019-06-18 13:17:50 +00:00
}
/**
2019-06-18 13:41:03 +00:00
* lists remotes
2019-06-18 13:17:50 +00:00
*/
public async listRemotes(): Promise<
{
remote: string;
url: string;
}[]
> {
const remotes = await plugins.isomorphicGit.listRemotes({
fs: this.smartgitRef.envDeps.fs,
dir: this.repoDir,
});
return remotes;
2019-06-18 13:17:50 +00:00
}
2019-08-27 14:02:04 +00:00
/**
* ensures the existance of a remote within a repository
* @param remoteNameArg
2020-08-15 13:51:24 +00:00
* @param remoteUrlArg
2019-08-27 14:02:04 +00:00
*/
2019-06-20 12:19:54 +00:00
public async ensureRemote(remoteNameArg: string, remoteUrlArg: string): Promise<void> {
const remotes = await this.listRemotes();
const existingRemote =
remotes.find((itemArg) => itemArg.remote === remoteNameArg);
if (existingRemote) {
if (existingRemote.url !== remoteUrlArg) {
await plugins.isomorphicGit.deleteRemote({
remote: remoteNameArg,
fs: this.smartgitRef.envDeps.fs,
dir: this.repoDir
})
} else {
return;
};
2019-06-20 12:19:54 +00:00
}
await plugins.isomorphicGit.addRemote({
remote: remoteNameArg,
fs: this.smartgitRef.envDeps.fs,
url: remoteUrlArg
})
2019-06-20 12:19:54 +00:00
}
2019-06-18 13:17:50 +00:00
2019-06-18 13:41:03 +00:00
/**
* gets the url for a specific remote
*/
2019-06-20 12:19:54 +00:00
public async getUrlForRemote(remoteName: string): Promise<string> {
const remotes = await this.listRemotes();
const existingRemote = remotes.find(remoteArg => remoteArg.remote === remoteName);
return existingRemote?.url;
2019-06-18 13:41:03 +00:00
}
public async pushBranchToRemote(branchName: string, remoteName: string) {
await plugins.isomorphicGit.push({
fs: this.smartgitRef.envDeps.fs,
http: this.smartgitRef.envDeps.http,
ref: branchName,
remote: remoteName
})
2019-06-18 13:41:03 +00:00
}
2016-11-21 23:07:36 +00:00
}