astrocms 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/backend/auth.ts +54 -0
- package/backend/config.ts +50 -0
- package/backend/parsers/collections.ts +279 -0
- package/backend/parsers/components.ts +184 -0
- package/backend/parsers/frontmatter.ts +67 -0
- package/backend/parsers/glob.ts +57 -0
- package/backend/root.ts +7 -0
- package/backend/routes/auth.ts +42 -0
- package/backend/routes/claude.ts +401 -0
- package/backend/routes/collections.ts +9 -0
- package/backend/routes/components.ts +9 -0
- package/backend/routes/config.ts +7 -0
- package/backend/routes/file.ts +179 -0
- package/backend/routes/git.ts +458 -0
- package/backend/routes/tree.ts +100 -0
- package/backend/routes/upload.ts +33 -0
- package/backend/server.ts +173 -0
- package/backend/stubs/astro-content.ts +14 -0
- package/backend/stubs/astro-loaders.ts +17 -0
- package/bin/astrocms.js +66 -0
- package/dist/_cms/apl-B4CMkyY2.js +1 -0
- package/dist/_cms/asciiarmor-Df11BRmG.js +1 -0
- package/dist/_cms/asn1-EdZsLKOL.js +1 -0
- package/dist/_cms/asterisk-B-8jnY81.js +1 -0
- package/dist/_cms/brainfuck-C4LP7Hcl.js +1 -0
- package/dist/_cms/clike-B9uivgTg.js +1 -0
- package/dist/_cms/clojure-BMjYHr_A.js +1 -0
- package/dist/_cms/cmake-BQqOBYOt.js +1 -0
- package/dist/_cms/cobol-CWcv1MsR.js +1 -0
- package/dist/_cms/coffeescript-S37ZYGWr.js +1 -0
- package/dist/_cms/commonlisp-DBKNyK5s.js +1 -0
- package/dist/_cms/crystal-SjHAIU92.js +1 -0
- package/dist/_cms/css-BnMrqG3P.js +1 -0
- package/dist/_cms/cypher-C_CwsFkJ.js +1 -0
- package/dist/_cms/d-pRatUO7H.js +1 -0
- package/dist/_cms/diff-DbItnlRl.js +1 -0
- package/dist/_cms/dockerfile-BKs6k2Af.js +1 -0
- package/dist/_cms/dtd-DF_7sFjM.js +1 -0
- package/dist/_cms/dylan-DwRh75JA.js +1 -0
- package/dist/_cms/ebnf-CDyGwa7X.js +1 -0
- package/dist/_cms/ecl-Cabwm37j.js +1 -0
- package/dist/_cms/eiffel-CnydiIhH.js +1 -0
- package/dist/_cms/elm-vLlmbW-K.js +1 -0
- package/dist/_cms/erlang-BNw1qcRV.js +1 -0
- package/dist/_cms/factor-kuTfRLto.js +1 -0
- package/dist/_cms/fcl-Kvtd6kyn.js +1 -0
- package/dist/_cms/forth-Ffai-XNe.js +1 -0
- package/dist/_cms/fortran-DYz_wnZ1.js +1 -0
- package/dist/_cms/gas-Bneqetm1.js +1 -0
- package/dist/_cms/gherkin-heZmZLOM.js +1 -0
- package/dist/_cms/groovy-D9Dt4D0W.js +1 -0
- package/dist/_cms/haskell-Cw1EW3IL.js +1 -0
- package/dist/_cms/haxe-H-WmDvRZ.js +1 -0
- package/dist/_cms/http-DBlCnlav.js +1 -0
- package/dist/_cms/idl-BEugSyMb.js +1 -0
- package/dist/_cms/index-7Pc6H4sv.js +1 -0
- package/dist/_cms/index-893Rzaov.js +1 -0
- package/dist/_cms/index-9o-llXDY.js +508 -0
- package/dist/_cms/index-BdC39RoS.js +1 -0
- package/dist/_cms/index-BdChuKfE.js +7 -0
- package/dist/_cms/index-Bpfn86Of.js +1 -0
- package/dist/_cms/index-Bvz8WTJL.js +1 -0
- package/dist/_cms/index-C2vDuCBy.js +1 -0
- package/dist/_cms/index-C9zUJLja.css +1 -0
- package/dist/_cms/index-Cdwq3Ev5.js +1 -0
- package/dist/_cms/index-Ce64khPN.js +3 -0
- package/dist/_cms/index-CoIA6zkf.js +1 -0
- package/dist/_cms/index-DAAZmIPw.js +1 -0
- package/dist/_cms/index-DM5GktYD.js +1 -0
- package/dist/_cms/index-DQf5bTbe.js +1 -0
- package/dist/_cms/index-M4bKZFGh.js +1 -0
- package/dist/_cms/index-NwHME3i3.js +1 -0
- package/dist/_cms/index-enNvjuOI.js +2 -0
- package/dist/_cms/index-wHleENT0.js +1 -0
- package/dist/_cms/javascript-iXu5QeM3.js +1 -0
- package/dist/_cms/julia-DuME0IfC.js +1 -0
- package/dist/_cms/livescript-BwQOo05w.js +1 -0
- package/dist/_cms/lua-BgMRiT3U.js +1 -0
- package/dist/_cms/mathematica-DTrFuWx2.js +1 -0
- package/dist/_cms/mbox-CNhZ1qSd.js +1 -0
- package/dist/_cms/mirc-CjQqDB4T.js +1 -0
- package/dist/_cms/mllike-CXdrOF99.js +1 -0
- package/dist/_cms/modelica-Dc1JOy9r.js +1 -0
- package/dist/_cms/mscgen-BA5vi2Kp.js +1 -0
- package/dist/_cms/mumps-BT43cFF4.js +1 -0
- package/dist/_cms/nginx-DdIZxoE0.js +1 -0
- package/dist/_cms/nsis-LdVXkNf5.js +1 -0
- package/dist/_cms/ntriples-BfvgReVJ.js +1 -0
- package/dist/_cms/octave-Ck1zUtKM.js +1 -0
- package/dist/_cms/oz-BzwKVEFT.js +1 -0
- package/dist/_cms/pascal--L3eBynH.js +1 -0
- package/dist/_cms/perl-CdXCOZ3F.js +1 -0
- package/dist/_cms/pig-CevX1Tat.js +1 -0
- package/dist/_cms/powershell-CFHJl5sT.js +1 -0
- package/dist/_cms/properties-C78fOPTZ.js +1 -0
- package/dist/_cms/protobuf-ChK-085T.js +1 -0
- package/dist/_cms/pug-DeIclll2.js +1 -0
- package/dist/_cms/puppet-DMA9R1ak.js +1 -0
- package/dist/_cms/python-BuPzkPfP.js +1 -0
- package/dist/_cms/q-pXgVlZs6.js +1 -0
- package/dist/_cms/r-B6wPVr8A.js +1 -0
- package/dist/_cms/rpm-CTu-6PCP.js +1 -0
- package/dist/_cms/ruby-B2Rjki9n.js +1 -0
- package/dist/_cms/sas-B4kiWyti.js +1 -0
- package/dist/_cms/scheme-C41bIUwD.js +1 -0
- package/dist/_cms/shell-CjFT_Tl9.js +1 -0
- package/dist/_cms/sieve-C3Gn_uJK.js +1 -0
- package/dist/_cms/simple-mode-GW_nhZxv.js +1 -0
- package/dist/_cms/smalltalk-CnHTOXQT.js +1 -0
- package/dist/_cms/solr-DehyRSwq.js +1 -0
- package/dist/_cms/sparql-DkYu6x3z.js +1 -0
- package/dist/_cms/spreadsheet-BCZA_wO0.js +1 -0
- package/dist/_cms/sql-D0XecflT.js +1 -0
- package/dist/_cms/stex-C3f8Ysf7.js +1 -0
- package/dist/_cms/stylus-B533Al4x.js +1 -0
- package/dist/_cms/swift-BzpIVaGY.js +1 -0
- package/dist/_cms/tcl-DVfN8rqt.js +1 -0
- package/dist/_cms/textile-CnDTJFAw.js +1 -0
- package/dist/_cms/tiddlywiki-DO-Gjzrf.js +1 -0
- package/dist/_cms/tiki-DGYXhP31.js +1 -0
- package/dist/_cms/toml-Bm5Em-hy.js +1 -0
- package/dist/_cms/troff-wAsdV37c.js +1 -0
- package/dist/_cms/ttcn-CfJYG6tj.js +1 -0
- package/dist/_cms/ttcn-cfg-B9xdYoR4.js +1 -0
- package/dist/_cms/turtle-B1tBg_DP.js +1 -0
- package/dist/_cms/vb-CmGdzxic.js +1 -0
- package/dist/_cms/vbscript-BuJXcnF6.js +1 -0
- package/dist/_cms/velocity-D8B20fx6.js +1 -0
- package/dist/_cms/verilog-C6RDOZhf.js +1 -0
- package/dist/_cms/vhdl-lSbBsy5d.js +1 -0
- package/dist/_cms/webidl-ZXfAyPTL.js +1 -0
- package/dist/_cms/xquery-DzFWVndE.js +1 -0
- package/dist/_cms/yacas-BJ4BC0dw.js +1 -0
- package/dist/_cms/z80-Hz9HOZM7.js +1 -0
- package/dist/index.html +13 -0
- package/package.json +77 -0
- package/patches/hast-util-to-estree+3.1.3.patch +14 -0
|
@@ -0,0 +1,458 @@
|
|
|
1
|
+
import { Hono } from 'hono'
|
|
2
|
+
import { execFile, spawn } from 'child_process'
|
|
3
|
+
import { promisify } from 'util'
|
|
4
|
+
import { unlink } from 'fs/promises'
|
|
5
|
+
import { join } from 'path'
|
|
6
|
+
import { ROOT_DIR } from '../root.js'
|
|
7
|
+
|
|
8
|
+
const exec = promisify(execFile)
|
|
9
|
+
|
|
10
|
+
let gitRoot: string | undefined
|
|
11
|
+
|
|
12
|
+
async function getGitRoot() {
|
|
13
|
+
if (!gitRoot) {
|
|
14
|
+
const { stdout } = await exec('git', ['rev-parse', '--show-toplevel'], {
|
|
15
|
+
cwd: ROOT_DIR,
|
|
16
|
+
})
|
|
17
|
+
gitRoot = stdout.trimEnd()
|
|
18
|
+
}
|
|
19
|
+
return gitRoot
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
async function git(...args: string[]) {
|
|
23
|
+
const root = await getGitRoot()
|
|
24
|
+
// `core.quotepath=false` emits filenames literally as UTF-8 instead of
|
|
25
|
+
// C-style escape sequences like "d\303\251j\303\240.png", so paths we read
|
|
26
|
+
// from porcelain/diff output round-trip cleanly back into `git add` etc.
|
|
27
|
+
const { stdout } = await exec(
|
|
28
|
+
'git',
|
|
29
|
+
['-c', 'core.quotepath=false', ...args],
|
|
30
|
+
{
|
|
31
|
+
cwd: root,
|
|
32
|
+
maxBuffer: 10 * 1024 * 1024, // 10MB
|
|
33
|
+
}
|
|
34
|
+
)
|
|
35
|
+
return stdout.trimEnd()
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function getBranch() {
|
|
39
|
+
return process.env.GIT_BRANCH || 'main'
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
async function ensureAuthedRemote() {
|
|
43
|
+
const pat = process.env.GIT_PAT
|
|
44
|
+
const repoUrl = process.env.GIT_REPO_URL
|
|
45
|
+
if (pat && repoUrl) {
|
|
46
|
+
const authedUrl = repoUrl.replace(
|
|
47
|
+
'https://',
|
|
48
|
+
`https://x-access-token:${pat}@`
|
|
49
|
+
)
|
|
50
|
+
await git('remote', 'set-url', 'origin', authedUrl)
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
async function isWorktreeClean(): Promise<boolean> {
|
|
55
|
+
const status = await git('status', '--porcelain')
|
|
56
|
+
return status.trim().length === 0
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
async function countBehind(): Promise<number> {
|
|
60
|
+
const branch = getBranch()
|
|
61
|
+
try {
|
|
62
|
+
const out = await git('rev-list', '--count', `HEAD..origin/${branch}`)
|
|
63
|
+
const n = parseInt(out.trim(), 10)
|
|
64
|
+
return Number.isFinite(n) ? n : 0
|
|
65
|
+
} catch {
|
|
66
|
+
return 0
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// --- Remote poll state ---
|
|
71
|
+
|
|
72
|
+
interface RemoteState {
|
|
73
|
+
updateAvailable: boolean
|
|
74
|
+
behind: number
|
|
75
|
+
lastCheckedAt: number | null
|
|
76
|
+
lastPulledAt: number | null
|
|
77
|
+
error?: string
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
const remoteState: RemoteState = {
|
|
81
|
+
updateAvailable: false,
|
|
82
|
+
behind: 0,
|
|
83
|
+
lastCheckedAt: null,
|
|
84
|
+
lastPulledAt: null,
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
async function checkRemote(): Promise<void> {
|
|
88
|
+
try {
|
|
89
|
+
await ensureAuthedRemote()
|
|
90
|
+
await git('fetch', 'origin', getBranch())
|
|
91
|
+
const behind = await countBehind()
|
|
92
|
+
remoteState.lastCheckedAt = Date.now()
|
|
93
|
+
remoteState.error = undefined
|
|
94
|
+
if (behind > 0) {
|
|
95
|
+
const clean = await isWorktreeClean()
|
|
96
|
+
if (clean) {
|
|
97
|
+
await git('pull', '--ff-only', 'origin', getBranch())
|
|
98
|
+
remoteState.behind = 0
|
|
99
|
+
remoteState.updateAvailable = false
|
|
100
|
+
remoteState.lastPulledAt = Date.now()
|
|
101
|
+
} else {
|
|
102
|
+
remoteState.behind = behind
|
|
103
|
+
remoteState.updateAvailable = true
|
|
104
|
+
}
|
|
105
|
+
} else {
|
|
106
|
+
remoteState.behind = 0
|
|
107
|
+
remoteState.updateAvailable = false
|
|
108
|
+
}
|
|
109
|
+
} catch (err) {
|
|
110
|
+
remoteState.error = String((err as any)?.message ?? err)
|
|
111
|
+
remoteState.lastCheckedAt = Date.now()
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
const REMOTE_CHECK_THROTTLE_MS = 60_000
|
|
116
|
+
let checkInFlight = false
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Fire-and-forget remote check, called from the hot path (`/status`,
|
|
120
|
+
* `/remote-status`). Runs at most once per REMOTE_CHECK_THROTTLE_MS so
|
|
121
|
+
* a burst of UI requests doesn't flood `git fetch`.
|
|
122
|
+
*/
|
|
123
|
+
function checkRemoteIfStale(): void {
|
|
124
|
+
if (checkInFlight) return
|
|
125
|
+
const last = remoteState.lastCheckedAt ?? 0
|
|
126
|
+
if (Date.now() - last < REMOTE_CHECK_THROTTLE_MS) return
|
|
127
|
+
checkInFlight = true
|
|
128
|
+
checkRemote().finally(() => {
|
|
129
|
+
checkInFlight = false
|
|
130
|
+
})
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
export const gitRoutes = new Hono()
|
|
134
|
+
|
|
135
|
+
// Git status (file list with staged info).
|
|
136
|
+
// `-uall` enumerates files inside untracked directories individually so they
|
|
137
|
+
// each get a row in the review UI instead of collapsing to just the dir.
|
|
138
|
+
function remoteSnapshot() {
|
|
139
|
+
return {
|
|
140
|
+
updateAvailable: remoteState.updateAvailable,
|
|
141
|
+
behind: remoteState.behind,
|
|
142
|
+
lastCheckedAt: remoteState.lastCheckedAt,
|
|
143
|
+
lastPulledAt: remoteState.lastPulledAt,
|
|
144
|
+
error: remoteState.error,
|
|
145
|
+
branch: getBranch(),
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
interface PorcelainEntry {
|
|
150
|
+
indexStatus: string
|
|
151
|
+
worktreeStatus: string
|
|
152
|
+
path: string
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* Parse `git status --porcelain -z` output. `-z` uses NUL as the record
|
|
157
|
+
* separator and disables all quoting, so paths containing spaces or
|
|
158
|
+
* non-ASCII characters round-trip cleanly. Renames/copies (R/C) emit the
|
|
159
|
+
* origin path as an extra NUL-terminated field right after the main one;
|
|
160
|
+
* we drop it since the UI only cares about the current path.
|
|
161
|
+
*/
|
|
162
|
+
function parsePorcelainZ(raw: string): PorcelainEntry[] {
|
|
163
|
+
const fields = raw.split('\0')
|
|
164
|
+
const out: PorcelainEntry[] = []
|
|
165
|
+
for (let i = 0; i < fields.length; i++) {
|
|
166
|
+
const entry = fields[i]
|
|
167
|
+
if (!entry || entry.length < 3) continue
|
|
168
|
+
const indexStatus = entry[0]
|
|
169
|
+
const worktreeStatus = entry[1]
|
|
170
|
+
const path = entry.substring(3)
|
|
171
|
+
out.push({ indexStatus, worktreeStatus, path })
|
|
172
|
+
if (indexStatus === 'R' || indexStatus === 'C') i++ // skip origin path
|
|
173
|
+
}
|
|
174
|
+
return out
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
gitRoutes.get('/status', async (c) => {
|
|
178
|
+
// Piggyback a throttled remote check on the most-called endpoint. Non
|
|
179
|
+
// blocking: the request returns immediately with whatever we know now;
|
|
180
|
+
// the follow-up fetch updates `lastPulledAt` and the UI detects the
|
|
181
|
+
// change on its next status fetch.
|
|
182
|
+
checkRemoteIfStale()
|
|
183
|
+
try {
|
|
184
|
+
const raw = await git('status', '--porcelain', '-z', '-uall')
|
|
185
|
+
const files = parsePorcelainZ(raw)
|
|
186
|
+
.map(({ indexStatus, worktreeStatus, path }) => {
|
|
187
|
+
const status =
|
|
188
|
+
indexStatus !== ' ' && indexStatus !== '?'
|
|
189
|
+
? indexStatus
|
|
190
|
+
: worktreeStatus !== ' '
|
|
191
|
+
? worktreeStatus
|
|
192
|
+
: indexStatus
|
|
193
|
+
return {
|
|
194
|
+
status,
|
|
195
|
+
staged: indexStatus !== ' ' && indexStatus !== '?',
|
|
196
|
+
path,
|
|
197
|
+
}
|
|
198
|
+
})
|
|
199
|
+
// Sort by path so the row order stays stable when a file toggles
|
|
200
|
+
// between staged and unstaged (git's native output groups by status).
|
|
201
|
+
.sort((a, b) => a.path.localeCompare(b.path))
|
|
202
|
+
return c.json({ files, remote: remoteSnapshot() })
|
|
203
|
+
} catch (err) {
|
|
204
|
+
return c.json({ error: String(err) }, 500)
|
|
205
|
+
}
|
|
206
|
+
})
|
|
207
|
+
|
|
208
|
+
gitRoutes.get('/remote-status', (c) => {
|
|
209
|
+
checkRemoteIfStale()
|
|
210
|
+
return c.json(remoteSnapshot())
|
|
211
|
+
})
|
|
212
|
+
|
|
213
|
+
/**
|
|
214
|
+
* Split a unified diff produced by `git diff` into a per-file map keyed by the
|
|
215
|
+
* `b/` (post-image) path. Expects the standard `diff --git a/X b/X` header.
|
|
216
|
+
*/
|
|
217
|
+
function splitDiffByFile(raw: string): Record<string, string> {
|
|
218
|
+
const out: Record<string, string> = {}
|
|
219
|
+
if (!raw) return out
|
|
220
|
+
let currentPath: string | null = null
|
|
221
|
+
let buf: string[] = []
|
|
222
|
+
for (const line of raw.split('\n')) {
|
|
223
|
+
if (line.startsWith('diff --git ')) {
|
|
224
|
+
if (currentPath) out[currentPath] = buf.join('\n')
|
|
225
|
+
buf = [line]
|
|
226
|
+
const match = line.match(/^diff --git a\/(.+?) b\/(.+)$/)
|
|
227
|
+
currentPath = match ? match[2] : null
|
|
228
|
+
} else {
|
|
229
|
+
buf.push(line)
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
if (currentPath) out[currentPath] = buf.join('\n')
|
|
233
|
+
return out
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
// All diffs in one shot: tracked changes (staged + unstaged vs HEAD) plus
|
|
237
|
+
// synthesized "added file" diffs for every untracked file. Returns a map
|
|
238
|
+
// keyed by file path so the UI can render all of them without N per-file
|
|
239
|
+
// round-trips.
|
|
240
|
+
gitRoutes.get('/diffs', async (c) => {
|
|
241
|
+
try {
|
|
242
|
+
const statusOut = await git('status', '--porcelain', '-z', '-uall')
|
|
243
|
+
const untracked = parsePorcelainZ(statusOut)
|
|
244
|
+
.filter((e) => e.indexStatus === '?')
|
|
245
|
+
.map((e) => e.path)
|
|
246
|
+
|
|
247
|
+
const trackedDiff = await git('diff', 'HEAD')
|
|
248
|
+
const diffs = splitDiffByFile(trackedDiff)
|
|
249
|
+
|
|
250
|
+
for (const path of untracked) {
|
|
251
|
+
try {
|
|
252
|
+
const out = await git('diff', '--no-index', '--', '/dev/null', path)
|
|
253
|
+
diffs[path] = out
|
|
254
|
+
} catch (err: any) {
|
|
255
|
+
// `git diff --no-index` exits with status 1 when files differ;
|
|
256
|
+
// stdout still carries the diff body in that case.
|
|
257
|
+
if (err?.stdout) diffs[path] = String(err.stdout)
|
|
258
|
+
else diffs[path] = ''
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
return c.json({ diffs })
|
|
263
|
+
} catch (err) {
|
|
264
|
+
return c.json({ error: String(err) }, 500)
|
|
265
|
+
}
|
|
266
|
+
})
|
|
267
|
+
|
|
268
|
+
// Stage files. `-A` (--all) is required so new deletions stage correctly
|
|
269
|
+
// (`git add -- deleted_file` alone fails with "pathspec did not match").
|
|
270
|
+
// We also filter out paths that are already fully staged to stay safe if
|
|
271
|
+
// the caller sends redundant entries — running `git add` on an already
|
|
272
|
+
// staged deletion, for instance, would error out.
|
|
273
|
+
gitRoutes.post('/stage', async (c) => {
|
|
274
|
+
const body = await c.req.json<{ paths: string[] }>()
|
|
275
|
+
if (!body.paths?.length) {
|
|
276
|
+
return c.json({ error: 'Missing paths' }, 400)
|
|
277
|
+
}
|
|
278
|
+
try {
|
|
279
|
+
const statusOut = await git(
|
|
280
|
+
'status',
|
|
281
|
+
'--porcelain',
|
|
282
|
+
'-z',
|
|
283
|
+
'-uall',
|
|
284
|
+
'--',
|
|
285
|
+
...body.paths
|
|
286
|
+
)
|
|
287
|
+
const pending = new Set<string>()
|
|
288
|
+
for (const { worktreeStatus, path } of parsePorcelainZ(statusOut)) {
|
|
289
|
+
// worktree != ' ' covers modifications, deletions, and untracked files
|
|
290
|
+
// (porcelain writes '??' for untracked).
|
|
291
|
+
if (worktreeStatus !== ' ') pending.add(path)
|
|
292
|
+
}
|
|
293
|
+
const toStage = body.paths.filter((p) => pending.has(p))
|
|
294
|
+
if (toStage.length) {
|
|
295
|
+
await git('add', '-A', '--', ...toStage)
|
|
296
|
+
}
|
|
297
|
+
return c.json({ ok: true, staged: toStage.length })
|
|
298
|
+
} catch (err) {
|
|
299
|
+
return c.json({ error: String(err) }, 500)
|
|
300
|
+
}
|
|
301
|
+
})
|
|
302
|
+
|
|
303
|
+
// Unstage files
|
|
304
|
+
gitRoutes.post('/unstage', async (c) => {
|
|
305
|
+
const body = await c.req.json<{ paths: string[] }>()
|
|
306
|
+
if (!body.paths?.length) {
|
|
307
|
+
return c.json({ error: 'Missing paths' }, 400)
|
|
308
|
+
}
|
|
309
|
+
try {
|
|
310
|
+
await git('reset', 'HEAD', '--', ...body.paths)
|
|
311
|
+
return c.json({ ok: true })
|
|
312
|
+
} catch (err) {
|
|
313
|
+
return c.json({ error: String(err) }, 500)
|
|
314
|
+
}
|
|
315
|
+
})
|
|
316
|
+
|
|
317
|
+
// Commit staged files and optionally push
|
|
318
|
+
gitRoutes.post('/commit', async (c) => {
|
|
319
|
+
const body = await c.req.json<{ message: string; push?: boolean }>()
|
|
320
|
+
if (!body.message) {
|
|
321
|
+
return c.json({ error: 'Missing commit message' }, 400)
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
try {
|
|
325
|
+
const commitOutput = await git('commit', '-m', body.message)
|
|
326
|
+
|
|
327
|
+
let pushOutput = ''
|
|
328
|
+
if (body.push) {
|
|
329
|
+
await ensureAuthedRemote()
|
|
330
|
+
pushOutput = await git('push', 'origin', getBranch())
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
return c.json({ ok: true, commit: commitOutput, push: pushOutput })
|
|
334
|
+
} catch (err) {
|
|
335
|
+
return c.json({ error: String(err) }, 500)
|
|
336
|
+
}
|
|
337
|
+
})
|
|
338
|
+
|
|
339
|
+
// Pull from remote
|
|
340
|
+
gitRoutes.post('/pull', async (c) => {
|
|
341
|
+
try {
|
|
342
|
+
await ensureAuthedRemote()
|
|
343
|
+
const clean = await isWorktreeClean()
|
|
344
|
+
if (!clean) {
|
|
345
|
+
return c.json(
|
|
346
|
+
{
|
|
347
|
+
ok: false,
|
|
348
|
+
error: 'Working tree is not clean. Commit or discard changes first.',
|
|
349
|
+
},
|
|
350
|
+
400
|
|
351
|
+
)
|
|
352
|
+
}
|
|
353
|
+
const out = await git('pull', '--ff-only', 'origin', getBranch())
|
|
354
|
+
remoteState.behind = 0
|
|
355
|
+
remoteState.updateAvailable = false
|
|
356
|
+
remoteState.lastPulledAt = Date.now()
|
|
357
|
+
return c.json({ ok: true, output: out })
|
|
358
|
+
} catch (err) {
|
|
359
|
+
return c.json({ error: String((err as any)?.message ?? err) }, 500)
|
|
360
|
+
}
|
|
361
|
+
})
|
|
362
|
+
|
|
363
|
+
// Discard a specific hunk (single @@ block) from the working tree.
|
|
364
|
+
// The hunk comes from `git diff HEAD`, so it can span both staged and
|
|
365
|
+
// unstaged changes. We unstage the file first so the worktree is the
|
|
366
|
+
// source of truth, then reverse-apply the hunk.
|
|
367
|
+
gitRoutes.post('/discard-hunk', async (c) => {
|
|
368
|
+
const body = await c.req.json<{ path: string; hunk: string }>()
|
|
369
|
+
if (!body.path || !body.hunk) {
|
|
370
|
+
return c.json({ error: 'Missing path or hunk' }, 400)
|
|
371
|
+
}
|
|
372
|
+
if (body.path.includes('..')) {
|
|
373
|
+
return c.json({ error: 'Invalid path' }, 400)
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
const hunkBody = body.hunk.endsWith('\n') ? body.hunk : body.hunk + '\n'
|
|
377
|
+
const patch = [
|
|
378
|
+
`diff --git a/${body.path} b/${body.path}`,
|
|
379
|
+
`--- a/${body.path}`,
|
|
380
|
+
`+++ b/${body.path}`,
|
|
381
|
+
hunkBody,
|
|
382
|
+
].join('\n')
|
|
383
|
+
|
|
384
|
+
try {
|
|
385
|
+
const root = await getGitRoot()
|
|
386
|
+
|
|
387
|
+
// Drop any staging for this file so the patch applies to the worktree only
|
|
388
|
+
try {
|
|
389
|
+
await git('reset', 'HEAD', '--', body.path)
|
|
390
|
+
} catch {
|
|
391
|
+
// Ignore: file may not be tracked in HEAD yet (newly added)
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
await new Promise<void>((resolve, reject) => {
|
|
395
|
+
const child = spawn('git', ['apply', '--reverse'], { cwd: root })
|
|
396
|
+
let stderr = ''
|
|
397
|
+
child.stderr.on('data', (b) => (stderr += b.toString()))
|
|
398
|
+
child.on('error', reject)
|
|
399
|
+
child.on('close', (code) => {
|
|
400
|
+
if (code === 0) resolve()
|
|
401
|
+
else
|
|
402
|
+
reject(
|
|
403
|
+
new Error(stderr.trim() || `git apply exited with code ${code}`)
|
|
404
|
+
)
|
|
405
|
+
})
|
|
406
|
+
child.stdin.write(patch)
|
|
407
|
+
child.stdin.end()
|
|
408
|
+
})
|
|
409
|
+
|
|
410
|
+
return c.json({ ok: true })
|
|
411
|
+
} catch (err) {
|
|
412
|
+
return c.json(
|
|
413
|
+
{ error: String((err as any)?.message ?? err) },
|
|
414
|
+
500
|
|
415
|
+
)
|
|
416
|
+
}
|
|
417
|
+
})
|
|
418
|
+
|
|
419
|
+
// Discard all changes for a specific file (staged + unstaged)
|
|
420
|
+
gitRoutes.post('/discard', async (c) => {
|
|
421
|
+
const body = await c.req.json<{ path: string }>()
|
|
422
|
+
if (!body.path) {
|
|
423
|
+
return c.json({ error: 'Missing path' }, 400)
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
if (body.path.includes('..')) {
|
|
427
|
+
return c.json({ error: 'Invalid path' }, 400)
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
try {
|
|
431
|
+
// porcelain format: XY where X=index (staged), Y=worktree
|
|
432
|
+
const status = await git(
|
|
433
|
+
'status',
|
|
434
|
+
'--porcelain',
|
|
435
|
+
'-z',
|
|
436
|
+
'--',
|
|
437
|
+
body.path
|
|
438
|
+
)
|
|
439
|
+
const indexStatus = status[0]
|
|
440
|
+
|
|
441
|
+
if (indexStatus === '?') {
|
|
442
|
+
// Untracked file: delete it
|
|
443
|
+
const root = await getGitRoot()
|
|
444
|
+
await unlink(join(root, body.path))
|
|
445
|
+
} else if (indexStatus === 'A') {
|
|
446
|
+
// Newly added (staged): unstage then delete
|
|
447
|
+
await git('reset', 'HEAD', '--', body.path)
|
|
448
|
+
const root = await getGitRoot()
|
|
449
|
+
await unlink(join(root, body.path))
|
|
450
|
+
} else {
|
|
451
|
+
// Tracked file: fully restore to HEAD
|
|
452
|
+
await git('checkout', 'HEAD', '--', body.path)
|
|
453
|
+
}
|
|
454
|
+
return c.json({ ok: true, path: body.path })
|
|
455
|
+
} catch (err) {
|
|
456
|
+
return c.json({ error: String(err) }, 500)
|
|
457
|
+
}
|
|
458
|
+
})
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import { Hono } from 'hono'
|
|
2
|
+
import { readdir } from 'fs/promises'
|
|
3
|
+
import { join, relative } from 'path'
|
|
4
|
+
import { ROOT_DIR } from '../root.js'
|
|
5
|
+
import { loadConfig } from '../config.js'
|
|
6
|
+
import { getFrontmatter, getByPath } from '../parsers/frontmatter.js'
|
|
7
|
+
import { matchGlob } from '../parsers/glob.js'
|
|
8
|
+
|
|
9
|
+
interface TreeNode {
|
|
10
|
+
name: string
|
|
11
|
+
path: string
|
|
12
|
+
type: 'file' | 'directory'
|
|
13
|
+
children?: TreeNode[]
|
|
14
|
+
data?: Record<string, unknown>
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
interface IncludeSpec {
|
|
18
|
+
pattern: string
|
|
19
|
+
fields: string[]
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function parseIncludeParams(raw: string[]): IncludeSpec[] {
|
|
23
|
+
// Each entry: "<glob>:<field1>[,<field2>...]".
|
|
24
|
+
const out: IncludeSpec[] = []
|
|
25
|
+
for (const p of raw) {
|
|
26
|
+
const idx = p.indexOf(':')
|
|
27
|
+
if (idx < 0) continue
|
|
28
|
+
const pattern = p.slice(0, idx)
|
|
29
|
+
const fields = p
|
|
30
|
+
.slice(idx + 1)
|
|
31
|
+
.split(',')
|
|
32
|
+
.map((s) => s.trim())
|
|
33
|
+
.filter(Boolean)
|
|
34
|
+
if (pattern && fields.length) out.push({ pattern, fields })
|
|
35
|
+
}
|
|
36
|
+
return out
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
async function buildTree(
|
|
40
|
+
dir: string,
|
|
41
|
+
contentRoot: string,
|
|
42
|
+
includes: IncludeSpec[]
|
|
43
|
+
): Promise<TreeNode[]> {
|
|
44
|
+
const entries = await readdir(dir, { withFileTypes: true })
|
|
45
|
+
const nodes: TreeNode[] = []
|
|
46
|
+
|
|
47
|
+
for (const entry of entries.sort((a, b) => a.name.localeCompare(b.name))) {
|
|
48
|
+
const fullPath = join(dir, entry.name)
|
|
49
|
+
const relPath = relative(contentRoot, fullPath)
|
|
50
|
+
if (entry.isDirectory()) {
|
|
51
|
+
const children = await buildTree(fullPath, contentRoot, includes)
|
|
52
|
+
nodes.push({
|
|
53
|
+
name: entry.name,
|
|
54
|
+
path: relPath,
|
|
55
|
+
type: 'directory',
|
|
56
|
+
children,
|
|
57
|
+
})
|
|
58
|
+
} else {
|
|
59
|
+
nodes.push({ name: entry.name, path: relPath, type: 'file' })
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// Attach requested frontmatter fields to files whose path matches an
|
|
64
|
+
// include glob. Folder grouping (index files, locale folders) stays on
|
|
65
|
+
// the client, so directories are never inspected here.
|
|
66
|
+
if (includes.length > 0) {
|
|
67
|
+
await Promise.all(
|
|
68
|
+
nodes.map(async (node) => {
|
|
69
|
+
if (node.type !== 'file') return
|
|
70
|
+
const fields = new Set<string>()
|
|
71
|
+
for (const inc of includes) {
|
|
72
|
+
if (matchGlob(node.path, inc.pattern)) {
|
|
73
|
+
for (const f of inc.fields) fields.add(f)
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
if (fields.size === 0) return
|
|
77
|
+
const fm = await getFrontmatter(join(dir, node.name))
|
|
78
|
+
const data: Record<string, unknown> = {}
|
|
79
|
+
for (const f of fields) {
|
|
80
|
+
const v = getByPath(fm, f)
|
|
81
|
+
if (v !== undefined) data[f] = v
|
|
82
|
+
}
|
|
83
|
+
if (Object.keys(data).length > 0) node.data = data
|
|
84
|
+
})
|
|
85
|
+
)
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
return nodes
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
export const treeRoutes = new Hono()
|
|
92
|
+
|
|
93
|
+
treeRoutes.get('/', async (c) => {
|
|
94
|
+
const config = await loadConfig()
|
|
95
|
+
const contentRoot = join(ROOT_DIR, config.contentDir)
|
|
96
|
+
const url = new URL(c.req.url)
|
|
97
|
+
const includes = parseIncludeParams(url.searchParams.getAll('include'))
|
|
98
|
+
const tree = await buildTree(contentRoot, contentRoot, includes)
|
|
99
|
+
return c.json(tree)
|
|
100
|
+
})
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import { Hono } from 'hono'
|
|
2
|
+
import { writeFile, mkdir } from 'fs/promises'
|
|
3
|
+
import { join } from 'path'
|
|
4
|
+
import { ROOT_DIR } from '../root.js'
|
|
5
|
+
import { loadConfig } from '../config.js'
|
|
6
|
+
|
|
7
|
+
export const uploadRoutes = new Hono()
|
|
8
|
+
|
|
9
|
+
// Upload a media file (targetDir is relative to CONTENT_DIR)
|
|
10
|
+
uploadRoutes.post('/', async (c) => {
|
|
11
|
+
const formData = await c.req.formData()
|
|
12
|
+
const file = formData.get('file') as File | null
|
|
13
|
+
const targetDir = formData.get('targetDir') as string | null
|
|
14
|
+
|
|
15
|
+
if (!file || !targetDir) {
|
|
16
|
+
return c.json({ error: 'Missing file or targetDir' }, 400)
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
if (targetDir.includes('..')) {
|
|
20
|
+
return c.json({ error: 'Invalid path' }, 400)
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const config = await loadConfig()
|
|
24
|
+
const fullDir = join(ROOT_DIR, config.contentDir, targetDir)
|
|
25
|
+
await mkdir(fullDir, { recursive: true })
|
|
26
|
+
|
|
27
|
+
const relPath = join(targetDir, file.name)
|
|
28
|
+
|
|
29
|
+
const buffer = Buffer.from(await file.arrayBuffer())
|
|
30
|
+
await writeFile(join(fullDir, file.name), buffer)
|
|
31
|
+
|
|
32
|
+
return c.json({ ok: true, path: relPath, name: file.name })
|
|
33
|
+
})
|