-
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.
Add config file support to @wordpress/env (#18121)
* Add path/pathName to Context object We will use this info to build the docker config object * Add a utility to resolve a list of dependencies. It resolves the dependencies from the .wpenv file and returns them in the same "context" format as detect-context. * Use dependency config during wp-env start Autopopulates the docker config with volume mappings for the dependency contexts. * Add whitespace * Cleanup variable and file names from feedback - Rename pathName to pathBasename - Use .wp-env instead of .wpenv - Inline useless function - Clarify default param in jsdoc for detectContext * Activate dependencies on clean action * Don't activate deps on test environment clean * Use wp-env.json for config file * Use correct filename in comment Co-Authored-By: Enrique Piqueras <[email protected]>
- Loading branch information
1 parent
96f7d34
commit 43db91b
Showing
4 changed files
with
87 additions
and
11 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 |
---|---|---|
|
@@ -11,8 +11,9 @@ const wait = require( 'util' ).promisify( setTimeout ); | |
/** | ||
* Internal dependencies | ||
*/ | ||
const detectContext = require( './detect-context' ); | ||
const createDockerComposeConfig = require( './create-docker-compose-config' ); | ||
const detectContext = require( './detect-context' ); | ||
const resolveDependencies = require( './resolve-dependencies' ); | ||
|
||
// Config Variables | ||
const cwd = process.cwd(); | ||
|
@@ -38,14 +39,15 @@ const setupSite = ( isTests = false ) => | |
} --title=${ cwdName } --admin_user=admin --admin_password=password [email protected]`, | ||
isTests | ||
); | ||
const activateContext = ( context, isTests = false ) => | ||
wpCliRun( `wp ${ context.type } activate ${ cwdName }`, isTests ); | ||
const activateContext = ( { type, pathBasename }, isTests = false ) => | ||
wpCliRun( `wp ${ type } activate ${ pathBasename }`, isTests ); | ||
const resetDatabase = ( isTests = false ) => | ||
wpCliRun( 'wp db reset --yes', isTests ); | ||
|
||
module.exports = { | ||
async start( { ref, spinner = {} } ) { | ||
const context = await detectContext(); | ||
const dependencies = await resolveDependencies(); | ||
|
||
spinner.text = `Downloading WordPress@${ ref } 0/100%.`; | ||
const gitFetchOptions = { | ||
|
@@ -100,7 +102,7 @@ module.exports = { | |
spinner.text = `Starting WordPress@${ ref }.`; | ||
fs.writeFileSync( | ||
dockerComposeOptions.config, | ||
createDockerComposeConfig( cwd, cwdName, cwdTestsPath, context ) | ||
createDockerComposeConfig( cwdTestsPath, context, dependencies ) | ||
); | ||
|
||
// These will bring up the database container, | ||
|
@@ -127,6 +129,7 @@ module.exports = { | |
await Promise.all( [ | ||
activateContext( context ), | ||
activateContext( context, true ), | ||
...dependencies.map( activateContext ), | ||
] ); | ||
|
||
// Remove dangling containers and finish. | ||
|
@@ -142,6 +145,8 @@ module.exports = { | |
|
||
async clean( { environment, spinner } ) { | ||
const context = await detectContext(); | ||
const dependencies = await resolveDependencies(); | ||
const activateDependencies = () => Promise.all( dependencies.map( activateContext ) ); | ||
|
||
const description = `${ environment } environment${ | ||
environment === 'all' ? 's' : '' | ||
|
@@ -155,6 +160,7 @@ module.exports = { | |
resetDatabase() | ||
.then( setupSite ) | ||
.then( activateContext.bind( null, context ) ) | ||
.then( activateDependencies ) | ||
.catch( () => {} ) | ||
); | ||
} | ||
|
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,44 @@ | ||
'use strict'; | ||
|
||
/** | ||
* External dependencies | ||
*/ | ||
const util = require( 'util' ); | ||
const fs = require( 'fs' ); | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
const detectContext = require( './detect-context' ); | ||
|
||
/** | ||
* Promisified dependencies | ||
*/ | ||
const readFile = util.promisify( fs.readFile ); | ||
|
||
/** | ||
* Returns an array of dependencies to be mounted in the Docker image. | ||
* | ||
* Reads from the wp-env.json file in the current directory and uses detect | ||
* context to make sure the specified dependencies exist and are plugins | ||
* and/or themes. | ||
* | ||
* @return {Array<detectContext.Context>} An array of dependencies in the context format. | ||
*/ | ||
module.exports = async function resolveDependencies() { | ||
const envFile = await readFile( './wp-env.json' ); | ||
const { themes, plugins } = JSON.parse( envFile ); | ||
|
||
const dependencyResolvers = []; | ||
if ( Array.isArray( themes ) ) { | ||
dependencyResolvers.push( ...themes.map( detectContext ) ); | ||
} | ||
|
||
if ( Array.isArray( plugins ) ) { | ||
dependencyResolvers.push( ...plugins.map( detectContext ) ); | ||
} | ||
|
||
// Return all dependencies which have been detected to be a plugin or a theme. | ||
const dependencies = await Promise.all( dependencyResolvers ); | ||
return dependencies.filter( ( { type } ) => !! type ); | ||
}; |