|
| 1 | +import { |
| 2 | + CONFIG_FILE_PATH, |
| 3 | + expandPurgeTargets, |
| 4 | + loadConfig, |
| 5 | + purgeCache, |
| 6 | + saveConfig, |
| 7 | + validateCredentials, |
| 8 | +} from "./cloudflare.js"; |
| 9 | +import { promptForConfig } from "./prompt.js"; |
| 10 | + |
| 11 | +export async function main(args) { |
| 12 | + const [command, ...rest] = args; |
| 13 | + |
| 14 | + if (!command || command === "--help" || command === "-h") { |
| 15 | + printHelp(); |
| 16 | + return; |
| 17 | + } |
| 18 | + |
| 19 | + if (command === "login") { |
| 20 | + await runLogin(rest); |
| 21 | + return; |
| 22 | + } |
| 23 | + |
| 24 | + if (command === "purge") { |
| 25 | + await runPurge(rest); |
| 26 | + return; |
| 27 | + } |
| 28 | + |
| 29 | + throw new Error(`Unknown command: ${command}`); |
| 30 | +} |
| 31 | + |
| 32 | +async function runLogin(args) { |
| 33 | + const parsed = parseFlags(args); |
| 34 | + const config = await promptForConfig({ |
| 35 | + apiToken: parsed.options["api-token"], |
| 36 | + zoneId: parsed.options["zone-id"], |
| 37 | + domain: parsed.options.domain, |
| 38 | + }); |
| 39 | + |
| 40 | + await validateCredentials(config); |
| 41 | + await saveConfig(config); |
| 42 | + |
| 43 | + console.log(`Saved Cloudflare credentials to ${CONFIG_FILE_PATH}`); |
| 44 | +} |
| 45 | + |
| 46 | +async function runPurge(args) { |
| 47 | + const parsed = parseFlags(args); |
| 48 | + const everything = Boolean(parsed.options.everything); |
| 49 | + const config = await loadConfig(); |
| 50 | + |
| 51 | + if (!config) { |
| 52 | + throw new Error("No Cloudflare login found. Run `cloudflare-cli login` first."); |
| 53 | + } |
| 54 | + |
| 55 | + if (everything && parsed.positionals.length > 0) { |
| 56 | + throw new Error("Use either --everything or explicit paths/URLs, not both."); |
| 57 | + } |
| 58 | + |
| 59 | + if (!everything && parsed.positionals.length === 0) { |
| 60 | + throw new Error("Provide one or more paths/URLs to purge, or use --everything."); |
| 61 | + } |
| 62 | + |
| 63 | + const domain = parsed.options.domain || config.domain; |
| 64 | + const request = everything |
| 65 | + ? { purge_everything: true } |
| 66 | + : { files: expandPurgeTargets(parsed.positionals, domain) }; |
| 67 | + |
| 68 | + await purgeCache(config, request); |
| 69 | + |
| 70 | + if (everything) { |
| 71 | + console.log(`Purged Cloudflare cache for zone ${config.zoneId}`); |
| 72 | + } else { |
| 73 | + console.log(`Purged ${request.files.length} Cloudflare cache entr${request.files.length === 1 ? "y" : "ies"}`); |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +function parseFlags(args) { |
| 78 | + const options = {}; |
| 79 | + const positionals = []; |
| 80 | + |
| 81 | + for (let index = 0; index < args.length; index += 1) { |
| 82 | + const current = args[index]; |
| 83 | + if (current === "--everything") { |
| 84 | + options.everything = true; |
| 85 | + continue; |
| 86 | + } |
| 87 | + if (current.startsWith("--")) { |
| 88 | + const key = current.slice(2); |
| 89 | + const value = args[index + 1]; |
| 90 | + if (!value || value.startsWith("--")) { |
| 91 | + throw new Error(`Missing value for ${current}`); |
| 92 | + } |
| 93 | + options[key] = value; |
| 94 | + index += 1; |
| 95 | + continue; |
| 96 | + } |
| 97 | + positionals.push(current); |
| 98 | + } |
| 99 | + |
| 100 | + return { options, positionals }; |
| 101 | +} |
| 102 | + |
| 103 | +function printHelp() { |
| 104 | + console.log(`cloudflare-cli |
| 105 | +
|
| 106 | +Usage: |
| 107 | + cloudflare-cli login [--api-token TOKEN] [--zone-id ZONE_ID] [--domain DOMAIN] |
| 108 | + cloudflare-cli purge --everything |
| 109 | + cloudflare-cli purge [--domain DOMAIN] /path https://example.com/path |
| 110 | +`); |
| 111 | +} |
0 commit comments