@@ -28,7 +28,7 @@ const fsReaddirAsync = util.promisify(fs.readdir.bind(fs));
28
28
const fsReadFileAsync = util . promisify ( fs . readFile . bind ( fs ) ) ;
29
29
const fsUnlinkAsync = util . promisify ( fs . unlink . bind ( fs ) ) ;
30
30
const fsWriteFileAsync = util . promisify ( fs . writeFile . bind ( fs ) ) ;
31
- const removeFolderAsync = util . promisify ( removeFolder ) ;
31
+ const rmAsync = util . promisify ( removeFolder ) ;
32
32
33
33
export async function installBrowsersWithProgressBar ( packagePath : string ) {
34
34
const browsersPath = browserPaths . browsersPath ( packagePath ) ;
@@ -60,18 +60,27 @@ async function validateCache(packagePath: string, browsersPath: string, linksDir
60
60
}
61
61
}
62
62
63
- // 2. Delete all unused browsers.
63
+ // 2. Delete all stale browser extract directories and .zip files.
64
+ // NOTE: this must not run concurrently with other installations.
65
+ let staleFiles = ( await fsReaddirAsync ( browsersPath ) ) . map ( file => path . join ( browsersPath , file ) ) ;
66
+ staleFiles = staleFiles . filter ( file => browserPaths . isBrowserZipFile ( file ) || browserPaths . isBrowserExtractDirectory ( file ) ) ;
67
+ for ( const staleFile of staleFiles ) {
68
+ logPolitely ( 'Removing leftover from interrupted installation ' + staleFile ) ;
69
+ await rmAsync ( staleFile ) . catch ( e => { } ) ;
70
+ }
71
+
72
+ // 3. Delete all unused browsers.
64
73
let downloadedBrowsers = ( await fsReaddirAsync ( browsersPath ) ) . map ( file => path . join ( browsersPath , file ) ) ;
65
74
downloadedBrowsers = downloadedBrowsers . filter ( file => browserPaths . isBrowserDirectory ( file ) ) ;
66
75
const directories = new Set < string > ( downloadedBrowsers ) ;
67
76
for ( const browser of allBrowsers )
68
77
directories . delete ( browserPaths . browserDirectory ( browsersPath , browser ) ) ;
69
78
for ( const directory of directories ) {
70
79
logPolitely ( 'Removing unused browser at ' + directory ) ;
71
- await removeFolderAsync ( directory ) . catch ( e => { } ) ;
80
+ await rmAsync ( directory ) . catch ( e => { } ) ;
72
81
}
73
82
74
- // 3 . Install missing browsers for this package.
83
+ // 4 . Install missing browsers for this package.
75
84
const myBrowsers = JSON . parse ( ( await fsReadFileAsync ( path . join ( packagePath , 'browsers.json' ) ) ) . toString ( ) ) [ 'browsers' ] as browserPaths . BrowserDescriptor [ ] ;
76
85
for ( const browser of myBrowsers ) {
77
86
const browserPath = browserPaths . browserDirectory ( browsersPath , browser ) ;
0 commit comments