@wordpress/env 6.0.0 → 8.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (55) hide show
  1. package/README.md +130 -66
  2. package/lib/build-docker-compose-config.js +67 -96
  3. package/lib/cache.js +1 -0
  4. package/lib/cli.js +39 -10
  5. package/lib/commands/clean.js +13 -1
  6. package/lib/commands/destroy.js +21 -42
  7. package/lib/commands/index.js +1 -0
  8. package/lib/commands/install-path.js +1 -0
  9. package/lib/commands/logs.js +1 -0
  10. package/lib/commands/run.js +45 -21
  11. package/lib/commands/start.js +29 -8
  12. package/lib/commands/stop.js +1 -0
  13. package/lib/config/add-or-replace-port.js +12 -3
  14. package/lib/config/db-env.js +1 -0
  15. package/lib/config/detect-directory-type.js +1 -1
  16. package/lib/config/get-cache-directory.js +39 -0
  17. package/lib/config/get-config-from-environment-vars.js +106 -0
  18. package/lib/config/index.js +7 -5
  19. package/lib/config/load-config.js +92 -0
  20. package/lib/config/merge-configs.js +105 -0
  21. package/lib/config/parse-config.js +501 -157
  22. package/lib/config/parse-source-string.js +164 -0
  23. package/lib/config/post-process-config.js +202 -0
  24. package/lib/config/read-raw-config-file.js +7 -33
  25. package/lib/config/test/__snapshots__/config-integration.js.snap +295 -0
  26. package/lib/config/test/add-or-replace-port.js +29 -1
  27. package/lib/config/test/config-integration.js +164 -0
  28. package/lib/config/test/get-cache-directory.js +57 -0
  29. package/lib/config/test/merge-configs.js +121 -0
  30. package/lib/config/test/parse-config.js +393 -0
  31. package/lib/config/test/parse-source-string.js +154 -0
  32. package/lib/config/test/post-process-config.js +299 -0
  33. package/lib/config/test/read-raw-config-file.js +19 -63
  34. package/lib/config/test/validate-config.js +363 -0
  35. package/lib/config/validate-config.js +119 -54
  36. package/lib/env.js +2 -0
  37. package/lib/execute-lifecycle-script.js +86 -0
  38. package/lib/get-host-user.js +27 -0
  39. package/lib/init-config.js +186 -63
  40. package/lib/md5.js +1 -0
  41. package/lib/parse-xdebug-mode.js +1 -0
  42. package/lib/retry.js +1 -0
  43. package/lib/test/__snapshots__/md5.js.snap +9 -0
  44. package/lib/test/build-docker-compose-config.js +134 -0
  45. package/lib/test/cache.js +154 -0
  46. package/lib/test/cli.js +149 -0
  47. package/lib/test/execute-lifecycle-script.js +59 -0
  48. package/lib/test/md5.js +35 -0
  49. package/lib/test/parse-xdebug-mode.js +41 -0
  50. package/lib/validate-run-container.js +41 -0
  51. package/lib/wordpress.js +4 -19
  52. package/package.json +2 -2
  53. package/lib/config/config.js +0 -353
  54. package/lib/config/test/__snapshots__/config.js.snap +0 -83
  55. package/lib/config/test/config.js +0 -1241
@@ -3,206 +3,550 @@
3
3
  * External dependencies
4
4
  */
5
5
  const path = require( 'path' );
6
- const os = require( 'os' );
7
6
 
8
7
  /**
9
8
  * Internal dependencies
10
9
  */
11
- const { ValidationError } = require( './validate-config' );
10
+ const readRawConfigFile = require( './read-raw-config-file' );
11
+ const {
12
+ parseSourceString,
13
+ includeTestsPath,
14
+ } = require( './parse-source-string' );
15
+ const {
16
+ ValidationError,
17
+ checkPort,
18
+ checkStringArray,
19
+ checkObjectWithValues,
20
+ checkVersion,
21
+ checkValidURL,
22
+ } = require( './validate-config' );
23
+ const getConfigFromEnvironmentVars = require( './get-config-from-environment-vars' );
24
+ const detectDirectoryType = require( './detect-directory-type' );
12
25
  const { getLatestWordPressVersion } = require( '../wordpress' );
