All files / apps/wp-editor/tools/cli index.ts

0% Statements 0/62
0% Branches 0/18
0% Functions 0/20
0% Lines 0/52

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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                                                                                                                                                                                                                                                               
import { parseArgs } from "node:util";
import { derivedStatus } from "../../shared/rules.ts";
import { credentials, storePath } from "../lib/config.ts";
import { Refusal } from "../../shared/refusal.ts";
import type { Store } from "../../server/adapters/store.ts";
import { createStore } from "../../server/adapters/store.ts";
import { createWordPress } from "../../server/adapters/wordpress.ts";
import { markScanned, openChangeset, validate } from "../../server/services/operations.ts";
import { probe } from "./probe.ts";
 
// eslint-disable-next-line no-console -- this CLI's output is what Claude reads
const print = (line: string) => console.log(line);
 
const { positionals, values } = parseArgs({
    allowPositionals: true,
    options: {
        instructions: { type: "string" },
        override: { type: "boolean" },
        ids: { type: "string" },
        limit: { type: "string" },
    },
});
 
const [command, ...rest] = positionals;
 
const openChangesetOf = async (store: Store) => {
    const open = (await store.listChangesets()).find(({ status }) => status === "open");
 
    if (!open) throw new Refusal("No open changeset; start one with `pnpm editor new`.");
 
    return open;
};
 
const commands: Record<string, () => Promise<void>> = {
    new: async () => {
        const title = rest[0];
 
        if (!title)
            throw new Refusal(
                'Usage: pnpm editor new "<title>" [--instructions=…] [--override] [--ids=posts-12,…]',
            );
 
        const changeset = await openChangeset(
            createStore(storePath()),
            createWordPress(credentials()),
            {
                title,
                instructions: values.instructions,
                override: values.override,
                ids: values.ids?.split(","),
            },
        );
 
        print(`Opened ${changeset.id} with ${changeset.keys.length} articles.`);
    },
 
    todo: async () => {
        const store = createStore(storePath());
        const { id, keys, scanned } = await openChangesetOf(store);
        const done = new Set(scanned);
        const todo = keys.filter((key) => !done.has(key)).slice(0, Number(values.limit ?? 15));
 
        print(`${id}: ${keys.length - done.size} left`);
        for (const key of todo) print(`${store.root}/changesets/${id}/base/${key}.html`);
    },
 
    scanned: async () => {
        const store = createStore(storePath());
        const { id } = await openChangesetOf(store);
        const { keys, scanned } = await markScanned(store, id, rest);
 
        print(`${scanned.length}/${keys.length} scanned.`);
    },
 
    validate: async () => {
        const store = createStore(storePath());
        const { id } = await openChangesetOf(store);
        const { removed, markerWarnings } = await validate(store, id);
 
        const keys = removed.length ? `: ${removed.join(", ")}` : "";
 
        print(`Removed ${removed.length} no-op proposals${keys}.`);
        for (const key of markerWarnings) print(`WARNING ${key}: block markers differ from base.`);
    },
 
    status: async () => {
        const store = createStore(storePath());
 
        const changesets = await store.listChangesets();
        const itemsOf = await Promise.all(changesets.map(({ id }) => store.readItems(id)));
 
        for (const [index, changeset] of changesets.entries()) {
            const items = itemsOf[index] ?? [];
            const count = (test: (state: (typeof items)[number]["state"]) => boolean) =>
                items.filter(({ state }) => test(state)).length;
 
            print(
                [
                    `${changeset.id} [${derivedStatus(changeset, items)}]`,
                    `scanned ${changeset.scanned.length}/${changeset.keys.length}`,
                    `proposals ${items.filter(({ proposal }) => proposal).length}`,
                    `review requested ${items.filter(({ proposal }) => proposal?.reviewRequested).length}`,
                    `unreviewed ${count((s) => s.level === "unreviewed")}`,
                    `seen ${count((s) => s.level === "seen")}`,
                    `confirmed ${count((s) => s.level === "confirmed")}`,
                    `rejected ${count((s) => !!s.rejected)}`,
                    `applied ${count((s) => !!s.applied && !s.restored)}`,
                ].join(" · "),
            );
        }
    },
 
    probe: () => probe(createWordPress(credentials()), print),
};
 
try {
    const run = commands[command ?? ""];
 
    if (!run) throw new Refusal(`Usage: pnpm editor <${Object.keys(commands).join("|")}>`);
 
    await run();
} catch (error) {
    if (!(error instanceof Refusal)) throw error;
    // eslint-disable-next-line no-console -- a refusal is reported on stderr
    console.error(error.message);
    process.exitCode = 1;
}