Programmatic Usage of Playground CLI
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. The options you pass to runCLI map directly to the CLI flags.
Running a WordPress instance with a specific version
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.
import { runCLI } from "@wp-playground/cli";
const cliServer = await runCLI({
command: 'server',
php: '8.3',
wp: 'latest',
login: true,
});
Run the code above using your preferred TypeScript runtime, e.g. tsx:
npx tsx my-script.ts
Setting a blueprint
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.
import { runCLI, RunCLIServer } from "@wp-playground/cli";
const cliServer: RunCLIServer = await runCLI({
command: 'server',
wp: 'latest',
blueprint: {
steps: [
{
"step": "setSiteOptions",
"options": {
"blogname": "Blueprint Title",
"blogdescription": "A great blog description"
}
}
],
},
});
For full type-safety when defining your blueprint object, you can import and use the BlueprintDeclaration type from the @wp-playground/blueprints package:
import type { BlueprintDeclaration } from '@wp-playground/blueprints';
const myBlueprint: BlueprintDeclaration = {
landingPage: "/wp-admin/",
steps: [
{
"step": "installTheme",
"themeData": {
"resource": "wordpress.org/themes",
"slug": "twentytwentyone"
},
"options": {
"activate": true
}
}
]
};