|
| 1 | +import fs from 'fs'; |
| 2 | +import path from 'path'; |
| 3 | +import { recursiveCopyDirectory } from '@studio/common/lib/fs-utils'; |
| 4 | +import semver from 'semver'; |
| 5 | +import { getSqliteVersionFromInstallation } from 'cli/lib/sqlite-integration'; |
| 6 | +import { |
| 7 | + getAiInstructionsPath, |
| 8 | + getLanguagePacksPath, |
| 9 | + getPhpMyAdminPath, |
| 10 | + getSqliteCommandPath, |
| 11 | + getSqlitePluginPath, |
| 12 | + getWordPressVersionPath, |
| 13 | + getWpCliPharPath, |
| 14 | + getWpFilesPath, |
| 15 | +} from '../server-files'; |
| 16 | +import { updateLatestSqliteCommandVersion } from './sqlite-command'; |
| 17 | +import { getWordPressVersionFromInstallation, updateLatestWordPressVersion } from './wordpress'; |
| 18 | +import { updateLatestWpCliVersion } from './wp-cli'; |
| 19 | + |
| 20 | +// Compare the WordPress version in the bundled `wp-files/latest/wordpress` directory (that ships |
| 21 | +// with the CLI) to `~/.studio/server-files/wordpress-versions/latest`. If the bundled directory is |
| 22 | +// newer, rename the old `wordpress-versions/latest` directory to `wordpress-versions/$VERSION` and |
| 23 | +// copy the bundled directory to `wordpress-versions/latest`. |
| 24 | +async function copyBundledLatestWpVersion() { |
| 25 | + const bundledWpVersionPath = path.join( getWpFilesPath(), 'latest', 'wordpress' ); |
| 26 | + const bundledWpVersion = await getWordPressVersionFromInstallation( bundledWpVersionPath ); |
| 27 | + const bundledWpSemver = semver.coerce( bundledWpVersion ); |
| 28 | + |
| 29 | + if ( ! bundledWpVersion || ! bundledWpSemver ) { |
| 30 | + return; |
| 31 | + } |
| 32 | + |
| 33 | + const latestWpVersionPath = getWordPressVersionPath( 'latest' ); |
| 34 | + const latestWpVersion = await getWordPressVersionFromInstallation( latestWpVersionPath ); |
| 35 | + const latestWpSemver = semver.coerce( latestWpVersion ); |
| 36 | + |
| 37 | + if ( ! latestWpVersion || ! latestWpSemver ) { |
| 38 | + await recursiveCopyDirectory( bundledWpVersionPath, latestWpVersionPath ); |
| 39 | + } else if ( semver.gt( bundledWpSemver, latestWpSemver ) ) { |
| 40 | + try { |
| 41 | + await fs.promises.rename( latestWpVersionPath, getWordPressVersionPath( latestWpVersion ) ); |
| 42 | + } catch { |
| 43 | + // Assume the target directory already exists. Do nothing |
| 44 | + } |
| 45 | + await recursiveCopyDirectory( bundledWpVersionPath, latestWpVersionPath ); |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +const SQLITE_FILENAME = 'sqlite-database-integration'; |
| 50 | + |
| 51 | +async function copyBundledSqlite() { |
| 52 | + const bundledSqlitePath = path.join( getWpFilesPath(), SQLITE_FILENAME ); |
| 53 | + const bundledSqliteVersion = semver.coerce( |
| 54 | + await getSqliteVersionFromInstallation( bundledSqlitePath ), |
| 55 | + { includePrerelease: true } |
| 56 | + ); |
| 57 | + if ( ! bundledSqliteVersion ) { |
| 58 | + return; |
| 59 | + } |
| 60 | + const installedSqlitePath = getSqlitePluginPath(); |
| 61 | + const isSqliteInstalled = fs.existsSync( installedSqlitePath ); |
| 62 | + const installedSqliteVersion = semver.coerce( |
| 63 | + await getSqliteVersionFromInstallation( installedSqlitePath ), |
| 64 | + { includePrerelease: true } |
| 65 | + ); |
| 66 | + const isBundledVersionNewer = |
| 67 | + installedSqliteVersion && semver.gt( bundledSqliteVersion, installedSqliteVersion ); |
| 68 | + if ( ! isSqliteInstalled || isBundledVersionNewer ) { |
| 69 | + console.log( `Copying bundled SQLite version ${ bundledSqliteVersion }…` ); |
| 70 | + await recursiveCopyDirectory( bundledSqlitePath, getSqlitePluginPath() ); |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +async function copyBundledWpCli() { |
| 75 | + const bundledWpCLIInstalled = fs.existsSync( getWpCliPharPath() ); |
| 76 | + if ( bundledWpCLIInstalled ) { |
| 77 | + return; |
| 78 | + } |
| 79 | + const bundledWpCLIPath = path.join( getWpFilesPath(), 'wp-cli', 'wp-cli.phar' ); |
| 80 | + await fs.promises.copyFile( bundledWpCLIPath, getWpCliPharPath() ); |
| 81 | +} |
| 82 | + |
| 83 | +async function copyBundledSqliteCommand() { |
| 84 | + const bundledSqliteCommandPath = path.join( getWpFilesPath(), 'sqlite-command' ); |
| 85 | + if ( ! fs.existsSync( bundledSqliteCommandPath ) ) { |
| 86 | + return; |
| 87 | + } |
| 88 | + // Always copy to ensure files are complete and up-to-date |
| 89 | + await recursiveCopyDirectory( bundledSqliteCommandPath, getSqliteCommandPath() ); |
| 90 | +} |
| 91 | + |
| 92 | +async function copyBundledTranslations() { |
| 93 | + const bundledTranslationsPath = path.join( |
| 94 | + getWpFilesPath(), |
| 95 | + 'latest', |
| 96 | + 'available-site-translations.json' |
| 97 | + ); |
| 98 | + if ( ! fs.existsSync( bundledTranslationsPath ) ) { |
| 99 | + return; |
| 100 | + } |
| 101 | + const installedTranslationsPath = path.join( |
| 102 | + getWordPressVersionPath( 'latest' ), |
| 103 | + 'available-site-translations.json' |
| 104 | + ); |
| 105 | + |
| 106 | + await fs.promises.copyFile( bundledTranslationsPath, installedTranslationsPath ); |
| 107 | +} |
| 108 | + |
| 109 | +async function copyBundledAiInstructions() { |
| 110 | + const bundledAiInstructionsPath = path.join( getWpFilesPath(), 'skills' ); |
| 111 | + if ( ! fs.existsSync( bundledAiInstructionsPath ) ) { |
| 112 | + return; |
| 113 | + } |
| 114 | + await recursiveCopyDirectory( bundledAiInstructionsPath, getAiInstructionsPath() ); |
| 115 | +} |
| 116 | + |
| 117 | +async function copyBundledPhpMyAdmin() { |
| 118 | + const bundledPath = path.join( getWpFilesPath(), 'phpmyadmin' ); |
| 119 | + if ( ! fs.existsSync( bundledPath ) ) { |
| 120 | + return; |
| 121 | + } |
| 122 | + // Always copy to ensure files are complete and up-to-date |
| 123 | + await recursiveCopyDirectory( bundledPath, getPhpMyAdminPath() ); |
| 124 | +} |
| 125 | + |
| 126 | +async function copyBundledLanguagePacks() { |
| 127 | + const bundledLanguagePacksPath = path.join( getWpFilesPath(), 'latest', 'languages' ); |
| 128 | + if ( ! fs.existsSync( bundledLanguagePacksPath ) ) { |
| 129 | + return; |
| 130 | + } |
| 131 | + const installedLanguagePacksPath = getLanguagePacksPath(); |
| 132 | + await fs.promises.mkdir( installedLanguagePacksPath, { recursive: true } ); |
| 133 | + await recursiveCopyDirectory( bundledLanguagePacksPath, installedLanguagePacksPath ); |
| 134 | +} |
| 135 | + |
| 136 | +export async function setupServerFiles() { |
| 137 | + const steps: [ string, () => Promise< void > ][] = [ |
| 138 | + [ 'WordPress version', copyBundledLatestWpVersion ], |
| 139 | + [ 'SQLite integration', copyBundledSqlite ], |
| 140 | + [ 'WP-CLI', copyBundledWpCli ], |
| 141 | + [ 'SQLite command', copyBundledSqliteCommand ], |
| 142 | + [ 'translations', copyBundledTranslations ], |
| 143 | + [ 'language packs', copyBundledLanguagePacks ], |
| 144 | + [ 'AI instructions', copyBundledAiInstructions ], |
| 145 | + [ 'phpMyAdmin', copyBundledPhpMyAdmin ], |
| 146 | + ]; |
| 147 | + |
| 148 | + await Promise.all( |
| 149 | + steps.map( ( [ name, step ] ) => |
| 150 | + step().catch( ( error ) => { |
| 151 | + console.error( `Failed to set up dependency ${ name }:`, error ); |
| 152 | + } ) |
| 153 | + ) |
| 154 | + ); |
| 155 | +} |
| 156 | + |
| 157 | +export async function updateServerFiles() { |
| 158 | + const steps: [ string, () => Promise< void > ][] = [ |
| 159 | + [ 'WordPress version', updateLatestWordPressVersion ], |
| 160 | + [ 'WP-CLI', updateLatestWpCliVersion ], |
| 161 | + [ 'SQLite integration', updateLatestSqliteCommandVersion ], |
| 162 | + ]; |
| 163 | + |
| 164 | + await Promise.all( |
| 165 | + steps.map( ( [ name, step ] ) => |
| 166 | + step().catch( ( error ) => { |
| 167 | + console.error( `Failed to update dependency ${ name }:`, error ); |
| 168 | + } ) |
| 169 | + ) |
| 170 | + ); |
| 171 | +} |
0 commit comments