Skip to content

Commit 5622c4f

Browse files
committed
feat: add retry
1 parent f54e2d7 commit 5622c4f

File tree

2 files changed

+4279
-10
lines changed

2 files changed

+4279
-10
lines changed

src/index.ts

+20-8
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,13 @@ import * as tar from 'tar'
1010
*
1111
* @param options - 获取包的选项。
1212
* @param options.name - 要获取的 npm 包的名称。
13+
* @param options.retry - 可选。重试次数,默认为 1。
1314
* @param options.dist - 可选。要在包导出中查找的分发目录。
1415
* @returns 获取包的主文件的内容。
1516
* @throws 如果包无法获取、解压或读取,将抛出错误。
1617
*/
17-
export async function fetchWithPack(options: { name: string, dist?: string }) {
18-
const { name, dist } = options
18+
export async function fetchWithPack(options: { name: string, dist?: string, retry?: number }) {
19+
let { name, dist, retry = 1 } = options
1920
const url = typeof __filename !== 'undefined' ? __filename : fileURLToPath(import.meta.url)
2021
const tempDir = path.join(url, '..', 'temp')
2122
const tarballPattern = `${name.replace('@', '').replace('/', '-')}-.*.tgz`
@@ -25,12 +26,23 @@ export async function fetchWithPack(options: { name: string, dist?: string }) {
2526

2627
// Fetch the package tarball using npm pack
2728
await new Promise((resolve, reject) => {
28-
exec(`npm pack ${name} --pack-destination ${tempDir}`, (error) => {
29-
if (error)
30-
reject(error)
31-
else
32-
resolve(true)
33-
})
29+
const fetch = () => {
30+
exec(`npm pack ${name} --pack-destination ${tempDir}`, (error) => {
31+
if (error) {
32+
if (retry > 0) {
33+
fetch()
34+
retry--
35+
}
36+
else {
37+
reject(error)
38+
}
39+
}
40+
else {
41+
resolve(true)
42+
}
43+
})
44+
}
45+
fetch()
3446
})
3547

3648
const [tarballPath] = await fs.readdir(tempDir).then(files => files.filter(file => file.match(tarballPattern)))

0 commit comments

Comments
 (0)