|
3 | 3 | import { browse } from "./src/browse.ts"; |
4 | 4 | import { post } from "./src/post.ts"; |
5 | 5 |
|
6 | | -const args = process.argv.slice(2); |
| 6 | +const PRODUCTION_URL = "https://just-be.dev"; |
| 7 | +const WORKER_NAME = "just-be-dev"; |
| 8 | +const WORKERS_DEV_SUBDOMAIN = "just-be"; |
| 9 | + |
| 10 | +function siteUrlFromBranch(branch: string | undefined): string { |
| 11 | + if (!branch || branch === "main") return PRODUCTION_URL; |
| 12 | + // Sanitize branch name for subdomain use: replace non-alphanumeric chars with hyphens |
| 13 | + const sanitized = branch |
| 14 | + .toLowerCase() |
| 15 | + .replace(/[^a-z0-9-]+/g, "-") |
| 16 | + .replace(/^-+|-+$/g, ""); |
| 17 | + return `https://${sanitized}-${WORKER_NAME}.${WORKERS_DEV_SUBDOMAIN}.workers.dev`; |
| 18 | +} |
| 19 | + |
| 20 | +// Extract global --branch / -b flag and leave remaining args for command parsing |
| 21 | +const rawArgs = process.argv.slice(2); |
| 22 | +let branch: string | undefined; |
| 23 | +const args: string[] = []; |
| 24 | + |
| 25 | +for (let i = 0; i < rawArgs.length; i++) { |
| 26 | + if ((rawArgs[i] === "--branch" || rawArgs[i] === "-b") && i + 1 < rawArgs.length) { |
| 27 | + branch = rawArgs[++i]; |
| 28 | + } else { |
| 29 | + args.push(rawArgs[i]); |
| 30 | + } |
| 31 | +} |
| 32 | + |
7 | 33 | const command = args[0]; |
| 34 | +const siteUrl = siteUrlFromBranch(branch); |
8 | 35 |
|
9 | 36 | async function main() { |
10 | 37 | if (!command) { |
11 | | - // Default: browse all posts |
12 | | - await browse(); |
| 38 | + await browse(siteUrl); |
13 | 39 | } else if (command === "post") { |
14 | 40 | const content = args[1]; |
15 | | - await post(content); |
| 41 | + await post(content, siteUrl); |
16 | 42 | } else { |
17 | 43 | console.error(`Unknown command: ${command}`); |
18 | 44 | console.log("Usage:"); |
19 | | - console.log(" micro - Browse all posts"); |
20 | | - console.log(" micro post - Create a new post (TUI editor)"); |
21 | | - console.log(' micro post "text" - Create a new post directly'); |
| 45 | + console.log(" micro [--branch <name>] - Browse all posts"); |
| 46 | + console.log(" micro post [--branch <name>] - Create a new post (TUI editor)"); |
| 47 | + console.log(' micro post [--branch <name>] "text" - Create a new post directly'); |
22 | 48 | process.exit(1); |
23 | 49 | } |
24 | 50 | } |
|
0 commit comments