@wordpress/env 5.16.0 → 7.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 (54) hide show
  1. package/README.md +189 -120
  2. package/lib/build-docker-compose-config.js +67 -96
  3. package/lib/cache.js +1 -0
  4. package/lib/cli.js +23 -3
  5. package/lib/commands/clean.js +14 -1
  6. package/lib/commands/destroy.js +12 -37
  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 +79 -15
  11. package/lib/commands/start.js +32 -10
  12. package/lib/commands/stop.js +1 -0
  13. package/lib/config/add-or-replace-port.js +41 -0
  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 +86 -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 +104 -0
  21. package/lib/config/parse-config.js +418 -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 +271 -0
  26. package/lib/config/test/add-or-replace-port.js +81 -0
  27. package/lib/config/test/config-integration.js +143 -0
  28. package/lib/config/test/get-cache-directory.js +57 -0
  29. package/lib/config/test/merge-configs.js +111 -0
  30. package/lib/config/test/parse-config.js +342 -0
  31. package/lib/config/test/parse-source-string.js +154 -0
  32. package/lib/config/test/post-process-config.js +295 -0
  33. package/lib/config/test/read-raw-config-file.js +19 -63
  34. package/lib/config/test/validate-config.js +305 -0
  35. package/lib/config/validate-config.js +103 -40
  36. package/lib/env.js +2 -0
  37. package/lib/execute-after-setup.js +51 -0
  38. package/lib/get-host-user.js +31 -0
  39. package/lib/init-config.js +181 -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-after-setup.js +66 -0
  48. package/lib/test/md5.js +35 -0
  49. package/lib/test/parse-xdebug-mode.js +41 -0
  50. package/lib/wordpress.js +5 -33
  51. package/package.json +2 -2
  52. package/lib/config/config.js +0 -357
  53. package/lib/config/test/__snapshots__/config.js.snap +0 -83
  54. package/lib/config/test/config.js +0 -1203
@@ -3,206 +3,467 @@
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
+ checkString,
18
+ checkPort,
19
+ checkStringArray,
20
+ checkObjectWithValues,
21
+ checkVersion,
22
+ checkValidURL,
23
+ } = require( './validate-config' );
24
+ const getConfigFromEnvironmentVars = require( './get-config-from-environment-vars' );
25
+ const detectDirectoryType = require( './detect-directory-type' );
12
26
  const { getLatestWordPressVersion } = require( '../wordpress' );
27
+ const mergeConfigs = require( './merge-configs' );
13
28
 
14
29
  /**
15
- * @typedef {import('./config').WPServiceConfig} WPServiceConfig
16
- * @typedef {import('./config').WPSource} WPSource
30
+ * @typedef {import('./parse-source-string').WPSource} WPSource
17
31
  */
18
32
 
