|
| 1 | +import { basename, join } from 'path'; |
| 2 | +import type { |
| 3 | + BlueprintDeclaration, |
| 4 | + StepDefinition, |
| 5 | +} from '@wp-playground/blueprints'; |
| 6 | +import fs from 'fs'; |
| 7 | +import type { RunCLIArgs } from './run-cli'; |
| 8 | + |
| 9 | +export function expandAutoMounts(args: RunCLIArgs): RunCLIArgs { |
| 10 | + const path = process.cwd(); |
| 11 | + |
| 12 | + const mount = [...(args.mount || [])]; |
| 13 | + const mountBeforeInstall = [...(args.mountBeforeInstall || [])]; |
| 14 | + |
| 15 | + if (isPluginDirectory(path)) { |
| 16 | + const pluginName = basename(path); |
| 17 | + mount.push(`${path}:/wordpress/wp-content/plugins/${pluginName}`); |
| 18 | + } else if (isThemeDirectory(path)) { |
| 19 | + const themeName = basename(path); |
| 20 | + mount.push(`${path}:/wordpress/wp-content/themes/${themeName}`); |
| 21 | + } else if (containsWpContentDirectories(path)) { |
| 22 | + mount.push(...wpContentMounts(path)); |
| 23 | + } else if (containsFullWordPressInstallation(path)) { |
| 24 | + /** |
| 25 | + * We don't want Playground and WordPress to modify the OS filesystem on their own |
| 26 | + * by creating files like wp-config.php or wp-content/db.php. |
| 27 | + * To ensure WordPress can write to the /wordpress/ and /wordpress/wp-content/ directories, |
| 28 | + * we leave these directories as MEMFS nodes and mount individual files |
| 29 | + * and directories into them instead of mounting the entire directory as a NODEFS node. |
| 30 | + */ |
| 31 | + const files = fs.readdirSync(path); |
| 32 | + const mounts: string[] = []; |
| 33 | + for (const file of files) { |
| 34 | + if (file.startsWith('wp-content')) { |
| 35 | + continue; |
| 36 | + } |
| 37 | + mounts.push(`${path}/${file}:/wordpress/${file}`); |
| 38 | + } |
| 39 | + mountBeforeInstall.push( |
| 40 | + ...mounts, |
| 41 | + ...wpContentMounts(join(path, 'wp-content')) |
| 42 | + ); |
| 43 | + } else { |
| 44 | + /** |
| 45 | + * By default, mount the current working directory as the Playground root. |
| 46 | + * This allows users to run and PHP or HTML files using the Playground CLI. |
| 47 | + */ |
| 48 | + mount.push(`${path}:/wordpress`); |
| 49 | + } |
| 50 | + |
| 51 | + const blueprint = (args.blueprint as BlueprintDeclaration) || {}; |
| 52 | + blueprint.steps = [...(blueprint.steps || []), ...getSteps(path)]; |
| 53 | + |
| 54 | + /** |
| 55 | + * If Playground is mounting a full WordPress directory, |
| 56 | + * it doesn't need to setup WordPress. |
| 57 | + */ |
| 58 | + const skipWordPressSetup = |
| 59 | + args.skipWordPressSetup || containsFullWordPressInstallation(path); |
| 60 | + |
| 61 | + return { |
| 62 | + ...args, |
| 63 | + blueprint, |
| 64 | + mount, |
| 65 | + mountBeforeInstall, |
| 66 | + skipWordPressSetup, |
| 67 | + } as RunCLIArgs; |
| 68 | +} |
| 69 | + |
| 70 | +export function containsFullWordPressInstallation(path: string): boolean { |
| 71 | + const files = fs.readdirSync(path); |
| 72 | + return ( |
| 73 | + files.includes('wp-admin') && |
| 74 | + files.includes('wp-includes') && |
| 75 | + files.includes('wp-content') |
| 76 | + ); |
| 77 | +} |
| 78 | + |
| 79 | +export function containsWpContentDirectories(path: string): boolean { |
| 80 | + const files = fs.readdirSync(path); |
| 81 | + return ( |
| 82 | + files.includes('themes') || |
| 83 | + files.includes('plugins') || |
| 84 | + files.includes('mu-plugins') || |
| 85 | + files.includes('uploads') |
| 86 | + ); |
| 87 | +} |
| 88 | + |
| 89 | +export function isThemeDirectory(path: string): boolean { |
| 90 | + const files = fs.readdirSync(path); |
| 91 | + if (!files.includes('style.css')) { |
| 92 | + return false; |
| 93 | + } |
| 94 | + const styleCssContent = fs.readFileSync(join(path, 'style.css'), 'utf8'); |
| 95 | + const themeNameRegex = /^(?:[ \t]*<\?php)?[ \t/*#@]*Theme Name:(.*)$/im; |
| 96 | + return !!themeNameRegex.exec(styleCssContent); |
| 97 | +} |
| 98 | + |
| 99 | +export function isPluginDirectory(path: string): boolean { |
| 100 | + const files = fs.readdirSync(path); |
| 101 | + const pluginNameRegex = /^(?:[ \t]*<\?php)?[ \t/*#@]*Plugin Name:(.*)$/im; |
| 102 | + const pluginNameMatch = files |
| 103 | + .filter((file) => file.endsWith('.php')) |
| 104 | + .find((file) => { |
| 105 | + const fileContent = fs.readFileSync(join(path, file), 'utf8'); |
| 106 | + return !!pluginNameRegex.exec(fileContent); |
| 107 | + }); |
| 108 | + return !!pluginNameMatch; |
| 109 | +} |
| 110 | + |
| 111 | +/** |
| 112 | + * Returns a list of files and directories in the wp-content directory |
| 113 | + * to be mounted individually. |
| 114 | + * |
| 115 | + * This is needed because WordPress needs to be able to write to the |
| 116 | + * wp-content directory without Playground modifying the OS filesystem. |
| 117 | + * |
| 118 | + * See expandAutoMounts for more details. |
| 119 | + */ |
| 120 | +export function wpContentMounts(wpContentDir: string): string[] { |
| 121 | + const files = fs.readdirSync(wpContentDir); |
| 122 | + return ( |
| 123 | + files |
| 124 | + /** |
| 125 | + * index.php is added by WordPress automatically and |
| 126 | + * can't be mounted from the current working directory |
| 127 | + * because it already exists. |
| 128 | + * |
| 129 | + * Because index.php should be empty, it's safe to not include it. |
| 130 | + */ |
| 131 | + .filter((file) => !file.startsWith('index.php')) |
| 132 | + .map( |
| 133 | + (file) => |
| 134 | + `${wpContentDir}/${file}:/wordpress/wp-content/${file}` |
| 135 | + ) |
| 136 | + ); |
| 137 | +} |
| 138 | + |
| 139 | +export function getSteps(path: string): StepDefinition[] { |
| 140 | + if (isPluginDirectory(path)) { |
| 141 | + return [ |
| 142 | + { |
| 143 | + step: 'activatePlugin', |
| 144 | + pluginPath: `/wordpress/wp-content/plugins/${basename(path)}`, |
| 145 | + }, |
| 146 | + ]; |
| 147 | + } else if (isThemeDirectory(path)) { |
| 148 | + return [ |
| 149 | + { |
| 150 | + step: 'activateTheme', |
| 151 | + themeFolderName: basename(path), |
| 152 | + }, |
| 153 | + ]; |
| 154 | + } else if ( |
| 155 | + containsWpContentDirectories(path) || |
| 156 | + containsFullWordPressInstallation(path) |
| 157 | + ) { |
| 158 | + /** |
| 159 | + * Playground needs to ensure there is an active theme. |
| 160 | + * Otherwise when WordPress loads it will show a white screen. |
| 161 | + */ |
| 162 | + return [ |
| 163 | + { |
| 164 | + step: 'runPHP', |
| 165 | + code: `<?php |
| 166 | + require_once '/wordpress/wp-load.php'; |
| 167 | + $theme = wp_get_theme(); |
| 168 | + if (!$theme->exists()) { |
| 169 | + $themes = wp_get_themes(); |
| 170 | + if (count($themes) > 0) { |
| 171 | + $themeName = array_keys($themes)[0]; |
| 172 | + switch_theme($themeName); |
| 173 | + } |
| 174 | + } |
| 175 | + `, |
| 176 | + }, |
| 177 | + ]; |
| 178 | + } |
| 179 | + return []; |
| 180 | +} |
0 commit comments