diff --git a/__tests__/devenv-e2e/001-create.spec.js b/__tests__/devenv-e2e/001-create.spec.js index 90f4ba2c9..6530ccafb 100644 --- a/__tests__/devenv-e2e/001-create.spec.js +++ b/__tests__/devenv-e2e/001-create.spec.js @@ -68,7 +68,7 @@ describe( 'vip dev-env create', () => { it( 'should use sane defaults', async () => { const slug = getProjectSlug(); const expectedMultisite = false; - const expectedPhpVersion = '8.2'; + const expectedPhpVersion = '8.4'; const expectedElasticsearch = false; const expectedPhpMyAdmin = false; const expectedXDebug = false; diff --git a/__tests__/lib/dev-environment/dev-environment-cli.js b/__tests__/lib/dev-environment/dev-environment-cli.js index 0e28e12af..d91867225 100644 --- a/__tests__/lib/dev-environment/dev-environment-cli.js +++ b/__tests__/lib/dev-environment/dev-environment-cli.js @@ -594,13 +594,14 @@ describe( 'lib/dev-environment/dev-environment-cli', () => { expect( actual ).toStrictEqual( expected ); } ); - it.each( [ [ '7.4' ], [ 'ghcr.io/automattic/vip-container-images/php-fpm-ubuntu:7.3' ] ] )( - 'should throw an error for invalid version', - async version => { - expect( () => resolvePhpVersion( version ) ).toThrow( - `Unknown or unsupported PHP version: ${ version }` - ); - } - ); + it.each( [ + [ '7.4' ], + [ 'ghcr.io/automattic/vip-container-images/php-fpm-ubuntu:7.3' ], + [ 'toString' ], + ] )( 'should throw an error for invalid version', async version => { + expect( () => resolvePhpVersion( version ) ).toThrow( + `Unknown or unsupported PHP version: ${ version }` + ); + } ); } ); } ); diff --git a/src/bin/vip-dev-env-update.js b/src/bin/vip-dev-env-update.js index 2c6085f83..9080cb134 100755 --- a/src/bin/vip-dev-env-update.js +++ b/src/bin/vip-dev-env-update.js @@ -6,6 +6,7 @@ import debugLib from 'debug'; import command from '../lib/cli/command'; import { DEV_ENVIRONMENT_NOT_FOUND, + DEV_ENVIRONMENT_DEFAULT_PHP_VERSION, DEV_ENVIRONMENT_PHP_VERSIONS, } from '../lib/constants/dev-environment'; import { @@ -109,7 +110,7 @@ cmd.argv( process.argv, async ( arg, opt ) => { elasticsearch: currentInstanceData.elasticsearch, php: currentInstanceData.php || - DEV_ENVIRONMENT_PHP_VERSIONS[ Object.keys( DEV_ENVIRONMENT_PHP_VERSIONS )[ 0 ] ].image, + DEV_ENVIRONMENT_PHP_VERSIONS[ DEV_ENVIRONMENT_DEFAULT_PHP_VERSION ].image, mariadb: currentInstanceData.mariadb, phpmyadmin: currentInstanceData.phpmyadmin, xdebug: currentInstanceData.xdebug, diff --git a/src/lib/constants/dev-environment.ts b/src/lib/constants/dev-environment.ts index f99c18b0c..ce9721dea 100644 --- a/src/lib/constants/dev-environment.ts +++ b/src/lib/constants/dev-environment.ts @@ -27,29 +27,31 @@ interface PhpImage { label: string; } -export const DEV_ENVIRONMENT_PHP_VERSIONS: Record< string, PhpImage > = { - 8.2: { +export const DEV_ENVIRONMENT_PHP_VERSIONS = { + '8.2': { image: 'ghcr.io/automattic/vip-container-images/php-fpm:8.2', - label: '8.2 (recommended)', + label: '8.2', }, - 8.3: { + '8.3': { image: 'ghcr.io/automattic/vip-container-images/php-fpm:8.3', label: '8.3', }, - 8.4: { + '8.4': { image: 'ghcr.io/automattic/vip-container-images/php-fpm:8.4', label: '8.4', }, - 8.5: { + '8.5': { image: 'ghcr.io/automattic/vip-container-images/php-fpm:8.5', label: '8.5 (experimental)', }, -} as const; +} as const satisfies Record< string, PhpImage >; + +export const DEV_ENVIRONMENT_DEFAULT_PHP_VERSION: keyof typeof DEV_ENVIRONMENT_PHP_VERSIONS = '8.4'; export const DEV_ENVIRONMENT_DEFAULTS = { title: 'VIP Dev', multisite: false, - phpVersion: Object.keys( DEV_ENVIRONMENT_PHP_VERSIONS )[ 0 ], + phpVersion: DEV_ENVIRONMENT_DEFAULT_PHP_VERSION, } as const; export const DEV_ENVIRONMENT_VERSION = '2.3.3'; diff --git a/src/lib/dev-environment/dev-environment-cli.ts b/src/lib/dev-environment/dev-environment-cli.ts index af46f1287..bfe136b6e 100644 --- a/src/lib/dev-environment/dev-environment-cli.ts +++ b/src/lib/dev-environment/dev-environment-cli.ts @@ -23,6 +23,7 @@ import { Args } from '../cli/command'; import { DEV_ENVIRONMENT_FULL_COMMAND, DEV_ENVIRONMENT_DEFAULTS, + DEV_ENVIRONMENT_DEFAULT_PHP_VERSION, DEV_ENVIRONMENT_PROMPT_INTRO, DEV_ENVIRONMENT_COMPONENTS, DEV_ENVIRONMENT_NOT_FOUND, @@ -775,7 +776,7 @@ export function resolvePhpVersion( version: string ): string { debug( `Resolving PHP version %j`, version ); let result: string; - if ( ! ( version in DEV_ENVIRONMENT_PHP_VERSIONS ) ) { + if ( ! Object.hasOwn( DEV_ENVIRONMENT_PHP_VERSIONS, version ) ) { const images = Object.values( DEV_ENVIRONMENT_PHP_VERSIONS ); const image = images.find( value => value.image === version ); if ( image ) { @@ -784,7 +785,8 @@ export function resolvePhpVersion( version: string ): string { throw new UserError( `Unknown or unsupported PHP version: ${ version }.` ); } } else { - result = DEV_ENVIRONMENT_PHP_VERSIONS[ version ].image; + result = + DEV_ENVIRONMENT_PHP_VERSIONS[ version as keyof typeof DEV_ENVIRONMENT_PHP_VERSIONS ].image; } debug( 'Resolved PHP image: %j', result ); @@ -797,9 +799,15 @@ export async function promptForPhpVersion( initialValue: string ): Promise< stri let answer = initialValue; if ( isStdinTTY ) { const choices = []; - Object.keys( DEV_ENVIRONMENT_PHP_VERSIONS ).forEach( version => { + ( + Object.keys( DEV_ENVIRONMENT_PHP_VERSIONS ) as ( keyof typeof DEV_ENVIRONMENT_PHP_VERSIONS )[] + ).forEach( version => { const phpImage = DEV_ENVIRONMENT_PHP_VERSIONS[ version ]; - choices.push( { message: phpImage.label, value: version } ); + const label = + version === DEV_ENVIRONMENT_DEFAULT_PHP_VERSION + ? `${ phpImage.label } (recommended)` + : phpImage.label; + choices.push( { message: label, value: version } ); } ); const images = Object.values( DEV_ENVIRONMENT_PHP_VERSIONS ); let initial = images.findIndex( version => version.image === initialValue ); diff --git a/src/lib/dev-environment/dev-environment-core.ts b/src/lib/dev-environment/dev-environment-core.ts index 25d46564e..8982265a7 100644 --- a/src/lib/dev-environment/dev-environment-core.ts +++ b/src/lib/dev-environment/dev-environment-core.ts @@ -42,6 +42,7 @@ import { DEV_ENVIRONMENT_WORDPRESS_CACHE_KEY, DEV_ENVIRONMENT_WORDPRESS_VERSION_TTL, DEV_ENVIRONMENT_PHP_VERSIONS, + DEV_ENVIRONMENT_DEFAULT_PHP_VERSION, DEV_ENVIRONMENT_VERSION, } from '../constants/dev-environment'; import { createProxyAgent } from '../http/proxy-agent'; @@ -321,8 +322,7 @@ function preProcessInstanceData( instanceData: InstanceData ): Required< Instanc mariadb: instanceData.mariadb ?? '', elasticsearch: instanceData.elasticsearch || false, // NOSONAR php: - instanceData.php ?? - DEV_ENVIRONMENT_PHP_VERSIONS[ Object.keys( DEV_ENVIRONMENT_PHP_VERSIONS )[ 0 ] ].image, + instanceData.php ?? DEV_ENVIRONMENT_PHP_VERSIONS[ DEV_ENVIRONMENT_DEFAULT_PHP_VERSION ].image, xdebug: Boolean( instanceData.xdebug ), phpmyadmin: Boolean( instanceData.phpmyadmin ), photon: Boolean( instanceData.photon ),