Skip to content

Commit

Permalink
feat: fetch add retry
Browse files Browse the repository at this point in the history
winchesHe committed Mar 28, 2024
1 parent 9dfb069 commit 9c75e89
Showing 3 changed files with 32 additions and 5 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -51,6 +51,7 @@
"prepare": "husky install"
},
"dependencies": {
"async-retry": "1.3.3",
"chalk": "5.3.0",
"commander": "11.0.0",
"gradient-string": "2.0.2",
14 changes: 14 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 17 additions & 5 deletions src/helpers/fetch.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Readable } from 'stream';
import { pipeline } from 'stream/promises';

import retry from 'async-retry';
import tar from 'tar';

async function fetchTarStream(url: string) {
@@ -14,10 +15,21 @@ async function fetchTarStream(url: string) {
}

export async function downloadTemplate(root: string, url: string) {
await pipeline(
await fetchTarStream(url),
tar.x({
cwd: root
})
await retry(
async (bail) => {
try {
await pipeline(
await fetchTarStream(url),
tar.x({
cwd: root
})
);
} catch (error) {
bail(new Error(`Failed to download ${url} Error: ${error}`));
}
},
{
retries: 3
}
);
}

0 comments on commit 9c75e89

Please sign in to comment.