@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
@@ -0,0 +1,106 @@
1
+ 'use strict';
2
+ /**
3
+ * Internal dependencies
4
+ */
5
+ const {
6
+ parseSourceString,
7
+ includeTestsPath,
8
+ } = require( './parse-source-string' );
9
+ const { checkPort, checkVersion, checkString } = require( './validate-config' );
10
+
11
+ /**
12
+ * @typedef {import('./parse-source-string').WPSource} WPSource
13
+ */
14
+
15
+ /**
16
+ * Environment variable configuration.
17
+ *
18
+ * @typedef WPEnvironmentVariableConfig
19
+ * @property {?number} port An override for the development environment's port.
20
+ * @property {?number} testsPort An override for the testing environment's port.
21
+ * @property {?WPSource} coreSource An override for all environment's coreSource.
22
+ * @property {?string} phpVersion An override for all environment's PHP version.
23
+ * @property {?Object.<string, string>} lifecycleScripts An override for various lifecycle scripts.
24
+ */
25
+
26
+ /**
27
+ * Gets configuration options from environment variables.
28
+ *
29
+ * @param {string} cacheDirectoryPath Path to the work directory located in ~/.wp-env.
30
+ *
31
+ * @return {WPEnvironmentVariableConfig} Any configuration options parsed from the environment variables.
32
+ */
33
+ module.exports = function getConfigFromEnvironmentVars( cacheDirectoryPath ) {
34
+ const environmentConfig = {
35
+ port: getPortFromEnvironmentVariable( 'WP_ENV_PORT' ),
36
+ testsPort: getPortFromEnvironmentVariable( 'WP_ENV_TESTS_PORT' ),
37
+ lifecycleScripts: getLifecycleScriptOverrides(),
38
+ };
39
+
40
+ if ( process.env.WP_ENV_CORE ) {
41
+ environmentConfig.coreSource = includeTestsPath(
42
+ parseSourceString( process.env.WP_ENV_CORE, {
43
+ cacheDirectoryPath,
44
+ } ),
45
+ { cacheDirectoryPath }
46
+ );
47
+ }
48
+
49
+ if ( process.env.WP_ENV_PHP_VERSION ) {
50
+ checkVersion(
51
+ 'environment variable',
52
+ 'WP_ENV_PHP_VERSION',
53
+ process.env.WP_ENV_PHP_VERSION
54
+ );
55
+ environmentConfig.phpVersion = process.env.WP_ENV_PHP_VERSION;
56
+ }
57
+
58
+ return environmentConfig;
59
+ };
60
+
61
+ /**
62
+ * Parses an environment variable which should be a port.
63
+ *
64
+ * @param {string} varName The environment variable to check (e.g. WP_ENV_PORT).
65
+ *
66
+ * @return {number} The parsed port number.
67
+ */
68
+ function getPortFromEnvironmentVariable( varName ) {
69
+ if ( ! process.env[ varName ] ) {
70
+ return undefined;
71
+ }
72
+
73
+ const port = parseInt( process.env[ varName ] );
74
+
75
+ // Throw an error if it is not parseable as a number.
76
+ checkPort( 'environment variable', varName, port );
77
+
78
+ return port;
79
+ }
80
+
81
+ /**
82
+ * Parses the lifecycle script environment variables.
83
+ *
84
+ * @return {Object.<string, string>} The parsed lifecycle scripts.
85
+ */
86
+ function getLifecycleScriptOverrides() {
87
+ const lifecycleScripts = {};
88
+
89
+ // Find all of the lifecycle script overrides and parse them.
90
+ const lifecycleEnvironmentVars = {
91
+ WP_ENV_LIFECYCLE_SCRIPT_AFTER_START: 'afterStart',
92
+ WP_ENV_LIFECYCLE_SCRIPT_AFTER_CLEAN: 'afterClean',
93
+ WP_ENV_LIFECYCLE_SCRIPT_AFTER_DESTROY: 'afterDestroy',
94
+ };
95
+ for ( const envVar in lifecycleEnvironmentVars ) {
96
+ const scriptValue = process.env[ envVar ];
97
+ if ( scriptValue === undefined ) {
98
+ continue;
99
+ }
100
+
101
+ checkString( 'environment variable', envVar, scriptValue );
102
+ lifecycleScripts[ lifecycleEnvironmentVars[ envVar ] ] = scriptValue;
103
+ }
104
+
105
+ return lifecycleScripts;
106
+ }
@@ -1,18 +1,20 @@
1
+ 'use strict';
1
2
  /**
2
3
  * Internal dependencies
3
4
  */
