@wp-playground/blueprints 0.1.29 → 0.1.32

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,11 +1,10 @@
1
- import { UniversalPHP } from '@php-wasm/universal';
2
- import { BaseStep } from '.';
1
+ import { StepHandler } from '.';
3
2
 
4
- export interface LoginStep extends BaseStep {
3
+ export type LoginStep = {
5
4
  step: 'login';
6
5
  username?: string;
7
6
  password?: string;
8
- }
7
+ };
9
8
 
10
9
  /**
11
10
  * Logs in to the Playground.
@@ -16,11 +15,12 @@ export interface LoginStep extends BaseStep {
16
15
  * @param user The user to log in as. Defaults to 'admin'.
17
16
  * @param password The password to log in with. Defaults to 'password'.
18
17
  */
19
- export async function login(
20
- playground: UniversalPHP,
21
- user = 'admin',
22
- password = 'password'
23
- ) {
18
+ export const login: StepHandler<LoginStep> = async (
19
+ playground,
20
+ { username = 'admin', password = 'password' } = {},
21
+ progress
22
+ ) => {
23
+ progress?.tracker.setCaption(progress?.initialCaption || 'Logging in');
24
24
  await playground.request({
25
25
  url: '/wp-login.php',
26
26
  });
@@ -29,9 +29,9 @@ export async function login(
29
29
  url: '/wp-login.php',
30
30
  method: 'POST',
31
31
  formData: {
32
- log: user,
32
+ log: username,
33
33
  pwd: password,
34
34
  rememberme: 'forever',
35
35
  },
36
36
  });
37
- }
37
+ };
@@ -1,7 +1,6 @@
1
- import { UniversalPHP } from '@php-wasm/universal';
2
- import { BaseStep } from '.';
1
+ import { StepHandler } from '.';
3
2
 
