@@ -10,12 +10,13 @@ import * as tar from 'tar'
10
10
*
11
11
* @param options - 获取包的选项。
12
12
* @param options.name - 要获取的 npm 包的名称。
13
+ * @param options.retry - 可选。重试次数,默认为 1。
13
14
* @param options.dist - 可选。要在包导出中查找的分发目录。
14
15
* @returns 获取包的主文件的内容。
15
16
* @throws 如果包无法获取、解压或读取,将抛出错误。
16
17
*/
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
19
20
const url = typeof __filename !== 'undefined' ? __filename : fileURLToPath ( import . meta. url )
20
21
const tempDir = path . join ( url , '..' , 'temp' )
21
22
const tarballPattern = `${ name . replace ( '@' , '' ) . replace ( '/' , '-' ) } -.*.tgz`
@@ -25,12 +26,23 @@ export async function fetchWithPack(options: { name: string, dist?: string }) {
25
26
26
27
// Fetch the package tarball using npm pack
27
28
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 ( )
34
46
} )
35
47
36
48
const [ tarballPath ] = await fs . readdir ( tempDir ) . then ( files => files . filter ( file => file . match ( tarballPattern ) ) )
0 commit comments