Skip to content

Commit c87dbbe

Browse files
committed
Refactor withGit helper: move to end of file and make callback a positional parameter
1 parent facffb8 commit c87dbbe

File tree

1 file changed

+47
-61
lines changed
  • packages/cli-kit/src/public/node

1 file changed

+47
-61
lines changed

packages/cli-kit/src/public/node/git.ts

Lines changed: 47 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,9 @@ export async function initializeGitRepository(directory: string, initialBranch =
2727
outputDebug(outputContent`Initializing git repository at ${outputToken.path(directory)}...`)
2828
await ensureGitIsPresentOrAbort()
2929
// We use init and checkout instead of `init --initial-branch` because the latter is only supported in git 2.28+
30-
await withGit({
31-
directory,
32-
callback: async (repo) => {
33-
await repo.init()
34-
await repo.checkoutLocalBranch(initialBranch)
35-
},
30+
await withGit({directory}, async (repo) => {
31+
await repo.init()
32+
await repo.checkoutLocalBranch(initialBranch)
3633
})
3734
}
3835

@@ -46,7 +43,7 @@ export async function initializeGitRepository(directory: string, initialBranch =
4643
* @returns Files ignored by the lockfile.
4744
*/
4845
export async function checkIfIgnoredInGitRepository(directory: string, files: string[]): Promise<string[]> {
49-
return withGit({directory, callback: (repo) => repo.checkIgnore(files)})
46+
return withGit({directory}, (repo) => repo.checkIgnore(files))
5047
}
5148

5249
export interface GitIgnoreTemplate {
@@ -197,12 +194,9 @@ export async function downloadGitRepository(cloneOptions: GitCloneOptions): Prom
197194
await git(simpleGitOptions).clone(repository!, destination, options)
198195

199196
if (latestTag) {
200-
await withGit({
201-
directory: destination,
202-
callback: async (localGitRepository) => {
203-
const latestTag = await getLocalLatestTag(localGitRepository, repoUrl)
204-
await localGitRepository.checkout(latestTag)
205-
},
197+
await withGit({directory: destination}, async (localGitRepository) => {
198+
const latestTag = await getLocalLatestTag(localGitRepository, repoUrl)
199+
await localGitRepository.checkout(latestTag)
206200
})
207201
}
208202
} catch (err) {
@@ -240,10 +234,7 @@ async function getLocalLatestTag(repository: SimpleGit, repoUrl: string): Promis
240234
* @returns The latest commit of the repository.
241235
*/
242236
export async function getLatestGitCommit(directory?: string): Promise<DefaultLogFields & ListLogLine> {
243-
const logs = await withGit({
244-
directory,
245-
callback: (repo) => repo.log({maxCount: 1}),
246-
})
237+
const logs = await withGit({directory}, (repo) => repo.log({maxCount: 1}))
247238
if (!logs.latest) {
248239
throw new AbortError(
249240
'Must have at least one commit to run command',
@@ -262,7 +253,7 @@ export async function getLatestGitCommit(directory?: string): Promise<DefaultLog
262253
* @returns A promise that resolves when the files are added to the index.
263254
*/
264255
export async function addAllToGitFromDirectory(directory?: string): Promise<void> {
265-
await withGit({directory, callback: (repo) => repo.raw('add', '--all')})
256+
await withGit({directory}, (repo) => repo.raw('add', '--all'))
266257
}
267258

268259
export interface CreateGitCommitOptions {
@@ -279,10 +270,7 @@ export interface CreateGitCommitOptions {
279270
*/
280271
export async function createGitCommit(message: string, options?: CreateGitCommitOptions): Promise<string> {
281272
const commitOptions = options?.author ? {'--author': options.author} : undefined
282-
const result = await withGit({
283-
directory: options?.directory,
284-
callback: (repo) => repo.commit(message, commitOptions),
285-
})
273+
const result = await withGit({directory: options?.directory}, (repo) => repo.commit(message, commitOptions))
286274
return result.commit
287275
}
288276

@@ -293,7 +281,7 @@ export async function createGitCommit(message: string, options?: CreateGitCommit
293281
* @returns The HEAD symbolic reference of the repository.
294282
*/
295283
export async function getHeadSymbolicRef(directory?: string): Promise<string> {
296-
const ref = await withGit({directory, callback: (repo) => repo.raw('symbolic-ref', '-q', 'HEAD')})
284+
const ref = await withGit({directory}, (repo) => repo.raw('symbolic-ref', '-q', 'HEAD'))
297285
if (!ref) {
298286
throw new AbortError(
299287
"Git HEAD can't be detached to run command",
@@ -346,7 +334,7 @@ export async function ensureInsideGitDirectory(directory?: string): Promise<void
346334
* @returns True if the directory is inside a .git directory tree.
347335
*/
348336
export async function insideGitDirectory(directory?: string): Promise<boolean> {
349-
return withGit({directory, callback: (repo) => repo.checkIsRepo()})
337+
return withGit({directory}, (repo) => repo.checkIsRepo())
350338
}
351339

352340
export class GitDirectoryNotCleanError extends AbortError {}
@@ -369,29 +357,7 @@ export async function ensureIsClean(directory?: string): Promise<void> {
369357
* @returns True is the .git directory is clean.
370358
*/
371359
export async function isClean(directory?: string): Promise<boolean> {
372-
return (await withGit({directory, callback: (git: SimpleGit) => git.status()})).isClean()
373-
}
374-
375-
async function withGit<T>({
376-
directory,
377-
callback,
378-
}: {
379-
directory?: string
380-
callback: (git: SimpleGit) => Promise<T>
381-
}): Promise<T> {
382-
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
383-
// @ts-ignore
384-
const repo = git({baseDir: directory})
385-
try {
386-
return await callback(repo)
387-
} catch (err) {
388-
if (err instanceof Error) {
389-
const abortError = new AbortError(err.message)
390-
abortError.stack = err.stack
391-
throw abortError
392-
}
393-
throw err
394-
}
360+
return (await withGit({directory}, (git: SimpleGit) => git.status())).isClean()
395361
}
396362

397363
/**
@@ -401,7 +367,7 @@ async function withGit<T>({
401367
* @returns String with the latest tag or undefined if no tags are found.
402368
*/
403369
export async function getLatestTag(directory?: string): Promise<string | undefined> {
404-
const tags = await withGit({directory, callback: (repo) => repo.tags()})
370+
const tags = await withGit({directory}, (repo) => repo.tags())
405371
return tags.latest
406372
}
407373

@@ -416,19 +382,39 @@ export async function removeGitRemote(directory: string, remoteName = 'origin'):
416382
outputDebug(outputContent`Removing git remote ${remoteName} from ${outputToken.path(directory)}...`)
417383
await ensureGitIsPresentOrAbort()
418384

419-
await withGit({
420-
directory,
421-
callback: async (repo) => {
422-
// Check if remote exists first
423-
const remotes = await repo.getRemotes()
424-
const remoteExists = remotes.some((remote: {name: string}) => remote.name === remoteName)
425-
426-
if (!remoteExists) {
427-
outputDebug(outputContent`Remote ${remoteName} does not exist, no action needed`)
428-
return
429-
}
385+
await withGit({directory}, async (repo) => {
386+
// Check if remote exists first
387+
const remotes = await repo.getRemotes()
388+
const remoteExists = remotes.some((remote: {name: string}) => remote.name === remoteName)
430389

431-
await repo.removeRemote(remoteName)
432-
},
390+
if (!remoteExists) {
391+
outputDebug(outputContent`Remote ${remoteName} does not exist, no action needed`)
392+
return
393+
}
394+
395+
await repo.removeRemote(remoteName)
433396
})
434397
}
398+
399+
async function withGit<T>(
400+
{
401+
directory,
402+
}: {
403+
directory?: string
404+
},
405+
callback: (git: SimpleGit) => Promise<T>,
406+
): Promise<T> {
407+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
408+
// @ts-ignore
409+
const repo = git({baseDir: directory})
410+
try {
411+
return await callback(repo)
412+
} catch (err) {
413+
if (err instanceof Error) {
414+
const abortError = new AbortError(err.message)
415+
abortError.stack = err.stack
416+
throw abortError
417+
}
418+
throw err
419+
}
420+
}

0 commit comments

Comments
 (0)