19
33
  /**
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.
34
+ * The root configuration options.
35
+ *
36
+ * @typedef WPRootConfigOptions
37
+ * @property {number} port The port to use in the development environment.
38
+ * @property {number} testsPort The port to use in the tests environment.
39
+ * @property {string|null} afterSetup The command(s) to run after configuring WordPress on start and clean.
40
+ */
41
+
42
+ /**
43
+ * The environment-specific configuration options. (development/tests/etc)
44
+ *
45
+ * @typedef WPEnvironmentConfig
46
+ * @property {WPSource} coreSource The WordPress installation to load in the environment.
47
+ * @property {WPSource[]} pluginSources Plugins to load in the environment.
48
+ * @property {WPSource[]} themeSources Themes to load in the environment.
49
+ * @property {number} port The port to use.
50
+ * @property {Object} config Mapping of wp-config.php constants to their desired values.
51
+ * @property {Object.<string, WPSource>} mappings Mapping of WordPress directories to local directories which should be mounted.
52
+ * @property {string|null} phpVersion Version of PHP to use in the environments, of the format 0.0.
53
+ */
54
+
55
+ /**
56
+ * The root configuration options.
57
+ *
58
+ * @typedef {WPEnvironmentConfig & WPRootConfigOptions} WPRootConfig
59
+ */
60
+
61
+ /**
62
+ * A WordPress installation, plugin or theme to be loaded into the environment.
63
+ *
64
+ * @typedef WPSource
65
+ * @property {'local'|'git'|'zip'} type The source type.
66
+ * @property {string} path The path to the WordPress installation, plugin or theme.
67
+ * @property {?string} url The URL to the source download if the source type is not local.
68
+ * @property {?string} ref The git ref for the source if the source type is 'git'.
69
+ * @property {string} basename Name that identifies the WordPress installation, plugin or theme.
70
+ */
71
+
72
+ /**
73
+ * Given a directory, this parses any relevant config files and
74
+ * constructs an object in the format used internally.
75
+ *
76
+ *
77
+ * @param {string} configDirectoryPath A path to the directory we are parsing the config for.
78
+ * @param {string} cacheDirectoryPath Path to the work directory located in ~/.wp-env.
79
+ *
80
+ * @return {WPRootConfig} Parsed config.
81
+ */
82
+ async function parseConfig( configDirectoryPath, cacheDirectoryPath ) {
83
+ // The local config will be used to override any defaults.
84
+ const localConfig = await parseConfigFile(
85
+ getConfigFilePath( configDirectoryPath ),
86
+ { cacheDirectoryPath }
87
+ );
88
+
89
+ // Any overrides that can be used in place
90
+ // of properties set by the local config.
91
+ const overrideConfig = await parseConfigFile(
92
+ getConfigFilePath( configDirectoryPath, 'override' ),
93
+ { cacheDirectoryPath }
94
+ );
95
+
96
+ // It's important to know whether or not the user
97
+ // has configured the tool using a JSON file.
98
+ const hasUserConfig = localConfig || overrideConfig;
99
+
100
+ // The default config will be used when no local config
101
+ // file is present in this directory. We should also
102
+ // infer the project type when there is no local
103
+ // config file present to use.
104
+ const defaultConfig = await getDefaultConfig( configDirectoryPath, {
105
+ shouldInferType: ! hasUserConfig,
106
+ cacheDirectoryPath,
107
+ } );
108
+
109
+ // Users can provide overrides in environment
110
+ // variables that supercede all other options.
111
+ const environmentVarOverrides =
112
+ getEnvironmentVarOverrides( cacheDirectoryPath );
113
+
114
+ // Merge all of our configs so that we have a complete object
115
+ // containing the desired options in order of precedence.
116
+ return mergeConfigs(
117
+ defaultConfig,
118
+ localConfig ?? {},
119
+ overrideConfig ?? {},
120
+ environmentVarOverrides
121
+ );
122
+ }
123
+
124
+ /**
125
+ * Gets the path to the config file.
126
+ *
127
+ * @param {string} configDirectoryPath The path to the directory containing config files.
128
+ * @param {string} type The type of config file we're interested in: 'local' or 'override'.
129
+ *
130
+ * @return {string} The path to the config file.
22
131
  */
23
- const HOME_PATH_PREFIX = `~${ path.sep }`;
132
+ function getConfigFilePath( configDirectoryPath, type = 'local' ) {
133
+ let fileName;
134
+ switch ( type ) {
135
+ case 'local': {
136
+ fileName = '.wp-env.json';
137
+ break;
138
+ }
139
+
140
+ case 'override': {
141
+ fileName = '.wp-env.override.json';
142
+ break;
143
+ }
144
+
145
+ default: {
146
+ throw new Error( `Invalid config file type "${ type }.` );
147
+ }
148
+ }
149
+
150
+ return path.resolve( configDirectoryPath, fileName );
151
+ }
24
152
 
