1- import { writeFile , mkdir , stat } from "fs/promises" ;
1+ import { writeFile , mkdir , stat , rm } from "fs/promises" ;
22import { fetch } from "undici" ;
33import { extract } from "tar" ;
44import { join } from "path" ;
@@ -13,14 +13,20 @@ async function downloadAndExtract(pkg: string, version: string) {
1313 ? `https://registry.npmjs.org/${ pkg . replace ( "/" , "%2F" ) } /-/${ pkg . split ( "/" ) [ 1 ] } -${ encodeURIComponent ( version ) } .tgz`
1414 : `https://registry.npmjs.org/${ pkg } /-/${ pkg } -${ version } .tgz` ;
1515
16- console . log ( `Downloading version ${ pkg } @${ version } ...` ) ;
17- const response = await fetch ( tarballUrl ) ;
18- if ( ! response . ok ) throw new Error ( `Failed to download package: ${ response . statusText } ` ) ;
19-
20- const tarball = await response . arrayBuffer ( ) ;
21- await writeFile ( join ( modulePath , "package.tgz" ) , new Uint8Array ( tarball ) ) ; // Now safe
22-
23- await extract ( { file : join ( modulePath , "package.tgz" ) , cwd : modulePath , strip : 1 } ) ;
16+ try {
17+ console . log ( `Downloading version ${ pkg } @${ version } ...` ) ;
18+ const response = await fetch ( tarballUrl ) ;
19+ if ( ! response . ok ) throw new Error ( `Failed to download package ${ pkg } @${ version } : ${ response . statusText } ` ) ;
20+
21+ const tarball = await response . arrayBuffer ( ) ;
22+ await writeFile ( join ( modulePath , "package.tgz" ) , new Uint8Array ( tarball ) ) ; // Now safe
23+
24+ await extract ( { file : join ( modulePath , "package.tgz" ) , cwd : modulePath , strip : 1 } ) ;
25+ } catch ( err ) {
26+ // Remove the partial download so the next run doesn't treat it as cached
27+ await rm ( modulePath , { recursive : true , force : true } ) ;
28+ throw err ;
29+ }
2430}
2531
2632async function getLatestVersion ( pkg : string ) : Promise < string > {
0 commit comments