Skip to content

Commit

Permalink
feat: implement network fallback
Browse files Browse the repository at this point in the history
  • Loading branch information
pi0 committed Sep 8, 2022
1 parent 73053c2 commit 9d92c9a
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
"giget": "jiti ./src/cli.ts",
"lint": "eslint --ext .ts,.js,.mjs,.cjs .",
"prepack": "unbuild",
"play": "rm -rf .tmp && pnpm giget unjs/template .tmp/clone",
"play": "rm -rf .tmp && DEBUG=1 pnpm giget unjs/template .tmp/clone",
"release": "pnpm test && standard-version && git push --follow-tags && pnpm publish",
"test": "pnpm lint && vitest run --coverage"
},
Expand Down
14 changes: 13 additions & 1 deletion src/giget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ import type { GitInfo } from './types'
export interface DownloadRepoOptions extends Partial<GitInfo> {
}

function debug (...args) {
if (process.env.DEBUG) {
console.debug('[giget]', ...args)
}
}

export async function downloadRepo (input: string, dir: string, _opts: DownloadRepoOptions = {}) {
const parsed = parseInput(input)
const opts = { ...parsed, ..._opts }
Expand All @@ -23,7 +29,13 @@ export async function downloadRepo (input: string, dir: string, _opts: DownloadR
const tarPath = resolve(tmpDir, opts.ref + '.tar.gz')
await mkdir(dirname(tarPath), { recursive: true })
const tarUrl = getTarUrl(opts)
await download(tarUrl, tarPath)
await download(tarUrl, tarPath).catch((err) => {
if (!existsSync(tarPath)) {
throw err
}
// Accept netwrok errors if we have a cached version
debug('Download error. Using cached version:', err)
})

const pathFilter = opts.subpath.replace(/^\//, '')
await extract({
Expand Down

0 comments on commit 9d92c9a

Please sign in to comment.