|
| 1 | +import path from "node:path"; |
| 2 | +import { z } from "zod"; |
| 3 | +import { hasBlockingFindings, printFindings } from "../checks/summary.js"; |
| 4 | +import { discoverPluginProject } from "../plugin/discover.js"; |
| 5 | +import type { PluginProject } from "../types.js"; |
| 6 | +import { ui } from "../ui.js"; |
| 7 | +import { validatePluginPack, type PackValidationResult } from "./pack.js"; |
| 8 | + |
| 9 | +const verifyOptionsSchema = z.object({ |
| 10 | + ignore: z.array(z.string()).default([]), |
| 11 | + skipReadmeValidator: z.boolean().default(false), |
| 12 | + wpPath: z.string().optional(), |
| 13 | + json: z.boolean().default(false) |
| 14 | +}); |
| 15 | + |
| 16 | +export type VerifyOptions = z.input<typeof verifyOptionsSchema>; |
| 17 | + |
| 18 | +export type VerifySummary = { |
| 19 | + ok: boolean; |
| 20 | + plugin: { |
| 21 | + rootDir: string; |
| 22 | + mainFile: string; |
| 23 | + name: string; |
| 24 | + slug: string; |
| 25 | + version?: string; |
| 26 | + readmePath?: string; |
| 27 | + }; |
| 28 | + validation: PackValidationResult; |
| 29 | +}; |
| 30 | + |
| 31 | +export async function verify(pluginPath: string | undefined, rawOptions: VerifyOptions): Promise<void> { |
| 32 | + const options = verifyOptionsSchema.parse(rawOptions); |
| 33 | + |
| 34 | + if (!options.json) { |
| 35 | + ui.intro("Verify WordPress plugin"); |
| 36 | + } |
| 37 | + |
| 38 | + const rootDir = path.resolve(pluginPath ?? process.cwd()); |
| 39 | + const project = options.json |
| 40 | + ? await discoverPluginProject(rootDir) |
| 41 | + : await ui.task("Discovering WordPress plugin", () => discoverPluginProject(rootDir), (value) => |
| 42 | + `Discovered ${value.headers.pluginName}` |
| 43 | + ); |
| 44 | + const validation = options.json |
| 45 | + ? await validatePluginPack(project, options) |
| 46 | + : await ui.task("Verifying plugin", () => validatePluginPack(project, options)); |
| 47 | + const summary = summarizeVerifyResult(project, validation); |
| 48 | + |
| 49 | + if (options.json) { |
| 50 | + console.log(JSON.stringify(summary, null, 2)); |
| 51 | + if (!summary.ok) { |
| 52 | + process.exitCode = 1; |
| 53 | + } |
| 54 | + return; |
| 55 | + } |
| 56 | + |
| 57 | + printFindings("Readme validation", validation.readmeFindings); |
| 58 | + printFindings("Plugin Check", validation.pluginCheckFindings); |
| 59 | + |
| 60 | + if (!summary.ok) { |
| 61 | + throw new Error("Verification reported blocking findings."); |
| 62 | + } |
| 63 | + |
| 64 | + ui.success("Verification passed."); |
| 65 | +} |
| 66 | + |
| 67 | +export function summarizeVerifyResult( |
| 68 | + project: PluginProject, |
| 69 | + validation: PackValidationResult |
| 70 | +): VerifySummary { |
| 71 | + return { |
| 72 | + ok: !hasBlockingFindings(validation.readmeFindings) && !hasBlockingFindings(validation.pluginCheckFindings), |
| 73 | + plugin: { |
| 74 | + rootDir: project.rootDir, |
| 75 | + mainFile: project.mainFile, |
| 76 | + name: project.headers.pluginName, |
| 77 | + slug: project.slug, |
| 78 | + version: project.version, |
| 79 | + readmePath: project.readmePath |
| 80 | + }, |
| 81 | + validation |
| 82 | + }; |
| 83 | +} |
0 commit comments