26
+ const mergeConfigs = require( './merge-configs' );
13
27
 
14
28
  /**
15
- * @typedef {import('./config').WPServiceConfig} WPServiceConfig
16
- * @typedef {import('./config').WPSource} WPSource
29
+ * @typedef {import('./parse-source-string').WPSource} WPSource
17
30
  */
18
31
 
19
32
  /**
20
- * The string at the beginning of a source path that points to a home-relative
21
- * directory. Will be '~/' on unix environments and '~\' on Windows.
33
+ * The root configuration options.
34
+ *
35
+ * @typedef WPRootConfigOptions
36
+ * @property {number} port The port to use in the development environment.
37
+ * @property {number} testsPort The port to use in the tests environment.
38
+ * @property {Object.<string, string|null>} lifecycleScripts The scripts to run at certain points in the command lifecycle.
39
+ * @property {Object.<string, string|null>} lifecycleScripts.afterStart The script to run after the "start" command has completed.
40
+ * @property {Object.<string, string|null>} lifecycleScripts.afterClean The script to run after the "clean" command has completed.
41
+ * @property {Object.<string, string|null>} lifecycleScripts.afterDestroy The script to run after the "destroy" command has completed.
42
+ * @property {Object.<string, WPEnvironmentConfig>} env The environment-specific configuration options.
43
+ */
44
+
45
+ /**
46
+ * The environment-specific configuration options. (development/tests/etc)
47
+ *
48
+ * @typedef WPEnvironmentConfig
49
+ * @property {WPSource} coreSource The WordPress installation to load in the environment.
50
+ * @property {WPSource[]} pluginSources Plugins to load in the environment.
51
+ * @property {WPSource[]} themeSources Themes to load in the environment.
52
+ * @property {number} port The port to use.
53
+ * @property {Object} config Mapping of wp-config.php constants to their desired values.
54
+ * @property {Object.<string, WPSource>} mappings Mapping of WordPress directories to local directories which should be mounted.
55
+ * @property {string|null} phpVersion Version of PHP to use in the environments, of the format 0.0.
56
+ */
57
+
58
+ /**
59
+ * The root configuration options.
60
+ *
61
+ * @typedef {WPEnvironmentConfig & WPRootConfigOptions} WPRootConfig
62
+ */
63
+
64
+ /**
65
+ * A WordPress installation, plugin or theme to be loaded into the environment.
66
+ *
67
+ * @typedef WPSource
68
+ * @property {'local'|'git'|'zip'} type The source type.
69
+ * @property {string} path The path to the WordPress installation, plugin or theme.
70
+ * @property {?string} url The URL to the source download if the source type is not local.
71
+ * @property {?string} ref The git ref for the source if the source type is 'git'.
72
+ * @property {string} basename Name that identifies the WordPress installation, plugin or theme.
73
+ */
74
+
75
+ /**
76
+ * An object containing all of the default configuration options for environment-specific configurations.
77
+ * Unless otherwise set at the root-level or the environment-level, these are the values that will be
78
+ * parsed into the environment. This is useful for tracking known configuration options since these
79
+ * are the only configuration options that can be set in each environment.
22
80
  */
