diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 6766a525c1..6a30dc5969 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -321,7 +321,7 @@ jobs: with: submodules: true - uses: ./.github/actions/prepare-playground - - run: packages/playground/cli/tests/test-running-unbuilt-cli.sh + - run: packages/playground/cli/src/tests/test-running-unbuilt-cli.sh build: runs-on: ubuntu-latest diff --git a/.gitignore b/.gitignore index 3858cf9026..3a9402b277 100644 --- a/.gitignore +++ b/.gitignore @@ -64,7 +64,7 @@ php.js.bak # @php-wasm/cli saves this file to disk so PHP has access to it. # Since it is dynamically generated from the Node.js TLS module, # we do not want to commit it to the repository. -packages/php-wasm/cli/src/ca-bundle.crt +packages/php-wasm/cli/src/lib/ca-bundle.crt # PHPUnit .phpunit.result.cache diff --git a/packages/php-wasm/cli/package.json b/packages/php-wasm/cli/package.json index cde3e7040f..e372a8d179 100644 --- a/packages/php-wasm/cli/package.json +++ b/packages/php-wasm/cli/package.json @@ -21,7 +21,7 @@ }, "license": "GPL-2.0-or-later", "type": "module", - "main": "main.js", + "main": "cli.js", "bin": { "php-wasm-cli": "php-wasm.js" }, diff --git a/packages/php-wasm/cli/project.json b/packages/php-wasm/cli/project.json index cf5fd07ea9..a7d71e7bf6 100644 --- a/packages/php-wasm/cli/project.json +++ b/packages/php-wasm/cli/project.json @@ -29,7 +29,7 @@ "executor": "@nx/vite:build", "outputs": ["{options.outputPath}"], "options": { - "main": "dist/packages/php-wasm/cli/main.js", + "main": "dist/packages/php-wasm/cli/cli.js", "outputPath": "dist/packages/php-wasm/cli" }, "defaultConfiguration": "production", @@ -45,7 +45,7 @@ "dev": { "executor": "nx:run-commands", "options": { - "command": "node --no-warnings --experimental-wasm-stack-switching --experimental-wasm-jspi --loader=./packages/meta/src/node-es-module-loader/loader.mts ./packages/php-wasm/cli/src/main.ts", + "command": "node --no-warnings --experimental-wasm-stack-switching --experimental-wasm-jspi --loader=./packages/meta/src/node-es-module-loader/loader.mts ./packages/php-wasm/cli/src/cli.ts", "tty": true } }, @@ -57,7 +57,7 @@ "command": "node -e \"if (parseInt(process.versions.node) < 22) { console.error('Node.js version 22 or greater is required'); process.exit(1); }\"", "forwardAllArgs": false }, - "node --stack-trace-limit=100 --inspect --experimental-strip-types --experimental-transform-types --import ./packages/meta/src/node-es-module-loader/register.mts --watch ./packages/php-wasm/cli/src/main.ts" + "node --stack-trace-limit=100 --inspect --experimental-strip-types --experimental-transform-types --import ./packages/meta/src/node-es-module-loader/register.mts --watch ./packages/php-wasm/cli/src/cli.ts" ], "tty": true, "parallel": false @@ -71,7 +71,7 @@ "command": "node -e \"if (parseInt(process.versions.node) < 22) { console.error('Node.js version 22 or greater is required'); process.exit(1); }\"", "forwardAllArgs": false }, - "node --inspect --experimental-strip-types --experimental-transform-types --import ./packages/meta/src/node-es-module-loader/register.mts ./packages/php-wasm/cli/src/main.ts" + "node --inspect --experimental-strip-types --experimental-transform-types --import ./packages/meta/src/node-es-module-loader/register.mts ./packages/php-wasm/cli/src/cli.ts" ], "tty": true, "parallel": false @@ -80,7 +80,7 @@ "start": { "executor": "@wp-playground/nx-extensions:built-script", "options": { - "scriptPath": "dist/packages/php-wasm/cli/main.js" + "scriptPath": "dist/packages/php-wasm/cli/php-wasm.js" }, "dependsOn": ["build"] }, diff --git a/packages/php-wasm/cli/public/php-wasm.js b/packages/php-wasm/cli/public/php-wasm.js index 26bc3e556e..0bee25f1fc 100644 --- a/packages/php-wasm/cli/public/php-wasm.js +++ b/packages/php-wasm/cli/public/php-wasm.js @@ -1,2 +1,2 @@ #!/usr/bin/env node -import './main.js'; +import './run-cli.js'; diff --git a/packages/php-wasm/cli/src/cli.ts b/packages/php-wasm/cli/src/cli.ts new file mode 100644 index 0000000000..1ddbfe404f --- /dev/null +++ b/packages/php-wasm/cli/src/cli.ts @@ -0,0 +1,3 @@ +import { parseOptionsAndRunCLI } from './lib/run-cli'; + +parseOptionsAndRunCLI(); diff --git a/packages/php-wasm/cli/src/php.ini b/packages/php-wasm/cli/src/lib/php.ini similarity index 100% rename from packages/php-wasm/cli/src/php.ini rename to packages/php-wasm/cli/src/lib/php.ini diff --git a/packages/php-wasm/cli/src/main.ts b/packages/php-wasm/cli/src/lib/run-cli.ts similarity index 51% rename from packages/php-wasm/cli/src/main.ts rename to packages/php-wasm/cli/src/lib/run-cli.ts index 4babb1fcb4..6763a71d15 100644 --- a/packages/php-wasm/cli/src/main.ts +++ b/packages/php-wasm/cli/src/lib/run-cli.ts @@ -1,54 +1,145 @@ -/** - * A CLI script that runs PHP CLI via the WebAssembly build. - */ +import yargs from 'yargs'; +import { hideBin } from 'yargs/helpers'; import os from 'os'; import { writeFileSync, existsSync, mkdtempSync, chmodSync } from 'fs'; import { rootCertificates } from 'tls'; - import { LatestSupportedPHPVersion, SupportedPHPVersionsList, } from '@php-wasm/universal'; import type { SupportedPHPVersion } from '@php-wasm/universal'; - import { FileLockManagerForNode } from '@php-wasm/node'; import { PHP } from '@php-wasm/universal'; import { loadNodeRuntime, useHostFilesystem } from '@php-wasm/node'; import { startBridge } from '@php-wasm/xdebug-bridge'; import path from 'path'; +import { logger, LogSeverity } from '@php-wasm/logger'; + +const LogVerbosity = { + Quiet: { name: 'quiet', severity: LogSeverity.Fatal }, + Normal: { name: 'normal', severity: LogSeverity.Info }, + Debug: { name: 'debug', severity: LogSeverity.Debug }, +} as const; + +type LogVerbosity = (typeof LogVerbosity)[keyof typeof LogVerbosity]['name']; -let args = process.argv.slice(2); -if (!args.length) { - args = ['--help']; +interface CLIDefaults { + directive: string[]; + config: string; } -const baseUrl = (import.meta || {}).url; +interface CLIArgs { + _: string[]; + config: string; + directive: string[]; + xdebug?: boolean; + experimentalDevtools?: boolean; + verbosity?: LogVerbosity; + help?: boolean; +} -// Write the ca-bundle.crt file to disk so that PHP can find it. -const caBundlePath = new URL('ca-bundle.crt', baseUrl).pathname; -if (!existsSync(caBundlePath)) { - writeFileSync(caBundlePath, rootCertificates.join('\n')); +function parseCliArgs(defaults: CLIDefaults) { + return yargs(hideBin(process.argv)) + .usage( + ` +PHP.wasm CLI + +Usage: php-wasm-cli [options] + ` + ) + .option('config', { + alias: 'c', + type: 'string', + describe: 'Path to configuration file', + default: defaults.config, + }) + .option('directive', { + alias: 'd', + type: 'array', + describe: 'Set configuration directives (key=value)', + default: defaults.directive, + }) + .option('xdebug', { + type: 'boolean', + describe: 'Enable Xdebug.', + default: false, + }) + .option('experimental-devtools', { + type: 'boolean', + describe: 'Enable experimental browser development tools.', + default: false, + }) + .option('verbosity', { + type: 'string', + describe: 'Output logs', + choices: Object.values(LogVerbosity).map( + (verbosity) => verbosity.name + ), + default: 'normal', + }) + .strictCommands() + .help() + .epilog( + ` +Examples: + php-wasm-cli # Start with default settings + php-wasm-cli --config /path/to/php.ini # Run PHP CLI with custom config file + php-wasm-cli --xdebug --experimental-devtools # Enable Xdebug and Devtools + ` + ) + .parseSync() as CLIArgs; } -args.unshift('-d', `openssl.cafile=${caBundlePath}`); -async function run() { - const defaultPhpIniPath = new URL('php.ini', baseUrl).pathname; +function mapArgsArray(args: CLIArgs): string[] { + const argv: string[] = []; + + if (args.config) { + argv.push('-c', args.config); + } + + if (Array.isArray(args.directive)) { + args.directive.forEach((d) => { + argv.push('-d', d); + }); + } + + if (Array.isArray(args._)) { + argv.push(...args._); + } + + return argv; +} + +export async function parseOptionsAndRunCLI(): Promise { const phpVersion = (process.env['PHP'] || LatestSupportedPHPVersion) as SupportedPHPVersion; if (!SupportedPHPVersionsList.includes(phpVersion)) { throw new Error(`Unsupported PHP version ${phpVersion}`); } - const hasXdebugOption = args.some((arg) => arg.startsWith('--xdebug')); - if (hasXdebugOption) { - args = args.filter((arg) => arg !== '--xdebug'); + const baseUrl = (import.meta || {}).url; + + const defaultPhpIniPath = new URL('php.ini', baseUrl).pathname; + // Write the ca-bundle.crt file to disk so that PHP can find it. + const caBundlePath = new URL('ca-bundle.crt', baseUrl).pathname; + if (!existsSync(caBundlePath)) { + writeFileSync(caBundlePath, rootCertificates.join('\n')); + } + + const args = parseCliArgs({ + directive: [`openssl.cafile=${caBundlePath}`], + config: defaultPhpIniPath, + }); + + if (args.help) { + return; } - const hasDevtoolsOption = args.some((arg) => - arg.startsWith('--experimental-devtools') - ); - if (hasDevtoolsOption) { - args = args.filter((arg) => arg !== '--experimental-devtools'); + if (args.verbosity) { + const severity = Object.values(LogVerbosity).find( + (v) => v.name === args.verbosity + )!.severity; + logger.setSeverityFilterLevel(severity); } // npm scripts set the TMPDIR env variable @@ -99,24 +190,20 @@ ${process.argv[0]} ${process.execArgv.join(' ')} ${process.argv[1]} PATH: `${tempDir}:${envVariables['PATH']}`, }, }, - withXdebug: hasXdebugOption, + withXdebug: args.xdebug, }) ); useHostFilesystem(php); - if (hasDevtoolsOption && hasXdebugOption) { + if (args.experimentalDevtools && args.xdebug) { const bridge = await startBridge({}); bridge.start(); } - const hasMinusCOption = args.some((arg) => arg.startsWith('-c')); - if (!hasMinusCOption) { - args.unshift('-c', defaultPhpIniPath); - } + const response = await php.cli(['php', ...mapArgsArray(args)]); - const response = await php.cli(['php', ...args]); response.stderr.pipeTo( new WritableStream({ write(chunk) { @@ -147,5 +234,3 @@ ${process.argv[0]} ${process.execArgv.join(' ')} ${process.argv[1]} }, 100); }); } - -run(); diff --git a/packages/php-wasm/cli/vite.config.ts b/packages/php-wasm/cli/vite.config.ts index cc8dbd5fd6..da8671fe5b 100644 --- a/packages/php-wasm/cli/vite.config.ts +++ b/packages/php-wasm/cli/vite.config.ts @@ -33,7 +33,7 @@ export default defineConfig({ 'ws', 'os', ], - input: 'packages/php-wasm/cli/src/main.ts', + input: 'packages/php-wasm/cli/src/cli.ts', output: { format: 'esm', entryFileNames: '[name].js', diff --git a/packages/php-wasm/xdebug-bridge/src/cli.ts b/packages/php-wasm/xdebug-bridge/src/cli.ts index 042dd6f91f..1ddbfe404f 100644 --- a/packages/php-wasm/xdebug-bridge/src/cli.ts +++ b/packages/php-wasm/xdebug-bridge/src/cli.ts @@ -1,3 +1,3 @@ -import { main } from './lib/run-cli'; +import { parseOptionsAndRunCLI } from './lib/run-cli'; -main(); +parseOptionsAndRunCLI(); diff --git a/packages/php-wasm/xdebug-bridge/src/lib/run-cli.ts b/packages/php-wasm/xdebug-bridge/src/lib/run-cli.ts index 5c0375816f..a22e0eb3f4 100644 --- a/packages/php-wasm/xdebug-bridge/src/lib/run-cli.ts +++ b/packages/php-wasm/xdebug-bridge/src/lib/run-cli.ts @@ -31,18 +31,18 @@ Usage: xdebug-bridge [options] .option('port', { alias: 'p', type: 'number', - description: 'Xdebug port to listen on', + describe: 'Xdebug port to listen on', default: 9003, }) .option('host', { alias: 'h', type: 'string', - description: 'Xdebug host to bind to', + describe: 'Xdebug host to bind to', default: 'localhost', }) .option('php-root', { type: 'string', - description: 'Path to PHP root directory', + describe: 'Path to PHP root directory', default: './', }) .option('verbosity', { @@ -66,7 +66,7 @@ Examples: .parseSync() as CLIArgs; } -export async function main(): Promise { +export async function parseOptionsAndRunCLI(): Promise { const args = parseCliArgs(); if (args.help) { diff --git a/packages/php-wasm/xdebug-bridge/src/tests/run-cli.spec.ts b/packages/php-wasm/xdebug-bridge/src/tests/run-cli.spec.ts index dc325a16cf..98d4db6f32 100644 --- a/packages/php-wasm/xdebug-bridge/src/tests/run-cli.spec.ts +++ b/packages/php-wasm/xdebug-bridge/src/tests/run-cli.spec.ts @@ -1,6 +1,6 @@ import './mocker'; import { vi } from 'vitest'; -import { main } from '../lib/run-cli'; +import { parseOptionsAndRunCLI } from '../lib/run-cli'; import { logger, LogSeverity } from '@php-wasm/logger'; import { startBridge } from '../lib/start-bridge'; import type { XdebugCDPBridge } from '../lib/xdebug-cdp-bridge'; @@ -26,7 +26,7 @@ describe('CLI', () => { }); it('calls startBridge with default arguments', async () => { - await main(); + await parseOptionsAndRunCLI(); expect(startBridge).toHaveBeenCalledWith({ cdpPort: 9229, @@ -49,7 +49,7 @@ describe('CLI', () => { '/var/www' ); - await main(); + await parseOptionsAndRunCLI(); expect(startBridge).toHaveBeenCalledWith({ cdpPort: 9229, @@ -63,7 +63,7 @@ describe('CLI', () => { process.argv.push('--help'); try { - await main(); + await parseOptionsAndRunCLI(); } catch (e: any) { expect(e.message).toBe('process.exit unexpectedly called with "0"'); } @@ -74,7 +74,7 @@ describe('CLI', () => { it('runs cli with verbosity option set to quiet', async () => { process.argv.push('--verbosity', 'quiet'); - await main(); + await parseOptionsAndRunCLI(); expect(logger.setSeverityFilterLevel).toHaveBeenCalledWith( LogSeverity.Fatal @@ -84,7 +84,7 @@ describe('CLI', () => { it('runs cli with verbosity option set to normal', async () => { process.argv.push('--verbosity', 'normal'); - await main(); + await parseOptionsAndRunCLI(); expect(logger.setSeverityFilterLevel).toHaveBeenCalledWith( LogSeverity.Info @@ -94,7 +94,7 @@ describe('CLI', () => { it('runs cli with verbosity option set to debug', async () => { process.argv.push('--verbosity', 'debug'); - await main(); + await parseOptionsAndRunCLI(); expect(logger.setSeverityFilterLevel).toHaveBeenCalledWith( LogSeverity.Debug diff --git a/packages/playground/cli/project.json b/packages/playground/cli/project.json index 3e066764ff..6d90a7446d 100644 --- a/packages/playground/cli/project.json +++ b/packages/playground/cli/project.json @@ -136,7 +136,6 @@ "executor": "@nx/vite:test", "outputs": ["{workspaceRoot}/coverage/packages/playground/cli"], "options": { - "passWithNoTests": true, "reportsDirectory": "../../../coverage/packages/playground/cli" } }, diff --git a/packages/playground/cli/src/cli.ts b/packages/playground/cli/src/cli.ts index 60fc43a78d..57a086013e 100644 --- a/packages/playground/cli/src/cli.ts +++ b/packages/playground/cli/src/cli.ts @@ -1,4 +1,4 @@ -import { parseOptionsAndRunCLI } from './run-cli'; +import { parseOptionsAndRunCLI } from './lib/run-cli'; // Do not await this as top-level await is not supported in all environments. parseOptionsAndRunCLI(); diff --git a/packages/playground/cli/src/index.ts b/packages/playground/cli/src/index.ts index 080825f594..f783dd26be 100644 --- a/packages/playground/cli/src/index.ts +++ b/packages/playground/cli/src/index.ts @@ -1 +1 @@ -export * from './run-cli'; +export * from './lib/run-cli'; diff --git a/packages/playground/cli/src/blueprints-v1/blueprints-v1-handler.ts b/packages/playground/cli/src/lib/blueprints-v1/blueprints-v1-handler.ts similarity index 99% rename from packages/playground/cli/src/blueprints-v1/blueprints-v1-handler.ts rename to packages/playground/cli/src/lib/blueprints-v1/blueprints-v1-handler.ts index fc0cb976b9..6437d8d695 100644 --- a/packages/playground/cli/src/blueprints-v1/blueprints-v1-handler.ts +++ b/packages/playground/cli/src/lib/blueprints-v1/blueprints-v1-handler.ts @@ -59,6 +59,7 @@ export class BlueprintsV1Handler { import.meta.dirname, '..', '..', + '..', importedWorkerV1UrlString ); } diff --git a/packages/playground/cli/src/blueprints-v1/download.ts b/packages/playground/cli/src/lib/blueprints-v1/download.ts similarity index 100% rename from packages/playground/cli/src/blueprints-v1/download.ts rename to packages/playground/cli/src/lib/blueprints-v1/download.ts diff --git a/packages/playground/cli/src/blueprints-v1/worker-thread-v1.ts b/packages/playground/cli/src/lib/blueprints-v1/worker-thread-v1.ts similarity index 100% rename from packages/playground/cli/src/blueprints-v1/worker-thread-v1.ts rename to packages/playground/cli/src/lib/blueprints-v1/worker-thread-v1.ts diff --git a/packages/playground/cli/src/blueprints-v2/blueprint-v2-declaration.ts b/packages/playground/cli/src/lib/blueprints-v2/blueprint-v2-declaration.ts similarity index 100% rename from packages/playground/cli/src/blueprints-v2/blueprint-v2-declaration.ts rename to packages/playground/cli/src/lib/blueprints-v2/blueprint-v2-declaration.ts diff --git a/packages/playground/cli/src/blueprints-v2/blueprints-v2-handler.ts b/packages/playground/cli/src/lib/blueprints-v2/blueprints-v2-handler.ts similarity index 99% rename from packages/playground/cli/src/blueprints-v2/blueprints-v2-handler.ts rename to packages/playground/cli/src/lib/blueprints-v2/blueprints-v2-handler.ts index 332855bf6b..dac456dadf 100644 --- a/packages/playground/cli/src/blueprints-v2/blueprints-v2-handler.ts +++ b/packages/playground/cli/src/lib/blueprints-v2/blueprints-v2-handler.ts @@ -47,6 +47,7 @@ export class BlueprintsV2Handler { import.meta.dirname, '..', '..', + '..', importedWorkerV2UrlString ); } diff --git a/packages/playground/cli/src/blueprints-v2/get-v2-runner.ts b/packages/playground/cli/src/lib/blueprints-v2/get-v2-runner.ts similarity index 93% rename from packages/playground/cli/src/blueprints-v2/get-v2-runner.ts rename to packages/playground/cli/src/lib/blueprints-v2/get-v2-runner.ts index a9a3b78cd4..5925420ed0 100644 --- a/packages/playground/cli/src/blueprints-v2/get-v2-runner.ts +++ b/packages/playground/cli/src/lib/blueprints-v2/get-v2-runner.ts @@ -10,7 +10,7 @@ export async function getV2Runner(): Promise { * for the new .phar file. */ // @ts-ignore - const v2_runner_url = (await import('../../public/blueprints.phar?url')) + const v2_runner_url = (await import('../../../public/blueprints.phar?url')) .default; /** diff --git a/packages/playground/cli/src/blueprints-v2/run-blueprint-v2.ts b/packages/playground/cli/src/lib/blueprints-v2/run-blueprint-v2.ts similarity index 100% rename from packages/playground/cli/src/blueprints-v2/run-blueprint-v2.ts rename to packages/playground/cli/src/lib/blueprints-v2/run-blueprint-v2.ts diff --git a/packages/playground/cli/src/blueprints-v2/worker-thread-v2.ts b/packages/playground/cli/src/lib/blueprints-v2/worker-thread-v2.ts similarity index 100% rename from packages/playground/cli/src/blueprints-v2/worker-thread-v2.ts rename to packages/playground/cli/src/lib/blueprints-v2/worker-thread-v2.ts diff --git a/packages/playground/cli/src/is-valid-wordpress-slug.ts b/packages/playground/cli/src/lib/is-valid-wordpress-slug.ts similarity index 100% rename from packages/playground/cli/src/is-valid-wordpress-slug.ts rename to packages/playground/cli/src/lib/is-valid-wordpress-slug.ts diff --git a/packages/playground/cli/src/load-balancer.ts b/packages/playground/cli/src/lib/load-balancer.ts similarity index 100% rename from packages/playground/cli/src/load-balancer.ts rename to packages/playground/cli/src/lib/load-balancer.ts diff --git a/packages/playground/cli/src/mounts.ts b/packages/playground/cli/src/lib/mounts.ts similarity index 100% rename from packages/playground/cli/src/mounts.ts rename to packages/playground/cli/src/lib/mounts.ts diff --git a/packages/playground/cli/src/resolve-blueprint.ts b/packages/playground/cli/src/lib/resolve-blueprint.ts similarity index 100% rename from packages/playground/cli/src/resolve-blueprint.ts rename to packages/playground/cli/src/lib/resolve-blueprint.ts diff --git a/packages/playground/cli/src/run-cli.ts b/packages/playground/cli/src/lib/run-cli.ts similarity index 99% rename from packages/playground/cli/src/run-cli.ts rename to packages/playground/cli/src/lib/run-cli.ts index 9b2819a7d9..6511675891 100644 --- a/packages/playground/cli/src/run-cli.ts +++ b/packages/playground/cli/src/lib/run-cli.ts @@ -64,7 +64,13 @@ export async function parseOptionsAndRunCLI() { * Perhaps the two could be handled by the same code? */ const yargsObject = yargs(process.argv.slice(2)) - .usage('Usage: wp-playground [options]') + .usage( + ` +WordPress Playground CLI + +Usage: wp-playground-cli [options] + ` + ) .positional('command', { describe: 'Command to run', choices: ['server', 'run-blueprint', 'build-snapshot'] as const, diff --git a/packages/playground/cli/src/start-server.ts b/packages/playground/cli/src/lib/start-server.ts similarity index 100% rename from packages/playground/cli/src/start-server.ts rename to packages/playground/cli/src/lib/start-server.ts diff --git a/packages/playground/cli/tests/blueprints-v2.spec.ts b/packages/playground/cli/src/tests/blueprints-v2.spec.ts similarity index 95% rename from packages/playground/cli/tests/blueprints-v2.spec.ts rename to packages/playground/cli/src/tests/blueprints-v2.spec.ts index acc3ef5224..6aa2c1a71a 100644 --- a/packages/playground/cli/tests/blueprints-v2.spec.ts +++ b/packages/playground/cli/src/tests/blueprints-v2.spec.ts @@ -2,7 +2,7 @@ import { loadNodeRuntime } from '@php-wasm/node'; import { type PHPRequestHandler } from '@php-wasm/universal'; import { bootRequestHandler } from '@wp-playground/wordpress'; import { rootCertificates } from 'node:tls'; -import { runBlueprintV2 } from '../src/blueprints-v2/run-blueprint-v2'; +import { runBlueprintV2 } from '../lib/blueprints-v2/run-blueprint-v2'; import { RecommendedPHPVersion } from '@wp-playground/common'; describe('V2 runner', () => { diff --git a/packages/playground/cli/tests/mount-examples/not-plugin/foo.php b/packages/playground/cli/src/tests/mount-examples/not-plugin/foo.php similarity index 100% rename from packages/playground/cli/tests/mount-examples/not-plugin/foo.php rename to packages/playground/cli/src/tests/mount-examples/not-plugin/foo.php diff --git a/packages/playground/cli/tests/mount-examples/not-theme/style.css b/packages/playground/cli/src/tests/mount-examples/not-theme/style.css similarity index 100% rename from packages/playground/cli/tests/mount-examples/not-theme/style.css rename to packages/playground/cli/src/tests/mount-examples/not-theme/style.css diff --git a/packages/playground/cli/tests/mount-examples/nothing/.gitkeep b/packages/playground/cli/src/tests/mount-examples/nothing/.gitkeep similarity index 100% rename from packages/playground/cli/tests/mount-examples/nothing/.gitkeep rename to packages/playground/cli/src/tests/mount-examples/nothing/.gitkeep diff --git a/packages/playground/cli/tests/mount-examples/php/index.php b/packages/playground/cli/src/tests/mount-examples/php/index.php similarity index 100% rename from packages/playground/cli/tests/mount-examples/php/index.php rename to packages/playground/cli/src/tests/mount-examples/php/index.php diff --git a/packages/playground/cli/tests/mount-examples/plugin/sample-plugin.php b/packages/playground/cli/src/tests/mount-examples/plugin/sample-plugin.php similarity index 100% rename from packages/playground/cli/tests/mount-examples/plugin/sample-plugin.php rename to packages/playground/cli/src/tests/mount-examples/plugin/sample-plugin.php diff --git a/packages/playground/cli/tests/mount-examples/static-html/index.html b/packages/playground/cli/src/tests/mount-examples/static-html/index.html similarity index 63% rename from packages/playground/cli/tests/mount-examples/static-html/index.html rename to packages/playground/cli/src/tests/mount-examples/static-html/index.html index fd1a6b22a1..fa1315f49d 100644 --- a/packages/playground/cli/tests/mount-examples/static-html/index.html +++ b/packages/playground/cli/src/tests/mount-examples/static-html/index.html @@ -3,8 +3,8 @@ Static HTML - - -