4
- export interface RunWpInstallationWizardStep extends BaseStep {
3
+ export interface RunWpInstallationWizardStep {
5
4
  step: 'runWpInstallationWizard';
6
5
  options: WordPressInstallationOptions;
7
6
  }
@@ -17,10 +16,9 @@ export interface WordPressInstallationOptions {
17
16
  * @param playground The playground client.
18
17
  * @param options Installation options.
19
18
  */
20
- export async function runWpInstallationWizard(
21
- playground: UniversalPHP,
22
- options: WordPressInstallationOptions
23
- ) {
19
+ export const runWpInstallationWizard: StepHandler<
20
+ RunWpInstallationWizardStep
21
+ > = async (playground, { options }) => {
24
22
  await playground.request({
25
23
  url: '/wp-admin/install.php?step=2',
26
24
  method: 'POST',
@@ -37,4 +35,4 @@ export async function runWpInstallationWizard(
37
35
  admin_email: 'admin@localhost.com',
38
36
  },
39
37
  });
40
- }
38
+ };
@@ -1,24 +1,16 @@
1
- import { PHPResponse, UniversalPHP } from '@php-wasm/universal';
1
+ import { PHPResponse } from '@php-wasm/universal';
2
2
  import { phpVar } from '@php-wasm/util';
3
- import { BaseStep } from '.';
3
+ import { StepHandler } from '.';
4
4
 
5
- export type SiteOptions = Record<string, unknown>;
6
-
7
- export interface SetSiteOptionsStep extends BaseStep {
5
+ export type SetSiteOptionsStep = {
8
6
  step: 'setSiteOptions';
9
- options: SiteOptions;
10
- }
11
-
12
- export interface UpdateUserMetaStep extends BaseStep {
13
- step: 'updateUserMeta';
14
- meta: UserMeta;
15
- userId: number;
16
- }
7
+ options: Record<string, unknown>;
8
+ };
17
9
 
18
- export async function setSiteOptions(
19
- client: UniversalPHP,
20
- options: SiteOptions
21
- ) {
10
+ export const setSiteOptions: StepHandler<SetSiteOptionsStep> = async (
11
+ client,
12
+ options
13
+ ) => {
22
14
  const result = await client.run({
23
15
  code: `<?php
24
16
  include 'wordpress/wp-load.php';
@@ -30,14 +22,17 @@ export async function setSiteOptions(
30
22
  `,
31
23
  });
32
24
  assertSuccess(result);
33
- }
25
+ };
34
26
 
35
- export type UserMeta = Record<string, unknown>;
36
- export async function updateUserMeta(
37
- client: UniversalPHP,
38
- meta: UserMeta,
39
- userId: number
40
- ) {
27
+ export interface UpdateUserMetaStep {
28
+ step: 'updateUserMeta';
29
+ meta: Record<string, unknown>;
30
+ userId: number;
31
+ }
32
+ export const updateUserMeta: StepHandler<UpdateUserMetaStep> = async (
33
+ client,
34
+ { meta, userId }
35
+ ) => {
41
36
  const result = await client.run({
42
37
  code: `<?php
43
38
  include 'wordpress/wp-load.php';
@@ -49,7 +44,7 @@ export async function updateUserMeta(
49
44
  `,
50
45
  });
51
46
  assertSuccess(result);
52
- }
47
+ };
53
48
 
54
49
  async function assertSuccess(result: PHPResponse) {
55
50
  if (result.text !== 'Success') {
package/src/lib/run.ts DELETED
@@ -1,154 +0,0 @@
1
- import { UniversalPHP } from '@php-wasm/universal';
2
- import {
3
- activatePlugin,
4
- installPlugin,
5
- installTheme,
6
- login,
7
- replaceSite,
8
- setSiteOptions,
9
- submitImporterForm,
10
- updateUserMeta,
11
- } from './steps';
12
- import { zipNameToHumanName } from './steps/common';
13
- import { unzip } from './steps/import-export';
14
- import { CompiledBlueprint, CompiledStep } from './compile';
15
- import { Resource } from './resources';
16
-
17
- export async function runBlueprintSteps(
18
- compiledBlueprint: CompiledBlueprint,
19
- playground: UniversalPHP
20
- ) {
21
- // Start fetching resources early
22
- for (const { resource } of compiledBlueprint.resources) {
23
- resource.resolve();
24
- }
25
-
26
- // Run all parsed steps
27
- for (const step of compiledBlueprint.steps) {
28
- await runBlueprintStep(step, playground);
29
- }
30
- }
31
-
32
- async function runBlueprintStep(step: CompiledStep, playground: UniversalPHP) {
33
- step.progress.fillSlowly();
34
-
35
- const args = step.args;
36
- switch (args.step) {
37
- case 'installPlugin': {
38
- const name = zipNameToHumanName(args.pluginZipFile.name);
39
- step.progress.setCaption(`Installing the ${name} plugin`);
40
- try {
41
- await installPlugin(
42
- playground,
43
- await args.pluginZipFile.resolve(),
44
- args.options
45
- );
46
- } catch (error) {
47
- console.error(
48
- `Proceeding without the ${name} plugin. Could not install it in wp-admin. ` +
49
- `The original error was: ${error}`
50
- );
51
- console.error(error);
52
- }
53
- break;
54
- }
55
- case 'installTheme': {
56
- const name = zipNameToHumanName(args.themeZipFile.name);
57
- step.progress.setCaption(`Installing the ${name} theme`);
58
- try {
59
- await installTheme(
60
- playground,
61
- await args.themeZipFile.resolve(),
62
- args.options
63
- );
64
- } catch (error) {
65
- console.error(
66
- `Proceeding without the ${name} theme. Could not install it in wp-admin. ` +
67
- `The original error was: ${error}`
68
- );
69
- console.error(error);
70
- }
71
- break;
72
- }
73
- case 'login':
74
- step.progress.setCaption(
75
- `Logging in${args.username ? ' as ' + args.username : ''}`
76
- );
77
- await login(playground, args.username, args.password);
78
- break;
79
- case 'activatePlugin':
80
- step.progress.setCaption(`Activating ${args.plugin}`);
81
- await activatePlugin(playground, args.plugin);
82
- break;
83
- case 'replaceSite':
84
- await replaceSite(playground, await args.fullSiteZip.resolve());
85
- break;
86
- case 'rm':
87
- await playground.unlink(args.path);
88
- break;
89
- case 'rmdir':
90
- await playground.rmdir(args.path);
91
- break;
92
- case 'cp':
93
- await playground.writeFile(
94
- args.toPath,
95
- await playground.readFileAsBuffer(args.fromPath)
96
- );
97
- break;
98
- case 'mv':
99
- await playground.mv(args.fromPath, args.toPath);
100
- break;
101
- case 'mkdir':
102
- await playground.mkdirTree(args.path);
103
- break;
104
- case 'importFile':
105
- await submitImporterForm(playground, await args.file.resolve());
106
- break;
107
- case 'setPhpIniEntry':
108
- await playground.setPhpIniEntry(args.key, args.value);
109
- break;
110
- case 'setSiteOptions':
111
- await setSiteOptions(playground, args.options);
112
- break;
113
- case 'updateUserMeta':
114
- await updateUserMeta(playground, args.meta, args.userId);
115
- break;
116
- case 'request':
117
- await playground.request(args.request);
118
- break;
119
- case 'runPHP': {
120
- await playground.run({
121
- code: args.code,
122
- });
123
- break;
124
- }
125
- case 'runPHPWithOptions':
126
- await playground.run(args.options);
127
- break;
128
- case 'writeFile':
129
- await playground.writeFile(
130
- args.path,
131
- args.data instanceof Resource
132
- ? await fileToUint8Array(await args.data.resolve())
133
- : args.data
134
- );
135
- break;
136
- case 'unzip':
137
- await unzip(playground, args.zipPath, args.extractToPath);
138
- break;
139
- default:
140
- throw new Error(`Invalid step: ${step.args.step}`);
141
- }
142
- step.progress.finish();
143
- }
144
-
145
- function fileToUint8Array(file: File) {
146
- return new Promise<Uint8Array>((resolve, reject) => {
147
- const reader = new FileReader();
148
- reader.onload = () => {
149
- resolve(new Uint8Array(reader.result as ArrayBuffer));
150
- };
151
- reader.onerror = reject;
152
- reader.readAsArrayBuffer(file);
153
- });
154
- }