23
- const HOME_PATH_PREFIX = `~${ path.sep }`;
81
+ const DEFAULT_ENVIRONMENT_CONFIG = {
82
+ core: null,
83
+ phpVersion: null,
84
+ plugins: [],
85
+ themes: [],
86
+ port: 8888,
87
+ testsPort: 8889,
88
+ mappings: {},
89
+ config: {
90
+ FS_METHOD: 'direct',
91
+ WP_DEBUG: true,
92
+ SCRIPT_DEBUG: true,
93
+ WP_ENVIRONMENT_TYPE: 'local',
94
+ WP_PHP_BINARY: 'php',
95
+ WP_TESTS_EMAIL: 'admin@example.org',
96
+ WP_TESTS_TITLE: 'Test Blog',
97
+ WP_TESTS_DOMAIN: 'localhost',
98
+ WP_SITEURL: 'http://localhost',
99
+ WP_HOME: 'http://localhost',
100
+ },
101
+ };
102
+
103
+ /**
104
+ * Given a directory, this parses any relevant config files and
105
+ * constructs an object in the format used internally.
106
+ *
107
+ *
108
+ * @param {string} configDirectoryPath A path to the directory we are parsing the config for.
109
+ * @param {string} cacheDirectoryPath Path to the work directory located in ~/.wp-env.
110
+ *
111
+ * @return {WPRootConfig} Parsed config.
112
+ */
113
+ async function parseConfig( configDirectoryPath, cacheDirectoryPath ) {
114
+ // The local config will be used to override any defaults.
115
+ const localConfig = await parseConfigFile(
116
+ getConfigFilePath( configDirectoryPath ),
117
+ { cacheDirectoryPath }
118
+ );
119
+
120
+ // Any overrides that can be used in place
121
+ // of properties set by the local config.
122
+ const overrideConfig = await parseConfigFile(
123
+ getConfigFilePath( configDirectoryPath, 'override' ),
124
+ { cacheDirectoryPath }
125
+ );
126
+
127
+ // It's important to know whether or not the user
128
+ // has configured the tool using a JSON file.
129
+ const hasUserConfig = localConfig || overrideConfig;
130
+
131
+ // The default config will be used when no local config
132
+ // file is present in this directory. We should also
133
+ // infer the project type when there is no local
134
+ // config file present to use.
135
+ const defaultConfig = await getDefaultConfig( configDirectoryPath, {
136
+ shouldInferType: ! hasUserConfig,
137
+ cacheDirectoryPath,
138
+ } );
139
+
140
+ // Users can provide overrides in environment
141
+ // variables that supercede all other options.
142
+ const environmentVarOverrides =
143
+ getEnvironmentVarOverrides( cacheDirectoryPath );
144
+
145
+ // Merge all of our configs so that we have a complete object
146
+ // containing the desired options in order of precedence.
147
+ return mergeConfigs(
148
+ defaultConfig,
149
+ localConfig ?? {},
150
+ overrideConfig ?? {},
151
+ environmentVarOverrides
152
+ );
153
+ }
154
+
155
+ /**
156
+ * Gets the path to the config file.
157
+ *
158
+ * @param {string} configDirectoryPath The path to the directory containing config files.
159
+ * @param {string} type The type of config file we're interested in: 'local' or 'override'.
160
+ *
161
+ * @return {string} The path to the config file.
162
+ */
163
+ function getConfigFilePath( configDirectoryPath, type = 'local' ) {
164
+ let fileName;
165
+ switch ( type ) {
166
+ case 'local': {
167
+ fileName = '.wp-env.json';
168
+ break;
169
+ }
170
+
171
+ case 'override': {
172
+ fileName = '.wp-env.override.json';
173
+ break;
174
+ }
175
+
176
+ default: {
177
+ throw new Error( `Invalid config file type "${ type }.` );
178
+ }
179
+ }
180
+
181
+ return path.resolve( configDirectoryPath, fileName );
182
+ }
24
183
 
