|
| 1 | +import { execFileSync } from "node:child_process"; |
| 2 | +import { existsSync } from "node:fs"; |
| 3 | + |
| 4 | +const cursorPrefix = "plugins/cursor"; |
| 5 | +const defaultRemote = "https://github.com/Automattic/wordpress-cursor-plugin.git"; |
| 6 | +const defaultBranch = "sync/from-build-with-wordpress"; |
| 7 | + |
| 8 | +const args = process.argv.slice(2); |
| 9 | +const dryRun = args.includes("--dry-run"); |
| 10 | +const remote = readOption("--remote") ?? defaultRemote; |
| 11 | +const branch = readOption("--branch") ?? defaultBranch; |
| 12 | +const splitBranch = `export-wordpress-cursor-plugin-${Date.now()}`; |
| 13 | + |
| 14 | +if (!existsSync(cursorPrefix)) { |
| 15 | + throw new Error(`${cursorPrefix} does not exist. Run pnpm build first.`); |
| 16 | +} |
| 17 | + |
| 18 | +if (!dryRun) { |
| 19 | + const status = run("git", ["status", "--porcelain"], { capture: true }); |
| 20 | + if (status.trim()) { |
| 21 | + throw new Error("Working tree must be clean before exporting the Cursor plugin."); |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +runStep("Create subtree split", ["git", "subtree", "split", `--prefix=${cursorPrefix}`, "-b", splitBranch]); |
| 26 | +runStep("Push split branch", ["git", "push", remote, `${splitBranch}:refs/heads/${branch}`]); |
| 27 | +runStep("Remove local split branch", ["git", "branch", "-D", splitBranch]); |
| 28 | + |
| 29 | +console.log(`Cursor plugin export branch: ${branch}`); |
| 30 | +console.log("Open or update a PR in https://github.com/Automattic/wordpress-cursor-plugin"); |
| 31 | + |
| 32 | +function readOption(name) { |
| 33 | + const index = args.indexOf(name); |
| 34 | + if (index === -1) { |
| 35 | + return undefined; |
| 36 | + } |
| 37 | + return args[index + 1]; |
| 38 | +} |
| 39 | + |
| 40 | +function runStep(label, command) { |
| 41 | + console.log(`${label}: ${command.join(" ")}`); |
| 42 | + if (dryRun) { |
| 43 | + return; |
| 44 | + } |
| 45 | + run(command[0], command.slice(1)); |
| 46 | +} |
| 47 | + |
| 48 | +function run(command, commandArgs, options = {}) { |
| 49 | + return execFileSync(command, commandArgs, { |
| 50 | + encoding: "utf8", |
| 51 | + stdio: options.capture ? ["ignore", "pipe", "inherit"] : "inherit", |
| 52 | + }); |
| 53 | +} |
0 commit comments