11import { statOrNull , spawn , debug , debug7z } from "./util"
2- import { readdir , mkdirs , move , remove } from "fs-extra-p"
2+ import { rename , remove } from "fs-extra-p"
33import { download } from "./httpRequest"
44import { path7za } from "7zip-bin"
55import * as path from "path"
66import { homedir } from "os"
77import { Promise as BluebirdPromise } from "bluebird"
8+ import { writeFile } from "fs"
89
910//noinspection JSUnusedLocalSymbols
1011const __awaiter = require ( "./awaiter" )
@@ -47,9 +48,6 @@ async function doDownloadFpm(version: string, osAndArch: string): Promise<string
4748 return path . join ( fpmDir , "fpm" )
4849 }
4950
50- // the only version currently supported (i.e. all clients are consumed the same version
51- await emptyDir ( cacheDir , dirName )
52-
5351 // 7z cannot be extracted from the input stream, temp file is required
5452 const tempName = getTempName ( )
5553 const archiveName = path . join ( cacheDir , tempName + ".7z" )
@@ -68,27 +66,18 @@ async function doDownloadFpm(version: string, osAndArch: string): Promise<string
6866 stdio : [ "ignore" , debug . enabled ? "inherit" : "ignore" , "inherit" ] ,
6967 } )
7068
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+ ] )
7380
7481 debug ( `fpm downloaded to ${ fpmDir } ` )
7582 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 )
9483}
0 commit comments