-
Notifications
You must be signed in to change notification settings - Fork 38
Add wp-env setup for this repo #265
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
72b3bf8
add wp-env environment to the project
MaggieCabrera 8558cea
add varible to select active theme
MaggieCabrera 9b72bbe
updated documentation
MaggieCabrera b1290ad
fix error messages on theme activation
MaggieCabrera 48307fa
change logic when port is used to avoid re creating instances of the env
MaggieCabrera a03d9fc
refactor for cleanup and updated docs
MaggieCabrera File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| *.DS_Store | ||
| node_modules | ||
| .wp-env-port | ||
|
|
||
| #files not allowed by the guidelines | ||
| thumbs.db | ||
|
|
||
This file contains hidden or 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,8 @@ | ||
| { | ||
| "core": "WordPress/WordPress", | ||
| "plugins": [ "https://downloads.wordpress.org/plugin/gutenberg.latest-stable.zip" ], | ||
| "themes": [ "https://downloads.wordpress.org/theme/twentytwentythree.latest-stable.zip" ], | ||
| "mappings": { | ||
| "wp-content/themes": "." | ||
| } | ||
| } | ||
This file contains hidden or 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,7 @@ | ||
| { | ||
| "core": "WordPress/WordPress", | ||
| "themes": [ "https://downloads.wordpress.org/theme/twentytwentythree.latest-stable.zip" ], | ||
| "mappings": { | ||
| "wp-content/themes": "." | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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,144 @@ | ||
| #!/usr/bin/env node | ||
| /** | ||
| * Wraps wp-env with auto-detected ports and theme activation. | ||
| * Saves the active port to .wp-env-port so stop/destroy target the right instance. | ||
| * | ||
| * Usage: | ||
| * node env.mjs start [--theme <theme-slug>] [--config <file>] | ||
| * node env.mjs stop | ||
| * node env.mjs destroy | ||
| * | ||
| * npm run env:start -- --theme blue-note | ||
| * npm run env:stop | ||
| * npm run env:destroy | ||
| */ | ||
| import { createServer } from 'net'; | ||
| import { spawn } from 'child_process'; | ||
| import { readFileSync, writeFileSync, existsSync, unlinkSync } from 'fs'; | ||
|
|
||
| const PORT_FILE = '.wp-env-port'; | ||
|
|
||
| function isPortFree( port ) { | ||
| return new Promise( ( resolve ) => { | ||
| const server = createServer(); | ||
| server.on( 'error', () => resolve( false ) ); | ||
| server.listen( port, () => server.close( () => resolve( true ) ) ); | ||
| } ); | ||
| } | ||
|
|
||
| async function findFreePort( start = 8888 ) { | ||
| return ( await isPortFree( start ) ) ? start : findFreePort( start + 1 ); | ||
| } | ||
|
|
||
| function readSavedPort() { | ||
| if ( existsSync( PORT_FILE ) ) { | ||
| const port = parseInt( readFileSync( PORT_FILE, 'utf8' ).trim(), 10 ); | ||
| if ( ! isNaN( port ) ) return port; | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| function runWpEnv( wpEnvArgs, env ) { | ||
| return new Promise( ( resolve ) => { | ||
| const child = spawn( 'wp-env', wpEnvArgs, { stdio: 'inherit', env } ); | ||
| child.on( 'exit', ( code ) => resolve( code ?? 0 ) ); | ||
| } ); | ||
| } | ||
|
|
||
| const args = process.argv.slice( 2 ); | ||
| const command = args[ 0 ]; | ||
|
|
||
| // --- stop / destroy --- | ||
| if ( command === 'stop' || command === 'destroy' ) { | ||
| const savedPort = readSavedPort(); | ||
| const wpEnvEnv = { ...process.env }; | ||
| if ( savedPort ) { | ||
| wpEnvEnv.WP_ENV_PORT = String( savedPort ); | ||
| wpEnvEnv.WP_ENV_TESTS_PORT = String( savedPort + 1 ); | ||
| } | ||
| const code = await runWpEnv( args, wpEnvEnv ); | ||
| if ( code === 0 && command === 'destroy' ) { | ||
| try { unlinkSync( PORT_FILE ); } catch {} | ||
| } | ||
| process.exit( code ); | ||
| } | ||
|
|
||
| // --- start --- | ||
|
|
||
| // Extract --theme <slug> from args, pass the rest to wp-env. | ||
| const themeIndex = args.indexOf( '--theme' ); | ||
| let themeSlug = null; | ||
| if ( themeIndex !== -1 ) { | ||
| themeSlug = args[ themeIndex + 1 ]; | ||
| args.splice( themeIndex, 2 ); | ||
| } | ||
|
|
||
| const savedPort = readSavedPort(); | ||
| const alreadyRunning = savedPort && ! ( await isPortFree( savedPort ) ); | ||
|
|
||
| if ( alreadyRunning ) { | ||
| // Instance is already running — skip wp-env start, just activate the theme. | ||
| console.log( `wp-env is already running on port ${ savedPort }.` ); | ||
| if ( themeSlug ) { | ||
| activateTheme( themeSlug, buildEnv( savedPort ) ); | ||
| } else { | ||
| process.exit( 0 ); | ||
| } | ||
| } else { | ||
| const port = await findFreePort(); | ||
| const testsPort = await findFreePort( port + 1 ); | ||
|
|
||
| if ( port !== 8888 ) { | ||
| console.log( `Port 8888 is in use, starting on port ${ port } instead.` ); | ||
| } | ||
|
|
||
| const wpEnvEnv = buildEnv( port, testsPort ); | ||
| const child = spawn( 'wp-env', args, { stdio: 'inherit', env: wpEnvEnv } ); | ||
|
|
||
| child.on( 'exit', ( code ) => { | ||
| if ( code !== 0 ) { | ||
| process.exit( code ); | ||
| } | ||
|
|
||
| // Persist port so stop/destroy can target this instance. | ||
| writeFileSync( PORT_FILE, String( port ) ); | ||
|
|
||
| if ( ! themeSlug ) { | ||
| process.exit( 0 ); | ||
| } | ||
|
|
||
| activateTheme( themeSlug, wpEnvEnv ); | ||
| } ); | ||
| } | ||
|
|
||
| function buildEnv( port, testsPort = port + 1 ) { | ||
| return { ...process.env, WP_ENV_PORT: String( port ), WP_ENV_TESTS_PORT: String( testsPort ) }; | ||
| } | ||
|
|
||
| function activateTheme( slug, wpEnvEnv ) { | ||
| console.log( `Activating theme: ${ slug }` ); | ||
| const activate = spawn( 'wp-env', [ 'run', 'cli', 'wp', 'theme', 'activate', slug ], { | ||
| stdio: [ 'inherit', 'pipe', 'pipe' ], | ||
| env: wpEnvEnv, | ||
| } ); | ||
|
|
||
| let activateOutput = ''; | ||
| activate.stdout.on( 'data', ( data ) => { | ||
| const text = data.toString(); | ||
| activateOutput += text; | ||
| process.stdout.write( text ); | ||
| } ); | ||
| activate.stderr.on( 'data', ( data ) => { | ||
| const text = data.toString(); | ||
| activateOutput += text; | ||
| process.stderr.write( text ); | ||
| } ); | ||
|
|
||
| activate.on( 'exit', ( activateCode ) => { | ||
| if ( activateCode !== 0 || ! activateOutput.includes( 'Success:' ) ) { | ||
| console.error( `Error: Failed to activate theme '${ slug }'. Make sure the theme slug is correct.` ); | ||
| process.exit( 1 ); | ||
| } | ||
| process.exit( 0 ); | ||
| } ); | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Atlas needs TT3 as a dependency