4
- const readConfig = require( './config' );
5
+ const loadConfig = require( './load-config' );
5
6
  const { ValidationError } = require( './validate-config' );
6
7
  const dbEnv = require( './db-env' );
7
8
 
8
9
  /**
9
- * @typedef {import('./config').WPConfig} WPConfig
10
- * @typedef {import('./config').WPServiceConfig} WPServiceConfig
11
- * @typedef {import('./config').WPSource} WPSource
10
+ * @typedef {import('./load-config').WPConfig} WPConfig
11
+ * @typedef {import('./parse-config').WPRootConfig} WPRootConfig
12
+ * @typedef {import('./parse-config').WPEnvironmentConfig} WPEnvironmentConfig
13
+ * @typedef {import('./parse-source-string').WPSource} WPSource
12
14
  */
13
15
 
14
16
  module.exports = {
15
17
  ValidationError,
16
- readConfig,
18
+ loadConfig,
17
19
  dbEnv,
18
20
  };
@@ -0,0 +1,92 @@
1
+ 'use strict';
2
+ /**
3
+ * External dependencies
4
+ */
5
+ const path = require( 'path' );
6
+ const fs = require( 'fs' ).promises;
7
+
8
+ /**
9
+ * Internal dependencies
10
+ */
11
+ const getCacheDirectory = require( './get-cache-directory' );
12
+ const md5 = require( '../md5' );
13
+ const { parseConfig, getConfigFilePath } = require( './parse-config' );
14
+ const postProcessConfig = require( './post-process-config' );
15
+
16
+ /**
17
+ * @typedef {import('./parse-config').WPRootConfig} WPRootConfig
18
+ * @typedef {import('./parse-config').WPEnvironmentConfig} WPEnvironmentConfig
19
+ */
20
+
21
+ /**
22
+ * wp-env configuration.
23
+ *
24
+ * @typedef WPConfig
25
+ * @property {string} name Name of the environment.
26
+ * @property {string} configDirectoryPath Path to the .wp-env.json file.
27
+ * @property {string} workDirectoryPath Path to the work directory located in ~/.wp-env.
28
+ * @property {string} dockerComposeConfigPath Path to the docker-compose.yml file.
29
+ * @property {boolean} detectedLocalConfig If true, wp-env detected local config and used it.
30
+ * @property {Object.<string, string>} lifecycleScripts Any lifecycle scripts that we might need to execute.
31
+ * @property {Object.<string, WPEnvironmentConfig>} env Specific config for different environments.
32
+ * @property {boolean} debug True if debug mode is enabled.
33
+ */
34
+
35
+ /**
36
+ * Loads any configuration from a given directory.
37
+ *
38
+ * @param {string} configDirectoryPath The directory we want to load the config from.
39
+ *
40
+ * @return {WPConfig} The config object we've loaded.
41
+ */
42
+ module.exports = async function loadConfig( configDirectoryPath ) {
43
+ const configFilePath = getConfigFilePath( configDirectoryPath );
44
+
45
+ const cacheDirectoryPath = path.resolve(
46
+ await getCacheDirectory(),
47
+ md5( configFilePath )
48
+ );
49
+
50
+ // Parse any configuration we found in the given directory.
51
+ // This comes merged and prepared for internal consumption.
52
+ let config = await parseConfig( configDirectoryPath, cacheDirectoryPath );
53
+
54
+ // Make sure to perform any additional post-processing that
55
+ // may be needed before the config object is ready for
56
+ // consumption elsewhere in the tool.
57
+ config = postProcessConfig( config );
58
+
59
+ return {
60
+ name: path.basename( configDirectoryPath ),
61
+ dockerComposeConfigPath: path.resolve(
62
+ cacheDirectoryPath,
63
+ 'docker-compose.yml'
64
+ ),
65
+ configDirectoryPath,
66
+ workDirectoryPath: cacheDirectoryPath,
67
+ detectedLocalConfig: await hasLocalConfig( [
68
+ configFilePath,
69
+ getConfigFilePath( configDirectoryPath, 'override' ),
70
+ ] ),
71
+ lifecycleScripts: config.lifecycleScripts,
72
+ env: config.env,
73
+ };
74
+ };
75
+
76
+ /**
77
+ * Checks to see whether or not there is any configuration present in the directory.
78
+ *
79
+ * @param {string[]} configFilePaths The config files we want to check for existence.
80
+ *
81
+ * @return {Promise<boolean>} A promise indicating whether or not a local config is present.
82
+ */
83
+ async function hasLocalConfig( configFilePaths ) {
84
+ for ( const filePath of configFilePaths ) {
85
+ try {
86
+ await fs.stat( filePath );
87
+ return true;
88
+ } catch {}
89
+ }
90
+
91
+ return false;
92
+ }
@@ -0,0 +1,105 @@
1
+ 'use strict';
2
+ /**
3
+ * @typedef {import('./parse-config').WPEnvironmentConfig} WPEnvironmentConfig
4
+ */
5
+
6
+ /**
7
+ * Deep-merges the values in the given service environment. This allows us to
8
+ * merge the wp-config.php values instead of overwriting them.
9
+ *
10
+ * @param {WPEnvironmentConfig} defaultConfig The default config options.
11
+ * @param {...string} configs The config options to merge.
12
+ *
13
+ * @return {WPEnvironmentConfig} The merged config object.
14
+ */
15
+ module.exports = function mergeConfigs( defaultConfig, ...configs ) {
16
+ // Start with our default config object. This has all
17
+ // of the options filled out already and we can use
18
+ // that to make performing the merge easier.
19
+ let config = defaultConfig;
20
+
21
+ // Merge the configs
22
+ for ( const merge of configs ) {
23
+ config = mergeConfig( config, merge );
24
+ }
25
+
26
+ return config;
27
+ };
28
+
29
+ /**
30
+ * Merges a config object into another.
31
+ *
32
+ * @param {WPEnvironmentConfig} config The config object to be merged into.
33
+ * @param {WPEnvironmentConfig} toMerge The config object to merge.
34
+ *
35
+ * @return {WPEnvironmentConfig} The merged config object.
36
+ */
37
+ function mergeConfig( config, toMerge ) {
38
+ // Begin by updating any of the config options that the object already has.
39
+ for ( const option in config ) {
40
+ // We don't need to do anything if the merge source doesn't have a property to give.
41
+ if ( toMerge[ option ] === undefined ) {
42
+ continue;
43
+ }
44
+
45
+ switch ( option ) {
46
+ // Some config options are merged together instead of entirely replaced.
47
+ case 'config':
48
+ case 'mappings':
49
+ case 'lifecycleScripts': {
50
+ config[ option ] = Object.assign(
51
+ config[ option ],
52
+ toMerge[ option ]
53
+ );
54
+ break;
55
+ }
56
+
57
+ // Environment-specific config options are recursively merged.
58
+ case 'env': {
59
+ for ( const environment in config.env ) {
60
+ // Once again, we don't need to do anything if the merge source has nothing to give.
61
+ if ( toMerge.env[ environment ] === undefined ) {
62
+ continue;
63
+ }
64
+
65
+ config.env[ environment ] = mergeConfig(
66
+ config.env[ environment ],
67
+ toMerge.env[ environment ]
68
+ );
69
+ }
70
+ break;
71
+ }
72
+
73
+ default: {
74
+ config[ option ] = toMerge[ option ];
75
+ break;
76
+ }
77
+ }
78
+ }
79
+
80
+ // Next, add any new options that the config object doesn't already have.
81
+ for ( const option in toMerge ) {
82
+ // Environment-specific config options should be checked individually.
83
+ if ( option === 'env' ) {
84
+ for ( const environment in toMerge.env ) {
85
+ // The presence of the environment means it would have been merged above.
86
+ if ( config.env[ environment ] !== undefined ) {
87
+ continue;
88
+ }
89
+
90
+ config.env[ environment ] = toMerge[ environment ];
91
+ }
92
+
93
+ continue;
94
+ }
95
+
96
+ // As above, the presence of the option means it would have been merged above.
97
+ if ( config[ option ] !== undefined ) {
98
+ continue;
99
+ }
100
+
101
+ config[ option ] = toMerge[ option ];
102
+ }
103
+
104
+ return config;
105
+ }