diff --git a/README.md b/README.md index ce229fe..73628b2 100644 --- a/README.md +++ b/README.md @@ -31,7 +31,7 @@ npm install https://github.com/rindeal-js/git-remote-refs-cmp ```ts import { gitRemoteRefsCmp } from 'git-remote-refs-cmp' -async function compareRemotes() { +(async () => { const sourceRemote = 'https://github.com/source-repo.git' const targetRemote = 'https://github.com/target-repo.git' const diff = await gitRemoteRefsCmp(sourceRemote, targetRemote) @@ -40,9 +40,47 @@ async function compareRemotes() { } else { console.log('No differences found.') } -} +})() +``` + +If you want to run more than one comparison query in your script: + +```ts +import { GitRemoteRefsCmp } from 'git-remote-refs-cmp' + +(async () => { + const cmp = GitRemoteRefsCmp() + cmp.init() -compareRemotes() + const diff = cmp.compare(sourceRemote, targetRemote) + const diff2 = cmp.compare(sourceRemote2, targetRemote2) + ... +})() +``` + +For a lower level access and handling: + +```ts +import { + GitCommandManager, + GitLsRemoteParser, + GitLsRemoteOutputCmp, +} from 'git-remote-refs-cmp' + +(async () => { + const git = new GitCommandManager() + await git.init() + const parser = new GitLsRemoteParser() + const lsRemoteCmp = new GitLsRemoteOutputCmp() + + let [source, target] = await Promise.all( + [sourceRemote, targetRemote].map(async (remote) => + parser.parse(await git.lsRemote({ remote }), remote) + ) + ) + let diff = this.lsRemoteCmp.compare(source, target) + ... +})() ``` ## ⚠️ Limitations diff --git a/src/GitRemoteRefsCmp.ts b/src/GitRemoteRefsCmp.ts index b3f24c5..e6214de 100644 --- a/src/GitRemoteRefsCmp.ts +++ b/src/GitRemoteRefsCmp.ts @@ -41,15 +41,26 @@ class GitRemoteRefsCmp { await this.git.init() } + public isInitialized(): boolean { + return this.git.isInitialized() + } + + public async ensureIsInitialized() { + if ( ! this.isInitialized() ) { + await this.init() + } + } + private async processRemote(remote: string): Promise { const rawOutput = await this.git.lsRemote({ remote }) return this.parser.parse(rawOutput, remote) } public async compare(sourceRemote: string, targetRemote: string): Promise { - console.assert(this.git.isInitialized()) - const promises = [sourceRemote, targetRemote].map(this.processRemote) - const [source, target] = await Promise.all(promises) + await this.ensureIsInitialized() + const [source, target] = await Promise.all( + [sourceRemote, targetRemote].map(this.processRemote) + ) return this.lsRemoteCmp.compare(source, target) } } diff --git a/src/index.ts b/src/index.ts index 9d9792e..ebe82aa 100644 --- a/src/index.ts +++ b/src/index.ts @@ -12,6 +12,18 @@ import { } from './GitRemoteRefsCmp' +export type { + GitCommandManager, +} from './GitCommandManager' +export type { + GitLsRemoteOutput, +} from './GitLsRemoteOutput' +export type { + GitLsRemoteOutputCmp, +} from './GitLsRemoteOutputCmp' +export type { + GitLsRemoteParser, +} from './GitLsRemoteParser' export type { GitLsRemoteRefDiff, GitLsRemoteZeroRefsDiff,