25
153
  /**
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[]`.
154
+ * Gets the default config that can be overridden.
30
155
  *
31
- * @param {Object} config A config object to validate.
156
+ * @param {string} configDirectoryPath A path to the config file's directory.
32
157
  * @param {Object} options
33
- * @param {string} options.workDirectoryPath Path to the work directory located in ~/.wp-env.
34
- * @return {WPServiceConfig} Parsed environment-level configuration.
158
+ * @param {string} options.shouldInferType Indicates whether or not we should infer the type of project wp-env is being used in.
159
+ * @param {string} options.cacheDirectoryPath Path to the work directory located in ~/.wp-env.
160
+ *
161
+ * @return {Promise<WPEnvironmentConfig>} The default config object.
35
162
  */
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;
163
+ async function getDefaultConfig(
164
+ configDirectoryPath,
165
+ { shouldInferType, cacheDirectoryPath }
166
+ ) {
167
+ // Our default config should try to infer what type of project
168
+ // this is in order to automatically map the current directory.
169
+ const detectedType = shouldInferType
170
+ ? await detectDirectoryType( configDirectoryPath )
171
+ : null;
172
+
173
+ // The default configuration should contain all possible options and
174
+ // environments whether they're empty or not. This makes using the
175
+ // config objects easier because once merged we don't need to
176
+ // verify that a given option exists before using it.
177
+ const rawConfig = {
178
+ core: detectedType === 'core' ? '.' : null,
179
+ phpVersion: null,
180
+ plugins: detectedType === 'plugin' ? [ '.' ] : [],
181
+ themes: detectedType === 'theme' ? [ '.' ] : [],
182
+ port: 8888,
183
+ testsPort: 8889,
184
+ mappings: {},
185
+ config: {
186
+ WP_DEBUG: true,
187
+ SCRIPT_DEBUG: true,
188
+ WP_ENVIRONMENT_TYPE: 'local',
189
+ WP_PHP_BINARY: 'php',
190
+ WP_TESTS_EMAIL: 'admin@example.org',
191
+ WP_TESTS_TITLE: 'Test Blog',
192
+ WP_TESTS_DOMAIN: 'localhost',
193
+ WP_SITEURL: 'http://localhost',
194
+ WP_HOME: 'http://localhost',
195
+ },
196
+ afterSetup: null,
197
+ env: {
198
+ development: {},
199
+ tests: {
200
+ config: { WP_DEBUG: false, SCRIPT_DEBUG: false },
56
201
  },
57
- {}
58
- ),
202
+ },
59
203
  };
60
- };
61
204
 
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
- }
205
+ return await parseRootConfig( 'default', rawConfig, {
206
+ cacheDirectoryPath,
207
+ } );
208
+ }
71
209
 
72
- coreSource = `WordPress/WordPress#${ wpVersion }`;
210
+ /**
211
+ * Gets a service configuration object containing overrides from our environment variables.
212
+ *
213
+ * @param {string} cacheDirectoryPath Path to the work directory located in ~/.wp-env.
214
+ *
215
+ * @return {WPEnvironmentConfig} An object containing the environment variable overrides.
216
+ */
217
+ function getEnvironmentVarOverrides( cacheDirectoryPath ) {
218
+ const overrides = getConfigFromEnvironmentVars( cacheDirectoryPath );
219
+
220
+ // Create a service config object so we can merge it with the others
221
+ // and override anything that the configuration options need to.
222
+ const overrideConfig = {
223
+ env: {
224
+ development: {},
225
+ tests: {},
226
+ },
227
+ };
228
+
229
+ // We're going to take care to set it at both the root-level and the
230
+ // environment level. This is not totally necessary, but, it's a
231
+ // better representation of how broad the override is.
232
+
233
+ if ( overrides.port ) {
234
+ overrideConfig.port = overrides.port;
235
+ overrideConfig.env.development.port = overrides.port;
73
236
  }
74
- return parseSourceString( coreSource, options );
237
+
238
+ if ( overrides.testsPort ) {
239
+ overrideConfig.testsPort = overrides.testsPort;
240
+ overrideConfig.env.tests.port = overrides.testsPort;
241
+ }
242
+
243
+ if ( overrides.coreSource ) {
244
+ overrideConfig.coreSource = overrides.coreSource;
245
+ overrideConfig.env.development.coreSource = overrides.coreSource;
246
+ overrideConfig.env.tests.coreSource = overrides.coreSource;
247
+ }
248
+
249
+ if ( overrides.phpVersion ) {
250
+ overrideConfig.phpVersion = overrides.phpVersion;
251
+ overrideConfig.env.development.phpVersion = overrides.phpVersion;
252
+ overrideConfig.env.tests.phpVersion = overrides.phpVersion;
253
+ }
254
+
255
+ if ( overrides.afterSetup ) {
256
+ overrideConfig.afterSetup = overrides.afterSetup;
257
+ }
258
+
259
+ return overrideConfig;
75
260
  }
76
261
 
77
262
  /**
78
- * Parses a source string into a source object.
263
+ * Parses a raw config into an unvalidated service config.
79
264
  *
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.
265
+ * @param {string} configFile The config file that we're parsing.
266
+ * @param {Object} options
267
+ * @param {string} options.cacheDirectoryPath Path to the work directory located in ~/.wp-env.
83
268
  *
84
- * @return {?WPSource} A source object.
269
+ * @return {Promise<WPRootConfig|null>} The parsed root config object.
85
270
  */