25
184
  /**
26
- * Parses a config object. Takes environment-level configuration in the format
27
- * specified in .wp-env.json, validates it, and converts it into the format used
28
- * internally. For example, `plugins: string[]` will be parsed into
29
- * `pluginSources: WPSource[]`.
185
+ * Gets the default config that can be overridden.
30
186
  *
31
- * @param {Object} config A config object to validate.
187
+ * @param {string} configDirectoryPath A path to the config file's directory.
32
188
  * @param {Object} options
33
- * @param {string} options.workDirectoryPath Path to the work directory located in ~/.wp-env.
34
- * @return {WPServiceConfig} Parsed environment-level configuration.
35
- */
36
- module.exports = async function parseConfig( config, options ) {
37
- return {
38
- port: config.port,
39
- phpVersion: config.phpVersion,
40
- coreSource: includeTestsPath(
41
- await parseCoreSource( config.core, options ),
42
- options
43
- ),
44
- pluginSources: config.plugins.map( ( sourceString ) =>
45
- parseSourceString( sourceString, options )
46
- ),
47
- themeSources: config.themes.map( ( sourceString ) =>
48
- parseSourceString( sourceString, options )
49
- ),
50
- config: config.config,
51
- mappings: Object.entries( config.mappings ).reduce(
52
- ( result, [ wpDir, localDir ] ) => {
53
- const source = parseSourceString( localDir, options );
54
- result[ wpDir ] = source;
55
- return result;
189
+ * @param {string} options.shouldInferType Indicates whether or not we should infer the type of project wp-env is being used in.
190
+ * @param {string} options.cacheDirectoryPath Path to the work directory located in ~/.wp-env.
191
+ *
192
+ * @return {Promise<WPEnvironmentConfig>} The default config object.
193
+ */
194
+ async function getDefaultConfig(
195
+ configDirectoryPath,
196
+ { shouldInferType, cacheDirectoryPath }
197
+ ) {
198
+ const detectedDirectoryType = shouldInferType
199
+ ? await detectDirectoryType( configDirectoryPath )
200
+ : null;
201
+
202
+ // The default configuration should contain all possible options and
203
+ // environments whether they're empty or not. This makes using the
204
+ // config objects easier because once merged we don't need to
205
+ // verify that a given option exists before using it.
206
+ const rawConfig = {
207
+ // Since the root config is the base "environment" config for
208
+ // all environments, we will start with those defaults.
209
+ ...DEFAULT_ENVIRONMENT_CONFIG,
210
+
211
+ // When the current directory has no configuration file we support a zero-config mode of operation.
212
+ // This works by using the default options and inferring how to map the current directory based
213
+ // on the contents of the directory.
214
+ core:
215
+ detectedDirectoryType === 'core'
216
+ ? '.'
217
+ : DEFAULT_ENVIRONMENT_CONFIG.core,
218
+ plugins:
219
+ detectedDirectoryType === 'plugin'
220
+ ? [ '.' ]
221
+ : DEFAULT_ENVIRONMENT_CONFIG.plugins,
222
+ themes:
223
+ detectedDirectoryType === 'theme'
224
+ ? [ '.' ]
225
+ : DEFAULT_ENVIRONMENT_CONFIG.themes,
226
+
227
+ // These configuration options are root-only and should not be present
228
+ // on environment-specific configuration objects.
229
+ lifecycleScripts: {
230
+ afterStart: null,
231
+ afterClean: null,
232
+ afterDestroy: null,
233
+ },
234
+ env: {
235
+ development: {},
236
+ tests: {
237
+ config: {
238
+ WP_DEBUG: false,
239
+ SCRIPT_DEBUG: false,
240
+ },
56
241
  },
57
- {}
58
- ),
242
+ },
59
243
  };
60
- };
61
244
 
62
- async function parseCoreSource( coreSource, options ) {
63
- // An empty source means we should use the latest version of WordPress.
64
- if ( ! coreSource ) {
65
- const wpVersion = await getLatestWordPressVersion();
66
- if ( ! wpVersion ) {
67
- throw new ValidationError(
68
- 'Could not find the latest WordPress version. There may be a network issue.'
69
- );
70
- }
245
+ return await parseRootConfig( 'default', rawConfig, {
246
+ cacheDirectoryPath,
247
+ } );
248
+ }
71
249
 
72
- coreSource = `WordPress/WordPress#${ wpVersion }`;
250
+ /**
251
+ * Gets a service configuration object containing overrides from our environment variables.
252
+ *
253
+ * @param {string} cacheDirectoryPath Path to the work directory located in ~/.wp-env.
254
+ *
255
+ * @return {WPEnvironmentConfig} An object containing the environment variable overrides.
256
+ */
257
+ function getEnvironmentVarOverrides( cacheDirectoryPath ) {
258
+ const overrides = getConfigFromEnvironmentVars( cacheDirectoryPath );
259
+
260
+ // Create a service config object so we can merge it with the others
261
+ // and override anything that the configuration options need to.
262
+ const overrideConfig = {
263
+ lifecycleScripts: overrides.lifecycleScripts,
264
+ env: {
265
+ development: {},
266
+ tests: {},
267
+ },
268
+ };
269
+
270
+ // We're going to take care to set it at both the root-level and the
271
+ // environment level. This is not totally necessary, but, it's a
272
+ // better representation of how broad the override is.
273
+
274
+ if ( overrides.port ) {
275
+ overrideConfig.port = overrides.port;
276
+ overrideConfig.env.development.port = overrides.port;
73
277
  }
74
- return parseSourceString( coreSource, options );
278
+
279
+ if ( overrides.testsPort ) {
280
+ overrideConfig.testsPort = overrides.testsPort;
281
+ overrideConfig.env.tests.port = overrides.testsPort;
282
+ }
283
+
284
+ if ( overrides.coreSource ) {
285
+ overrideConfig.coreSource = overrides.coreSource;
286
+ overrideConfig.env.development.coreSource = overrides.coreSource;
287
+ overrideConfig.env.tests.coreSource = overrides.coreSource;
288
+ }
289
+
290
+ if ( overrides.phpVersion ) {
291
+ overrideConfig.phpVersion = overrides.phpVersion;
292
+ overrideConfig.env.development.phpVersion = overrides.phpVersion;
293
+ overrideConfig.env.tests.phpVersion = overrides.phpVersion;
294
+ }
295
+
296
+ return overrideConfig;
75
297
  }
76
298
 
77
299
  /**
78
- * Parses a source string into a source object.
300
+ * Parses a raw config into an unvalidated service config.
79
301
  *
80
- * @param {?string} sourceString The source string. See README.md for documentation on valid source string patterns.
81
- * @param {Object} options
82
- * @param {string} options.workDirectoryPath Path to the work directory located in ~/.wp-env.
302
+ * @param {string} configFile The config file that we're parsing.
303
+ * @param {Object} options
304
+ * @param {string} options.cacheDirectoryPath Path to the work directory located in ~/.wp-env.
83
305
  *
84
- * @return {?WPSource} A source object.
306
+ * @return {Promise<WPRootConfig|null>} The parsed root config object.
85
307
  */
86
- function parseSourceString( sourceString, { workDirectoryPath } ) {
87
- if ( sourceString === null ) {
308
+ async function parseConfigFile( configFile, options ) {
309
+ const rawConfig = await readRawConfigFile( configFile );
310
+ if ( ! rawConfig ) {
88
311
  return null;
89
312
  }
90
313
 
91
- if (
92
- sourceString.startsWith( '.' ) ||
93
- sourceString.startsWith( HOME_PATH_PREFIX ) ||
94
- path.isAbsolute( sourceString )
95
- ) {
96
- let sourcePath;
97
- if ( sourceString.startsWith( HOME_PATH_PREFIX ) ) {
98
- sourcePath = path.resolve(
99
- os.homedir(),
100
- sourceString.slice( HOME_PATH_PREFIX.length )
314
+ return await parseRootConfig( configFile, rawConfig, options );
315
+ }
316
+
317
+ /**
318
+ * Parses the root config object.
319
+ *
320
+ * @param {string} configFile The config file we're parsing.
321
+ * @param {Object} rawConfig The raw config we're parsing.
322
+ * @param {Object} options
323
+ * @param {string} options.cacheDirectoryPath Path to the work directory located in ~/.wp-env.
324
+ *
325
+ * @return {Promise<WPRootConfig>} The root config object.
326
+ */
327
+ async function parseRootConfig( configFile, rawConfig, options ) {
328
+ const parsedConfig = await parseEnvironmentConfig(
329
+ configFile,
330
+ null,
331
+ rawConfig,
332
+ {
333
+ ...options,
334
+ rootConfig: true,
335
+ }
336
+ );
337
+
338
+ // Parse any root-only options.
339
+ if ( rawConfig.testsPort !== undefined ) {
340
+ checkPort( configFile, `testsPort`, rawConfig.testsPort );
341
+ parsedConfig.testsPort = rawConfig.testsPort;
342
+ }
343
+ parsedConfig.lifecycleScripts = {};
344
+ if ( rawConfig.lifecycleScripts ) {
345
+ checkObjectWithValues(
346
+ configFile,
347
+ 'lifecycleScripts',
348
+ rawConfig.lifecycleScripts,
349
+ [ 'null', 'string' ],
350
+ true
351
+ );
352
+ parsedConfig.lifecycleScripts = rawConfig.lifecycleScripts;
353
+ }
354
+
355
+ // Parse the environment-specific configs so they're accessible to the root.
356
+ parsedConfig.env = {};
357
+ if ( rawConfig.env ) {
358
+ checkObjectWithValues(
359
+ configFile,
360
+ 'env',
361
+ rawConfig.env,
362
+ [ 'object' ],
363
+ false
364
+ );
365
+ for ( const env in rawConfig.env ) {
366
+ parsedConfig.env[ env ] = await parseEnvironmentConfig(
367
+ configFile,
368
+ env,
369
+ rawConfig.env[ env ],
370
+ options
101
371
  );
102
- } else {
103
- sourcePath = path.resolve( sourceString );
104
372
  }
105
- const basename = path.basename( sourcePath );
106
- return {
107
- type: 'local',
108
- path: sourcePath,
109
- basename,
110
- };
111
373
  }
112
374
 
113
- const zipFields = sourceString.match(
114
- /^https?:\/\/([^\s$.?#].[^\s]*)\.zip(\?.+)?$/
115
- );
375
+ return parsedConfig;
376
+ }
116
377
 
117
- if ( zipFields ) {
118
- const wpOrgFields = sourceString.match(
119
- /^https?:\/\/downloads\.wordpress\.org\/(?:plugin|theme)\/([^\s\.]*)([^\s]*)?\.zip$/
378
+ /**
379
+ * Parses and validates a raw config object and returns a validated service config to use internally.
380
+ *
381
+ * @param {string} configFile The config file that we're parsing.
382
+ * @param {string|null} environment If set, the environment that we're parsing the config for.
383
+ * @param {Object} config A config object to parse.
384
+ * @param {Object} options
385
+ * @param {string} options.cacheDirectoryPath Path to the work directory located in ~/.wp-env.
386
+ * @param {boolean} options.rootConfig Indicates whether or not this is the root config object.
387
+ *
388
+ * @return {Promise<WPEnvironmentConfig>} The environment config object.
389
+ */
390
+ async function parseEnvironmentConfig(
391
+ configFile,
392
+ environment,
393
+ config,
394
+ options
395
+ ) {
396
+ if ( ! config ) {
397
+ return {};
398
+ }
399
+
400
+ const environmentPrefix = environment ? environment + '.' : '';
401
+
402
+ // Before we move forward with parsing we should make sure that there aren't any
403
+ // configuration options that do not exist. This helps prevent silent failures
404
+ // when a user sets up their configuration incorrectly.
405
+ for ( const key in config ) {
406
+ if ( DEFAULT_ENVIRONMENT_CONFIG[ key ] !== undefined ) {
407
+ continue;
408
+ }
409
+
410
+ // We should also check root-only options for the root config
411
+ // because these aren't part of the above defaults but are
412
+ // configuration options that we will parse.
413
+ switch ( key ) {
414
+ case 'testsPort':
415
+ case 'lifecycleScripts':
416
+ case 'env': {
417
+ if ( options.rootConfig ) {
418
+ continue;
419
+ }
420
+
421
+ break;
422
+ }
423
+ }
424
+
425
+ throw new ValidationError(
426
+ `Invalid ${ configFile }: "${ environmentPrefix }${ key }" is not a configuration option.`
120
427
  );
121
- const basename = wpOrgFields
122
- ? encodeURIComponent( wpOrgFields[ 1 ] )
123
- : encodeURIComponent( path.basename( zipFields[ 1 ] ) );
124
-
125
- return {
126
- type: 'zip',
127
- url: sourceString,
128
- path: path.resolve( workDirectoryPath, basename ),
129
- basename,
130
- };
131
- }
132
-
133
- // SSH URLs (git)
134
- const supportedProtocols = [ 'ssh:', 'git+ssh:' ];
135
- try {
136
- const sshUrl = new URL( sourceString );
137
- if ( supportedProtocols.includes( sshUrl.protocol ) ) {
138
- const pathElements = sshUrl.pathname
139
- .split( '/' )
140
- .filter( ( e ) => !! e );
141
- const basename = pathElements
142
- .slice( -1 )[ 0 ]
143
- .replace( /\.git/, '' );
144
- const workingPath = path.resolve(
145
- workDirectoryPath,
146
- ...pathElements.slice( 0, -1 ),
147
- basename
428
+ }
429
+
430
+ // Parse each option individually so that we can handle the validation
431
+ // and any conversion that is required to use the option.
432
+ const parsedConfig = {};
433
+
434
+ if ( config.port !== undefined ) {
435
+ checkPort( configFile, `${ environmentPrefix }port`, config.port );
436
+ parsedConfig.port = config.port;
437
+ }
438
+
439
+ if ( config.phpVersion !== undefined ) {
440
+ // Support null as a valid input.
441
+ if ( config.phpVersion !== null ) {
442
+ checkVersion(
443
+ configFile,
444
+ `${ environmentPrefix }phpVersion`,
445
+ config.phpVersion
148
446
  );
149
- return {
150
- type: 'git',
151
- url: sshUrl.href.split( '#' )[ 0 ],
152
- ref: sshUrl.hash.slice( 1 ) || undefined,
153
- path: workingPath,
154
- clonePath: workingPath,
155
- basename,
156
- };
157
447
  }
158
- } catch ( err ) {}
448
+ parsedConfig.phpVersion = config.phpVersion;
449
+ }
159
450
 
160
- const gitHubFields = sourceString.match(
161
- /^([^\/]+)\/([^#\/]+)(\/([^#]+))?(?:#(.+))?$/
162
- );
451
+ if ( config.core !== undefined ) {
452
+ parsedConfig.coreSource = includeTestsPath(
453
+ await parseCoreSource( config.core, options ),
454
+ options
455
+ );
456
+ }
163
457
 
164
- if ( gitHubFields ) {
165
- return {
166
- type: 'git',
167
- url: `https://github.com/${ gitHubFields[ 1 ] }/${ gitHubFields[ 2 ] }.git`,
168
- ref: gitHubFields[ 5 ],
169
- path: path.resolve(
170
- workDirectoryPath,
171
- gitHubFields[ 2 ],
172
- gitHubFields[ 4 ] || '.'
173
- ),
174
- clonePath: path.resolve( workDirectoryPath, gitHubFields[ 2 ] ),
175
- basename: gitHubFields[ 4 ] || gitHubFields[ 2 ],
176
- };
177
- }
178
-
179
- throw new ValidationError(
180
- `Invalid or unrecognized source: "${ sourceString }".`
181
- );
458
+ if ( config.plugins !== undefined ) {
459
+ checkStringArray(
460
+ configFile,
461
+ `${ environmentPrefix }plugins`,
462
+ config.plugins
463
+ );
464
+ parsedConfig.pluginSources = config.plugins.map( ( sourceString ) =>
465
+ parseSourceString( sourceString, options )
466
+ );
467
+ }
468
+
469
+ if ( config.themes !== undefined ) {
470
+ checkStringArray(
471
+ configFile,
472
+ `${ environmentPrefix }themes`,
473
+ config.themes
474
+ );
475
+ parsedConfig.themeSources = config.themes.map( ( sourceString ) =>
476
+ parseSourceString( sourceString, options )
477
+ );
478
+ }
479
+
480
+ if ( config.config !== undefined ) {
481
+ checkObjectWithValues(
482
+ configFile,
483
+ `${ environmentPrefix }config`,
484
+ config.config,
485
+ [ 'string', 'number', 'boolean' ],
486
+ true
487
+ );
488
+ parsedConfig.config = config.config;
489
+
490
+ // There are some configuration options that have a special purpose and need to be validated too.
491
+ for ( const key in parsedConfig.config ) {
492
+ switch ( key ) {
493
+ case 'WP_HOME':
494
+ case 'WP_SITEURL': {
495
+ checkValidURL(
496
+ configFile,
497
+ `${ environmentPrefix }config.${ key }`,
498
+ parsedConfig.config[ key ]
499
+ );
500
+ break;
501
+ }
502
+ }
503
+ }
504
+ }
505
+
506
+ if ( config.mappings !== undefined ) {
507
+ checkObjectWithValues(
508
+ configFile,
509
+ `${ environmentPrefix }mappings`,
510
+ config.mappings,
511
+ [ 'string' ],
512
+ false
513
+ );
514
+ parsedConfig.mappings = Object.entries( config.mappings ).reduce(
515
+ ( result, [ wpDir, localDir ] ) => {
516
+ const source = parseSourceString( localDir, options );
517
+ result[ wpDir ] = source;
518
+ return result;
519
+ },
520
+ {}
521
+ );
522
+ }
523
+
524
+ return parsedConfig;
182
525
  }
183
- module.exports.parseSourceString = parseSourceString;
184
526
 
185
527
  /**
186
- * Given a source object, returns a new source object with the testsPath
187
- * property set correctly. Only the 'core' source requires a testsPath.
528
+ * Parses a WordPress Core source string or defaults to the latest version.
188
529
  *
189
- * @param {?WPSource} source A source object.
190
- * @param {Object} options
191
- * @param {string} options.workDirectoryPath Path to the work directory located in ~/.wp-env.
192
- *
193
- * @return {?WPSource} A source object.
530
+ * @param {string|null} coreSource The WordPress course source string to parse.
531
+ * @param {Object} options Options to use while parsing.
532
+ * @return {Promise<Object>} The parsed source object.
194
533
  */
195
- function includeTestsPath( source, { workDirectoryPath } ) {
196
- if ( source === null ) {
197
- return null;
198
- }
534
+ async function parseCoreSource( coreSource, options ) {
535
+ // An empty source means we should use the latest version of WordPress.
536
+ if ( ! coreSource ) {
537
+ const wpVersion = await getLatestWordPressVersion();
538
+ if ( ! wpVersion ) {
539
+ throw new ValidationError(
540
+ 'Could not find the latest WordPress version. There may be a network issue.'
541
+ );
542
+ }
199
543
 
200
- return {
201
- ...source,
202
- testsPath: path.resolve(
203
- workDirectoryPath,
204
- 'tests-' + path.basename( source.path )
205
- ),
206
- };
544
+ coreSource = `WordPress/WordPress#${ wpVersion }`;
545
+ }
546
+ return parseSourceString( coreSource, options );
207
547
  }
208
- module.exports.includeTestsPath = includeTestsPath;
548
+
549
+ module.exports = {
550
+ parseConfig,
551
+ getConfigFilePath,
552
+ };