1
1
import { 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"
4
3
import { emptyDir , rename , unlink } from "fs-extra-p"
5
4
import * as path from "path"
6
5
import { getTempName } from "temp-file"
7
6
import { statOrNull } from "./fs"
8
- import { httpExecutor } from "./nodeHttpExecutor"
9
7
import { debug7zArgs , getCacheDirectory , log , spawn } from "./util"
10
8
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 > > ( )
17
10
18
11
export function getBinFromGithub ( name : string , version : string , checksum : string ) : Promise < string > {
19
12
const dirName = `${ name } -${ version } `
@@ -23,15 +16,23 @@ export function getBinFromGithub(name: string, version: string, checksum: string
23
16
export function getBin ( name : string , dirName : string , url : string , checksum : string ) : Promise < string > {
24
17
let promise = versionToPromise . get ( dirName )
25
18
// if rejected, we will try to download again
26
- if ( promise != null && ! promise . isRejected ( ) ) {
19
+ if ( promise != null ) {
27
20
return promise
28
21
}
29
22
30
- promise = doGetBin ( name , dirName , url , checksum ) as BluebirdPromise < string >
23
+ promise = doGetBin ( name , dirName , url , checksum )
31
24
versionToPromise . set ( dirName , promise )
32
25
return promise
33
26
}
34
27
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
+
35
36
// 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
36
37
// * don't need to find node_modules
37
38
// * 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
55
56
const archiveName = `${ tempUnpackDir } .7z`
56
57
// 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
57
58
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 )
89
60
await spawn ( path7za , debug7zArgs ( "x" ) . concat ( archiveName , `-o${ tempUnpackDir } ` ) , {
90
61
cwd : cachePath ,
91
62
} )
92
63
93
- await BluebirdPromise . all ( [
64
+ await Promise . all ( [
94
65
rename ( tempUnpackDir , dirPath )
95
66
. catch ( e => log . debug ( { ...logFlags , tempUnpackDir, e} , `cannot move downloaded into final location (another process downloaded faster?)` ) ) ,
96
67
unlink ( archiveName ) ,
0 commit comments