Skip to content

Commit 01a7020

Browse files
just-be-devclaude
andcommitted
feat(micro): replace MICRO_SITE_URL with --branch CLI flag
Instead of an env var, the CLI now derives the site URL from an optional --branch / -b flag. No branch (or "main") resolves to https://just-be.dev; any other branch resolves to the Cloudflare Workers preview URL pattern https://{branch}-just-be-dev.just-be.workers.dev. siteUrl is threaded as a required parameter into browse() and post() rather than being read from the environment inside each function. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent c0f7f6e commit 01a7020

File tree

4 files changed

+57
-25
lines changed

4 files changed

+57
-25
lines changed

packages/micro/README.md

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,20 @@ Creates a post directly from the command line.
5353

5454
- Bun >= 1.0.0
5555
- A running instance of the site with `MICRO_BUCKET` (R2) and `MICRO_SECRET` configured
56+
- `MICRO_SECRET` environment variable set locally to authenticate with the API
57+
58+
## Targeting a Branch Preview
59+
60+
By default the CLI talks to the production site (`https://just-be.dev`). Pass `--branch`
61+
(or `-b`) to target the Cloudflare Workers preview URL for any branch:
62+
63+
```bash
64+
micro --branch add-micro-blog-section
65+
micro post --branch add-micro-blog-section "Hello from the preview"
66+
```
67+
68+
Branch names are automatically sanitized for subdomain use. Passing `main` (or omitting
69+
the flag entirely) always resolves to the production URL.
5670

5771
## Social Media Syndication
5872

@@ -104,4 +118,5 @@ To test syndication without real API credentials, simply don't set the environme
104118

105119
## Development
106120

107-
The CLI talks to the site's HTTP API. Set `MICRO_SECRET` to authenticate and `MICRO_SITE_URL` to point at a local dev server (defaults to `https://just-be.dev`).
121+
The CLI talks to the site's HTTP API. Set `MICRO_SECRET` to authenticate, then use
122+
`--branch <name>` to point at the preview URL for your branch.

packages/micro/index.ts

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,48 @@
33
import { browse } from "./src/browse.ts";
44
import { post } from "./src/post.ts";
55

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+
733
const command = args[0];
34+
const siteUrl = siteUrlFromBranch(branch);
835

936
async function main() {
1037
if (!command) {
11-
// Default: browse all posts
12-
await browse();
38+
await browse(siteUrl);
1339
} else if (command === "post") {
1440
const content = args[1];
15-
await post(content);
41+
await post(content, siteUrl);
1642
} else {
1743
console.error(`Unknown command: ${command}`);
1844
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');
2248
process.exit(1);
2349
}
2450
}

packages/micro/src/browse.ts

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,27 +8,22 @@ interface Post {
88
syndicatedTo: Array<{ platform: string; id: string; url: string }>;
99
}
1010

11-
const DEFAULT_SITE_URL = "https://just-be.dev";
12-
13-
function getConfig() {
11+
function getSecret(): string {
1412
const secret = process.env.MICRO_SECRET;
1513
if (!secret) {
1614
console.error("Error: MICRO_SECRET environment variable not set");
1715
process.exit(1);
1816
}
19-
return {
20-
secret,
21-
siteUrl: process.env.MICRO_SITE_URL ?? DEFAULT_SITE_URL,
22-
};
17+
return secret;
2318
}
2419

25-
export async function browse() {
20+
export async function browse(siteUrl: string) {
2621
p.intro("Micro Blog Browser");
2722

2823
const spinner = p.spinner();
2924
spinner.start("Loading posts...");
3025

31-
const { secret, siteUrl } = getConfig();
26+
const secret = getSecret();
3227

3328
let posts: Post[];
3429
try {

packages/micro/src/post.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,17 @@
11
import * as p from "@clack/prompts";
22

33
const MAX_LENGTH = 280;
4-
const DEFAULT_SITE_URL = "https://just-be.dev";
54

6-
function getConfig() {
5+
function getSecret(): string {
76
const secret = process.env.MICRO_SECRET;
87
if (!secret) {
98
console.error("Error: MICRO_SECRET environment variable not set");
109
process.exit(1);
1110
}
12-
return {
13-
secret,
14-
siteUrl: process.env.MICRO_SITE_URL ?? DEFAULT_SITE_URL,
15-
};
11+
return secret;
1612
}
1713

18-
export async function post(content?: string) {
14+
export async function post(content: string | undefined, siteUrl: string) {
1915
let postContent = content;
2016

2117
if (!postContent) {
@@ -47,7 +43,7 @@ export async function post(content?: string) {
4743
}
4844
}
4945

50-
const { secret, siteUrl } = getConfig();
46+
const secret = getSecret();
5147
const spinner = p.spinner();
5248
spinner.start("Creating post...");
5349

0 commit comments

Comments
 (0)