Skip to content

Commit 03d58ba

Browse files
committed
feat(toolbar): configure vanilla example to use template-based generation
Switch vanilla example from hardcoded configs to auto-generated approach. Files like mu-plugin.php, .htaccess, and .wp-env.json are now generated from centralized templates by the setup-env.js script. - Add setup-env.js script that generates configs from templates - Remove mu-plugin.php, .htaccess, .wp-env.json from version control - Update .gitignore to exclude auto-generated example files
1 parent 10bcabf commit 03d58ba

File tree

4 files changed

+137
-0
lines changed

4 files changed

+137
-0
lines changed

.gitignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ node_modules
44

55
# Build outputs
66
dist
7+
build
8+
.next
9+
out
710

811
# Testing
912
coverage
@@ -13,6 +16,7 @@ test-results/
1316
# WordPress
1417
.wp-env
1518
.wp-env.override.json
19+
wp-env/
1620
uploads/
1721
debug.log
1822
__MACOSX
@@ -29,4 +33,7 @@ __MACOSX
2933
## Examples
3034
examples/**/package-lock.json
3135
examples/**/__MACOSX
36+
examples/**/mu-plugin.php
37+
examples/**/.htaccess
38+
examples/**/.wp-env.json
3239
``
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/bin/bash
2+
3+
# Calculate ports for this example
4+
EXAMPLE_PATH="vanilla/toolbar-demo"
5+
PORTS=$(node ../../../scripts/get-ports.js "$EXAMPLE_PATH" --json)
6+
7+
# Extract ports from JSON
8+
export FRONTEND_PORT=$(echo "$PORTS" | grep -o '"FRONTEND_PORT": [0-9]*' | grep -o '[0-9]*')
9+
export WP_PORT=$(echo "$PORTS" | grep -o '"WP_PORT": [0-9]*' | grep -o '[0-9]*')
10+
export WP_TEST_PORT=$(echo "$PORTS" | grep -o '"WP_TEST_PORT": [0-9]*' | grep -o '[0-9]*')
11+
12+
echo "Starting vanilla toolbar demo..."
13+
echo " Frontend: http://localhost:$FRONTEND_PORT"
14+
echo " WordPress: http://localhost:$WP_PORT"
15+
echo ""
16+
17+
# Start Vite dev server with calculated port
18+
cd example-app && vite --port "$FRONTEND_PORT"
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
#!/usr/bin/env node
2+
3+
/**
4+
* Generate .env file with calculated ports for this example
5+
*/
6+
7+
const { getPorts } = require('../../../../scripts/get-ports.js');
8+
const fs = require('fs');
9+
const path = require('path');
10+
11+
const examplePath = 'vanilla/toolbar-demo';
12+
const ports = getPorts(examplePath);
13+
14+
// Create .env file for Vite
15+
const envContent = `# Auto-generated by setup-env.js - DO NOT EDIT MANUALLY
16+
VITE_FRONTEND_PORT=${ports.FRONTEND_PORT}
17+
VITE_WP_URL=http://localhost:${ports.WP_PORT}
18+
VITE_WP_PORT=${ports.WP_PORT}
19+
VITE_WP_TEST_PORT=${ports.WP_TEST_PORT}
20+
`;
21+
22+
const envPath = path.join(__dirname, '../example-app/.env');
23+
fs.writeFileSync(envPath, envContent);
24+
25+
console.log(`✓ Generated .env for ${examplePath}`);
26+
console.log(` Frontend: ${ports.FRONTEND_PORT}`);
27+
console.log(` WordPress: ${ports.WP_PORT}`);
28+
console.log(` WP Test: ${ports.WP_TEST_PORT}`);
29+
30+
// Update .wp-env.json
31+
const wpEnvConfig = {
32+
phpVersion: '8.0',
33+
plugins: [
34+
'https://github.com/wp-graphql/wp-graphql/releases/latest/download/wp-graphql.zip'
35+
],
36+
config: {
37+
WP_DEBUG: true,
38+
WP_DEBUG_LOG: true,
39+
GRAPHQL_DEBUG: true
40+
},
41+
port: ports.WP_PORT,
42+
testsPort: ports.WP_TEST_PORT,
43+
mappings: {
44+
db: './wp-env/db',
45+
'wp-content/mu-plugins': './mu-plugin.php',
46+
'.htaccess': './.htaccess'
47+
},
48+
lifecycleScripts: {
49+
afterStart: 'wp-env run cli -- wp rewrite structure \'/%postname%/\' --hard && wp-env run cli -- wp theme activate twentytwentyfour'
50+
}
51+
};
52+
53+
const wpEnvPath = path.join(__dirname, '../.wp-env.json');
54+
fs.writeFileSync(wpEnvPath, JSON.stringify(wpEnvConfig, null, 2) + '\n');
55+
56+
console.log(`✓ Updated .wp-env.json`);
57+
58+
// Generate mu-plugin.php from template
59+
const muPluginTemplatePath = path.join(__dirname, '../../../../scripts/templates/mu-plugin.php');
60+
const muPluginTemplate = fs.readFileSync(muPluginTemplatePath, 'utf8');
61+
const muPluginContent = muPluginTemplate.replace(/{{FRONTEND_PORT}}/g, ports.FRONTEND_PORT);
62+
63+
const muPluginPath = path.join(__dirname, '../mu-plugin.php');
64+
fs.writeFileSync(muPluginPath, muPluginContent);
65+
console.log(`✓ Generated mu-plugin.php`);
66+
67+
// Copy .htaccess from template
68+
const htaccessTemplatePath = path.join(__dirname, '../../../../scripts/templates/.htaccess');
69+
const htaccessPath = path.join(__dirname, '../.htaccess');
70+
fs.copyFileSync(htaccessTemplatePath, htaccessPath);
71+
console.log(`✓ Generated .htaccess`);
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#!/bin/bash
2+
3+
# Calculate ports for this example
4+
EXAMPLE_PATH="vanilla/toolbar-demo"
5+
PORTS=$(node ../../../scripts/get-ports.js "$EXAMPLE_PATH" --json)
6+
7+
# Extract ports from JSON
8+
export WP_PORT=$(echo "$PORTS" | grep -o '"WP_PORT": [0-9]*' | grep -o '[0-9]*')
9+
export WP_TEST_PORT=$(echo "$PORTS" | grep -o '"WP_TEST_PORT": [0-9]*' | grep -o '[0-9]*')
10+
11+
echo "Starting WordPress for vanilla toolbar demo..."
12+
echo " WordPress: http://localhost:$WP_PORT"
13+
echo " WP Test: http://localhost:$WP_TEST_PORT"
14+
echo ""
15+
16+
# Update .wp-env.json with calculated ports
17+
cat > .wp-env.json <<EOF
18+
{
19+
"phpVersion": "8.0",
20+
"plugins": [
21+
"https://github.com/wp-graphql/wp-graphql/releases/latest/download/wp-graphql.zip"
22+
],
23+
"config": {
24+
"WP_DEBUG": true,
25+
"WP_DEBUG_LOG": true,
26+
"GRAPHQL_DEBUG": true
27+
},
28+
"port": $WP_PORT,
29+
"testsPort": $WP_TEST_PORT,
30+
"mappings": {
31+
"db": "./wp-env/db",
32+
"wp-content/mu-plugins": "./mu-plugin.php"
33+
},
34+
"lifecycleScripts": {
35+
"afterStart": "wp-env run cli -- wp rewrite structure '/%postname%/' && wp-env run cli -- wp rewrite flush"
36+
}
37+
}
38+
EOF
39+
40+
# Start wp-env
41+
npx wp-env start

0 commit comments

Comments
 (0)