|
| 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`); |
0 commit comments