11import { path7za } from "7zip-bin"
2- import BluebirdPromise from "bluebird-lst"
3- import { CancellationToken , DownloadOptions } from "builder-util-runtime"
2+ import { appBuilderPath } from "app-builder-bin"
43import { emptyDir , rename , unlink } from "fs-extra-p"
54import * as path from "path"
65import { getTempName } from "temp-file"
76import { statOrNull } from "./fs"
8- import { httpExecutor } from "./nodeHttpExecutor"
97import { debug7zArgs , getCacheDirectory , log , spawn } from "./util"
108
11- const versionToPromise = new Map < string , BluebirdPromise < string > > ( )
12-
13- export function getBinFromBintray ( name : string , version : string , sha2 : string ) : Promise < string > {
14- const dirName = `${ name } -${ version } `
15- return getBin ( name , dirName , `https://dl.bintray.com/electron-userland/bin/${ dirName } .7z` , sha2 )
16- }
9+ const versionToPromise = new Map < string , Promise < string > > ( )
1710
1811export function getBinFromGithub ( name : string , version : string , checksum : string ) : Promise < string > {
1912 const dirName = `${ name } -${ version } `
@@ -23,15 +16,23 @@ export function getBinFromGithub(name: string, version: string, checksum: string
2316export function getBin ( name : string , dirName : string , url : string , checksum : string ) : Promise < string > {
2417 let promise = versionToPromise . get ( dirName )
2518 // if rejected, we will try to download again
26- if ( promise != null && ! promise . isRejected ( ) ) {
19+ if ( promise != null ) {
2720 return promise
2821 }
2922
30- promise = doGetBin ( name , dirName , url , checksum ) as BluebirdPromise < string >
23+ promise = doGetBin ( name , dirName , url , checksum )
3124 versionToPromise . set ( dirName , promise )
3225 return promise
3326}
3427
28+ export function download ( url : string , output : string , checksum ?: string | null ) : Promise < void > {
29+ const args = [ "download" , "--url" , url , "--output" , output ]
30+ if ( checksum != null ) {
31+ args . push ( "--sha512" , checksum )
32+ }
33+ return spawn ( appBuilderPath , args )
34+ }
35+
3536// we cache in the global location - in the home dir, not in the node_modules/.cache (https://www.npmjs.com/package/find-cache-dir) because
3637// * don't need to find node_modules
3738// * don't pollute user project dir (important in case of 1-package.json project structure)
@@ -55,42 +56,12 @@ async function doGetBin(name: string, dirName: string, url: string, checksum: st
5556 const archiveName = `${ tempUnpackDir } .7z`
5657 // 7z doesn't create out dir, so, we don't create dir in parallel to download - dir creation will create parent dirs for archive file also
5758 await emptyDir ( tempUnpackDir )
58- const options : DownloadOptions = {
59- skipDirCreation : true ,
60- cancellationToken : new CancellationToken ( ) ,
61- }
62-
63- if ( checksum . length === 64 && ! checksum . includes ( "+" ) && ! checksum . includes ( "Z" ) && ! checksum . includes ( "=" ) ) {
64- ( options as any ) . sha2 = checksum
65- }
66- else {
67- ( options as any ) . sha512 = checksum
68- }
69-
70- for ( let attemptNumber = 1 ; attemptNumber < 4 ; attemptNumber ++ ) {
71- try {
72- await httpExecutor . download ( url , archiveName , options )
73- }
74- catch ( e ) {
75- if ( attemptNumber >= 3 ) {
76- throw e
77- }
78-
79- log . warn ( { ...logFlags , attempt : attemptNumber } , `cannot download: ${ e } ` )
80- await new BluebirdPromise ( ( resolve , reject ) => {
81- setTimeout ( ( ) =>
82- httpExecutor
83- . download ( url , archiveName , options )
84- . then ( resolve ) . catch ( reject ) , 1000 * attemptNumber )
85- } )
86- }
87- }
88-
59+ await download ( url , archiveName , checksum )
8960 await spawn ( path7za , debug7zArgs ( "x" ) . concat ( archiveName , `-o${ tempUnpackDir } ` ) , {
9061 cwd : cachePath ,
9162 } )
9263
93- await BluebirdPromise . all ( [
64+ await Promise . all ( [
9465 rename ( tempUnpackDir , dirPath )
9566 . catch ( e => log . debug ( { ...logFlags , tempUnpackDir, e} , `cannot move downloaded into final location (another process downloaded faster?)` ) ) ,
9667 unlink ( archiveName ) ,
0 commit comments