1
1
import { promisify } from 'util' ;
2
+ import * as fs from 'fs' ;
2
3
import * as _ from 'lodash' ;
3
4
import * as rimraf from 'rimraf' ;
4
5
import * as path from 'path' ;
@@ -10,6 +11,13 @@ import { CertCheckServer } from '../cert-check-server';
10
11
import { delay } from '../util' ;
11
12
12
13
const deleteFolder = promisify ( rimraf ) ;
14
+ const readFile = promisify ( fs . readFile ) ;
15
+ const canAccess = ( path : string ) =>
16
+ new Promise ( ( resolve ) => {
17
+ fs . access ( path , ( error : Error | null ) => resolve ( ! error ) ) ;
18
+ } ) ;
19
+
20
+ const FIREFOX_PREF_REGEX = / \w + _ p r e f \( " ( [ ^ " ] + ) " , ( .* ) \) ; /
13
21
14
22
let browsers : _ . Dictionary < BrowserInstance > = { } ;
15
23
@@ -40,13 +48,34 @@ export class FreshFirefox {
40
48
41
49
const firefoxProfile = path . join ( this . config . configPath , 'firefox-profile' ) ;
42
50
51
+ let existingPrefs : _ . Dictionary < any > = { }
52
+
53
+ if ( await canAccess ( firefoxProfile ) ) {
54
+ // If the profile exists, then we've run this before and not deleted the profile,
55
+ // so it probably worked. If so, reuse the existing preferences to avoids issues
56
+ // where on pref setup firefox behaves badly (opening a 2nd window) on OSX.
57
+ const prefContents = await readFile ( path . join ( firefoxProfile , 'prefs.js' ) , {
58
+ encoding : 'utf8'
59
+ } ) ;
60
+
61
+ existingPrefs = _ ( prefContents )
62
+ . split ( '\n' )
63
+ . reduce ( ( prefs : _ . Dictionary < any > , line ) => {
64
+ const match = FIREFOX_PREF_REGEX . exec ( line ) ;
65
+ if ( match ) {
66
+ prefs [ match [ 1 ] ] = match [ 2 ] ;
67
+ }
68
+ return prefs
69
+ } , { } ) ;
70
+ }
71
+
43
72
const browser = await launchBrowser ( certCheckServer . checkCertUrl , {
44
73
browser : 'firefox' ,
45
74
profile : firefoxProfile ,
46
75
proxy : `localhost:${ proxyPort } ` ,
47
76
// Don't intercept our cert testing requests
48
77
noProxy : certCheckServer . host ,
49
- prefs : {
78
+ prefs : _ . assign ( existingPrefs , {
50
79
// By default james-launcher only configures HTTP, so we need to add HTTPS:
51
80
'network.proxy.ssl' : '"localhost"' ,
52
81
'network.proxy.ssl_port' : proxyPort ,
@@ -76,7 +105,7 @@ export class FreshFirefox {
76
105
"datareporting.policy.dataSubmissionEnabled" : false ,
77
106
"datareporting.policy.dataSubmissionPolicyAccepted" : false ,
78
107
"datareporting.policy.dataSubmissionPolicyBypassNotification" : true
79
- }
108
+ } )
80
109
} , this . config . configPath ) ;
81
110
82
111
let success = false ;
0 commit comments