Skip to content

Commit 1550509

Browse files
committed
chore: error not exit
1 parent d8c1af0 commit 1550509

File tree

1 file changed

+18
-25
lines changed

1 file changed

+18
-25
lines changed

src/index.ts

+18-25
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import path from 'node:path'
55
import process from 'node:process'
66
import { fileURLToPath } from 'node:url'
77
import JSON5 from 'json5'
8+
import { retryAsync } from 'lazy-js-utils'
89
import { jsShell } from 'lazy-js-utils/dist/node'
910
import * as tar from 'tar'
1011
/**
@@ -99,23 +100,11 @@ export async function fetchAndExtractPackage(options: { name: string, dist?: str
99100
}
100101
}
101102

102-
async function retryAsync<T>(fn: () => Promise<T>, retries: number): Promise<T> {
103-
try {
104-
return await fn()
105-
}
106-
catch (error: any) {
107-
if (retries > 0) {
108-
return retryAsync(fn, retries - 1)
109-
}
110-
else {
111-
throw error
112-
}
113-
}
114-
}
115-
116103
export async function downloadWitchPack(name: string, tempDir: string, logger: any = console) {
117104
await fsp.mkdir(tempDir, { recursive: true })
118-
const { result, status } = await jsShell(`npm pack ${name} --pack-destination ${tempDir}`)
105+
const { result, status } = await jsShell(`npm pack ${name} --pack-destination ${tempDir}`, {
106+
errorExit: false,
107+
})
119108
if (status !== 0) {
120109
logger.error(result)
121110
return Promise.reject(result)
@@ -129,7 +118,9 @@ export async function downloadWitchPack(name: string, tempDir: string, logger: a
129118

130119
export async function downloadWithNpmHttp(name: string, tempDir: string, tempFile: string, logger: any = console) {
131120
await fsp.mkdir(tempDir, { recursive: true })
132-
const { result, status } = await jsShell(`npm view ${name} dist.tarball`)
121+
const { result, status } = await jsShell(`npm view ${name} dist.tarball`, {
122+
errorExit: false,
123+
})
133124
if (status !== 0) {
134125
logger.error(result)
135126
return Promise.reject(result)
@@ -160,17 +151,19 @@ export async function downloadWithNpmHttp(name: string, tempDir: string, tempFil
160151

161152
export async function downloadWithHttp(name: string, tempDir: string, tempFile: string, logger: any = console) {
162153
await fsp.mkdir(tempDir, { recursive: true })
163-
const tarballUrl = await Promise.any([
164-
getTarballUrlFromRegistry(name),
165-
getTarballUrlFromYarn(name),
166-
getTarballUrlFromTencent(name),
167-
]).catch((error) => {
154+
let tarballUrl: string
155+
try {
156+
tarballUrl = await Promise.any([
157+
getTarballUrlFromRegistry(name),
158+
getTarballUrlFromYarn(name),
159+
getTarballUrlFromTencent(name),
160+
])
161+
}
162+
catch (error) {
168163
logger.error(`[fetch-npm]: Failed to fetch tarball URL from all sources: ${error}`)
169-
throw error
170-
})
164+
return Promise.reject(error)
165+
}
171166

172-
if (!tarballUrl)
173-
return ''
174167
const protocol = new URL(tarballUrl).protocol
175168
const lib = protocol === 'https:' ? https : http
176169
const tgzPath = path.join(tempDir, `${tempFile}.tgz`)

0 commit comments

Comments
 (0)