@@ -26,9 +26,12 @@ type SupportedPlatforms = 'darwin'|'linux'|'win32'|'wsl';
26
26
27
27
const instances = new Set < Launcher > ( ) ;
28
28
29
+ type ValidPrefTypes = string | boolean | number
30
+
29
31
export interface Options {
30
32
startingUrl ?: string ;
31
33
chromeFlags ?: Array < string > ;
34
+ prefs ?: Record < string , ValidPrefTypes > ;
32
35
port ?: number ;
33
36
handleSIGINT ?: boolean ;
34
37
chromePath ?: string ;
@@ -105,6 +108,7 @@ class Launcher {
105
108
private chromePath ?: string ;
106
109
private ignoreDefaultFlags ?: boolean ;
107
110
private chromeFlags : string [ ] ;
111
+ private prefs : Record < string , ValidPrefTypes > ;
108
112
private requestedPort ?: number ;
109
113
private connectionPollInterval : number ;
110
114
private maxConnectionRetries : number ;
@@ -127,6 +131,7 @@ class Launcher {
127
131
// choose the first one (default)
128
132
this . startingUrl = defaults ( this . opts . startingUrl , 'about:blank' ) ;
129
133
this . chromeFlags = defaults ( this . opts . chromeFlags , [ ] ) ;
134
+ this . prefs = defaults ( this . opts . prefs , { } ) ;
130
135
this . requestedPort = defaults ( this . opts . port , 0 ) ;
131
136
this . chromePath = this . opts . chromePath ;
132
137
this . ignoreDefaultFlags = defaults ( this . opts . ignoreDefaultFlags , false ) ;
@@ -197,6 +202,11 @@ class Launcher {
197
202
this . outFile = this . fs . openSync ( `${ this . userDataDir } /chrome-out.log` , 'a' ) ;
198
203
this . errFile = this . fs . openSync ( `${ this . userDataDir } /chrome-err.log` , 'a' ) ;
199
204
205
+ // set preferences
206
+ if ( Object . keys ( this . prefs ) . length ) {
207
+ this . setBrowserPrefs ( )
208
+ }
209
+
200
210
// fix for Node4
201
211
// you can't pass a fd to fs.writeFileSync
202
212
this . pidFile = `${ this . userDataDir } /chrome.pid` ;
@@ -206,6 +216,23 @@ class Launcher {
206
216
this . tmpDirandPidFileReady = true ;
207
217
}
208
218
219
+ private setBrowserPrefs ( ) {
220
+ const preferenceFile = `${ this . userDataDir } /Preferences` ;
221
+ try {
222
+ if ( this . fs . existsSync ( preferenceFile ) ) {
223
+ // overwrite existing file
224
+ const file = this . fs . readFileSync ( preferenceFile , 'utf-8' ) ;
225
+ const content = JSON . parse ( file . toString ( ) ) ;
226
+ this . fs . writeFileSync ( preferenceFile , JSON . stringify ( { ...content , ...this . prefs } ) , 'utf-8' ) ;
227
+ } else {
228
+ // create new Preference file
229
+ this . fs . writeFileSync ( preferenceFile , JSON . stringify ( { ...this . prefs } ) , 'utf-8' ) ;
230
+ }
231
+ } catch ( err ) {
232
+ log . log ( 'ChromeLauncher' , `Failed to set browser prefs: ${ err . message } ` ) ;
233
+ }
234
+ }
235
+
209
236
async launch ( ) {
210
237
if ( this . requestedPort !== 0 ) {
211
238
this . port = this . requestedPort ;
@@ -259,7 +286,9 @@ class Launcher {
259
286
{ detached : true , stdio : [ 'ignore' , this . outFile , this . errFile ] , env : this . envVars } ) ;
260
287
this . chrome = chrome ;
261
288
262
- this . fs . writeFileSync ( this . pidFile , chrome . pid . toString ( ) ) ;
289
+ if ( chrome . pid ) {
290
+ this . fs . writeFileSync ( this . pidFile , chrome . pid . toString ( ) ) ;
291
+ }
263
292
264
293
log . verbose ( 'ChromeLauncher' , `Chrome running with pid ${ chrome . pid } on port ${ this . port } .` ) ;
265
294
return chrome . pid ;
@@ -347,7 +376,9 @@ class Launcher {
347
376
// if you don't explicitly set `stdio`
348
377
execSync ( `taskkill /pid ${ this . chrome . pid } /T /F` , { stdio : 'pipe' } ) ;
349
378
} else {
350
- process . kill ( - this . chrome . pid ) ;
379
+ if ( this . chrome . pid ) {
380
+ process . kill ( - this . chrome . pid ) ;
381
+ }
351
382
}
352
383
} catch ( err ) {
353
384
const message = `Chrome could not be killed ${ err . message } ` ;
0 commit comments