-
Notifications
You must be signed in to change notification settings - Fork 209
/
wp-cli.ts
138 lines (123 loc) · 3.13 KB
/
wp-cli.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import { PHPResponse } from '@php-wasm/universal';
import { StepHandler } from '.';
import { phpVar } from '@php-wasm/util';
/**
* @inheritDoc wpCLI
* @hasRunnableExample
* @example
*
* <code>
* {
* "step": "wp-cli",
* "command": "wp post create --post_title='Test post' --post_excerpt='Some content'"
* }
* </code>
*/
export interface WPCLIStep {
/** The step identifier. */
step: 'wp-cli';
/** The WP CLI command to run. */
command: string | string[];
/** wp-cli.phar path */
wpCliPath?: string;
}
/**
* Runs PHP code using [WP-CLI](https://developer.wordpress.org/cli/commands/).
*/
export const wpCLI: StepHandler<WPCLIStep, Promise<PHPResponse>> = async (
playground,
{ command, wpCliPath = '/tmp/wp-cli.phar' }
) => {
if (!(await playground.fileExists(wpCliPath))) {
throw new Error(`wp-cli.phar not found at ${wpCliPath}`);
}
let args: string[];
if (typeof command === 'string') {
command = command.trim();
args = splitShellCommand(command);
} else {
args = command;
}
const cmd = args.shift();
if (cmd !== 'wp') {
throw new Error(`The first argument must be "wp".`);
}
await playground.writeFile('/tmp/stdout', '');
await playground.writeFile('/tmp/stderr', '');
await playground.writeFile(
'/wordpress/run-cli.php',
`<?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' );
// Set the argv global.
$GLOBALS['argv'] = array_merge([
"/tmp/wp-cli.phar",
"--path=/wordpress"
], ${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('/tmp/stderr', 'wb'));
require( ${phpVar(wpCliPath)} );
`
);
const result = await playground.run({
scriptPath: '/wordpress/run-cli.php',
});
if (result.errors) {
throw new Error(result.errors);
}
return result;
};
/**
* Naive shell command parser.
* Ensures that commands like `wp option set blogname "My blog name"` are split into
* `['wp', 'option', 'set', 'blogname', 'My blog name']` instead of
* `['wp', 'option', 'set', 'blogname', 'My', 'blog', 'name']`.
*
* @param command
* @returns
*/
export function splitShellCommand(command: string) {
const MODE_NORMAL = 0;
const MODE_IN_QUOTE = 1;
let mode = MODE_NORMAL;
let quote = '';
const parts: string[] = [];
let currentPart = '';
for (let i = 0; i < command.length; i++) {
const char = command[i];
if (mode === MODE_NORMAL) {
if (char === '"' || char === "'") {
mode = MODE_IN_QUOTE;
quote = char;
} else if (char.match(/\s/)) {
if (currentPart) {
parts.push(currentPart);
}
currentPart = '';
} else {
currentPart += char;
}
} else if (mode === MODE_IN_QUOTE) {
if (char === '\\') {
i++;
currentPart += command[i];
} else if (char === quote) {
mode = MODE_NORMAL;
quote = '';
} else {
currentPart += char;
}
}
}
if (currentPart) {
parts.push(currentPart);
}
return parts;
}