Setting the file. One moment. Wpcli Inspect · Wp Wpcli And Ops · WordPress/agent-skills · Skills Docsscripts/wpcli_inspect.mjs
scripts/wpcli_inspect.mjs
JavaScript·90 lines·3 KB
(a
===
"--allow-root"
) args.allowRoot
=
true
;
9 if (a.startsWith("--path=")) args.path = a.slice("--path=".length);
10 if (a.startsWith("--url=")) args.url = a.slice("--url=".length);
11 }
12 return args;
13}
14
15function runWp(cmdArgs, { pathArg, urlArg, allowRoot }) {
16 const args = [];
17 if (allowRoot) args.push("--allow-root");
18 if (pathArg) args.push(`--path=${pathArg}`);
19 if (urlArg) args.push(`--url=${urlArg}`);
20 args.push(...cmdArgs);
21
22 const out = spawnSync("wp", args, { encoding: "utf8" });
23 return {
24 ok: out.status === 0,
25 status: out.status,
26 error: out.error ? { message: out.error.message, code: out.error.code } : null,
27 stdout: (out.stdout || "").trim(),
28 stderr: (out.stderr || "").trim(),
29 args,
30 };
31}
32
33function main() {
34 const opts = parseArgs(process.argv.slice(2));
35
36 const info = runWp(["--info"], { pathArg: null, urlArg: null, allowRoot: opts.allowRoot });
37 const report = {
38 tool: { name: "wpcli_inspect", version: TOOL_VERSION },
39 wpCli: {
40 available: info.ok,
41 info,
42 },
43 wordpress: {
44 path: opts.path,
45 url: opts.url,
46 isInstalled: null,
47 coreVersion: null,
48 isMultisite: null,
49 siteurl: null,
50 home: null,
51 },
52 notes: [],
53 };
54
55 if (!info.ok) {
56 report.notes.push("WP-CLI not available on PATH. Install WP-CLI or run inside the intended container/environment.");
57 process.stdout.write(`${JSON.stringify(report, null, 2)}\n`);
58 return;
59 }
60
61 const isInstalled = runWp(["core", "is-installed"], { pathArg: opts.path, urlArg: opts.url, allowRoot: opts.allowRoot });
62 report.wordpress.isInstalled = isInstalled.ok;
63
64 if (!isInstalled.ok) {
65 report.notes.push("WordPress not detected at the given path/url. Check --path/--url (multisite) and that wp-config.php is present.");
66 process.stdout.write(`${JSON.stringify(report, null, 2)}\n`);
67 return;
68 }
69
70 const coreVersion = runWp(["core", "version"], { pathArg: opts.path, urlArg: opts.url, allowRoot: opts.allowRoot });
71 report.wordpress.coreVersion = coreVersion.ok ? coreVersion.stdout : null;
72
73 const isMultisite = runWp(["core", "is-installed", "--network"], {
74 pathArg: opts.path,
75 urlArg: opts.url,
76 allowRoot: opts.allowRoot,
77 });
78 // If network check passes, we can assume multisite. If it fails, it might still be multisite depending on context.
79 report.wordpress.isMultisite = isMultisite.ok;
80
81 const siteurl = runWp(["option", "get", "siteurl"], { pathArg: opts.path, urlArg: opts.url, allowRoot: opts.allowRoot });
82 report.wordpress.siteurl = siteurl.ok ? siteurl.stdout : null;
83
84 const home = runWp(["option", "get", "home"], { pathArg: opts.path, urlArg: opts.url, allowRoot: opts.allowRoot });
85 report.wordpress.home = home.ok ? home.stdout : null;
86
87 process.stdout.write(`${JSON.stringify(report, null, 2)}\n`);
88}
89
90main();