rvlib-shipkit 2.12.0 → 2.13.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.
Files changed (290) hide show
  1. package/CHANGELOG.md +48 -13
  2. package/README.md +1 -1
  3. package/package.json +4 -2
  4. package/src/agent-time/store.ts +2 -2
  5. package/src/check.ts +1 -1
  6. package/src/checks/SK001-package-json-shape.check.ts +4 -0
  7. package/src/checks/SK003-claude-md-shape.check.ts +1 -1
  8. package/src/checks/SK004-gitignore-policy.check.ts +27 -2
  9. package/src/checks/SK005-rvlib-versions.check.ts +1 -1
  10. package/src/checks/SK008-reviewer-agent.check.ts +2 -2
  11. package/src/checks/SK012-config-barrel-import.check.ts +1 -1
  12. package/src/checks/SK015-nested-repo.check.ts +1 -1
  13. package/src/checks/SK035-public-clean.check.ts +46 -0
  14. package/src/checks/SK036-draft-media-size.check.ts +28 -0
  15. package/src/cli/cmd_Backup.ts +152 -0
  16. package/src/cli/cmd_Ci.ts +8 -11
  17. package/src/cli/cmd_Drill.ts +145 -0
  18. package/src/cli/cmd_Home.ts +50 -3
  19. package/src/cli/cmd_Media.ts +7 -5
  20. package/src/cli/cmd_Review.ts +4 -2
  21. package/src/cli/cmd_Shot.ts +5 -2
  22. package/src/cli/cmd_Smoke.ts +4 -1
  23. package/src/cli/cmd_Social.ts +226 -0
  24. package/src/cli/cmd_Start.ts +1 -1
  25. package/src/cli/cmd_Time.ts +1 -1
  26. package/src/cli/cmd_Trailer.ts +5 -2
  27. package/src/cli/cmd_Veille.ts +173 -0
  28. package/src/cli/family-pages.ts +3 -0
  29. package/src/cli/registry.ts +13 -2
  30. package/src/cli.ts +5 -2
  31. package/src/collections/actions.ts +1 -49
  32. package/src/collections/checks.ts +2 -1
  33. package/src/collections/collection-types.ts +11 -2
  34. package/src/collections/providers.ts +4 -4
  35. package/src/collections/review.ts +2 -1
  36. package/src/collections/tests.ts +19 -8
  37. package/src/contracts/apply-contracts.ts +1 -3
  38. package/src/contracts/contract-manifests.ts +2 -3
  39. package/src/contracts/contract-types.ts +0 -1
  40. package/src/contracts/contracts-engine.ts +0 -3
  41. package/src/core/app-icon.ts +9 -3
  42. package/src/core/bun-builtins.ts +2 -2
  43. package/src/core/discover-checks.ts +37 -5
  44. package/src/core/fix-scope.ts +4 -2
  45. package/src/core/floor-resolve.ts +68 -48
  46. package/src/core/floor.ts +9 -0
  47. package/src/core/git-root.ts +24 -0
  48. package/src/core/last-good.ts +3 -1
  49. package/src/core/layout.ts +8 -0
  50. package/src/core/log-stamp.ts +36 -0
  51. package/src/core/loop-watch.ts +93 -0
  52. package/src/core/machine-link.ts +134 -0
  53. package/src/core/memory-watch.ts +41 -0
  54. package/src/core/one-way-in-policy.ts +27 -19
  55. package/src/core/run-ci.ts +6 -8
  56. package/src/core/run-fixers.ts +9 -9
  57. package/src/core/slots.ts +21 -0
  58. package/src/core/tree-rev.ts +60 -0
  59. package/src/core/unit-files.ts +42 -0
  60. package/src/drill/render.ts +140 -0
  61. package/src/drill/run.ts +269 -0
  62. package/src/fleet/scan-fleet.ts +6 -5
  63. package/src/home/daemon-jobs.ts +201 -0
  64. package/src/home/global-context.ts +34 -1
  65. package/src/home/home-routes.ts +22 -15
  66. package/src/home/hubs.ts +10 -3
  67. package/src/home/inbox-shape.ts +53 -4
  68. package/src/home/inbox.ts +16 -1
  69. package/src/home/lib-watch.ts +10 -3
  70. package/src/home/machine-stores.ts +15 -29
  71. package/src/home/run-daemon.ts +15 -18
  72. package/src/home/run-pm.ts +2 -0
  73. package/src/home/serve-daemon.ts +4 -3
  74. package/src/home/serve-web.ts +104 -1
  75. package/src/hooks/commit-message.ts +28 -19
  76. package/src/hooks/leak-guard.ts +10 -3
  77. package/src/hooks/run-hooks.ts +2 -6
  78. package/src/identities/identities.ts +334 -0
  79. package/src/identities/machine-route.ts +126 -0
  80. package/src/identities/machine.ts +62 -0
  81. package/src/init-repo.ts +1 -1
  82. package/src/ledger/check-stats.ts +10 -9
  83. package/src/ledger/gate-checks.ts +32 -1
  84. package/src/ledger/record-kept.ts +112 -0
  85. package/src/ledger/repo-view.ts +10 -0
  86. package/src/ledger/run-log.ts +17 -12
  87. package/src/ledger/run-timeline.ts +8 -4
  88. package/src/ledger/store.ts +7 -0
  89. package/src/ledger/suite-log.ts +17 -13
  90. package/src/media-kinds.ts +2 -1
  91. package/src/media-store/backup/daemon.ts +73 -0
  92. package/src/media-store/backup/keys.ts +25 -0
  93. package/src/media-store/backup/manifest.ts +193 -0
  94. package/src/media-store/backup/restore.ts +192 -0
  95. package/src/media-store/backup/run.ts +278 -0
  96. package/src/media-store/backup/secret.ts +86 -0
  97. package/src/media-store/backup/snapshot.ts +64 -0
  98. package/src/media-store/backup/sources.ts +199 -0
  99. package/src/media-store/daemon.ts +56 -70
  100. package/src/media-store/put.ts +4 -2
  101. package/src/media-store/space.ts +23 -4
  102. package/src/pages/forward.ts +35 -0
  103. package/src/pages/frame-client.ts +118 -0
  104. package/src/pages/routes.ts +29 -0
  105. package/src/pages/unit.ts +92 -0
  106. package/src/plan/planner.ts +12 -0
  107. package/src/plan/repo-source.ts +19 -5
  108. package/src/reflect/transcript.ts +35 -10
  109. package/src/release/public-clean.ts +301 -0
  110. package/src/release/published-here.ts +22 -0
  111. package/src/release/run-bump.ts +2 -1
  112. package/src/release/run-release.ts +4 -1
  113. package/src/repo-config.ts +53 -2
  114. package/src/review/http.ts +16 -2
  115. package/src/review/model.ts +15 -4
  116. package/src/review/notes.ts +77 -0
  117. package/src/review/project.ts +6 -38
  118. package/src/review/routes.ts +4 -12
  119. package/src/review/store.ts +41 -1
  120. package/src/review/trailer-media.ts +7 -4
  121. package/src/review/trailer-shots.ts +21 -4
  122. package/src/review/unit.ts +1 -1
  123. package/src/scaffold/files/agent-handoff.md +3 -0
  124. package/src/scaffold/files/agent-index.md +4 -12
  125. package/src/scaffold/project-scaffold.ts +5 -4
  126. package/src/serve/embed.ts +1 -1
  127. package/src/serve/hub-app/col-card.tsx +3 -17
  128. package/src/serve/hub-app/col-tables-view.tsx +2 -1
  129. package/src/serve/hub-app/cols-svc.ts +0 -13
  130. package/src/serve/hub-app/dock-st.ts +4 -2
  131. package/src/serve/hub-app/dock-view.tsx +2 -0
  132. package/src/serve/hub-app/frames-st.ts +35 -0
  133. package/src/serve/hub-app/hash-route.ts +64 -1
  134. package/src/serve/hub-app/home-st.ts +31 -7
  135. package/src/serve/hub-app/home-strip.css +9 -1
  136. package/src/serve/hub-app/home-strip.tsx +7 -6
  137. package/src/serve/hub-app/home.html +5 -0
  138. package/src/serve/hub-app/hub-app.tsx +49 -5
  139. package/src/serve/hub-app/hub.css +29 -11
  140. package/src/serve/hub-app/icons.tsx +16 -1
  141. package/src/serve/hub-app/identities-st.ts +210 -0
  142. package/src/serve/hub-app/identities-view.tsx +352 -0
  143. package/src/serve/hub-app/identities.css +48 -0
  144. package/src/serve/hub-app/identity-address.ts +52 -0
  145. package/src/serve/hub-app/inbox-st.ts +59 -6
  146. package/src/serve/hub-app/inbox-view.tsx +48 -6
  147. package/src/serve/hub-app/inbox.css +12 -1
  148. package/src/serve/hub-app/index.html +3 -0
  149. package/src/serve/hub-app/keys.ts +10 -2
  150. package/src/serve/hub-app/list-walk.ts +89 -0
  151. package/src/serve/hub-app/machine-st.ts +12 -0
  152. package/src/serve/hub-app/media-view.tsx +2 -1
  153. package/src/serve/hub-app/pages-frames.tsx +134 -0
  154. package/src/serve/hub-app/pages-svc.ts +111 -0
  155. package/src/serve/hub-app/palette-st.ts +18 -3
  156. package/src/serve/hub-app/palette-view.tsx +9 -2
  157. package/src/serve/hub-app/planned-view.tsx +2 -1
  158. package/src/serve/hub-app/project-list.css +18 -14
  159. package/src/serve/hub-app/project-list.tsx +88 -49
  160. package/src/serve/hub-app/projects-svc.ts +33 -3
  161. package/src/serve/hub-app/repo-sections.ts +19 -2
  162. package/src/serve/hub-app/repo-st.ts +75 -19
  163. package/src/serve/hub-app/review/review-cells.tsx +3 -3
  164. package/src/serve/hub-app/review/review-kit.ts +30 -7
  165. package/src/serve/hub-app/review/review-page.tsx +1 -1
  166. package/src/serve/hub-app/review/review-st.ts +19 -8
  167. package/src/serve/hub-app/review/review-thread.tsx +11 -9
  168. package/src/serve/hub-app/review/review-views.tsx +84 -19
  169. package/src/serve/hub-app/review/thread-st.ts +1 -10
  170. package/src/serve/hub-app/review/trailer-page.tsx +2 -1
  171. package/src/serve/hub-app/review.css +17 -8
  172. package/src/serve/hub-app/site/site-part.tsx +173 -0
  173. package/src/serve/hub-app/site/site-st.ts +157 -0
  174. package/src/serve/hub-app/site.css +35 -0
  175. package/src/serve/hub-app/social/calendar.tsx +182 -0
  176. package/src/serve/hub-app/social/comms-page.tsx +306 -0
  177. package/src/serve/hub-app/social/kit.tsx +244 -0
  178. package/src/serve/hub-app/social/post-card.tsx +456 -0
  179. package/src/serve/hub-app/social/problems.tsx +60 -0
  180. package/src/serve/hub-app/social/social-model.ts +154 -0
  181. package/src/serve/hub-app/social/social-page.tsx +674 -0
  182. package/src/serve/hub-app/social/social-st.ts +986 -0
  183. package/src/serve/hub-app/social.css +289 -0
  184. package/src/serve/hub-app/stores/stores-page.tsx +311 -0
  185. package/src/serve/hub-app/stores/stores-st.ts +286 -0
  186. package/src/serve/hub-app/stores.css +54 -0
  187. package/src/serve/hub-app/timeline-geometry.ts +1 -1
  188. package/src/serve/hub-app/timeline-view.tsx +3 -2
  189. package/src/serve/hub-app/tree-build.ts +6 -0
  190. package/src/serve/hub-app/tree-row.tsx +27 -4
  191. package/src/serve/hub-app/tree-svc.ts +18 -49
  192. package/src/serve/hub-app/veille/veille-page.tsx +514 -0
  193. package/src/serve/hub-app/veille/veille-st.ts +443 -0
  194. package/src/serve/hub-app/veille.css +84 -0
  195. package/src/serve/hub-runtime.ts +14 -9
  196. package/src/serve/project-gutter-route.ts +44 -12
  197. package/src/serve/run-lanes.ts +7 -4
  198. package/src/serve/same-origin.ts +1 -1
  199. package/src/serve/serve-hub.ts +20 -12
  200. package/src/serve/tracked-watch.ts +24 -3
  201. package/src/serve/warm-cell.ts +5 -1
  202. package/src/shipkit-files.ts +15 -4
  203. package/src/shipkit.ts +0 -1
  204. package/src/shot/box.ts +162 -0
  205. package/src/shot/browser-slot.ts +62 -0
  206. package/src/shot/client.ts +43 -7
  207. package/src/shot/clip-encoder.ts +93 -12
  208. package/src/shot/film-box.ts +188 -0
  209. package/src/shot/shooter.ts +50 -15
  210. package/src/shot/shot-ask.ts +10 -2
  211. package/src/shot/stepped-clip.ts +5 -4
  212. package/src/site/desk.ts +242 -0
  213. package/src/site/routes.ts +66 -0
  214. package/src/site/unit.ts +62 -0
  215. package/src/social/brands.ts +22 -0
  216. package/src/social/compose.ts +97 -0
  217. package/src/social/desk-index.ts +123 -0
  218. package/src/social/desk-wire.ts +64 -0
  219. package/src/social/drafts.ts +183 -0
  220. package/src/social/house.ts +24 -0
  221. package/src/social/load.ts +319 -0
  222. package/src/social/md.ts +70 -0
  223. package/src/social/media-facts.ts +53 -0
  224. package/src/social/media-kind.ts +189 -0
  225. package/src/social/media-limit.ts +42 -0
  226. package/src/social/media-of.ts +30 -0
  227. package/src/social/migrate.ts +123 -0
  228. package/src/social/open.ts +17 -0
  229. package/src/social/post-url.ts +158 -0
  230. package/src/social/presence.ts +332 -0
  231. package/src/social/public-read.ts +53 -0
  232. package/src/social/publish/bluesky.ts +168 -0
  233. package/src/social/publish/discord.ts +76 -0
  234. package/src/social/publish/index.ts +24 -0
  235. package/src/social/publish/secret.ts +13 -0
  236. package/src/social/push.ts +31 -0
  237. package/src/social/richtext.ts +37 -0
  238. package/src/social/routes.ts +180 -0
  239. package/src/social/stats.ts +134 -0
  240. package/src/social/text.ts +267 -0
  241. package/src/social/tracker-route.ts +39 -0
  242. package/src/social/tracker.ts +255 -0
  243. package/src/social/unit.ts +53 -0
  244. package/src/social/writes.ts +130 -0
  245. package/src/start-repo.ts +14 -9
  246. package/src/stores/collection.ts +27 -0
  247. package/src/stores/desk.ts +336 -0
  248. package/src/stores/model.ts +187 -0
  249. package/src/stores/routes.ts +126 -0
  250. package/src/stores/unit.ts +57 -0
  251. package/src/suite/calibrate.ts +127 -0
  252. package/src/suite/core-counters.ts +91 -0
  253. package/src/suite/judge-suites.ts +94 -34
  254. package/src/suite/suite-cli.ts +44 -10
  255. package/src/suite/suite-cpu-preload.ts +34 -9
  256. package/src/trailer/adapter.ts +4 -0
  257. package/src/trailer/build.ts +42 -11
  258. package/src/trailer/ffmpeg.ts +11 -0
  259. package/src/trailer/footage.ts +30 -13
  260. package/src/trailer/grammar.ts +18 -2
  261. package/src/trailer/graph.ts +28 -6
  262. package/src/trailer/passage.ts +17 -5
  263. package/src/trailer/plan.ts +74 -3
  264. package/src/trailer/stitch.ts +67 -26
  265. package/src/trailer/url.ts +1 -1
  266. package/src/trailer/versions.ts +24 -0
  267. package/src/ts/gate-typecheck.ts +32 -0
  268. package/src/ts/typecheck-cli.ts +9 -11
  269. package/src/tui/tui-app.tsx +0 -1
  270. package/src/veille/asks.ts +155 -0
  271. package/src/veille/auto.ts +31 -0
  272. package/src/veille/batch.ts +27 -0
  273. package/src/veille/chrome.ts +127 -0
  274. package/src/veille/collection.ts +28 -0
  275. package/src/veille/desk.ts +242 -0
  276. package/src/veille/identity.ts +80 -0
  277. package/src/veille/import.ts +68 -0
  278. package/src/veille/log.ts +183 -0
  279. package/src/veille/platforms.ts +118 -0
  280. package/src/veille/reads.ts +66 -0
  281. package/src/veille/replied.ts +110 -0
  282. package/src/veille/routes.ts +185 -0
  283. package/src/veille/service.ts +81 -0
  284. package/src/veille/watch.ts +116 -0
  285. package/src/veille/worth.ts +46 -0
  286. package/src/wiki/scan-md.ts +18 -0
  287. package/src/collections/posts.ts +0 -142
  288. package/src/contracts/manifests/social.contract.ts +0 -20
  289. package/src/scaffold/files/agent-coding.md +0 -35
  290. package/src/ts/typecheck-cache.ts +0 -11
