-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Env: Only perform expensive install work when required (#23809)
- Loading branch information
1 parent
160ed96
commit 87be0aa
Showing
10 changed files
with
404 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
const path = require( 'path' ); | ||
const fs = require( 'fs' ).promises; | ||
|
||
/** | ||
* Options for cache parsing. | ||
* | ||
* @typedef WPEnvCacheOptions | ||
* @property {string} workDirectoryPath Path to the work directory located in ~/.wp-env. | ||
*/ | ||
|
||
const CACHE_FILE_NAME = 'wp-env-cache.json'; | ||
|
||
/** | ||
* This function can be used to compare a possible new cache value against an | ||
* existing cache value. For example, we can use this to check if the configuration | ||
* has changed in a new run of wp-env start. | ||
* | ||
* @param {string} key A unique identifier for the cache. | ||
* @param {any} value The value to check against the existing cache. | ||
* @param {WPEnvCacheOptions} options Parsing options | ||
* | ||
* @return {boolean} If true, the value is different from the cache which exists. | ||
*/ | ||
async function didCacheChange( key, value, options ) { | ||
const existingValue = await getCache( key, options ); | ||
return value !== existingValue; | ||
} | ||
|
||
/** | ||
* This function persists the given cache value to the cache file. It creates the | ||
* file if it does not exist yet, and overwrites the existing cache value for the | ||
* given key if it already exists. | ||
* | ||
* @param {string} key A unique identifier for the cache. | ||
* @param {any} value The value to persist. | ||
* @param {WPEnvCacheOptions} options Parsing options | ||
*/ | ||
async function setCache( key, value, options ) { | ||
const existingCache = await getCacheFile( options ); | ||
existingCache[ key ] = value; | ||
|
||
await fs.writeFile( | ||
getPathToCacheFile( options.workDirectoryPath ), | ||
JSON.stringify( existingCache ) | ||
); | ||
} | ||
|
||
/** | ||
* This function retrieves the cache associated with the given key from the file. | ||
* Returns undefined if the key does not exist or if the cache file has not been | ||
* created yet. | ||
* | ||
* @param {string} key The unique identifier for the cache value. | ||
* @param {WPEnvCacheOptions} options Parsing options | ||
* | ||
* @return {any?} The cache value. Undefined if it has not been set or if the cache | ||
* file has not been created. | ||
*/ | ||
async function getCache( key, options ) { | ||
const cache = await getCacheFile( options ); | ||
return cache[ key ]; | ||
} | ||
|
||
/** | ||
* Returns the data stored in the cache file as a JS object. Instead of throwing | ||
* an error, simply returns an empty object if the file cannot be retrieved. | ||
* | ||
* @param {WPEnvCacheOptions} options Parsing options | ||
* | ||
* @return {Object} The data from the cache file. Empty if the file does not exist. | ||
*/ | ||
async function getCacheFile( { workDirectoryPath } ) { | ||
const filename = getPathToCacheFile( workDirectoryPath ); | ||
try { | ||
const rawCache = await fs.readFile( filename ); | ||
return JSON.parse( rawCache ); | ||
} catch { | ||
return {}; | ||
} | ||
} | ||
|
||
function getPathToCacheFile( workDirectoryPath ) { | ||
return path.resolve( workDirectoryPath, CACHE_FILE_NAME ); | ||
} | ||
|
||
module.exports = { didCacheChange, setCache, getCache, getCacheFile }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
const crypto = require( 'crypto' ); | ||
|
||
/** | ||
* Hashes the given string using the MD5 algorithm. | ||
* | ||
* @param {any} data The data to hash. If not a string, converted with JSON.stringify. | ||
* @return {string} An MD5 hash string. | ||
*/ | ||
module.exports = function md5( data ) { | ||
const convertedData = | ||
typeof data === 'string' ? data : JSON.stringify( data ); | ||
|
||
return crypto.createHash( 'md5' ).update( convertedData ).digest( 'hex' ); | ||
}; |
Oops, something went wrong.