Static HTML

- + + +

Static HTML

+ diff --git a/packages/playground/cli/tests/mount-examples/theme/functions.php b/packages/playground/cli/src/tests/mount-examples/theme/functions.php similarity index 100% rename from packages/playground/cli/tests/mount-examples/theme/functions.php rename to packages/playground/cli/src/tests/mount-examples/theme/functions.php diff --git a/packages/playground/cli/tests/mount-examples/theme/index.php b/packages/playground/cli/src/tests/mount-examples/theme/index.php similarity index 100% rename from packages/playground/cli/tests/mount-examples/theme/index.php rename to packages/playground/cli/src/tests/mount-examples/theme/index.php diff --git a/packages/playground/cli/tests/mount-examples/theme/style.css b/packages/playground/cli/src/tests/mount-examples/theme/style.css similarity index 100% rename from packages/playground/cli/tests/mount-examples/theme/style.css rename to packages/playground/cli/src/tests/mount-examples/theme/style.css diff --git a/packages/playground/cli/tests/mount-examples/wordpress/index.php b/packages/playground/cli/src/tests/mount-examples/wordpress/index.php similarity index 100% rename from packages/playground/cli/tests/mount-examples/wordpress/index.php rename to packages/playground/cli/src/tests/mount-examples/wordpress/index.php diff --git a/packages/playground/cli/tests/mount-examples/wordpress/wp-admin/.gitkeep b/packages/playground/cli/src/tests/mount-examples/wordpress/wp-admin/.gitkeep similarity index 100% rename from packages/playground/cli/tests/mount-examples/wordpress/wp-admin/.gitkeep rename to packages/playground/cli/src/tests/mount-examples/wordpress/wp-admin/.gitkeep diff --git a/packages/playground/cli/tests/mount-examples/wordpress/wp-config-sample.php b/packages/playground/cli/src/tests/mount-examples/wordpress/wp-config-sample.php similarity index 100% rename from packages/playground/cli/tests/mount-examples/wordpress/wp-config-sample.php rename to packages/playground/cli/src/tests/mount-examples/wordpress/wp-config-sample.php diff --git a/packages/playground/cli/tests/mount-examples/wordpress/wp-config.php b/packages/playground/cli/src/tests/mount-examples/wordpress/wp-config.php similarity index 100% rename from packages/playground/cli/tests/mount-examples/wordpress/wp-config.php rename to packages/playground/cli/src/tests/mount-examples/wordpress/wp-config.php diff --git a/packages/playground/cli/tests/mount-examples/wordpress/wp-content/.gitkeep b/packages/playground/cli/src/tests/mount-examples/wordpress/wp-content/.gitkeep similarity index 100% rename from packages/playground/cli/tests/mount-examples/wordpress/wp-content/.gitkeep rename to packages/playground/cli/src/tests/mount-examples/wordpress/wp-content/.gitkeep diff --git a/packages/playground/cli/tests/mount-examples/wordpress/wp-content/plugins/.gitkeep b/packages/playground/cli/src/tests/mount-examples/wordpress/wp-content/plugins/.gitkeep similarity index 100% rename from packages/playground/cli/tests/mount-examples/wordpress/wp-content/plugins/.gitkeep rename to packages/playground/cli/src/tests/mount-examples/wordpress/wp-content/plugins/.gitkeep diff --git a/packages/playground/cli/tests/mount-examples/wordpress/wp-content/themes/.gitkeep b/packages/playground/cli/src/tests/mount-examples/wordpress/wp-content/themes/.gitkeep similarity index 100% rename from packages/playground/cli/tests/mount-examples/wordpress/wp-content/themes/.gitkeep rename to packages/playground/cli/src/tests/mount-examples/wordpress/wp-content/themes/.gitkeep diff --git a/packages/playground/cli/tests/mount-examples/wordpress/wp-includes/.gitkeep b/packages/playground/cli/src/tests/mount-examples/wordpress/wp-includes/.gitkeep similarity index 100% rename from packages/playground/cli/tests/mount-examples/wordpress/wp-includes/.gitkeep rename to packages/playground/cli/src/tests/mount-examples/wordpress/wp-includes/.gitkeep diff --git a/packages/playground/cli/tests/mount-examples/wordpress/wp-load.php b/packages/playground/cli/src/tests/mount-examples/wordpress/wp-load.php similarity index 100% rename from packages/playground/cli/tests/mount-examples/wordpress/wp-load.php rename to packages/playground/cli/src/tests/mount-examples/wordpress/wp-load.php diff --git a/packages/playground/cli/tests/mount-examples/wordpress/wp-login.php b/packages/playground/cli/src/tests/mount-examples/wordpress/wp-login.php similarity index 100% rename from packages/playground/cli/tests/mount-examples/wordpress/wp-login.php rename to packages/playground/cli/src/tests/mount-examples/wordpress/wp-login.php diff --git a/packages/playground/cli/tests/mount-examples/wp-content-only-mu-plugins/mu-plugins/.gitkeep b/packages/playground/cli/src/tests/mount-examples/wp-content-only-mu-plugins/mu-plugins/.gitkeep similarity index 100% rename from packages/playground/cli/tests/mount-examples/wp-content-only-mu-plugins/mu-plugins/.gitkeep rename to packages/playground/cli/src/tests/mount-examples/wp-content-only-mu-plugins/mu-plugins/.gitkeep diff --git a/packages/playground/cli/tests/mount-examples/wp-content-only-themes/themes/.gitkeep b/packages/playground/cli/src/tests/mount-examples/wp-content-only-themes/themes/.gitkeep similarity index 100% rename from packages/playground/cli/tests/mount-examples/wp-content-only-themes/themes/.gitkeep rename to packages/playground/cli/src/tests/mount-examples/wp-content-only-themes/themes/.gitkeep diff --git a/packages/playground/cli/tests/mount-examples/wp-content/plugins/.gitkeep b/packages/playground/cli/src/tests/mount-examples/wp-content/plugins/.gitkeep similarity index 100% rename from packages/playground/cli/tests/mount-examples/wp-content/plugins/.gitkeep rename to packages/playground/cli/src/tests/mount-examples/wp-content/plugins/.gitkeep diff --git a/packages/playground/cli/tests/mount-examples/wp-content/themes/.gitkeep b/packages/playground/cli/src/tests/mount-examples/wp-content/themes/.gitkeep similarity index 100% rename from packages/playground/cli/tests/mount-examples/wp-content/themes/.gitkeep rename to packages/playground/cli/src/tests/mount-examples/wp-content/themes/.gitkeep diff --git a/packages/playground/cli/tests/run-cli.spec.ts b/packages/playground/cli/src/tests/run-cli.spec.ts similarity index 99% rename from packages/playground/cli/tests/run-cli.spec.ts rename to packages/playground/cli/src/tests/run-cli.spec.ts index f0e43cb2bb..4bb33c85bb 100644 --- a/packages/playground/cli/tests/run-cli.spec.ts +++ b/packages/playground/cli/src/tests/run-cli.spec.ts @@ -1,6 +1,6 @@ import path from 'node:path'; -import { runCLI } from '../src/run-cli'; -import type { RunCLIServer } from '../src/run-cli'; +import { runCLI } from '../lib/run-cli'; +import type { RunCLIServer } from '../lib/run-cli'; import type { MockInstance } from 'vitest'; import { vi } from 'vitest'; import { mkdtemp, writeFile } from 'node:fs/promises'; @@ -42,6 +42,7 @@ describe.each(blueprintVersions)( afterEach(async () => { if (cliServer) { try { + await cliServer.playground.dispose(); await cliServer[Symbol.asyncDispose](); } catch { // Ignore any dispose-related errors diff --git a/packages/playground/cli/tests/test-running-unbuilt-cli.sh b/packages/playground/cli/src/tests/test-running-unbuilt-cli.sh similarity index 100% rename from packages/playground/cli/tests/test-running-unbuilt-cli.sh rename to packages/playground/cli/src/tests/test-running-unbuilt-cli.sh diff --git a/packages/playground/cli/tsconfig.json b/packages/playground/cli/tsconfig.json index cbca055c81..e4e0c46e69 100644 --- a/packages/playground/cli/tsconfig.json +++ b/packages/playground/cli/tsconfig.json @@ -1,7 +1,6 @@ { "extends": "../../../tsconfig.base.json", "compilerOptions": { - "moduleResolution": "node", "forceConsistentCasingInFileNames": true, "strict": true, "noImplicitOverride": true, diff --git a/packages/playground/cli/tsconfig.spec.json b/packages/playground/cli/tsconfig.spec.json index ac3e0778d9..fb994b913c 100644 --- a/packages/playground/cli/tsconfig.spec.json +++ b/packages/playground/cli/tsconfig.spec.json @@ -2,7 +2,18 @@ "extends": "./tsconfig.json", "compilerOptions": { "outDir": "../../../dist/out-tsc", - "types": ["vitest/globals", "vitest/importMeta", "node"] + "types": ["vitest/globals", "vitest/importMeta", "vite/client", "node"] }, - "include": ["tests/**/*.test.ts", "tests/**/*.spec.ts", "tests/**/*.d.ts"] + "include": [ + "vite.config.ts", + "src/**/*.test.ts", + "src/**/*.spec.ts", + "src/**/*.test.tsx", + "src/**/*.spec.tsx", + "src/**/*.test.js", + "src/**/*.spec.js", + "src/**/*.test.jsx", + "src/**/*.spec.jsx", + "src/**/*.d.ts" + ] } diff --git a/packages/playground/cli/vite.config.ts b/packages/playground/cli/vite.config.ts index f76ab1f9cf..74dc532612 100644 --- a/packages/playground/cli/vite.config.ts +++ b/packages/playground/cli/vite.config.ts @@ -84,6 +84,8 @@ const plugins = [ map: null, }; } + + return null; }, }, ] as PluginOption[]; @@ -135,8 +137,8 @@ export default defineConfig({ entry: { index: 'src/index.ts', cli: 'src/cli.ts', - 'worker-thread-v1': 'src/blueprints-v1/worker-thread-v1.ts', - 'worker-thread-v2': 'src/blueprints-v2/worker-thread-v2.ts', + 'worker-thread-v1': 'src/lib/blueprints-v1/worker-thread-v1.ts', + 'worker-thread-v2': 'src/lib/blueprints-v2/worker-thread-v2.ts', }, name: 'playground-cli', formats: ['es', 'cjs'], @@ -145,11 +147,7 @@ export default defineConfig({ test: { globals: true, - cache: { - dir: '../../../node_modules/.vitest', - }, environment: 'node', - include: ['tests/**/*.{test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}'], reporters: ['default'], // Increase timeout to: // - Ensure CLI tests can download WordPress diff --git a/tsconfig.base.json b/tsconfig.base.json index d7cf9501ec..8545b69523 100644 --- a/tsconfig.base.json +++ b/tsconfig.base.json @@ -17,7 +17,7 @@ "noPropertyAccessFromIndexSignature": false, "baseUrl": ".", "paths": { - "@php-wasm/cli": ["packages/php-wasm/cli/src/main.ts"], + "@php-wasm/cli": ["packages/php-wasm/cli/src/cli.ts"], "@php-wasm/fs-journal": [ "packages/php-wasm/fs-journal/src/index.ts" ],