Setting the file. One moment. Perf Inspect · Wp Performance · WordPress/agent-skills · Skills Docsscripts/perf_inspect.mjs
scripts/perf_inspect.mjs
JavaScript·127 lines·5 KB
, allowRoot:
false
};
9 for (const a of argv) {
10 if (a === "--allow-root") args.allowRoot = true;
11 if (a.startsWith("--path=")) args.path = a.slice("--path=".length);
12 if (a.startsWith("--url=")) args.url = a.slice("--url=".length);
13 }
14 return args;
15}
16
17function existsFile(p) {
18 try {
19 return fs.statSync(p).isFile();
20 } catch {
21 return false;
22 }
23}
24
25function runWp(cmdArgs, { pathArg, urlArg, allowRoot }) {
26 const args = [];
27 if (allowRoot) args.push("--allow-root");
28 if (pathArg) args.push(`--path=${pathArg}`);
29 if (urlArg) args.push(`--url=${urlArg}`);
30 args.push(...cmdArgs);
31
32 const out = spawnSync("wp", args, { encoding: "utf8" });
33 return {
34 ok: out.status === 0,
35 status: out.status,
36 error: out.error ? { message: out.error.message, code: out.error.code } : null,
37 stdout: (out.stdout || "").trim(),
38 stderr: (out.stderr || "").trim(),
39 args,
40 };
41}
42
43function canRun(report, result, noteIfNotOk) {
44 report._runs.push({ cmd: result.args.join(" "), ok: result.ok, status: result.status, error: result.error });
45 if (!result.ok && noteIfNotOk) report.notes.push(noteIfNotOk);
46 return result.ok;
47}
48
49function main() {
50 const opts = parseArgs(process.argv.slice(2));
51 const report = {
52 tool: { name: "perf_inspect", version: TOOL_VERSION },
53 target: { path: opts.path, url: opts.url },
54 wpCli: { available: false },
55 wp: {
56 isInstalled: null,
57 coreVersion: null,
58 },
59 commands: {
60 doctor: { available: false },
61 profile: { available: false },
62 },
63 perfSignals: {
64 autoloadTotalBytes: null,
65 hasObjectCacheDropin: null,
66 hasAdvancedCacheDropin: null,
67 hasQueryMonitorPlugin: null,
68 hasPerformanceLabPlugin: null,
69 },
70 notes: [],
71 _runs: [],
72 };
73
74 const info = runWp(["--info"], { pathArg: null, urlArg: null, allowRoot: opts.allowRoot });
75 report.wpCli.available = info.ok;
76 report.wpCli.info = info;
77 if (!info.ok) {
78 report.notes.push("WP-CLI not available on PATH. Run in the intended environment (container/ssh) or install WP-CLI.");
79 process.stdout.write(`${JSON.stringify(report, null, 2)}\n`);
80 return;
81 }
82
83 const isInstalled = runWp(["core", "is-installed"], { pathArg: opts.path, urlArg: opts.url, allowRoot: opts.allowRoot });
84 report.wp.isInstalled = isInstalled.ok;
85 canRun(report, isInstalled, "WordPress not detected at the given --path/--url (check wp-config.php and targeting).");
86 if (!isInstalled.ok) {
87 process.stdout.write(`${JSON.stringify(report, null, 2)}\n`);
88 return;
89 }
90
91 const coreVersion = runWp(["core", "version"], { pathArg: opts.path, urlArg: opts.url, allowRoot: opts.allowRoot });
92 report.wp.coreVersion = coreVersion.ok ? coreVersion.stdout : null;
93 canRun(report, coreVersion);
94
95 const doctorHelp = runWp(["doctor", "--help"], { pathArg: opts.path, urlArg: opts.url, allowRoot: opts.allowRoot });
96 report.commands.doctor.available = doctorHelp.ok;
97 canRun(report, doctorHelp);
98
99 const profileHelp = runWp(["profile", "--help"], { pathArg: opts.path, urlArg: opts.url, allowRoot: opts.allowRoot });
100 report.commands.profile.available = profileHelp.ok;
101 canRun(report, profileHelp);
102
103 const autoloadBytes = runWp(["option", "list", "--autoload=on", "--format=total_bytes"], {
104 pathArg: opts.path,
105 urlArg: opts.url,
106 allowRoot: opts.allowRoot,
107 });
108 if (autoloadBytes.ok && /^\d+$/.test(autoloadBytes.stdout)) {
109 report.perfSignals.autoloadTotalBytes = Number(autoloadBytes.stdout);
110 }
111 canRun(report, autoloadBytes);
112
113 if (opts.path) {
114 const wpContent = path.join(opts.path, "wp-content");
115 report.perfSignals.hasObjectCacheDropin = existsFile(path.join(wpContent, "object-cache.php"));
116 report.perfSignals.hasAdvancedCacheDropin = existsFile(path.join(wpContent, "advanced-cache.php"));
117 report.perfSignals.hasQueryMonitorPlugin = existsFile(path.join(wpContent, "plugins", "query-monitor", "query-monitor.php"));
118 report.perfSignals.hasPerformanceLabPlugin = existsFile(path.join(wpContent, "plugins", "performance-lab", "load.php"));
119 }
120
121 if (!report.commands.doctor.available) report.notes.push("Tip: install WP-CLI doctor: `wp package install wp-cli/doctor-command`.");
122 if (!report.commands.profile.available) report.notes.push("Tip: install WP-CLI profile: `wp package install wp-cli/profile-command`.");
123
124 process.stdout.write(`${JSON.stringify(report, null, 2)}\n`);
125}
126
127main();
128