Skip to content

Commit 89323c0

Browse files
fellyphadamziel
andauthored
Adding runCLI section to the Playground CLI page (#2583)
## Motivation for the change, related issues This PR enhances the Playground CLI documentation by introducing a new major section on `Programmatic Usage with JavaScript`. This new content details how to control the CLI via the `runCLI` function, which is a key feature for enabling advanced use cases like automated end-to-end testing. In addition to this new content, the PR also includes some minor improvements to the existing documentation to enhance clarity, correct typos, and address minor inaccuracies. Overall, these changes make the documentation more complete and user-friendly, especially for developers looking to integrate the Playground CLI into their automated workflows. --------- Co-authored-by: Adam Zieliński <[email protected]>
1 parent c85dab0 commit 89323c0

File tree

1 file changed

+122
-4
lines changed

1 file changed

+122
-4
lines changed

packages/docs/site/docs/developers/05-local-development/04-wp-playground-cli.md

Lines changed: 122 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,15 @@ The Playground CLI requires Node.js 20.18 or higher, which is the recommended Lo
2020

2121
## Quickstart
2222

23-
Running the Playground CLI is as simple as go to a command-line and run:
23+
To run the Playground CLI, open a command line and use the following command:
2424

2525
```bash
2626
npx @wp-playground/cli@latest server
2727
```
2828

2929
![Playground CLI in Action](@site/static/img/developers/npx-wp-playground-server.gif)
3030

31-
With the previous command, you only get a fresh WordPress instance to test. Most of the developers want to see their work running. If this is your case, test a plugin or a theme. You can run the CLI on your project folder and run the Playground CLI with the `--auto-mount` flag:
31+
With the previous command, you only get a fresh WordPress instance to test. Most developers will want to test their own work. To test a plugin or a theme, navigate to your project folder and run the CLI with the `--auto-mount` flag:
3232

3333
```bash
3434
cd my-plugin-or-theme-directory
@@ -40,7 +40,7 @@ npx @wp-playground/cli@latest server --auto-mount
4040
By default, the CLI loads the latest stable version of WordPress and PHP 8.3 due to its improved performance. To specify your preferred versions, you can use the flag `--wp=<version>` and `--php=<version>`:
4141

4242
```bash
43-
npx @wp-playground/cli@latest server --wp=6.8 --php=8.4
43+
npx @wp-playground/cli@latest server --wp=6.8 --php=8.3
4444
```
4545

4646
### Loading Blueprints
@@ -117,8 +117,126 @@ The `server` command supports the following optional arguments:
117117

118118
## Need some help with the CLI?
119119

120-
With the Playground CLI, you can use the `--help` to get some support about the available commands.
120+
With the Playground CLI, you can use the `--help` flag to get the full list of available commands and arguments.
121121

122122
```bash
123123
npx @wp-playground/cli@latest --help
124124
```
125+
126+
## Programmatic Usage with JavaScript
127+
128+
The Playground CLI can also be controlled programmatically from your JavaScript/TypeScript code using the `runCLI` function. This gives you direct access to all CLI functionalities within your code, which is useful for automating end-to-end tests. Let's cover the basics of using `runCLI`.
129+
130+
### Running a WordPress instance with a specific version
131+
132+
Using the `runCLI` function, you can specify options like the PHP and WordPress versions. In the example below, we request PHP 8.3, the latest version of WordPress, and to be automatically logged in. All supported arguments are defined in the `RunCLIArgs` type.
133+
134+
```TypeScript
135+
import { runCLI, RunCLIArgs, RunCLIServer } from "@wp-playground/cli";
136+
137+
let cliServer: RunCLIServer;
138+
139+
cliServer = await runCLI({
140+
command: 'server',
141+
php: '8.3',
142+
wp: 'latest',
143+
login: true
144+
} as RunCLIArgs);
145+
```
146+
147+
To execute the code above, the developer can set their preferred method. A simple way to execute this code is to save it as a `.ts` file and run it with a tool like `tsx`. For example: `tsx my-script.ts`
148+
149+
### Setting a Blueprint
150+
151+
You can provide a blueprint in two ways: either as an object literal directly passed to the `blueprint` property, or as a string containing the path to an external `.json` file.
152+
153+
```TypeScript
154+
import { runCLI, RunCLIServer } from "@wp-playground/cli";
155+
156+
let cliServer: RunCLIServer;
157+
158+
cliServer = await runCLI({
159+
command: 'server',
160+
wp: 'latest',
161+
blueprint: {
162+
steps: [
163+
{
164+
"step": "setSiteOptions",
165+
"options": {
166+
"blogname": "Blueprint Title",
167+
"blogdescription": "A great blog description"
168+
}
169+
}
170+
],
171+
},
172+
});
173+
```
174+
175+
For full type-safety when defining your blueprint object, you can import and use the `BlueprintDeclaration` type from the `@wp-playground/blueprints` package:
176+
177+
```TypeScript
178+
import type { BlueprintDeclaration } from '@wp-playground/blueprints';
179+
180+
const myBlueprint: BlueprintDeclaration = {
181+
landingPage: "/wp-admin/",
182+
steps: [
183+
{
184+
"step": "installTheme",
185+
"themeData": {
186+
"resource": "wordpress.org/themes",
187+
"slug": "twentytwentyone"
188+
},
189+
"options": {
190+
"activate": true
191+
}
192+
}
193+
]
194+
};
195+
```
196+
197+
### Mounting a plugin programmatically
198+
199+
It is possible to mount local directories programmatically using `runCLI`. The options `mount` and `mount-before-install` are available. The `hostPath` property expects a path to a directory on your local machine. This path should be relative to where your script is being executed.
200+
201+
```TypeScript
202+
cliServer = await runCLI({
203+
command: 'server',
204+
login: true,
205+
'mount-before-install': [
206+
{
207+
hostPath: './[my-plugin-local-path]',
208+
vfsPath: '/wordpress/wp-content/plugins/my-plugin',
209+
},
210+
],
211+
});
212+
```
213+
214+
With those options we can combine mounting parts of the project with blueprints, for example:
215+
216+
```TypeScript
217+
218+
import { runCLI, RunCLIArgs, RunCLIServer } from "@wp-playground/cli";
219+
220+
let cliServer: RunCLIServer;
221+
222+
cliServer = await runCLI({
223+
command: 'server',
224+
php: '8.3',
225+
wp: 'latest',
226+
login: true,
227+
mount: [
228+
{
229+
"hostPath": "./plugin/",
230+
"vfsPath": "/wordpress/wp-content/plugins/playwright-test"
231+
}
232+
],
233+
blueprint: {
234+
steps: [
235+
{
236+
"step": "activatePlugin",
237+
"pluginPath": "/wordpress/wp-content/plugins/playwright-test/plugin-playwright.php"
238+
}
239+
]
240+
}
241+
} as RunCLIArgs);
242+
```

0 commit comments

Comments
 (0)