-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathwp-cli-command-handlers.ts
More file actions
80 lines (66 loc) · 2.21 KB
/
Copy pathwp-cli-command-handlers.ts
File metadata and controls
80 lines (66 loc) · 2.21 KB
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
import { randomBytes } from "node:crypto"
import { argValue } from "./command-args.js"
import { phpCliStreamConstants } from "./php-snippets.js"
interface WpCliTemporaryScriptFilesystem {
writeFile(path: string, contents: string): Promise<void>
unlink(path: string): Promise<void> | void
}
export function wpCliCommandFromArgs(args: string[]): string {
const explicit = argValue(args, "command")
if (explicit) {
return explicit.trim()
}
return args.join(" ").trim()
}
export function shellArgv(command: string): string[] {
const args: string[] = []
let current = ""
let quote = ""
for (let index = 0; index < command.length; index++) {
const char = command[index]
if (!quote && /\s/.test(char)) {
if (current) {
args.push(current)
current = ""
}
continue
}
if ((char === "'" || char === '"') && (!quote || quote === char)) {
quote = quote ? "" : char
continue
}
if (char === "\\" && index + 1 < command.length) {
current += command[++index]
continue
}
current += char
}
if (quote) {
throw new Error("Unclosed quote in wordpress.wp-cli command")
}
if (current) {
args.push(current)
}
return args
}
export function wpCliPhpScript(argv: string[]): string {
return `<?php
putenv('SHELL_PIPE=0');
$GLOBALS['argv'] = array_merge(array('/tmp/wp-cli.phar', '--path=/wordpress', '--no-color'), json_decode(${JSON.stringify(JSON.stringify(argv))}, true));
${phpCliStreamConstants()}
require '/tmp/wp-cli.phar';
`
}
export async function runWithTemporaryWpCliScript<T>(filesystem: WpCliTemporaryScriptFilesystem, runtimeId: string, argv: string[], run: (scriptPath: string) => Promise<T>): Promise<T> {
const runtimeNamespace = runtimeId.replace(/[^A-Za-z0-9_-]/g, "-").slice(0, 80) || "runtime"
const scriptPath = `/tmp/wp-codebox-wp-cli-${runtimeNamespace}-${randomBytes(16).toString("hex")}.php`
await filesystem.writeFile(scriptPath, wpCliPhpScript(argv))
try {
return await run(scriptPath)
} finally {
await Promise.resolve(filesystem.unlink(scriptPath)).catch(() => undefined)
}
}
export function cleanWpCliOutput(output: string): string {
return output.replace(/^#!\/usr\/bin\/env php\r?\n/, "")
}