@@ -26,6 +26,7 @@ import * as browserFetcher from './browserFetcher';
26
26
const fsMkdirAsync = util . promisify ( fs . mkdir . bind ( fs ) ) ;
27
27
const fsReaddirAsync = util . promisify ( fs . readdir . bind ( fs ) ) ;
28
28
const fsReadFileAsync = util . promisify ( fs . readFile . bind ( fs ) ) ;
29
+ const fsExistsAsync = ( filePath : string ) => fsReadFileAsync ( filePath ) . then ( ( ) => true ) . catch ( e => false ) ;
29
30
const fsUnlinkAsync = util . promisify ( fs . unlink . bind ( fs ) ) ;
30
31
const fsWriteFileAsync = util . promisify ( fs . writeFile . bind ( fs ) ) ;
31
32
const removeFolderAsync = util . promisify ( removeFolder ) ;
@@ -44,15 +45,24 @@ export async function installBrowsersWithProgressBar(packagePath: string) {
44
45
}
45
46
46
47
async function validateCache ( packagePath : string , browsersPath : string , linksDir : string ) {
47
- // 1. Collect unused downloads and package descriptors.
48
- const allBrowsers : browserPaths . BrowserDescriptor [ ] = [ ] ;
48
+ // 1. Collect used downloads and package descriptors.
49
+ const usedBrowserPaths : Set < string > = new Set ( ) ;
49
50
for ( const fileName of await fsReaddirAsync ( linksDir ) ) {
50
51
const linkPath = path . join ( linksDir , fileName ) ;
51
52
let linkTarget = '' ;
52
53
try {
53
54
linkTarget = ( await fsReadFileAsync ( linkPath ) ) . toString ( ) ;
54
55
const browsers = JSON . parse ( ( await fsReadFileAsync ( path . join ( linkTarget , 'browsers.json' ) ) ) . toString ( ) ) [ 'browsers' ] ;
55
- allBrowsers . push ( ...browsers ) ;
56
+ for ( const browser of browsers ) {
57
+ const usedBrowserPath = browserPaths . browserDirectory ( browsersPath , browser ) ;
58
+ const browserRevision = parseInt ( browser . revision , 10 ) ;
59
+ // Old browser installations don't have marker file.
60
+ const shouldHaveMarkerFile = ( browser . name === 'chromium' && browserRevision >= 786218 ) ||
61
+ ( browser . name === 'firefox' && browserRevision >= 1128 ) ||
62
+ ( browser . name === 'webkit' && browserRevision >= 1307 ) ;
63
+ if ( ! shouldHaveMarkerFile || ( await fsExistsAsync ( browserPaths . markerFilePath ( browsersPath , browser ) ) ) )
64
+ usedBrowserPaths . add ( usedBrowserPath ) ;
65
+ }
56
66
} catch ( e ) {
57
67
if ( linkTarget )
58
68
logPolitely ( 'Failed to process descriptor at ' + linkTarget ) ;
@@ -64,8 +74,8 @@ async function validateCache(packagePath: string, browsersPath: string, linksDir
64
74
let downloadedBrowsers = ( await fsReaddirAsync ( browsersPath ) ) . map ( file => path . join ( browsersPath , file ) ) ;
65
75
downloadedBrowsers = downloadedBrowsers . filter ( file => browserPaths . isBrowserDirectory ( file ) ) ;
66
76
const directories = new Set < string > ( downloadedBrowsers ) ;
67
- for ( const browser of allBrowsers )
68
- directories . delete ( browserPaths . browserDirectory ( browsersPath , browser ) ) ;
77
+ for ( const browserPath of usedBrowserPaths )
78
+ directories . delete ( browserPath ) ;
69
79
for ( const directory of directories ) {
70
80
logPolitely ( 'Removing unused browser at ' + directory ) ;
71
81
await removeFolderAsync ( directory ) . catch ( e => { } ) ;
@@ -76,6 +86,7 @@ async function validateCache(packagePath: string, browsersPath: string, linksDir
76
86
for ( const browser of myBrowsers ) {
77
87
const browserPath = browserPaths . browserDirectory ( browsersPath , browser ) ;
78
88
await browserFetcher . downloadBrowserWithProgressBar ( browserPath , browser ) ;
89
+ await fsWriteFileAsync ( browserPaths . markerFilePath ( browsersPath , browser ) , '' ) ;
79
90
}
80
91
}
81
92
0 commit comments