@@ -26,9 +26,12 @@ type SupportedPlatforms = 'darwin'|'linux'|'win32'|'wsl';
26
26
27
27
const instances = new Set < Launcher > ( ) ;
28
28
29
+ type JSONLike = | { [ property : string ] : JSONLike } | readonly JSONLike [ ] | string | number | boolean | null ;
30
+
29
31
export interface Options {
30
32
startingUrl ?: string ;
31
33
chromeFlags ?: Array < string > ;
34
+ prefs ?: Record < string , JSONLike > ;
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 , JSONLike > ;
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,8 @@ 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
+ this . setBrowserPrefs ( ) ;
206
+
200
207
// fix for Node4
201
208
// you can't pass a fd to fs.writeFileSync
202
209
this . pidFile = `${ this . userDataDir } /chrome.pid` ;
@@ -206,6 +213,28 @@ class Launcher {
206
213
this . tmpDirandPidFileReady = true ;
207
214
}
208
215
216
+ private setBrowserPrefs ( ) {
217
+ // don't set prefs if not defined
218
+ if ( Object . keys ( this . prefs ) . length === 0 ) {
219
+ return ;
220
+ }
221
+
222
+ const preferenceFile = `${ this . userDataDir } /Preferences` ;
223
+ try {
224
+ if ( this . fs . existsSync ( preferenceFile ) ) {
225
+ // overwrite existing file
226
+ const file = this . fs . readFileSync ( preferenceFile , 'utf-8' ) ;
227
+ const content = JSON . parse ( file ) ;
228
+ this . fs . writeFileSync ( preferenceFile , JSON . stringify ( { ...content , ...this . prefs } ) , 'utf-8' ) ;
229
+ } else {
230
+ // create new Preference file
231
+ this . fs . writeFileSync ( preferenceFile , JSON . stringify ( { ...this . prefs } ) , 'utf-8' ) ;
232
+ }
233
+ } catch ( err ) {
234
+ log . log ( 'ChromeLauncher' , `Failed to set browser prefs: ${ err . message } ` ) ;
235
+ }
236
+ }
237
+
209
238
async launch ( ) {
210
239
if ( this . requestedPort !== 0 ) {
211
240
this . port = this . requestedPort ;
@@ -259,7 +288,9 @@ class Launcher {
259
288
{ detached : true , stdio : [ 'ignore' , this . outFile , this . errFile ] , env : this . envVars } ) ;
260
289
this . chrome = chrome ;
261
290
262
- this . fs . writeFileSync ( this . pidFile , chrome . pid . toString ( ) ) ;
291
+ if ( chrome . pid ) {
292
+ this . fs . writeFileSync ( this . pidFile , chrome . pid . toString ( ) ) ;
293
+ }
263
294
264
295
log . verbose ( 'ChromeLauncher' , `Chrome running with pid ${ chrome . pid } on port ${ this . port } .` ) ;
265
296
return chrome . pid ;
@@ -347,7 +378,9 @@ class Launcher {
347
378
// if you don't explicitly set `stdio`
348
379
execSync ( `taskkill /pid ${ this . chrome . pid } /T /F` , { stdio : 'pipe' } ) ;
349
380
} else {
350
- process . kill ( - this . chrome . pid ) ;
381
+ if ( this . chrome . pid ) {
382
+ process . kill ( - this . chrome . pid ) ;
383
+ }
351
384
}
352
385
} catch ( err ) {
353
386
const message = `Chrome could not be killed ${ err . message } ` ;
0 commit comments