-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
wp-now: Add
executeWPCli()
function to download and execute WP-CLI …
…(#395) ## What? - Add the `executeWPCli` function to download and execute `wp-cli. - In the future, we may include the command `wp-now wp`. Currently, we drop it out until we improve the pthreads execution. Currently a PR in progress: WordPress/wordpress-playground#346 - Surface `emscriptenOptions` to catch print and print error for `wp-cli` execution. ## Why? - See https://github.com/WordPress/wordpress-playground/issues/269 ## How? It downloads the wp-cli.phar file if the file doesn't exist, then uses `php.cli()` to execute it. There are some limitations in the `wp-cli` features. Some of them may not work. ## Testing Instructions - Check out this branch. - Copy your path to your theme or plugin - After installing and building the project, run: - Run the tests `npx nx test wp-now` - Observe the tests pass. <!--details> <summary>~`WP_NOW_PROJECT_PATH=/path/to/your-theme-or-plugin npx nx preview wp-now wp user list`~ </summary> ``` > nx run wp-now:preview wp user list +----+------------+--------------+--------------+--------------+---------------+ | ID | user_login | display_name | user_email | user_registe | roles | | | | | | red | | +----+------------+--------------+--------------+--------------+---------------+ | 1 | admin | admin | admin@localh | 2023-05-19 1 | administrator | | | | | ost.com | 7:33:35 | | +----+------------+--------------+--------------+--------------+---------------+ > NX Successfully ran target preview for project wp-now and 12 tasks it depends on (10s) With additional flags: wp user list ``` </details!--> --------- Co-authored-by: Daniel Bachhuber <[email protected]>
- Loading branch information
1 parent
a76e14b
commit 00aa039
Showing
8 changed files
with
166 additions
and
4 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import startWPNow from './wp-now'; | ||
import { downloadWPCLI } from './download'; | ||
import { disableOutput } from './output'; | ||
import getWpCliPath from './get-wp-cli-path'; | ||
import getWpNowConfig from './config'; | ||
import { DEFAULT_PHP_VERSION, DEFAULT_WORDPRESS_VERSION } from './constants'; | ||
|
||
/** | ||
* 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[]) { | ||
await downloadWPCLI(); | ||
disableOutput(); | ||
const options = await getWpNowConfig({ | ||
php: DEFAULT_PHP_VERSION, | ||
wp: DEFAULT_WORDPRESS_VERSION, | ||
path: process.env.WP_NOW_PROJECT_PATH || process.cwd(), | ||
}); | ||
const { phpInstances, options: wpNowOptions } = await startWPNow({ | ||
...options, | ||
numberOfPhpInstances: 2, | ||
}); | ||
const [, php] = phpInstances; | ||
|
||
try { | ||
php.useHostFilesystem(); | ||
await php.cli([ | ||
'php', | ||
getWpCliPath(), | ||
`--path=${wpNowOptions.documentRoot}`, | ||
...args, | ||
]); | ||
} catch (resultOrError) { | ||
const success = | ||
resultOrError.name === 'ExitStatus' && resultOrError.status === 0; | ||
if (!success) { | ||
throw resultOrError; | ||
} | ||
} | ||
} |
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,13 @@ | ||
import path from 'path'; | ||
import getWpNowPath from './get-wp-now-path'; | ||
import getWpCliTmpPath from './get-wp-cli-tmp-path'; | ||
|
||
/** | ||
* The path for wp-cli phar file within the WP Now folder. | ||
*/ | ||
export default function getWpCliPath() { | ||
if (process.env.NODE_ENV !== 'test') { | ||
return path.join(getWpNowPath(), 'wp-cli.phar'); | ||
} | ||
return getWpCliTmpPath(); | ||
} |
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,11 @@ | ||
import path from 'path'; | ||
import os from 'os'; | ||
|
||
/** | ||
* The full path to the hidden WP-CLI folder in the user's tmp directory. | ||
*/ | ||
export default function getWpCliTmpPath() { | ||
const tmpDirectory = os.tmpdir(); | ||
|
||
return path.join(tmpDirectory, `wp-now-tests-wp-cli-hidden-folder`); | ||
} |
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