86
- function parseSourceString( sourceString, { workDirectoryPath } ) {
87
- if ( sourceString === null ) {
271
+ async function parseConfigFile( configFile, options ) {
272
+ const rawConfig = await readRawConfigFile( configFile );
273
+ if ( ! rawConfig ) {
88
274
  return null;
89
275
  }
90
276
 
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 )
277
+ return await parseRootConfig( configFile, rawConfig, options );
278
+ }
279
+
280
+ /**
281
+ * Parses the root config object.
282
+ *
283
+ * @param {string} configFile The config file we're parsing.
284
+ * @param {Object} rawConfig The raw config we're parsing.
285
+ * @param {Object} options
286
+ * @param {string} options.cacheDirectoryPath Path to the work directory located in ~/.wp-env.
287
+ *
288
+ * @return {Promise<WPRootConfig>} The root config object.
289
+ */
290
+ async function parseRootConfig( configFile, rawConfig, options ) {
291
+ const parsedConfig = await parseEnvironmentConfig(
292
+ configFile,
293
+ null,
294
+ rawConfig,
295
+ options
296
+ );
297
+
298
+ // Parse any root-only options.
299
+ if ( rawConfig.testsPort !== undefined ) {
300
+ checkPort( configFile, `testsPort`, rawConfig.testsPort );
301
+ parsedConfig.testsPort = rawConfig.testsPort;
302
+ }
303
+ if ( rawConfig.afterSetup !== undefined ) {
304
+ // Support null as a valid input.
305
+ if ( rawConfig.afterSetup !== null ) {
306
+ checkString( configFile, 'afterSetup', rawConfig.afterSetup );
307
+ }
308
+ parsedConfig.afterSetup = rawConfig.afterSetup;
309
+ }
310
+
311
+ // Parse the environment-specific configs so they're accessible to the root.
312
+ parsedConfig.env = {};
313
+ if ( rawConfig.env ) {
314
+ checkObjectWithValues( configFile, 'env', rawConfig.env, [ 'object' ] );
315
+ for ( const env in rawConfig.env ) {
316
+ parsedConfig.env[ env ] = await parseEnvironmentConfig(
317
+ configFile,
318
+ env,
319
+ rawConfig.env[ env ],
320
+ options
101
321
  );
102
- } else {
103
- sourcePath = path.resolve( sourceString );
104
322
  }
105
- const basename = path.basename( sourcePath );
106
- return {
107
- type: 'local',
108
- path: sourcePath,
109
- basename,
110
- };
111
323
  }
112
324
 
113
- const zipFields = sourceString.match(
114
- /^https?:\/\/([^\s$.?#].[^\s]*)\.zip(\?.+)?$/
115
- );
325
+ return parsedConfig;
326
+ }
116
327
 
117
- if ( zipFields ) {
118
- const wpOrgFields = sourceString.match(
119
- /^https?:\/\/downloads\.wordpress\.org\/(?:plugin|theme)\/([^\s\.]*)([^\s]*)?\.zip$/
120
- );
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
328
+ /**
329
+ * Parses and validates a raw config object and returns a validated service config to use internally.
330
+ *
331
+ * @param {string} configFile The config file that we're parsing.
332
+ * @param {string|null} environment If set, the environment that we're parsing the config for.
333
+ * @param {Object} config A config object to parse.
334
+ * @param {Object} options
335
+ * @param {string} options.cacheDirectoryPath Path to the work directory located in ~/.wp-env.
336
+ *
337
+ * @return {Promise<WPEnvironmentConfig>} The environment config object.
338
+ */
339
+ async function parseEnvironmentConfig(
340
+ configFile,
341
+ environment,
342
+ config,
343
+ options
344
+ ) {
345
+ if ( ! config ) {
346
+ return {};
347
+ }
348
+
349
+ const environmentPrefix = environment ? environment + '.' : '';
350
+
351
+ const parsedConfig = {};
352
+
353
+ if ( config.port !== undefined ) {
354
+ checkPort( configFile, `${ environmentPrefix }port`, config.port );
355
+ parsedConfig.port = config.port;
356
+ }
357
+
358
+ if ( config.phpVersion !== undefined ) {
359
+ // Support null as a valid input.
360
+ if ( config.phpVersion !== null ) {
361
+ checkVersion(
362
+ configFile,
363
+ `${ environmentPrefix }phpVersion`,
364
+ config.phpVersion
148
365
  );
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
366
  }
158
- } catch ( err ) {}
367
+ parsedConfig.phpVersion = config.phpVersion;
368
+ }
159
369
 
160
- const gitHubFields = sourceString.match(
161
- /^([^\/]+)\/([^#\/]+)(\/([^#]+))?(?:#(.+))?$/
162
- );
370
+ if ( config.core !== undefined ) {
371
+ parsedConfig.coreSource = includeTestsPath(
372
+ await parseCoreSource( config.core, options ),
373
+ options
374
+ );
375
+ }
163
376
 
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
- );
377
+ if ( config.plugins !== undefined ) {
378
+ checkStringArray(
379
+ configFile,
380
+ `${ environmentPrefix }plugins`,
381
+ config.plugins
382
+ );
383
+ parsedConfig.pluginSources = config.plugins.map( ( sourceString ) =>
384
+ parseSourceString( sourceString, options )
385
+ );
386
+ }
387
+
388
+ if ( config.themes !== undefined ) {
389
+ checkStringArray(
390
+ configFile,
391
+ `${ environmentPrefix }themes`,
392
+ config.themes
393
+ );
394
+ parsedConfig.themeSources = config.themes.map( ( sourceString ) =>
395
+ parseSourceString( sourceString, options )
396
+ );
397
+ }
398
+
399
+ if ( config.config !== undefined ) {
400
+ checkObjectWithValues(
401
+ configFile,
402
+ `${ environmentPrefix }config`,
403
+ config.config,
404
+ [ 'string', 'number', 'boolean', 'empty' ]
405
+ );
406
+ parsedConfig.config = config.config;
407
+
408
+ // There are some configuration options that have a special purpose and need to be validated too.
409
+ for ( const key in parsedConfig.config ) {
410
+ switch ( key ) {
411
+ case 'WP_HOME':
412
+ case 'WP_SITEURL': {
413
+ checkValidURL(
414
+ configFile,
415
+ `${ environmentPrefix }config.${ key }`,
416
+ parsedConfig.config[ key ]
417
+ );
418
+ break;
419
+ }
420
+ }
421
+ }
422
+ }
423
+
424
+ if ( config.mappings !== undefined ) {
425
+ checkObjectWithValues(
426
+ configFile,
427
+ `${ environmentPrefix }mappings`,
428
+ config.mappings,
429
+ [ 'string' ]
430
+ );
431
+ parsedConfig.mappings = Object.entries( config.mappings ).reduce(
432
+ ( result, [ wpDir, localDir ] ) => {
433
+ const source = parseSourceString( localDir, options );
434
+ result[ wpDir ] = source;
435
+ return result;
436
+ },
437
+ {}
438
+ );
439
+ }
440
+
441
+ return parsedConfig;
182
442
  }
183
- module.exports.parseSourceString = parseSourceString;
184
443
 
185
444
  /**
186
- * Given a source object, returns a new source object with the testsPath
187
- * property set correctly. Only the 'core' source requires a testsPath.
188
- *
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.
445
+ * Parses a WordPress Core source string or defaults to the latest version.
192
446
  *
193
- * @return {?WPSource} A source object.
447
+ * @param {string|null} coreSource The WordPress course source string to parse.
448
+ * @param {Object} options Options to use while parsing.
449
+ * @return {Promise<Object>} The parsed source object.
194
450
  */
195
- function includeTestsPath( source, { workDirectoryPath } ) {
196
- if ( source === null ) {
197
- return null;
198
- }
451
+ async function parseCoreSource( coreSource, options ) {
452
+ // An empty source means we should use the latest version of WordPress.
453
+ if ( ! coreSource ) {
454
+ const wpVersion = await getLatestWordPressVersion();
455
+ if ( ! wpVersion ) {
456
+ throw new ValidationError(
457
+ 'Could not find the latest WordPress version. There may be a network issue.'
458
+ );
459
+ }
199
460
 
200
- return {
201
- ...source,
202
- testsPath: path.resolve(
203
- workDirectoryPath,
204
- 'tests-' + path.basename( source.path )
205
- ),
206
- };
461
+ coreSource = `WordPress/WordPress#${ wpVersion }`;
462
+ }
463
+ return parseSourceString( coreSource, options );
207
464
  }
208
- module.exports.includeTestsPath = includeTestsPath;
465
+
466
+ module.exports = {
467
+ parseConfig,
468
+ getConfigFilePath,
469
+ };