Skip to content

Commit

Permalink
Remove previous cache before accessing remote
Browse files Browse the repository at this point in the history
  • Loading branch information
Takeshi committed Feb 17, 2024
1 parent 3a40916 commit 24b6b7f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 7 deletions.
11 changes: 8 additions & 3 deletions src/utils/clone.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,12 @@ export default async function clone(

const local = async () => exists(archivePath + '.tar.gz')
const remote = async () => {
const depth = options?.git?.depth || 1
// If the repository is cached, remove the old cache
if (await exists(archivePath)) {
await rm(archivePath)
}

const depth = options?.git?.depth || 1
const child = spawn('git', [
'clone',
'--depth',
Expand All @@ -50,6 +54,7 @@ export default async function clone(
reject(new GitlyCloneError(reason.message))
)
child.on('close', (code) => {
/* istanbul ignore next */
if (code === 0) {
// Create the archive after cloning
tar
Expand All @@ -70,14 +75,14 @@ export default async function clone(
)
.then(resolve)
.catch((error) => reject(new GitlyCloneError(error.message)))
} else {
} /* istanbul ignore next */ else {
reject(new GitlyCloneError('Failed to clone the repository'))
}
})
})
return archivePath
}

/* istanbul ignore next */
if ((await isOffline()) || options.cache) {
order = [local]
} else if (options.force || ['master', 'main'].includes(info.type)) {
Expand Down
16 changes: 12 additions & 4 deletions src/utils/download.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import fetch from './fetch'
import { isOffline } from './offline'
import parse from './parse'
import { getArchivePath, getArchiveUrl } from './archive'
import { rm } from 'shelljs'

/**
* Download the tar file from the repository
Expand All @@ -24,10 +25,17 @@ export default async function download(
options: GitlyOptions = {}
): Promise<string> {
const info = parse(repository, options)
const path = getArchivePath(info, options)
const archivePath = getArchivePath(info, options)
const url = getArchiveUrl(info, options)
const local = async () => exists(path)
const remote = async () => fetch(url, path, options)
const local = async () => exists(archivePath)
const remote = async () => {
// If the repository is cached, remove the old cache
if (await exists(archivePath)) {
rm(archivePath)
}

return fetch(url, archivePath, options)
}
let order = [local, remote]
if ((await isOffline()) || options.cache) {
order = [local]
Expand All @@ -38,7 +46,7 @@ export default async function download(
try {
const result = await execute(order)
if (typeof result === 'boolean') {
return path
return archivePath
}
return result
} catch (error) {
Expand Down

0 comments on commit 24b6b7f

Please sign in to comment.