-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
wp-now: Update Playground and PHP WASM dependencies (#350)
- update Playground dependencies from 0.6.16 to 1.0.2 --------- Co-authored-by: sejas <[email protected]>
- Loading branch information
1 parent
616f6cb
commit 3382daa
Showing
9 changed files
with
1,788 additions
and
248 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,121 @@ | ||
import startWPNow from './wp-now'; | ||
import { downloadWPCLI } from './download'; | ||
import { disableOutput } from './output'; | ||
import { rootCertificates } from 'tls'; | ||
import getWpCliPath from './get-wp-cli-path'; | ||
import getWpNowConfig from './config'; | ||
import { DEFAULT_PHP_VERSION, DEFAULT_WORDPRESS_VERSION } from './constants'; | ||
import { dirname } from 'path'; | ||
import { phpVar } from '@php-wasm/util'; | ||
import { createNodeFsMountHandler, loadNodeRuntime } from '@php-wasm/node'; | ||
import { | ||
PHP, | ||
MountHandler, | ||
writeFiles, | ||
setPhpIniEntries, | ||
} from '@php-wasm/universal'; | ||
import { readFileSync } from 'fs'; | ||
|
||
const isWindows = process.platform === 'win32'; | ||
|
||
/** | ||
* This is an unstable API. Multiple wp-cli commands may not work due to a current limitation on php-wasm and pthreads. | ||
* @param args The arguments to pass to wp-cli. | ||
*/ | ||
export async function executeWPCli(args: string[]) { | ||
export async function executeWPCli( | ||
projectPath: string, | ||
args: string[], | ||
{ phpVersion }: { phpVersion?: string } = {} | ||
): Promise<{ stdout: string; stderr: string; exitCode: number }> { | ||
await downloadWPCLI(); | ||
disableOutput(); | ||
const options = await getWpNowConfig({ | ||
php: DEFAULT_PHP_VERSION, | ||
php: phpVersion || DEFAULT_PHP_VERSION, | ||
wp: DEFAULT_WORDPRESS_VERSION, | ||
path: process.env.WP_NOW_PROJECT_PATH || process.cwd(), | ||
path: projectPath, | ||
}); | ||
const { phpInstances, options: wpNowOptions } = await startWPNow({ | ||
...options, | ||
numberOfPhpInstances: 2, | ||
|
||
const id = await loadNodeRuntime(options.phpVersion); | ||
const php = new PHP(id); | ||
php.mkdir(options.documentRoot); | ||
await php.mount( | ||
options.documentRoot, | ||
createNodeFsMountHandler(projectPath) as unknown as MountHandler | ||
); | ||
|
||
//Set the SAPI name to cli before running the script | ||
await php.setSapiName('cli'); | ||
|
||
php.mkdir('/tmp'); | ||
|
||
const wpCliPath = '/tmp/wp-cli.phar'; | ||
const stderrPath = '/tmp/stderr'; | ||
const runCliPath = '/tmp/run-cli.php'; | ||
const createFiles = { | ||
[wpCliPath]: readFileSync(getWpCliPath()), | ||
[stderrPath]: '', | ||
[runCliPath]: `<?php | ||
// Set up the environment to emulate a shell script | ||
// call. | ||
// Set SHELL_PIPE to 0 to ensure WP-CLI formats | ||
// the output as ASCII tables. | ||
// @see https://github.com/wp-cli/wp-cli/issues/1102 | ||
putenv( 'SHELL_PIPE=0' ); | ||
// When running PHP on Playground, the value of constant PHP_OS is set to Linux. | ||
// This implies that platform-specific logic won't work as expected. To solve this, | ||
// we use an environment variable to ensure WP-CLI runs the Windows-specific logic. | ||
putenv( 'WP_CLI_TEST_IS_WINDOWS=${isWindows ? 1 : 0}' ); | ||
// Set the argv global. | ||
$GLOBALS['argv'] = array_merge([ | ||
"${wpCliPath}", | ||
"--path=${options.documentRoot}" | ||
], ${phpVar(args)}); | ||
// Provide stdin, stdout, stderr streams outside of | ||
// the CLI SAPI. | ||
define('STDIN', fopen('php://stdin', 'rb')); | ||
define('STDOUT', fopen('php://stdout', 'wb')); | ||
define('STDERR', fopen('${stderrPath}', 'wb')); | ||
// Force disabling WordPress debugging mode to avoid parsing issues of WP-CLI command result | ||
define('WP_DEBUG', false); | ||
// Filter out errors below ERROR level to avoid parsing issues of WP-CLI command result | ||
error_reporting(E_ERROR); | ||
// WP-CLI uses this argument for checking updates. Seems it's not defined by Playground | ||
// when running a script, so we explicitly set it. | ||
// Reference: https://github.com/wp-cli/wp-cli/blob/main/php/WP_CLI/Runner.php#L1889 | ||
$_SERVER['argv'][0] = '${wpCliPath}'; | ||
require( '${wpCliPath}' );`, | ||
['/internal/shared/ca-bundle.crt']: rootCertificates.join('\n'), | ||
}; | ||
|
||
await writeFiles(php, '/', createFiles); | ||
|
||
await setPhpIniEntries(php, { | ||
'openssl.cafile': '/internal/shared/ca-bundle.crt', | ||
}); | ||
const [, php] = phpInstances; | ||
|
||
try { | ||
const vfsWpCliPath = '/wp-cli/wp-cli.phar'; | ||
php.mount(dirname(getWpCliPath()), dirname(vfsWpCliPath)); | ||
await php.cli([ | ||
'php', | ||
vfsWpCliPath, | ||
`--path=${wpNowOptions.documentRoot}`, | ||
...args, | ||
]); | ||
} catch (resultOrError) { | ||
const success = | ||
resultOrError.name === 'ExitStatus' && resultOrError.status === 0; | ||
if (!success) { | ||
throw resultOrError; | ||
} | ||
php.chdir(options.documentRoot); | ||
const result = await php.run({ | ||
scriptPath: runCliPath, | ||
}); | ||
const stderr = php | ||
.readFileAsText(stderrPath) | ||
.replace('PHP.run() output was: #!/usr/bin/env php', '') | ||
.trim(); | ||
return { | ||
stdout: result.text.replace('#!/usr/bin/env php', '').trim(), | ||
stderr, | ||
exitCode: result.exitCode, | ||
}; | ||
} catch (error) { | ||
const stderr = php | ||
.readFileAsText(stderrPath) | ||
.replace('PHP.run() output was: #!/usr/bin/env php', '') | ||
.trim(); | ||
return { stdout: '', stderr: stderr, exitCode: 1 }; | ||
} finally { | ||
php.exit(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
packages/wp-now/src/tests/mode-examples/theme-with-assets/page.html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
<title>Test Page that should be compressed</title> | ||
</head> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.