|
| 1 | +import fs from 'node:fs'; |
| 2 | +import path from 'node:path'; |
| 3 | +import { getBlueprintsPharPath } from 'cli/lib/dependency-management/paths'; |
| 4 | +import { runPhpCommand } from './php-process'; |
| 5 | +import type { NativePhpSupportedVersion } from '@studio/common/lib/php-binary-metadata'; |
| 6 | +import type { ServerConfig } from 'cli/lib/types/wordpress-server-ipc'; |
| 7 | + |
| 8 | +export async function runBlueprint( |
| 9 | + config: ServerConfig, |
| 10 | + blueprint: NonNullable< ServerConfig[ 'blueprint' ] >, |
| 11 | + phpVersion: NativePhpSupportedVersion, |
| 12 | + signal: AbortSignal |
| 13 | +): Promise< void > { |
| 14 | + // blueprints.phar accepts local paths only; remote URIs need the Playground runtime. |
| 15 | + if ( blueprint.uri.startsWith( 'http://' ) || blueprint.uri.startsWith( 'https://' ) ) { |
| 16 | + throw new Error( |
| 17 | + `Remote blueprint URIs are not supported by the native PHP runtime: ${ blueprint.uri }` |
| 18 | + ); |
| 19 | + } |
| 20 | + |
| 21 | + const enableDebugLog = config.enableDebugLog ?? false; |
| 22 | + const enableDebugDisplay = config.enableDebugDisplay ?? false; |
| 23 | + const defaultConstants: Record< string, boolean | string > = { |
| 24 | + // The SQLite driver requires a non-empty DB_NAME at runtime. |
| 25 | + DB_NAME: 'wordpress', |
| 26 | + WP_DEBUG: enableDebugLog || enableDebugDisplay, |
| 27 | + WP_DEBUG_LOG: enableDebugLog, |
| 28 | + WP_DEBUG_DISPLAY: enableDebugDisplay, |
| 29 | + }; |
| 30 | + |
| 31 | + blueprint.contents.constants = { |
| 32 | + ...blueprint.contents.constants, |
| 33 | + ...defaultConstants, |
| 34 | + }; |
| 35 | + // Native PHP selects PHP and installs WordPress before Blueprint execution. |
| 36 | + // Passing preferredVersions makes blueprints.phar validate versions it does not manage here. |
| 37 | + delete blueprint.contents.preferredVersions; |
| 38 | + |
| 39 | + const blueprintDir = path.dirname( blueprint.uri ); |
| 40 | + const tmpPath = path.join( blueprintDir, `studio-blueprint-${ config.siteId }.json` ); |
| 41 | + await fs.promises.writeFile( tmpPath, JSON.stringify( blueprint.contents ) ); |
| 42 | + |
| 43 | + // blueprints.phar detects SQLite under plugins, while Studio installs it under mu-plugins. |
| 44 | + const muPluginsSqlite = path.join( |
| 45 | + config.sitePath, |
| 46 | + 'wp-content', |
| 47 | + 'mu-plugins', |
| 48 | + 'sqlite-database-integration' |
| 49 | + ); |
| 50 | + const pluginsSqlite = path.join( |
| 51 | + config.sitePath, |
| 52 | + 'wp-content', |
| 53 | + 'plugins', |
| 54 | + 'sqlite-database-integration' |
| 55 | + ); |
| 56 | + const needsSymlink = fs.existsSync( muPluginsSqlite ) && ! fs.existsSync( pluginsSqlite ); |
| 57 | + let symlinkIno: number | undefined; |
| 58 | + if ( needsSymlink ) { |
| 59 | + fs.symlinkSync( muPluginsSqlite, pluginsSqlite, 'junction' ); |
| 60 | + // Remove only the entry created here, not unrelated content that replaced it. |
| 61 | + symlinkIno = fs.statSync( pluginsSqlite ).ino; |
| 62 | + } |
| 63 | + |
| 64 | + try { |
| 65 | + await runPhpCommand( |
| 66 | + [ |
| 67 | + getBlueprintsPharPath(), |
| 68 | + 'exec', |
| 69 | + tmpPath, |
| 70 | + '--mode=apply-to-existing-site', |
| 71 | + `--site-path=${ config.sitePath }`, |
| 72 | + `--site-url=${ config.absoluteUrl ?? `http://localhost:${ config.port }` }`, |
| 73 | + '--db-engine=sqlite', |
| 74 | + ], |
| 75 | + { phpVersion, signal } |
| 76 | + ); |
| 77 | + } finally { |
| 78 | + await fs.promises.unlink( tmpPath ).catch( () => {} ); |
| 79 | + if ( needsSymlink ) { |
| 80 | + try { |
| 81 | + if ( fs.statSync( pluginsSqlite ).ino === symlinkIno ) { |
| 82 | + await fs.promises.rm( pluginsSqlite, { recursive: true, force: true } ); |
| 83 | + } |
| 84 | + } catch { |
| 85 | + // Best effort - leaving the symlink behind is non-fatal. |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | +} |
0 commit comments