Skip to content

Commit dfb7bcd

Browse files
committed
feat: codeblog update command + OAuth username support
- codeblog update: self-update by downloading latest binary from npm - Auth: save username from OAuth callback - OAuth: pass username param back from web callback
1 parent e4bdc67 commit dfb7bcd

4 files changed

Lines changed: 82 additions & 2 deletions

File tree

‎packages/codeblog/src/auth/index.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ export namespace Auth {
88
type: z.enum(["jwt", "apikey"]),
99
value: z.string(),
1010
expires: z.number().optional(),
11+
username: z.string().optional(),
1112
})
1213
.meta({ ref: "AuthToken" })
1314
export type Token = z.infer<typeof Token>

‎packages/codeblog/src/auth/oauth.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,13 @@ export namespace OAuth {
1313
const { app, port } = Server.createCallbackServer(async (params) => {
1414
const token = params.get("token")
1515
const key = params.get("api_key")
16+
const username = params.get("username") || undefined
1617

1718
if (key) {
18-
await Auth.set({ type: "apikey", value: key })
19+
await Auth.set({ type: "apikey", value: key, username })
1920
log.info("authenticated with api key")
2021
} else if (token) {
21-
await Auth.set({ type: "jwt", value: token })
22+
await Auth.set({ type: "jwt", value: token, username })
2223
log.info("authenticated with jwt")
2324
} else {
2425
Server.stop()
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
import type { CommandModule } from "yargs"
2+
import { UI } from "../ui"
3+
4+
export const UpdateCommand: CommandModule = {
5+
command: "update",
6+
describe: "Update codeblog CLI to the latest version",
7+
builder: (yargs) =>
8+
yargs.option("force", {
9+
describe: "Force update even if already on latest",
10+
type: "boolean",
11+
default: false,
12+
}),
13+
handler: async (args) => {
14+
const pkg = await import("../../../package.json")
15+
const current = pkg.version
16+
17+
UI.info(`Current version: v${current}`)
18+
UI.info("Checking for updates...")
19+
20+
const res = await fetch("https://registry.npmjs.org/codeblog-app/latest")
21+
if (!res.ok) {
22+
UI.error("Failed to check for updates")
23+
process.exitCode = 1
24+
return
25+
}
26+
27+
const data = await res.json() as { version: string }
28+
const latest = data.version
29+
30+
if (current === latest && !args.force) {
31+
UI.success(`Already on latest version v${current}`)
32+
return
33+
}
34+
35+
UI.info(`Updating v${current} → v${latest}...`)
36+
37+
const os = process.platform === "win32" ? "windows" : process.platform === "darwin" ? "darwin" : "linux"
38+
const arch = process.arch === "arm64" ? "arm64" : "x64"
39+
const platform = `${os}-${arch}`
40+
const pkg_name = `codeblog-app-${platform}`
41+
const url = `https://registry.npmjs.org/${pkg_name}/-/${pkg_name}-${latest}.tgz`
42+
43+
const tmpdir = (await import("os")).tmpdir()
44+
const path = await import("path")
45+
const fs = await import("fs/promises")
46+
const tmp = path.join(tmpdir, `codeblog-update-${Date.now()}`)
47+
await fs.mkdir(tmp, { recursive: true })
48+
49+
const tgz = path.join(tmp, "pkg.tgz")
50+
const dlRes = await fetch(url)
51+
if (!dlRes.ok) {
52+
UI.error(`Failed to download update for ${platform}`)
53+
process.exitCode = 1
54+
return
55+
}
56+
57+
await Bun.write(tgz, dlRes)
58+
59+
const proc = Bun.spawn(["tar", "-xzf", tgz, "-C", tmp], { stdout: "ignore", stderr: "ignore" })
60+
await proc.exited
61+
62+
const bin = process.execPath
63+
const ext = os === "windows" ? ".exe" : ""
64+
const src = path.join(tmp, "package", "bin", `codeblog${ext}`)
65+
66+
await fs.copyFile(src, bin)
67+
if (os !== "windows") {
68+
await fs.chmod(bin, 0o755)
69+
}
70+
71+
await fs.rm(tmp, { recursive: true, force: true })
72+
73+
UI.success(`Updated to v${latest}!`)
74+
},
75+
}

‎packages/codeblog/src/index.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import { TuiCommand } from "./cli/cmd/tui"
3434
import { WeeklyDigestCommand } from "./cli/cmd/weekly-digest"
3535
import { TagsCommand } from "./cli/cmd/tags"
3636
import { ExploreCommand } from "./cli/cmd/explore"
37+
import { UpdateCommand } from "./cli/cmd/update"
3738

3839
const VERSION = (await import("../package.json")).version
3940

@@ -115,6 +116,8 @@ const cli = yargs(hideBin(process.argv))
115116
.command(DashboardCommand)
116117
.command(AgentsCommand)
117118
.command(MyPostsCommand)
119+
// Update
120+
.command(UpdateCommand)
118121
.fail((msg, err) => {
119122
if (
120123
msg?.startsWith("Unknown argument") ||

0 commit comments

Comments
 (0)