1
1
import { statOrNull , spawn , debug , debug7z } from "./util"
2
- import { readdir , mkdirs , move , remove } from "fs-extra-p"
2
+ import { rename , remove } from "fs-extra-p"
3
3
import { download } from "./httpRequest"
4
4
import { path7za } from "7zip-bin"
5
5
import * as path from "path"
6
6
import { homedir } from "os"
7
7
import { Promise as BluebirdPromise } from "bluebird"
8
+ import { writeFile } from "fs"
8
9
9
10
//noinspection JSUnusedLocalSymbols
10
11
const __awaiter = require ( "./awaiter" )
@@ -47,9 +48,6 @@ async function doDownloadFpm(version: string, osAndArch: string): Promise<string
47
48
return path . join ( fpmDir , "fpm" )
48
49
}
49
50
50
- // the only version currently supported (i.e. all clients are consumed the same version
51
- await emptyDir ( cacheDir , dirName )
52
-
53
51
// 7z cannot be extracted from the input stream, temp file is required
54
52
const tempName = getTempName ( )
55
53
const archiveName = path . join ( cacheDir , tempName + ".7z" )
@@ -68,27 +66,18 @@ async function doDownloadFpm(version: string, osAndArch: string): Promise<string
68
66
stdio : [ "ignore" , debug . enabled ? "inherit" : "ignore" , "inherit" ] ,
69
67
} )
70
68
71
- await BluebirdPromise . all ( [ move ( path . join ( tempUnpackDir , dirName ) , fpmDir , { clobber : true } ) , remove ( archiveName ) ] )
72
- await remove ( tempUnpackDir )
69
+ await BluebirdPromise . all < any > ( [
70
+ rename ( path . join ( tempUnpackDir , dirName ) , fpmDir )
71
+ . catch ( e => {
72
+ console . warn ( "Cannot move downloaded fpm into final location (another process downloaded faster?): " + e )
73
+ } ) ,
74
+ remove ( archiveName ) ,
75
+ ] )
76
+ await BluebirdPromise . all ( [
77
+ remove ( tempUnpackDir ) ,
78
+ writeFile ( path . join ( fpmDir , ".lastUsed" ) , Date . now ( ) . toString ( ) )
79
+ ] )
73
80
74
81
debug ( `fpm downloaded to ${ fpmDir } ` )
75
82
return path . join ( fpmDir , "fpm" )
76
- }
77
-
78
- // prefix to not delete dir or archived dir (.7z)
79
- async function emptyDir ( dir : string , excludeNamePrefix : string ) {
80
- let items : string [ ] | null = null
81
- try {
82
- items = await readdir ( dir )
83
- }
84
- catch ( e ) {
85
- await mkdirs ( dir )
86
- return
87
- }
88
-
89
- items = items !
90
- . filter ( it => ! it . startsWith ( excludeNamePrefix ) )
91
- . map ( it => path . join ( dir , it ) )
92
-
93
- await BluebirdPromise . map ( items , remove )
94
83
}
0 commit comments