@wordpress/env 8.10.0 → 8.11.1-next.f8d8eceb.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.
package/lib/cache.js CHANGED
@@ -40,6 +40,9 @@ async function didCacheChange( key, value, options ) {
40
40
  * @param {WPEnvCacheOptions} options Parsing options
41
41
  */
42
42
  async function setCache( key, value, options ) {
43
+ // Make sure the cache directory exists.
44
+ await fs.mkdir( options.workDirectoryPath, { recursive: true } );
45
+
43
46
  const existingCache = await getCacheFile( options );
44
47
  existingCache[ key ] = value;
45
48
 
@@ -28,6 +28,7 @@ const {
28
28
  configureWordPress,
29
29
  setupWordPressDirectories,
30
30
  readWordPressVersion,
31
+ canAccessWPORG,
31
32
  } = require( '../wordpress' );
32
33
  const { didCacheChange, setCache } = require( '../cache' );
33
34
  const md5 = require( '../md5' );
@@ -77,16 +78,24 @@ module.exports = async function start( {
77
78
  const configHash = md5( config );
78
79
  const { workDirectoryPath, dockerComposeConfigPath } = config;
79
80
  const shouldConfigureWp =
80
- update ||
81
- ( await didCacheChange( CONFIG_CACHE_KEY, configHash, {
82
- workDirectoryPath,
83
- } ) );
81
+ ( update ||
82
+ ( await didCacheChange( CONFIG_CACHE_KEY, configHash, {
83
+ workDirectoryPath,
84
+ } ) ) ) &&
85
+ // Don't reconfigure everything when we can't connect to the internet because
86
+ // the majority of update tasks involve connecting to the internet. (Such
87
+ // as downloading sources and pulling docker images.)
88
+ ( await canAccessWPORG() );
84
89
 
85
90
  const dockerComposeConfig = {
86
91
  config: dockerComposeConfigPath,
87
92
  log: config.debug,
88
93
  };
89
94
 
95
+ if ( ! ( await canAccessWPORG() ) ) {
96
+ spinner.info( 'wp-env is offline' );
97
+ }
98
+
90
99
  /**
91
100
  * If the Docker image is already running and the `wp-env` files have been
92
101
  * deleted, the start command will not complete successfully. Stopping
@@ -534,7 +534,7 @@ async function parseEnvironmentConfig(
534
534
  async function parseCoreSource( coreSource, options ) {
535
535
  // An empty source means we should use the latest version of WordPress.
536
536
  if ( ! coreSource ) {
537
- const wpVersion = await getLatestWordPressVersion();
537
+ const wpVersion = await getLatestWordPressVersion( options );
538
538
  if ( ! wpVersion ) {
539
539
  throw new ValidationError(
540
540
  'Could not find the latest WordPress version. There may be a network issue.'
@@ -15,6 +15,8 @@ jest.mock( 'fs', () => ( {
15
15
  promises: {
16
16
  readFile: jest.fn(),
17
17
  stat: jest.fn().mockResolvedValue( true ),
18
+ mkdir: jest.fn(),
19
+ writeFile: jest.fn(),
18
20
  },
19
21
  } ) );
20
22
 
package/lib/test/cache.js CHANGED
@@ -18,6 +18,7 @@ jest.mock( 'fs', () => ( {
18
18
  promises: {
19
19
  readFile: jest.fn(),
20
20
  writeFile: jest.fn(),
21
+ mkdir: jest.fn(),
21
22
  },
22
23
  } ) );
23
24
 
package/lib/wordpress.js CHANGED
@@ -7,12 +7,18 @@ const util = require( 'util' );
7
7
  const fs = require( 'fs' ).promises;
8
8
  const path = require( 'path' );
9
9
  const got = require( 'got' );
10
+ const dns = require( 'dns' ).promises;
10
11
 
11
12
  /**
12
13
  * Promisified dependencies
13
14
  */
14
15
  const copyDir = util.promisify( require( 'copy-dir' ) );
15
16
 
17
+ /**
18
+ * Internal dependencies
19
+ */
20
+ const { getCache, setCache } = require( './cache' );
21
+
16
22
  /**
17
23
  * @typedef {import('./config').WPConfig} WPConfig
18
24
  * @typedef {import('./config').WPEnvironmentConfig} WPEnvironmentConfig
@@ -243,19 +249,54 @@ async function readWordPressVersion( coreSource, spinner, debug ) {
243
249
  return versionMatch[ 1 ];
244
250
  }
245
251
 
252
+ /**
253
+ * Basically a quick check to see if we can connect to the internet.
254
+ *
255
+ * @return {boolean} True if we can connect to WordPress.org, false otherwise.
256
+ */
257
+ let IS_OFFLINE;
258
+ async function canAccessWPORG() {
259
+ // Avoid situations where some parts of the code think we're offline and others don't.
260
+ if ( IS_OFFLINE !== undefined ) {
261
+ return IS_OFFLINE;
262
+ }
263
+ IS_OFFLINE = !! ( await dns.resolve( 'WordPress.org' ).catch( () => {} ) );
264
+ return IS_OFFLINE;
265
+ }
266
+
246
267
  /**
247
268
  * Returns the latest stable version of WordPress by requesting the stable-check
248
269
  * endpoint on WordPress.org.
249
270
  *
271
+ * @param {Object} options an object with cacheDirectoryPath set to the path to the cache directory in ~/.wp-env.
250
272
  * @return {string} The latest stable version of WordPress, like "6.0.1"
251
273
  */
252
274
  let CACHED_WP_VERSION;
253
- async function getLatestWordPressVersion() {
275
+ async function getLatestWordPressVersion( options ) {
254
276
  // Avoid extra network requests.
255
277
  if ( CACHED_WP_VERSION ) {
256
278
  return CACHED_WP_VERSION;
257
279
  }
258
280
 
281
+ const cacheOptions = {
282
+ workDirectoryPath: options.cacheDirectoryPath,
283
+ };
284
+
285
+ // When we can't connect to the internet, we don't want to break wp-env or
286
+ // wait for the stable-check result to timeout.
287
+ if ( ! ( await canAccessWPORG() ) ) {
288
+ const latestVersion = await getCache(
289
+ 'latestWordPressVersion',
290
+ cacheOptions
291
+ );
292
+ if ( ! latestVersion ) {
293
+ throw new Error(
294
+ 'Could not find the current WordPress version in the cache and the network is not available.'
295
+ );
296
+ }
297
+ return latestVersion;
298
+ }
299
+
259
300
  const versions = await got(
260
301
  'https://api.wordpress.org/core/stable-check/1.0/'
261
302
  ).json();
@@ -263,6 +304,7 @@ async function getLatestWordPressVersion() {
263
304
  for ( const [ version, status ] of Object.entries( versions ) ) {
264
305
  if ( status === 'latest' ) {
265
306
  CACHED_WP_VERSION = version;
307
+ await setCache( 'latestWordPressVersion', version, cacheOptions );
266
308
  return version;
267
309
  }
268
310
  }
@@ -275,5 +317,6 @@ module.exports = {
275
317
  resetDatabase,
276
318
  setupWordPressDirectories,
277
319
  readWordPressVersion,
320
+ canAccessWPORG,
278
321
  getLatestWordPressVersion,
279
322
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wordpress/env",
3
- "version": "8.10.0",
3
+ "version": "8.11.1-next.f8d8eceb.0",
4
4
  "description": "A zero-config, self contained local WordPress environment for development and testing.",
5
5
  "author": "The WordPress Contributors",
6
6
  "license": "GPL-2.0-or-later",
@@ -51,5 +51,5 @@
51
51
  "scripts": {
52
52
  "test": "echo \"Error: run tests from root\" && exit 1"
53
53
  },
54
- "gitHead": "f83bb1a71e8fa416131b81a9f282a72a1dc6c694"
54
+ "gitHead": "8d8fd197e202b8104ffb1cb83048efd0a6c3faf4"
55
55
  }