@@ -0,0 +1,242 @@
1
+ // the site desk read (wiki build/shipkit/site.md): an identity's site, its address asked online, the repo that builds
2
+ // it, and through that repo's site unit its pages, its preview, what is online and what changed since. Nothing here
3
+ // writes: the preview and the deploy are the unit's own commands (routes.ts)
4
+ import { existsSync, readFileSync } from 'node:fs'
5
+ import { homedir } from 'node:os'
6
+ import { join } from 'node:path'
7
+ import { importFresh, repoUnitFiles } from '../core/discover-checks.ts'
8
+ import { loadIdentities, type Identity } from '../identities/identities.ts'
9
+ import { houseRepoRoots } from '../social/house.ts'
10
+ import { siteProblem, type SiteDated, type SitePage, type SiteUnit } from './unit.ts'
11
+
12
+ export type { SiteDated, SitePage }
13
+
14
+ export type Deployed = { commit: string; at: string; url: string; source: 'record' | 'cloudflare' }
15
+
16
+ /** a page a changed file touches (`*` every page), and the files */
17
+ export type SiteChange = { path: string; files: string[] }
18
+
19
+ export type SiteRepo = { name: string; house: boolean; path: string | null }
20
+
21
+ export type SiteDesk = {
22
+ pages: SitePage[]
23
+ preview: { url: string; up: boolean } | null
24
+ deploy: { about: string | null } | null
25
+ deployed: Deployed | null
26
+ changes: SiteChange[]
27
+ dated: { since: number; words: string } | null
28
+ deploying: boolean
29
+ }
30
+
31
+ export type SiteAnswer = {
32
+ id: string
33
+ name: string
34
+ address: string
35
+ /** the address asked online: its status, null when nothing answered; null with no address */
36
+ online: { status: number | null; at: string } | null
37
+ repo: SiteRepo | null
38
+ /** why there is no desk, in words; null when there is one */
39
+ missing: string | null
40
+ desk: SiteDesk | null
41
+ error: string | null
42
+ }
43
+
44
+ const UNIT = /\bsite\s*\(|kind:\s*['"]site['"]/
45
+ const FIVE_MIN = 5 * 60_000
46
+
47
+ /** the repo's site unit, null when it has none */
48
+ export async function loadSiteUnit(root: string): Promise<SiteUnit | null> {
49
+ const found: { file: string; unit: SiteUnit }[] = []
50
+ for (const f of repoUnitFiles(root)) {
51
+ if (f.endsWith('.check.shipkit.ts') || !UNIT.test(readFileSync(f, 'utf8'))) continue
52
+ const d = (await importFresh(f)) as { kind?: unknown } | undefined
53
+ if (d?.kind !== 'site') continue
54
+ const why = siteProblem(d as SiteUnit)
55
+ if (why !== null) throw new Error(`${f}: ${why}`)
56
+ found.push({ file: f, unit: d as SiteUnit })
57
+ }
58
+ if (found.length > 1) throw new Error(`two site units: ${found.map((x) => x.file).join(' and ')}; keep one`)
59
+ return found[0]?.unit ?? null
60
+ }
61
+
62
+ export async function run(cmd: readonly string[], cwd: string): Promise<{ ok: boolean; out: string; err: string }> {
63
+ try {
64
+ const p = Bun.spawn([...cmd], { cwd, stdin: 'ignore', stdout: 'pipe', stderr: 'pipe' })
65
+ const [out, err, code] = await Promise.all([new Response(p.stdout).text(), new Response(p.stderr).text(), p.exited])
66
+ return { ok: code === 0, out, err: err.trim() }
67
+ } catch (e) {
68
+ return { ok: false, out: '', err: (e as Error).message }
69
+ }
70
+ }
71
+
72
+ export const answers = (url: string, ms = 800): Promise<boolean> =>
73
+ fetch(url, { signal: AbortSignal.timeout(ms) }).then(
74
+ () => true,
75
+ () => false,
76
+ )
77
+
78
+ const probes = new Map<string, { at: number; status: number | null }>()
79
+
80
+ /** the address asked online, at most every five minutes */
81
+ async function probe(address: string): Promise<{ status: number | null; at: string }> {
82
+ let p = probes.get(address)
83
+ if (p == null || Date.now() - p.at > FIVE_MIN) {
84
+ const status = await fetch(address, { redirect: 'follow', signal: AbortSignal.timeout(4000) }).then(
85
+ (r) => r.status,
86
+ () => null,
87
+ )
88
+ p = { at: Date.now(), status }
89
+ probes.set(address, p)
90
+ }
91
+ return { status: p.status, at: new Date(p.at).toISOString() }
92
+ }
93
+
94
+ const cloudflare = new Map<string, { at: number; value: Deployed | null }>()
95
+
96
+ /** a deploy ended: what Cloudflare says is asked again */
97
+ export const forgetDeployed = (): void => cloudflare.clear()
98
+
99
+ /** the Cloudflare token the house config names (`serve.cloudflareToken`), read through its `credsReadCommand`; null when either is missing */
100
+ async function cloudflareToken(cwd: string): Promise<string | null> {
101
+ const { houseRootOf } = await import('../home/home-start.ts')
102
+ const { loadRepoConfig } = await import('../repo-config.ts')
103
+ const { readSecret } = await import('../smoke/run-smoke.ts')
104
+ const house = houseRootOf(cwd)
105
+ if (!house) return null
106
+ const c = await loadRepoConfig(house)
107
+ const name = c.serve?.cloudflareToken
108
+ if (!name || !c.credsReadCommand) return null
109
+ try {
110
+ return readSecret(c.credsReadCommand, name)
111
+ } catch {
112
+ return null
113
+ }
114
+ }
115
+
116
+ /** the last production deployment Cloudflare Pages reports, asked at most every five minutes */
117
+ async function fromCloudflare(pages: { account: string; project: string }, cwd: string): Promise<Deployed | null> {
118
+ const key = `${pages.account}/${pages.project}`
119
+ const kept = cloudflare.get(key)
120
+ if (kept && Date.now() - kept.at < FIVE_MIN) return kept.value
121
+ const token = await cloudflareToken(cwd)
122
+ let value: Deployed | null = null
123
+ if (token != null) {
124
+ const url = `https://api.cloudflare.com/client/v4/accounts/${pages.account}/pages/projects/${pages.project}/deployments?env=production&per_page=1`
125
+ const r = await fetch(url, { headers: { Authorization: `Bearer ${token}` }, signal: AbortSignal.timeout(5000) }).catch(() => null)
126
+ const d = r?.ok ? ((await r.json()) as { result?: { created_on: string; url: string; deployment_trigger?: { metadata?: { commit_hash?: string } } }[] }) : null
127
+ const last = d?.result?.[0]
128
+ const commit = last?.deployment_trigger?.metadata?.commit_hash
129
+ if (last && commit) value = { commit, at: last.created_on, url: last.url, source: 'cloudflare' }
130
+ }
131
+ cloudflare.set(key, { at: Date.now(), value })
132
+ return value
133
+ }
134
+
135
+ async function deployedOf(unit: SiteUnit, root: string): Promise<Deployed | null> {
136
+ const rec = unit.online?.record
137
+ if (rec != null && existsSync(join(root, rec))) {
138
+ const d = JSON.parse(readFileSync(join(root, rec), 'utf8')) as Omit<Deployed, 'source'>
139
+ return { commit: d.commit, at: d.at, url: d.url, source: 'record' }
140
+ }
141
+ return unit.online?.pages ? fromCloudflare(unit.online.pages, root) : null
142
+ }
143
+
144
+ const lines = (s: string): string[] => s.split('\n').filter(Boolean)
145
+
146
+ /** the files of the unit's sources that differ from the deployed commit, the working tree's and the new ones included;
147
+ * no rename detection: both sides of a move changed the site, and it costs 0.7 s on a big repo */
148
+ async function changedFiles(root: string, commit: string, sources: readonly string[]): Promise<string[]> {
149
+ if (sources.length === 0) return []
150
+ const [diff, fresh] = await Promise.all([run(['git', 'diff', '--no-renames', '--name-only', commit, '--', ...sources], root), run(['git', 'ls-files', '--others', '--exclude-standard', '--', ...sources], root)])
151
+ return [...new Set([...lines(diff.out), ...lines(fresh.out)])]
152
+ }
153
+
154
+ /** the files of the identity's folder the site reads, changed since the deploy: committed since, or not committed */
155
+ async function identityChanges(i: Identity, files: readonly string[], since: string): Promise<string[]> {
156
+ if (files.length === 0) return []
157
+ const [log, status] = await Promise.all([run(['git', 'log', `--since=${since}`, '--format=', '--name-only', '--', ...files], i.dir), run(['git', 'status', '--porcelain', '--', ...files], i.dir)])
158
+ return [...new Set([...lines(log.out), ...lines(status.out).map((l) => l.slice(3))])]
159
+ }
160
+
161
+ /** each changed file grouped under the page it touches; a file that touches none is left out */
162
+ export function changesOf(files: readonly string[], touches: SiteUnit['touches'], ctx: { pages: readonly SitePage[]; dated: readonly SiteDated[] }): SiteChange[] {
163
+ const by = new Map<string, string[]>()
164
+ for (const f of files) {
165
+ const path = touches == null ? '*' : touches(f, ctx)
166
+ if (path != null) by.set(path, [...(by.get(path) ?? []), f])
167
+ }
168
+ return [...by].map(([path, fs]) => ({ path, files: fs }))
169
+ }
170
+
171
+ const deploying = new Set<string>()
172
+ export const isDeploying = (id: string): boolean => deploying.has(id)
173
+ export const setDeploying = (id: string, on: boolean): void => void (on ? deploying.add(id) : deploying.delete(id))
174
+
175
+ /** the repo that builds an identity's site: a house repo, else a folder of `~/dev` by that name, else null */
176
+ export async function siteRepoOf(root: string, name: string): Promise<SiteRepo | null> {
177
+ if (name === '') return null
178
+ const house = (await houseRepoRoots(root)).find((r) => r.name === name)
179
+ if (house) return { name, house: true, path: house.path }
180
+ const dev = join(homedir(), 'dev', name)
181
+ return { name, house: false, path: existsSync(dev) ? dev : null }
182
+ }
183
+
184
+ async function deskOf(i: Identity, root: string, unit: SiteUnit): Promise<SiteDesk> {
185
+ const listed = await run(unit.pages, root)
186
+ if (!listed.ok) throw new Error(`${unit.pages.join(' ')}: ${listed.err || 'failed'}`)
187
+ const { pages, dated = [] } = JSON.parse(listed.out) as { pages: SitePage[]; dated?: SiteDated[] }
188
+ const [live, up] = await Promise.all([deployedOf(unit, root), unit.preview ? answers(unit.preview.url) : Promise.resolve(false)])
189
+ const [files, ids] = live ? await Promise.all([changedFiles(root, live.commit, unit.sources ?? []), identityChanges(i, unit.identityFiles ?? [], live.at)]) : [[], []]
190
+ const changes = changesOf(files, unit.touches, { pages, dated })
191
+ if (ids.length > 0) {
192
+ const every = changes.find((c) => c.path === '*')
193
+ if (every) every.files.push(...ids)
194
+ else changes.push({ path: '*', files: ids })
195
+ }
196
+ return {
197
+ pages,
198
+ preview: unit.preview ? { url: unit.preview.url, up } : null,
199
+ deploy: unit.deploy ? { about: unit.deploy.about ?? null } : null,
200
+ deployed: live,
201
+ changes,
202
+ dated: unit.dated && live ? { since: dated.filter((d) => d.at > live.at).length, words: unit.dated } : null,
203
+ deploying: deploying.has(i.id),
204
+ }
205
+ }
206
+
207
+ /** the identity named, its unit and the repo that holds it: what the preview and the deploy act on */
208
+ export async function siteTarget(root: string, id: string): Promise<{ i: Identity; repo: SiteRepo | null; unit: SiteUnit | null; unitError: string | null }> {
209
+ const ids = await loadIdentities({ cwd: root })
210
+ const i = ids.identities.find((x) => x.id === id)
211
+ if (i == null) throw new Error(`no identity ${id} (${ids.identities.map((x) => x.id).join(', ') || 'none'})`)
212
+ const repo = await siteRepoOf(root, i.siteRepo)
213
+ if (!repo?.house || repo.path == null) return { i, repo, unit: null, unitError: null }
214
+ try {
215
+ return { i, repo, unit: await loadSiteUnit(repo.path), unitError: null }
216
+ } catch (e) {
217
+ return { i, repo, unit: null, unitError: (e as Error).message }
218
+ }
219
+ }
220
+
221
+ /** why an identity's site has no desk, in words */
222
+ function missingOf(i: Identity, repo: SiteRepo | null): string {
223
+ if (repo == null) return `no repo builds it: siteRepo: in ${i.id}'s identity.md`
224
+ if (repo.path == null) return `${repo.name} is not on this disk`
225
+ if (!repo.house) return `${repo.name} is not wired to shipkit: no site unit, so no pages, no preview and no deploy here`
226
+ return `${repo.name} declares no site unit: a site({...}) in a .shipkit.ts (build/shipkit/site.md)`
227
+ }
228
+
229
+ export async function siteAnswer(root: string, id: string): Promise<SiteAnswer> {
230
+ const { i, repo, unit, unitError } = await siteTarget(root, id)
231
+ const online = i.site === '' ? Promise.resolve(null) : probe(i.site)
232
+ let desk: SiteDesk | null = null
233
+ let error = unitError
234
+ if (unit && repo?.path) {
235
+ try {
236
+ desk = await deskOf(i, repo.path, unit)
237
+ } catch (e) {
238
+ error = (e as Error).message
239
+ }
240
+ }
241
+ return { id: i.id, name: i.name, address: i.site, online: await online, repo, missing: unit || unitError ? null : missingOf(i, repo), desk, error }
242
+ }
@@ -0,0 +1,66 @@
1
+ // the site desk's routes (wiki build/shipkit/site.md, Routes), on the home daemon: an identity's site read, its local
2
+ // preview started when down, its deploy run on his press with the log streamed. Each names an identity, never a path
3
+ // or a command: the commands are the site unit's
4
+ import { bad, NO_STORE } from '../review/http.ts'
5
+ import { answers, forgetDeployed, isDeploying, setDeploying, siteAnswer, siteTarget } from './desk.ts'
6
+
7
+ const BASE = '/api/home/sites/'
8
+
9
+ /** the preview answers, started when down: detached, so it outlives the daemon */
10
+ async function preview(root: string, id: string): Promise<Response> {
11
+ const { repo, unit } = await siteTarget(root, id)
12
+ const p = unit?.preview
13
+ if (!p || !repo?.path) return bad(`${id}'s site has no local preview: a preview in its site unit`, 404)
14
+ if (await answers(p.url)) return Response.json({ ok: true, url: p.url }, { headers: NO_STORE })
15
+ Bun.spawn(['sh', '-c', 'nohup "$@" >/dev/null 2>&1 &', 'sh', ...p.start], { cwd: repo.path, stdin: 'ignore', stdout: 'ignore', stderr: 'ignore' })
16
+ for (let n = 0; n < 40; n++) {
17
+ await Bun.sleep(500)
18
+ if (await answers(p.url)) return Response.json({ ok: true, url: p.url }, { headers: NO_STORE })
19
+ }
20
+ return bad(`the preview did not answer on ${p.url} in 20 s: run « ${p.start.join(' ')} » in ${repo.name} to see why`, 500)
21
+ }
22
+
23
+ /** the unit's deploy, only on his press, one at a time per identity, its output streamed as it comes */
24
+ async function deploy(root: string, id: string): Promise<Response> {
25
+ const { repo, unit } = await siteTarget(root, id)
26
+ const d = unit?.deploy
27
+ if (!d || !repo?.path) return bad(`${id}'s site has no deploy: a deploy in its site unit`, 404)
28
+ if (isDeploying(id)) return bad(`${id}'s site is being deployed already`, 409)
29
+ let proc: ReturnType<typeof Bun.spawn<'ignore', 'pipe', 'pipe'>>
30
+ try {
31
+ proc = Bun.spawn([...d.cmd], { cwd: repo.path, stdin: 'ignore', stdout: 'pipe', stderr: 'pipe' })
32
+ } catch (e) {
33
+ return bad(`deploy: ${d.cmd.join(' ')}: ${(e as Error).message}`, 500)
34
+ }
35
+ setDeploying(id, true)
36
+ const enc = new TextEncoder()
37
+ const stream = new ReadableStream<Uint8Array>({
38
+ async start(c) {
39
+ const pump = async (s: ReadableStream<Uint8Array>): Promise<void> => {
40
+ for await (const chunk of s) c.enqueue(chunk)
41
+ }
42
+ await Promise.all([pump(proc.stdout), pump(proc.stderr)])
43
+ const code = await proc.exited
44
+ forgetDeployed()
45
+ setDeploying(id, false)
46
+ c.enqueue(enc.encode(code === 0 ? '\n✓ deployed\n' : `\n✗ failed (exit ${code})\n`))
47
+ c.close()
48
+ },
49
+ })
50
+ return new Response(stream, { headers: { 'Content-Type': 'text/plain; charset=utf-8', ...NO_STORE } })
51
+ }
52
+
53
+ /** null when the path is not this route's */
54
+ export async function siteRoutes(req: Request, path: string, root: string): Promise<Response | null> {
55
+ if (!path.startsWith(BASE)) return null
56
+ const [id = '', verb = ''] = path.slice(BASE.length).split('/').map(decodeURIComponent)
57
+ if (!/^[a-z0-9][a-z0-9-]*$/.test(id)) return bad(`sites: ${id} is no identity id`, 404)
58
+ try {
59
+ if (verb === '' && req.method === 'GET') return Response.json(await siteAnswer(root, id), { headers: NO_STORE })
60
+ if (verb === 'preview' && req.method === 'POST') return await preview(root, id)
61
+ if (verb === 'deploy' && req.method === 'POST') return await deploy(root, id)
62
+ return bad(`sites: no route ${req.method} ${path}`, 404)
63
+ } catch (e) {
64
+ return bad(e, (e as Error).message.startsWith('no identity') ? 404 : 500)
65
+ }
66
+ }
@@ -0,0 +1,62 @@
1
+ // a repo's parts of the site desk (wiki build/shipkit/site.md, What a project adds): the command that lists its pages,
2
+ // its local preview, its deploy, where what is online is read, what a deploy ships and the page a changed file touches.
3
+ // Every unit file imports this module, so it stays light: desk.ts finds and reads the unit
4
+
5
+ /** a page of the site, as the unit's `pages` command prints it */
6
+ export interface SitePage {
7
+ path: string
8
+ title: string
9
+ draft?: boolean
10
+ /** the repo folder the page is made from, when it has one */
11
+ dir?: string | null
12
+ }
13
+
14
+ /** a dated entry the site shows (a News message): counted when it is newer than the deploy */
15
+ export interface SiteDated {
16
+ id: string
17
+ at: string
18
+ }
19
+
20
+ export interface SiteDef {
21
+ /** argv from the repo root; prints `{ pages: SitePage[], dated?: SiteDated[] }` */
22
+ pages: readonly string[]
23
+ /** the local preview: its url, and the argv that serves it (detached, so it outlives the daemon) */
24
+ preview?: { url: string; start: readonly string[] }
25
+ /** the project's own deploy: argv from the repo root, and how long it takes in words */
26
+ deploy?: { cmd: readonly string[]; about?: string }
27
+ /** what is online: a record `{ commit, at, url }` the deploy writes, else a Cloudflare Pages project */
28
+ online?: { record?: string; pages?: { account: string; project: string } }
29
+ /** what a deploy ships, repo relative: a file there that differs from the deployed commit is a change */
30
+ sources?: readonly string[]
31
+ /** what the site reads from its identity's folder: a change there since the deploy touches every page */
32
+ identityFiles?: readonly string[]
33
+ /** the words for the dated entries newer than the deploy */
34
+ dated?: string
35
+ /** the page a changed file touches: its path, `*` every page, null none. Pure: it runs in the daemon */
36
+ touches?: (file: string, ctx: { pages: readonly SitePage[]; dated: readonly SiteDated[] }) => string | null
37
+ }
38
+
39
+ export type SiteUnit = SiteDef & { kind: 'site' }
40
+
41
+ const REL = /^(?!\/)(?!.*(^|\/)\.\.(\/|$)).+$/
42
+ const argv = (v: unknown): boolean => Array.isArray(v) && v.length > 0 && v.every((a) => typeof a === 'string' && a !== '')
43
+
44
+ /** why a site unit cannot be read, or null */
45
+ export function siteProblem(d: SiteDef): string | null {
46
+ if (!argv(d.pages)) return 'site: pages is an argv that prints the pages'
47
+ if (d.preview !== undefined && (typeof d.preview.url !== 'string' || !/^https?:\/\//.test(d.preview.url) || !argv(d.preview.start))) return 'site: preview is { url: an http url, start: an argv }'
48
+ if (d.deploy !== undefined && !argv(d.deploy.cmd)) return 'site: deploy.cmd is an argv'
49
+ if (d.online?.record !== undefined && !REL.test(d.online.record)) return `site: online.record '${d.online.record}' is a repo relative file`
50
+ const pages = d.online?.pages
51
+ if (pages !== undefined && (typeof pages.account !== 'string' || pages.account === '' || typeof pages.project !== 'string' || pages.project === '')) return 'site: online.pages is { account, project }'
52
+ for (const [key, list] of [['sources', d.sources], ['identityFiles', d.identityFiles]] as const)
53
+ for (const s of list ?? []) if (typeof s !== 'string' || !REL.test(s)) return `site: ${key} '${String(s)}' is a relative path`
54
+ if (d.touches !== undefined && typeof d.touches !== 'function') return 'site: touches is a function of a file'
55
+ return null
56
+ }
57
+
58
+ export function site(d: SiteDef): SiteUnit {
59
+ const why = siteProblem(d)
60
+ if (why !== null) throw new Error(why)
61
+ return { ...d, kind: 'site' }
62
+ }
@@ -0,0 +1,22 @@
1
+ // each platform's mark and color, for the social desks and a site's links: the logos are simple-icons 13.0.0
2
+ // (CC0), 24 unit paths, the site's globe Material Icons (Apache 2.0); a brand whose own color is black takes its known
3
+ // accent (Patreon coral, Steam blue, TikTok red) so a card keeps a color
4
+
5
+ export const BRANDS: Readonly<Record<string, { hex: string; d: string }>> = {
6
+ bluesky: { hex: '#0285FF', d: 'M12 10.8c-1.087-2.114-4.046-6.053-6.798-7.995C2.566.944 1.561 1.266.902 1.565.139 1.908 0 3.08 0 3.768c0 .69.378 5.65.624 6.479.815 2.736 3.713 3.66 6.383 3.364.136-.02.275-.039.415-.056-.138.022-.276.04-.415.056-3.912.58-7.387 2.005-2.83 7.078 5.013 5.19 6.87-1.113 7.823-4.308.953 3.195 2.05 9.271 7.733 4.308 4.267-4.308 1.172-6.498-2.74-7.078a8.741 8.741 0 0 1-.415-.056c.14.017.279.036.415.056 2.67.297 5.568-.628 6.383-3.364.246-.828.624-5.79.624-6.478 0-.69-.139-1.861-.902-2.206-.659-.298-1.664-.62-4.3 1.24C16.046 4.748 13.087 8.687 12 10.8Z' },
7
+ discord: { hex: '#5865F2', d: 'M20.317 4.3698a19.7913 19.7913 0 00-4.8851-1.5152.0741.0741 0 00-.0785.0371c-.211.3753-.4447.8648-.6083 1.2495-1.8447-.2762-3.68-.2762-5.4868 0-.1636-.3933-.4058-.8742-.6177-1.2495a.077.077 0 00-.0785-.037 19.7363 19.7363 0 00-4.8852 1.515.0699.0699 0 00-.0321.0277C.5334 9.0458-.319 13.5799.0992 18.0578a.0824.0824 0 00.0312.0561c2.0528 1.5076 4.0413 2.4228 5.9929 3.0294a.0777.0777 0 00.0842-.0276c.4616-.6304.8731-1.2952 1.226-1.9942a.076.076 0 00-.0416-.1057c-.6528-.2476-1.2743-.5495-1.8722-.8923a.077.077 0 01-.0076-.1277c.1258-.0943.2517-.1923.3718-.2914a.0743.0743 0 01.0776-.0105c3.9278 1.7933 8.18 1.7933 12.0614 0a.0739.0739 0 01.0785.0095c.1202.099.246.1981.3728.2924a.077.077 0 01-.0066.1276 12.2986 12.2986 0 01-1.873.8914.0766.0766 0 00-.0407.1067c.3604.698.7719 1.3628 1.225 1.9932a.076.076 0 00.0842.0286c1.961-.6067 3.9495-1.5219 6.0023-3.0294a.077.077 0 00.0313-.0552c.5004-5.177-.8382-9.6739-3.5485-13.6604a.061.061 0 00-.0312-.0286zM8.02 15.3312c-1.1825 0-2.1569-1.0857-2.1569-2.419 0-1.3332.9555-2.4189 2.157-2.4189 1.2108 0 2.1757 1.0952 2.1568 2.419 0 1.3332-.9555 2.4189-2.1569 2.4189zm7.9748 0c-1.1825 0-2.1569-1.0857-2.1569-2.419 0-1.3332.9554-2.4189 2.1569-2.4189 1.2108 0 2.1757 1.0952 2.1568 2.419 0 1.3332-.946 2.4189-2.1568 2.4189Z' },
8
+ instagram: { hex: '#E4405F', d: 'M7.0301.084c-1.2768.0602-2.1487.264-2.911.5634-.7888.3075-1.4575.72-2.1228 1.3877-.6652.6677-1.075 1.3368-1.3802 2.127-.2954.7638-.4956 1.6365-.552 2.914-.0564 1.2775-.0689 1.6882-.0626 4.947.0062 3.2586.0206 3.6671.0825 4.9473.061 1.2765.264 2.1482.5635 2.9107.308.7889.72 1.4573 1.388 2.1228.6679.6655 1.3365 1.0743 2.1285 1.38.7632.295 1.6361.4961 2.9134.552 1.2773.056 1.6884.069 4.9462.0627 3.2578-.0062 3.668-.0207 4.9478-.0814 1.28-.0607 2.147-.2652 2.9098-.5633.7889-.3086 1.4578-.72 2.1228-1.3881.665-.6682 1.0745-1.3378 1.3795-2.1284.2957-.7632.4966-1.636.552-2.9124.056-1.2809.0692-1.6898.063-4.948-.0063-3.2583-.021-3.6668-.0817-4.9465-.0607-1.2797-.264-2.1487-.5633-2.9117-.3084-.7889-.72-1.4568-1.3876-2.1228C21.2982 1.33 20.628.9208 19.8378.6165 19.074.321 18.2017.1197 16.9244.0645 15.6471.0093 15.236-.005 11.977.0014 8.718.0076 8.31.0215 7.0301.0839m.1402 21.6932c-1.17-.0509-1.8053-.2453-2.2287-.408-.5606-.216-.96-.4771-1.3819-.895-.422-.4178-.6811-.8186-.9-1.378-.1644-.4234-.3624-1.058-.4171-2.228-.0595-1.2645-.072-1.6442-.079-4.848-.007-3.2037.0053-3.583.0607-4.848.05-1.169.2456-1.805.408-2.2282.216-.5613.4762-.96.895-1.3816.4188-.4217.8184-.6814 1.3783-.9003.423-.1651 1.0575-.3614 2.227-.4171 1.2655-.06 1.6447-.072 4.848-.079 3.2033-.007 3.5835.005 4.8495.0608 1.169.0508 1.8053.2445 2.228.408.5608.216.96.4754 1.3816.895.4217.4194.6816.8176.9005 1.3787.1653.4217.3617 1.056.4169 2.2263.0602 1.2655.0739 1.645.0796 4.848.0058 3.203-.0055 3.5834-.061 4.848-.051 1.17-.245 1.8055-.408 2.2294-.216.5604-.4763.96-.8954 1.3814-.419.4215-.8181.6811-1.3783.9-.4224.1649-1.0577.3617-2.2262.4174-1.2656.0595-1.6448.072-4.8493.079-3.2045.007-3.5825-.006-4.848-.0608M16.953 5.5864A1.44 1.44 0 1 0 18.39 4.144a1.44 1.44 0 0 0-1.437 1.4424M5.8385 12.012c.0067 3.4032 2.7706 6.1557 6.173 6.1493 3.4026-.0065 6.157-2.7701 6.1506-6.1733-.0065-3.4032-2.771-6.1565-6.174-6.1498-3.403.0067-6.156 2.771-6.1496 6.1738M8 12.0077a4 4 0 1 1 4.008 3.9921A3.9996 3.9996 0 0 1 8 12.0077' },
9
+ itch: { hex: '#FA5C5C', d: 'M3.13 1.338C2.08 1.96.02 4.328 0 4.95v1.03c0 1.303 1.22 2.45 2.325 2.45 1.33 0 2.436-1.102 2.436-2.41 0 1.308 1.07 2.41 2.4 2.41 1.328 0 2.362-1.102 2.362-2.41 0 1.308 1.137 2.41 2.466 2.41h.024c1.33 0 2.466-1.102 2.466-2.41 0 1.308 1.034 2.41 2.363 2.41 1.33 0 2.4-1.102 2.4-2.41 0 1.308 1.106 2.41 2.435 2.41C22.78 8.43 24 7.282 24 5.98V4.95c-.02-.62-2.082-2.99-3.13-3.612-3.253-.114-5.508-.134-8.87-.133-3.362 0-7.945.053-8.87.133zm6.376 6.477a2.74 2.74 0 0 1-.468.602c-.5.49-1.19.795-1.947.795a2.786 2.786 0 0 1-1.95-.795c-.182-.178-.32-.37-.446-.59-.127.222-.303.412-.486.59a2.788 2.788 0 0 1-1.95.795c-.092 0-.187-.025-.264-.052-.107 1.113-.152 2.176-.168 2.95v.005l-.006 1.167c.02 2.334-.23 7.564 1.03 8.85 1.952.454 5.545.662 9.15.663 3.605 0 7.198-.21 9.15-.664 1.26-1.284 1.01-6.514 1.03-8.848l-.006-1.167v-.004c-.016-.775-.06-1.838-.168-2.95-.077.026-.172.052-.263.052a2.788 2.788 0 0 1-1.95-.795c-.184-.178-.36-.368-.486-.59-.127.22-.265.412-.447.59a2.786 2.786 0 0 1-1.95.794c-.76 0-1.446-.303-1.948-.793a2.74 2.74 0 0 1-.468-.602 2.738 2.738 0 0 1-.463.602 2.787 2.787 0 0 1-1.95.794h-.16a2.787 2.787 0 0 1-1.95-.793 2.738 2.738 0 0 1-.464-.602zm-2.004 2.59v.002c.795.002 1.5 0 2.373.953.687-.072 1.406-.108 2.125-.107.72 0 1.438.035 2.125.107.873-.953 1.578-.95 2.372-.953.376 0 1.876 0 2.92 2.934l1.123 4.028c.832 2.995-.266 3.068-1.636 3.07-2.03-.075-3.156-1.55-3.156-3.025-1.124.184-2.436.276-3.748.277-1.312 0-2.624-.093-3.748-.277 0 1.475-1.125 2.95-3.156 3.026-1.37-.004-2.468-.077-1.636-3.072l1.122-4.027c1.045-2.934 2.545-2.934 2.92-2.934zM12 12.714c-.002.002-2.14 1.964-2.523 2.662l1.4-.056v1.22c0 .056.56.033 1.123.007.562.026 1.124.05 1.124-.008v-1.22l1.4.055C14.138 14.677 12 12.713 12 12.713z' },
10
+ mastodon: { hex: '#6364FF', d: 'M23.268 5.313c-.35-2.578-2.617-4.61-5.304-5.004C17.51.242 15.792 0 11.813 0h-.03c-3.98 0-4.835.242-5.288.309C3.882.692 1.496 2.518.917 5.127.64 6.412.61 7.837.661 9.143c.074 1.874.088 3.745.26 5.611.118 1.24.325 2.47.62 3.68.55 2.237 2.777 4.098 4.96 4.857 2.336.792 4.849.923 7.256.38.265-.061.527-.132.786-.213.585-.184 1.27-.39 1.774-.753a.057.057 0 0 0 .023-.043v-1.809a.052.052 0 0 0-.02-.041.053.053 0 0 0-.046-.01 20.282 20.282 0 0 1-4.709.545c-2.73 0-3.463-1.284-3.674-1.818a5.593 5.593 0 0 1-.319-1.433.053.053 0 0 1 .066-.054c1.517.363 3.072.546 4.632.546.376 0 .75 0 1.125-.01 1.57-.044 3.224-.124 4.768-.422.038-.008.077-.015.11-.024 2.435-.464 4.753-1.92 4.989-5.604.008-.145.03-1.52.03-1.67.002-.512.167-3.63-.024-5.545zm-3.748 9.195h-2.561V8.29c0-1.309-.55-1.976-1.67-1.976-1.23 0-1.846.79-1.846 2.35v3.403h-2.546V8.663c0-1.56-.617-2.35-1.848-2.35-1.112 0-1.668.668-1.67 1.977v6.218H4.822V8.102c0-1.31.337-2.35 1.011-3.12.696-.77 1.608-1.164 2.74-1.164 1.311 0 2.302.5 2.962 1.498l.638 1.06.638-1.06c.66-.999 1.65-1.498 2.96-1.498 1.13 0 2.043.395 2.74 1.164.675.77 1.012 1.81 1.012 3.12z' },
11
+ patreon: { hex: '#FF424D', d: 'M22.957 7.21c-.004-3.064-2.391-5.576-5.191-6.482-3.478-1.125-8.064-.962-11.384.604C2.357 3.231 1.093 7.391 1.046 11.54c-.039 3.411.302 12.396 5.369 12.46 3.765.047 4.326-4.804 6.068-7.141 1.24-1.662 2.836-2.132 4.801-2.618 3.376-.836 5.678-3.501 5.673-7.031Z' },
12
+ reddit: { hex: '#FF4500', d: 'M12 0C5.373 0 0 5.373 0 12c0 3.314 1.343 6.314 3.515 8.485l-2.286 2.286C.775 23.225 1.097 24 1.738 24H12c6.627 0 12-5.373 12-12S18.627 0 12 0Zm4.388 3.199c1.104 0 1.999.895 1.999 1.999 0 1.105-.895 2-1.999 2-.946 0-1.739-.657-1.947-1.539v.002c-1.147.162-2.032 1.15-2.032 2.341v.007c1.776.067 3.4.567 4.686 1.363.473-.363 1.064-.58 1.707-.58 1.547 0 2.802 1.254 2.802 2.802 0 1.117-.655 2.081-1.601 2.531-.088 3.256-3.637 5.876-7.997 5.876-4.361 0-7.905-2.617-7.998-5.87-.954-.447-1.614-1.415-1.614-2.538 0-1.548 1.255-2.802 2.803-2.802.645 0 1.239.218 1.712.585 1.275-.79 2.881-1.291 4.64-1.365v-.01c0-1.663 1.263-3.034 2.88-3.207.188-.911.993-1.595 1.959-1.595Zm-8.085 8.376c-.784 0-1.459.78-1.506 1.797-.047 1.016.64 1.429 1.426 1.429.786 0 1.371-.369 1.418-1.385.047-1.017-.553-1.841-1.338-1.841Zm7.406 0c-.786 0-1.385.824-1.338 1.841.047 1.017.634 1.385 1.418 1.385.785 0 1.473-.413 1.426-1.429-.046-1.017-.721-1.797-1.506-1.797Zm-3.703 4.013c-.974 0-1.907.048-2.77.135-.147.015-.241.168-.183.305.483 1.154 1.622 1.964 2.953 1.964 1.33 0 2.47-.81 2.953-1.964.057-.137-.037-.29-.184-.305-.863-.087-1.795-.135-2.769-.135Z' },
13
+ site: { hex: '#E9739A', d: 'M11.99 2C6.47 2 2 6.48 2 12s4.47 10 9.99 10C17.52 22 22 17.52 22 12S17.52 2 11.99 2zm6.93 6h-2.95c-.32-1.25-.78-2.45-1.38-3.56 1.84.63 3.37 1.91 4.33 3.56zM12 4.04c.83 1.2 1.48 2.53 1.91 3.96h-3.82c.43-1.43 1.08-2.76 1.91-3.96zM4.26 14C4.1 13.36 4 12.69 4 12s.1-1.36.26-2h3.38c-.08.66-.14 1.32-.14 2 0 .68.06 1.34.14 2H4.26zm.82 2h2.95c.32 1.25.78 2.45 1.38 3.56-1.84-.63-3.37-1.9-4.33-3.56zm2.95-8H5.08c.96-1.66 2.49-2.93 4.33-3.56C8.81 5.55 8.35 6.75 8.03 8zM12 19.96c-.83-1.2-1.48-2.53-1.91-3.96h3.82c-.43 1.43-1.08 2.76-1.91 3.96zM14.34 14H9.66c-.09-.66-.16-1.32-.16-2 0-.68.07-1.35.16-2h4.68c.09.65.16 1.32.16 2 0 .68-.07 1.34-.16 2zm.25 5.56c.6-1.11 1.06-2.31 1.38-3.56h2.95c-.96 1.65-2.49 2.93-4.33 3.56zM16.36 14c.08-.66.14-1.32.14-2 0-.68-.06-1.34-.14-2h3.38c.16.64.26 1.31.26 2s-.1 1.36-.26 2h-3.38z' },
14
+ steam: { hex: '#1A9FFF', d: 'M11.979 0C5.678 0 .511 4.86.022 11.037l6.432 2.658c.545-.371 1.203-.59 1.912-.59.063 0 .125.004.188.006l2.861-4.142V8.91c0-2.495 2.028-4.524 4.524-4.524 2.494 0 4.524 2.031 4.524 4.527s-2.03 4.525-4.524 4.525h-.105l-4.076 2.911c0 .052.004.105.004.159 0 1.875-1.515 3.396-3.39 3.396-1.635 0-3.016-1.173-3.331-2.727L.436 15.27C1.862 20.307 6.486 24 11.979 24c6.627 0 11.999-5.373 11.999-12S18.605 0 11.979 0zM7.54 18.21l-1.473-.61c.262.543.714.999 1.314 1.25 1.297.539 2.793-.076 3.332-1.375.263-.63.264-1.319.005-1.949s-.75-1.121-1.377-1.383c-.624-.26-1.29-.249-1.878-.03l1.523.63c.956.4 1.409 1.5 1.009 2.455-.397.957-1.497 1.41-2.454 1.012H7.54zm11.415-9.303c0-1.662-1.353-3.015-3.015-3.015-1.665 0-3.015 1.353-3.015 3.015 0 1.665 1.35 3.015 3.015 3.015 1.663 0 3.015-1.35 3.015-3.015zm-5.273-.005c0-1.252 1.013-2.266 2.265-2.266 1.249 0 2.266 1.014 2.266 2.266 0 1.251-1.017 2.265-2.266 2.265-1.253 0-2.265-1.014-2.265-2.265z' },
15
+ threads: { hex: '#14171A', d: 'M12.186 24h-.007c-3.581-.024-6.334-1.205-8.184-3.509C2.35 18.44 1.5 15.586 1.472 12.01v-.017c.03-3.579.879-6.43 2.525-8.482C5.845 1.205 8.6.024 12.18 0h.014c2.746.02 5.043.725 6.826 2.098 1.677 1.29 2.858 3.13 3.509 5.467l-2.04.569c-1.104-3.96-3.898-5.984-8.304-6.015-2.91.022-5.11.936-6.54 2.717C4.307 6.504 3.616 8.914 3.589 12c.027 3.086.718 5.496 2.057 7.164 1.43 1.783 3.631 2.698 6.54 2.717 2.623-.02 4.358-.631 5.8-2.045 1.647-1.613 1.618-3.593 1.09-4.798-.31-.71-.873-1.3-1.634-1.75-.192 1.352-.622 2.446-1.284 3.272-.886 1.102-2.14 1.704-3.73 1.79-1.202.065-2.361-.218-3.259-.801-1.063-.689-1.685-1.74-1.752-2.964-.065-1.19.408-2.285 1.33-3.082.88-.76 2.119-1.207 3.583-1.291a13.853 13.853 0 0 1 3.02.142c-.126-.742-.375-1.332-.75-1.757-.513-.586-1.308-.883-2.359-.89h-.029c-.844 0-1.992.232-2.721 1.32L7.734 7.847c.98-1.454 2.568-2.256 4.478-2.256h.044c3.194.02 5.097 1.975 5.287 5.388.108.046.216.094.321.142 1.49.7 2.58 1.761 3.154 3.07.797 1.82.871 4.79-1.548 7.158-1.85 1.81-4.094 2.628-7.277 2.65Zm1.003-11.69c-.242 0-.487.007-.739.021-1.836.103-2.98.946-2.916 2.143.067 1.256 1.452 1.839 2.784 1.767 1.224-.065 2.818-.543 3.086-3.71a10.5 10.5 0 0 0-2.215-.221z' },
16
+ tiktok: { hex: '#FE2C55', d: 'M12.525.02c1.31-.02 2.61-.01 3.91-.02.08 1.53.63 3.09 1.75 4.17 1.12 1.11 2.7 1.62 4.24 1.79v4.03c-1.44-.05-2.89-.35-4.2-.97-.57-.26-1.1-.59-1.62-.93-.01 2.92.01 5.84-.02 8.75-.08 1.4-.54 2.79-1.35 3.94-1.31 1.92-3.58 3.17-5.91 3.21-1.43.08-2.86-.31-4.08-1.03-2.02-1.19-3.44-3.37-3.65-5.71-.02-.5-.03-1-.01-1.49.18-1.9 1.12-3.72 2.58-4.96 1.66-1.44 3.98-2.13 6.15-1.72.02 1.48-.04 2.96-.04 4.44-.99-.32-2.15-.23-3.02.37-.63.41-1.11 1.04-1.36 1.75-.21.51-.15 1.07-.14 1.61.24 1.64 1.82 3.02 3.5 2.87 1.12-.01 2.19-.66 2.77-1.61.19-.33.4-.67.41-1.06.1-1.79.06-3.57.07-5.36.01-4.03-.01-8.05.02-12.07z' },
17
+ x: { hex: '#14171A', d: 'M18.901 1.153h3.68l-8.04 9.19L24 22.846h-7.406l-5.8-7.584-6.638 7.584H.474l8.6-9.83L0 1.154h7.594l5.243 6.932ZM17.61 20.644h2.039L6.486 3.24H4.298Z' },
18
+ youtube: { hex: '#FF0000', d: 'M23.498 6.186a3.016 3.016 0 0 0-2.122-2.136C19.505 3.545 12 3.545 12 3.545s-7.505 0-9.377.505A3.017 3.017 0 0 0 .502 6.186C0 8.07 0 12 0 12s0 3.93.502 5.814a3.016 3.016 0 0 0 2.122 2.136c1.871.505 9.376.505 9.376.505s7.505 0 9.377-.505a3.015 3.015 0 0 0 2.122-2.136C24 15.93 24 12 24 12s0-3.93-.502-5.814zM9.545 15.568V8.432L15.818 12l-6.273 3.568z' },
19
+ }
20
+
21
+ /** the platform's color, gray for one without a mark */
22
+ export const brandHex = (id: string): string => BRANDS[id]?.hex ?? '#868E96'
@@ -0,0 +1,97 @@
1
+ // a message's text on each platform (wiki build/shipkit/social.md, The tags): a filled link, the tweet's weighted
2
+ // length, a message's length and text on each account. Pure, no fs: the desk's editor counts with it too
3
+ import { richText } from './richtext.ts'
4
+
5
+ const URL_RE = /https?:\/\/\S+/g
6
+ const EMOJI = /\p{Extended_Pictographic}/u
7
+ const light = (cp: number): boolean => cp <= 4351 || (cp >= 8192 && cp <= 8205) || (cp >= 8208 && cp <= 8223) || (cp >= 8242 && cp <= 8247)
8
+
9
+ /** the length X counts: a URL is 23, an emoji 2, a CJK character 2, the rest 1 */
10
+ export function tweetLength(text: string): number {
11
+ let n = 0
12
+ const rest = text.normalize('NFC').replace(URL_RE, () => {
13
+ n += 23
14
+ return ''
15
+ })
16
+ for (const { segment } of new Intl.Segmenter().segment(rest)) {
17
+ if (EMOJI.test(segment)) n += 2
18
+ else for (const ch of segment) n += light(ch.codePointAt(0)!) ? 1 : 2
19
+ }
20
+ return n
21
+ }
22
+
23
+ /** a venue link filled; a link that is only `{url}` is the post's own address, never encoded */
24
+ export function fillLink(template: string, v: { url: string; title: string; text: string }): string {
25
+ if (template === '{url}') return v.url
26
+ return template.replace(/\{(url|title|text)\}/g, (_, k: 'url' | 'title' | 'text') => encodeURIComponent(v[k]))
27
+ }
28
+
29
+ /** the length a platform counts: X weighted, Bluesky its shown text in graphemes, the rest graphemes */
30
+ export function messageLength(platform: string, text: string): number {
31
+ if (platform === 'x') return tweetLength(text)
32
+ const shown = platform === 'bluesky' ? richText(text).text : text
33
+ return [...new Intl.Segmenter().segment(shown)].length
34
+ }
35
+
36
+ /** a hashtag as a message's `tags` holds it */
37
+ export const TAG = /^#[\p{L}\p{N}_]+$/u
38
+ /** the Saturday tag, joined where it fits */
39
+ const SATURDAY = '#ScreenshotSaturday'
40
+ /** what each platform takes of a message's tags (wiki build/shipkit/social.md, The tags); an account with no rule takes none */
41
+ const TAGS: Readonly<Record<string, 'all' | 'one' | 'vertical'>> = { x: 'all', bluesky: 'all', mastodon: 'all', threads: 'one', tiktok: 'vertical', instagram: 'vertical', youtube: 'vertical' }
42
+
43
+ /** a message as one platform posts it: its text with the tags that platform takes (within its `max`), the footer of a
44
+ * vertical; X's links go to `reply`, the first reply under the post */
45
+ export function compose(
46
+ platform: string,
47
+ m: { text: string; tags: readonly string[]; footer: string },
48
+ o: { max: number | null; saturday: boolean },
49
+ ): { text: string; reply: string } {
50
+ let text = m.text.trim()
51
+ let reply = ''
52
+ if (platform === 'x') {
53
+ const links = text.match(URL_RE) ?? []
54
+ if (links.length > 0) {
55
+ reply = links.join('\n')
56
+ text = text
57
+ .replace(URL_RE, '')
58
+ .split('\n')
59
+ .map((l) => l.replace(/[ \t]{2,}/g, ' ').replace(/[ \t]+([.,;:!?])/g, '$1').trimEnd())
60
+ .join('\n')
61
+ .replace(/\n{3,}/g, '\n\n')
62
+ .trim()
63
+ }
64
+ }
65
+ const rule = TAGS[platform]
66
+ let tags = rule == null ? [] : rule === 'one' ? m.tags.slice(0, 1) : [...m.tags]
67
+ if (o.saturday && rule === 'all' && !tags.some((t) => t.toLowerCase() === SATURDAY.toLowerCase())) tags.push(SATURDAY)
68
+ const join = (t: readonly string[]): string => [text, t.join(' ')].filter(Boolean).join('\n\n')
69
+ const footer = rule === 'vertical' && m.footer !== '' ? `\n\n${m.footer}` : ''
70
+ // a tag that would pass the max goes, the last first: the Saturday one before the message's own
71
+ while (tags.length > 0 && o.max !== null && messageLength(platform, join(tags) + footer) > o.max) tags = tags.slice(0, -1)
72
+ return { text: join(tags) + footer, reply }
73
+ }
74
+
75
+ /** a text's tags written in it, the trailing lines made only of #tags: what a draft held before its tags had a field */
76
+ export function trailingTags(text: string): { text: string; tags: string[] } {
77
+ const lines = text.trimEnd().split('\n')
78
+ const tags: string[] = []
79
+ while (lines.length > 0 && /^\s*#[\p{L}\p{N}_]+(?:\s+#[\p{L}\p{N}_]+)*\s*$/u.test(lines.at(-1)!)) tags.unshift(...lines.pop()!.trim().split(/\s+/))
80
+ return { text: lines.join('\n').trim(), tags }
81
+ }
82
+
83
+ /** a text with every `#tag` dropped (a web page, a Patreon post: tags find nobody there), and each line they leave
84
+ * empty with them; a `#` inside a word or a URL stays */
85
+ export function withoutTags(text: string): string {
86
+ return text
87
+ .split('\n')
88
+ .map((l) => {
89
+ const kept = l.replace(/(^|[ \t])#[\p{L}\p{N}_]+/gu, '$1').replace(/[ \t]{2,}/g, ' ')
90
+ return kept.trim() === '' && l.trim() !== '' ? null : kept.trimEnd()
91
+ })
92
+ .filter((l) => l != null)
93
+ .join('\n')
94
+ .replace(/\n{3,}/g, '\n\n')
95
+ .trim()
96
+ }
97
+
@@ -0,0 +1,123 @@
1
+ // the desk.json answer kept while nothing it is built from moved (wiki build/shipkit/social.md, Routes). A stamp of
2
+ // stats says it: the social folder, the identities, the repo's config and social unit, the media a draft names outside
3
+ // its folder, and the day; HEAD moves the calendar alone. The desk is built, put on the wire and zipped once a stamp,
4
+ // and a page that holds the answer gets a 304. A stamp misses a media named outside the folder that appears where none
5
+ // stood: its error line clears at the next change
6
+ import { type Dirent, readdirSync, statSync } from 'node:fs'
7
+ import { join, relative } from 'node:path'
8
+ import { LAYOUT } from '../core/layout.ts'
9
+ import { headCommit } from '../core/git-root.ts'
10
+ import { unitFilesDeclaring } from '../core/unit-files.ts'
11
+ import { identitiesDir, readIdentities, type Identities } from '../identities/identities.ts'
12
+ import { loadRepoConfig } from '../repo-config.ts'
13
+ import { tagged } from '../review/http.ts'
14
+ import { wireOf } from './desk-wire.ts'
15
+ import { loadDesk, type Desk } from './load.ts'
16
+ import { commitMoments, dayOf, postedOf, timelineOf } from './presence.ts'
17
+ import { pushLog } from './push.ts'
18
+ import { STATS_PLATFORMS } from './stats.ts'
19
+ import { loadSocialUnit, type SocialUnit } from './unit.ts'
20
+
21
+ /** the calendar's model of every identity speaking in the repo: posts dated, slots, topics, the week ahead */
22
+ function timelines(desk: Desk, today: string): Record<string, ReturnType<typeof timelineOf>> {
23
+ const out: Record<string, ReturnType<typeof timelineOf>> = {}
24
+ for (const id of new Set([desk.speaksAs, ...desk.drafts.map((d) => d.identity)].filter((x): x is string => x != null))) {
25
+ const accounts = desk.identities.find((i) => i.id === id)?.accounts ?? []
26
+ const social = { ...desk, accounts, drafts: desk.drafts.filter((d) => d.identity === id) }
27
+ out[id] = timelineOf(social, postedOf(social, commitMoments(social, desk.root)), today)
28
+ }
29
+ return out
30
+ }
31
+
32
+ /** a folder's entries as last listed, kept on its time: a name that comes or goes moves the folder's time */
33
+ const listed = new Map<string, { at: number; entries: { name: string; dir: boolean; md: boolean }[] }>()
34
+
35
+ /** what the desk reads under `dir`, into `out`: every name, the time and size of each text file (`.md`) and each folder.
36
+ * A media's bytes are never read, so a media is its name alone, and a folder whose time stands is not listed again */
37
+ function tree(dir: string, out: string[]): void {
38
+ const at = statSync(dir, { throwIfNoEntry: false })
39
+ if (at == null || !at.isDirectory()) {
40
+ out.push(`${dir}:-`)
41
+ return
42
+ }
43
+ out.push(`${dir}:${at.mtimeMs}`)
44
+ let l = listed.get(dir)
45
+ if (l?.at !== at.mtimeMs) {
46
+ let entries: Dirent[]
47
+ try {
48
+ entries = readdirSync(dir, { withFileTypes: true })
49
+ } catch {
50
+ return
51
+ }
52
+ // a link is followed: it may stand for a folder
53
+ l = { at: at.mtimeMs, entries: entries.map((e) => ({ name: e.name, dir: e.isDirectory() || (e.isSymbolicLink() && (statSync(join(dir, e.name), { throwIfNoEntry: false })?.isDirectory() ?? false)), md: e.name.endsWith('.md') })) }
54
+ listed.set(dir, l)
55
+ }
56
+ for (const e of l.entries) {
57
+ const abs = join(dir, e.name)
58
+ if (e.dir) tree(abs, out)
59
+ else out.push(e.md ? stat(abs) : abs)
60
+ }
61
+ }
62
+
63
+ const stat = (abs: string): string => {
64
+ const s = statSync(abs, { throwIfNoEntry: false })
65
+ return s == null ? `${abs}:-` : `${abs}:${s.mtimeMs}:${s.size}`
66
+ }
67
+ const stampOf = (parts: string[]): string => Bun.hash(parts.join('\n')).toString(36)
68
+
69
+ type Part<T> = { stamp: string; value: T }
70
+ type Held = {
71
+ idsDir?: string | null
72
+ config?: Part<string | undefined>
73
+ ids?: Part<Identities>
74
+ unit?: Part<SocialUnit | null>
75
+ desk?: Part<Desk> & { outside: string[] }
76
+ answer?: { stamp: string; tag: string; text: string; zip?: Uint8Array<ArrayBuffer> }
77
+ }
78
+ const held = new Map<string, Held>()
79
+
80
+ /** the answer of `root` as it stands: rebuilt only for the parts whose stamp moved */
81
+ export async function deskWire(root: string): Promise<{ stamp: string; tag: string; text: string; zip?: Uint8Array<ArrayBuffer> }> {
82
+ const h = held.get(root) ?? {}
83
+ held.set(root, h)
84
+ if (h.idsDir === undefined) h.idsDir = await identitiesDir(root)
85
+
86
+ const configStamp = stat(join(root, 'repo.config.ts'))
87
+ const config = h.config?.stamp === configStamp ? h.config : (h.config = { stamp: configStamp, value: (await loadRepoConfig(root, { fresh: true }).catch(() => null))?.speaksAs })
88
+
89
+ const idsParts: string[] = []
90
+ if (h.idsDir != null) tree(h.idsDir, idsParts)
91
+ const idsStamp = stampOf(idsParts)
92
+ const ids = h.ids?.stamp === idsStamp ? h.ids : (h.ids = { stamp: idsStamp, value: readIdentities(h.idsDir) })
93
+
94
+ const unitStamp = stampOf(unitFilesDeclaring(root, 'social').map(stat))
95
+ const unit = h.unit?.stamp === unitStamp ? h.unit : (h.unit = { stamp: unitStamp, value: await loadSocialUnit(root) })
96
+
97
+ const today = dayOf(Date.now())
98
+ const social = join(root, LAYOUT.social)
99
+ const folder: string[] = [today, configStamp, idsStamp, unitStamp]
100
+ tree(social, folder)
101
+ folder.push(stat(pushLog(root)))
102
+ for (const p of h.desk?.outside ?? []) folder.push(stat(p))
103
+ const deskStamp = stampOf(folder)
104
+ let desk = h.desk
105
+ if (desk?.stamp !== deskStamp) {
106
+ const value = loadDesk(root, ids.value, { speaksAs: config.value, unit: unit.value })
107
+ const outside = [...new Set(value.drafts.flatMap((d) => d.media.map((m) => m.path)).filter((p) => relative(social, p).startsWith('..')))]
108
+ desk = h.desk = { stamp: deskStamp, value, outside }
109
+ }
110
+
111
+ const stamp = `${deskStamp}.${headCommit(root) ?? 'no head'}`
112
+ if (h.answer?.stamp === stamp) return h.answer
113
+ const d = desk.value
114
+ const text = JSON.stringify(wireOf({ ...d, timelines: timelines(d, today), statsPlatforms: STATS_PLATFORMS, article: unit.value?.preview != null }))
115
+ h.answer = { stamp, tag: `"${Bun.hash(text).toString(36)}"`, text }
116
+ return h.answer
117
+ }
118
+
119
+ /** `GET api/social/desk.json`: 304 while the page holds the answer, gzipped when it takes it */
120
+ export async function deskAnswer(req: Request, root: string): Promise<Response> {
121
+ const a = await deskWire(root)
122
+ return tagged(req, a.tag, () => a.text, a)
123
+ }