@@ -10,7 +10,7 @@ import {join} from 'path';
10
10
import { cosmiconfigSync } from 'cosmiconfig' ;
11
11
import type { Configuration , SupportedBrowser } from 'puppeteer-core' ;
12
12
13
- function getBooleanEnvVar ( name : string ) {
13
+ function getBooleanEnvVar ( name : string ) : boolean | undefined {
14
14
const env = process . env [ name ] ;
15
15
if ( env === undefined ) {
16
16
return ;
@@ -29,7 +29,7 @@ function getBooleanEnvVar(name: string) {
29
29
/**
30
30
* @internal
31
31
*/
32
- function isSupportedProduct ( product : unknown ) : product is SupportedBrowser {
32
+ function isSupportedBrowser ( product : unknown ) : product is SupportedBrowser {
33
33
switch ( product ) {
34
34
case 'chrome' :
35
35
case 'firefox' :
@@ -39,6 +39,36 @@ function isSupportedProduct(product: unknown): product is SupportedBrowser {
39
39
}
40
40
}
41
41
42
+ /**
43
+ * @internal
44
+ */
45
+ function getDefaultBrowser ( browser : unknown ) : SupportedBrowser {
46
+ // Validate configuration.
47
+ if ( browser && ! isSupportedBrowser ( browser ) ) {
48
+ throw new Error ( `Unsupported browser ${ browser } ` ) ;
49
+ }
50
+ switch ( browser ) {
51
+ case 'firefox' :
52
+ return 'firefox' ;
53
+ default :
54
+ return 'chrome' ;
55
+ }
56
+ }
57
+
58
+ /**
59
+ * @internal
60
+ */
61
+ function getLogLevel ( logLevel : unknown ) : 'silent' | 'error' | 'warn' {
62
+ switch ( logLevel ) {
63
+ case 'silent' :
64
+ return 'silent' ;
65
+ case 'error' :
66
+ return 'error' ;
67
+ default :
68
+ return 'warn' ;
69
+ }
70
+ }
71
+
42
72
/**
43
73
* @internal
44
74
*/
@@ -48,114 +78,56 @@ export const getConfiguration = (): Configuration => {
48
78
} ) . search ( ) ;
49
79
const configuration : Configuration = result ? result . config : { } ;
50
80
51
- configuration . logLevel = ( process . env [ 'PUPPETEER_LOGLEVEL' ] ??
52
- process . env [ 'npm_config_LOGLEVEL' ] ??
53
- process . env [ 'npm_package_config_LOGLEVEL' ] ??
54
- configuration . logLevel ??
55
- 'warn' ) as 'silent' | 'error' | 'warn' ;
81
+ configuration . logLevel = getLogLevel (
82
+ process . env [ 'PUPPETEER_LOGLEVEL' ] ?? configuration . logLevel
83
+ ) ;
56
84
57
85
// Merging environment variables.
58
- configuration . defaultBrowser = ( process . env [ 'PUPPETEER_BROWSER' ] ??
59
- process . env [ 'npm_config_puppeteer_browser' ] ??
60
- process . env [ 'npm_package_config_puppeteer_browser' ] ??
61
- configuration . defaultBrowser ??
62
- 'chrome' ) as SupportedBrowser ;
86
+ configuration . defaultBrowser = getDefaultBrowser (
87
+ process . env [ 'PUPPETEER_BROWSER' ] ?? configuration . defaultBrowser
88
+ ) ;
63
89
64
90
configuration . executablePath =
65
- process . env [ 'PUPPETEER_EXECUTABLE_PATH' ] ??
66
- process . env [ 'npm_config_puppeteer_executable_path' ] ??
67
- process . env [ 'npm_package_config_puppeteer_executable_path' ] ??
68
- configuration . executablePath ;
91
+ process . env [ 'PUPPETEER_EXECUTABLE_PATH' ] ?? configuration . executablePath ;
69
92
70
93
// Default to skipDownload if executablePath is set
71
94
if ( configuration . executablePath ) {
72
95
configuration . skipDownload = true ;
73
96
}
74
97
75
98
// Set skipDownload explicitly or from default
76
- configuration . skipDownload = Boolean (
77
- getBooleanEnvVar ( 'PUPPETEER_SKIP_DOWNLOAD' ) ??
78
- getBooleanEnvVar ( 'npm_config_puppeteer_skip_download' ) ??
79
- getBooleanEnvVar ( 'npm_package_config_puppeteer_skip_download' ) ??
80
- configuration . skipDownload
81
- ) ;
99
+ configuration . skipDownload =
100
+ getBooleanEnvVar ( 'PUPPETEER_SKIP_DOWNLOAD' ) ?? configuration . skipDownload ;
82
101
83
102
// Set skipChromeDownload explicitly or from default
84
- configuration . skipChromeDownload = Boolean (
103
+ configuration . skipChromeDownload =
85
104
getBooleanEnvVar ( 'PUPPETEER_SKIP_CHROME_DOWNLOAD' ) ??
86
- getBooleanEnvVar ( 'npm_config_puppeteer_skip_chrome_download' ) ??
87
- getBooleanEnvVar ( 'npm_package_config_puppeteer_skip_chrome_download' ) ??
88
- configuration . skipChromeDownload
89
- ) ;
105
+ configuration . skipChromeDownload ;
90
106
91
107
// Set skipChromeDownload explicitly or from default
92
- configuration . skipChromeHeadlessShellDownload = Boolean (
108
+ configuration . skipChromeHeadlessShellDownload =
93
109
getBooleanEnvVar ( 'PUPPETEER_SKIP_CHROME_HEADLESS_SHELL_DOWNLOAD' ) ??
94
- getBooleanEnvVar (
95
- 'npm_config_puppeteer_skip_chrome_headless_shell_download'
96
- ) ??
97
- getBooleanEnvVar (
98
- 'npm_package_config_puppeteer_skip_chrome_headless_shell_download'
99
- ) ??
100
- configuration . skipChromeHeadlessShellDownload
101
- ) ;
110
+ configuration . skipChromeHeadlessShellDownload ;
102
111
103
112
// Prepare variables used in browser downloading
104
113
if ( ! configuration . skipDownload ) {
105
114
configuration . browserRevision =
106
115
process . env [ 'PUPPETEER_BROWSER_REVISION' ] ??
107
- process . env [ 'npm_config_puppeteer_browser_revision' ] ??
108
- process . env [ 'npm_package_config_puppeteer_browser_revision' ] ??
109
116
configuration . browserRevision ;
110
117
111
- const downloadHost =
112
- process . env [ 'PUPPETEER_DOWNLOAD_HOST' ] ??
113
- process . env [ 'npm_config_puppeteer_download_host' ] ??
114
- process . env [ 'npm_package_config_puppeteer_download_host' ] ;
115
-
116
- if ( downloadHost && configuration . logLevel === 'warn' ) {
117
- console . warn (
118
- `PUPPETEER_DOWNLOAD_HOST is deprecated. Use PUPPETEER_DOWNLOAD_BASE_URL instead.`
119
- ) ;
120
- }
121
-
122
118
configuration . downloadBaseUrl =
123
119
process . env [ 'PUPPETEER_DOWNLOAD_BASE_URL' ] ??
124
- process . env [ 'npm_config_puppeteer_download_base_url' ] ??
125
- process . env [ 'npm_package_config_puppeteer_download_base_url' ] ??
126
- configuration . downloadBaseUrl ??
127
- downloadHost ;
128
- }
129
-
130
- if (
131
- Object . keys ( process . env ) . some ( key => {
132
- return key . startsWith ( 'npm_package_config_puppeteer_' ) ;
133
- } ) &&
134
- configuration . logLevel === 'warn'
135
- ) {
136
- console . warn (
137
- `Configuring Puppeteer via npm/package.json is deprecated. Use https://pptr.dev/guides/configuration instead.`
138
- ) ;
120
+ configuration . downloadBaseUrl ;
139
121
}
140
122
141
123
configuration . cacheDirectory =
142
124
process . env [ 'PUPPETEER_CACHE_DIR' ] ??
143
- process . env [ 'npm_config_puppeteer_cache_dir' ] ??
144
- process . env [ 'npm_package_config_puppeteer_cache_dir' ] ??
145
125
configuration . cacheDirectory ??
146
126
join ( homedir ( ) , '.cache' , 'puppeteer' ) ;
147
127
configuration . temporaryDirectory =
148
- process . env [ 'PUPPETEER_TMP_DIR' ] ??
149
- process . env [ 'npm_config_puppeteer_tmp_dir' ] ??
150
- process . env [ 'npm_package_config_puppeteer_tmp_dir' ] ??
151
- configuration . temporaryDirectory ;
128
+ process . env [ 'PUPPETEER_TMP_DIR' ] ?? configuration . temporaryDirectory ;
152
129
153
130
configuration . experiments ??= { } ;
154
131
155
- // Validate configuration.
156
- if ( ! isSupportedProduct ( configuration . defaultBrowser ) ) {
157
- throw new Error ( `Unsupported product ${ configuration . defaultBrowser } ` ) ;
158
- }
159
-
160
132
return configuration ;
161
133
} ;
0 commit comments