11import { promisify } from 'util' ;
2+ import * as fs from 'fs' ;
23import * as _ from 'lodash' ;
34import * as rimraf from 'rimraf' ;
45import * as path from 'path' ;
@@ -10,6 +11,13 @@ import { CertCheckServer } from '../cert-check-server';
1011import { delay } from '../util' ;
1112
1213const 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 \( " ( [ ^ " ] + ) " , ( .* ) \) ; /
1321
1422let browsers : _ . Dictionary < BrowserInstance > = { } ;
1523
@@ -40,13 +48,34 @@ export class FreshFirefox {
4048
4149 const firefoxProfile = path . join ( this . config . configPath , 'firefox-profile' ) ;
4250
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+
4372 const browser = await launchBrowser ( certCheckServer . checkCertUrl , {
4473 browser : 'firefox' ,
4574 profile : firefoxProfile ,
4675 proxy : `localhost:${ proxyPort } ` ,
4776 // Don't intercept our cert testing requests
4877 noProxy : certCheckServer . host ,
49- prefs : {
78+ prefs : _ . assign ( existingPrefs , {
5079 // By default james-launcher only configures HTTP, so we need to add HTTPS:
5180 'network.proxy.ssl' : '"localhost"' ,
5281 'network.proxy.ssl_port' : proxyPort ,
@@ -76,7 +105,7 @@ export class FreshFirefox {
76105 "datareporting.policy.dataSubmissionEnabled" : false ,
77106 "datareporting.policy.dataSubmissionPolicyAccepted" : false ,
78107 "datareporting.policy.dataSubmissionPolicyBypassNotification" : true
79- }
108+ } )
80109 } , this . config . configPath ) ;
81110
82111 let success = false ;
0 commit comments