rvlib-shipkit 2.20.0 → 2.22.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/CHANGELOG.md +35 -0
- package/package.json +1 -1
- package/src/asks/asks-queue.ts +8 -7
- package/src/assets/kits/identity-kit.ts +158 -2
- package/src/checks/SK004-gitignore-policy.check.ts +6 -3
- package/src/cli/cmd_Bump.ts +3 -3
- package/src/cli/cmd_GlobalProcesses.ts +2 -107
- package/src/cli/cmd_Home.ts +1 -1
- package/src/cli/cmd_Overview.ts +2 -1
- package/src/cli/cmd_Rec.ts +349 -0
- package/src/cli/cmd_Releases.ts +42 -3
- package/src/cli/cmd_Review.ts +22 -13
- package/src/cli/cmd_Secret.ts +10 -3
- package/src/cli/cmd_Sign.ts +24 -0
- package/src/cli/cmd_Social.ts +57 -8
- package/src/cli/cmd_Sound.ts +17 -4
- package/src/cli/cmd_Trailer.ts +67 -1
- package/src/cli/family-pages.ts +1 -0
- package/src/cli/registry.ts +6 -1
- package/src/collections/collection-types.ts +1 -1
- package/src/collections/review.ts +23 -4
- package/src/core/agent-spawn.ts +4 -17
- package/src/core/code-lines.ts +49 -0
- package/src/core/discover-checks.ts +27 -3
- package/src/core/hard-reset.ts +34 -0
- package/src/core/layout.ts +6 -0
- package/src/core/raw-access.ts +3 -13
- package/src/core/throwaway-worktree.ts +72 -0
- package/src/home/app-sign.ts +98 -0
- package/src/home/gui-app/ShipkitGui.swift +35 -0
- package/src/home/gui-app.ts +31 -11
- package/src/home/home-routes.ts +6 -0
- package/src/home/pm-switch.ts +112 -0
- package/src/home/serve-web.ts +1 -1
- package/src/identities/identities.ts +17 -6
- package/src/media-kinds.ts +5 -3
- package/src/media-store/forget.ts +54 -0
- package/src/media-store/put.ts +4 -3
- package/src/media-store/space.ts +63 -24
- package/src/messages/brief.ts +3 -3
- package/src/messages/score.ts +2 -0
- package/src/recordings/delete.ts +19 -0
- package/src/recordings/edit.ts +231 -0
- package/src/recordings/load.ts +37 -0
- package/src/recordings/marks-draw.ts +111 -0
- package/src/recordings/marks-png.ts +25 -0
- package/src/recordings/marks.ts +138 -0
- package/src/recordings/media-kind.ts +67 -0
- package/src/recordings/model.ts +170 -0
- package/src/recordings/open.ts +33 -0
- package/src/recordings/place.ts +89 -0
- package/src/recordings/presets.ts +99 -0
- package/src/recordings/recorder-app/Capture.swift +510 -0
- package/src/recordings/recorder-app/Clip.swift +107 -0
- package/src/recordings/recorder-app/Editor.swift +39 -0
- package/src/recordings/recorder-app/Tray.swift +298 -0
- package/src/recordings/recorder-app/main.swift +859 -0
- package/src/recordings/recorder-app.ts +46 -0
- package/src/recordings/recorder.shipkit.ts +11 -0
- package/src/recordings/routes.ts +140 -0
- package/src/recordings/run-recorder.ts +40 -0
- package/src/recordings/share.ts +107 -0
- package/src/recordings/take.ts +146 -0
- package/src/recordings/view.ts +40 -0
- package/src/release/gate-checkout.ts +74 -0
- package/src/release/release-scope.ts +35 -26
- package/src/release/run-bump.ts +79 -37
- package/src/release/run-release.ts +67 -53
- package/src/releases/add.ts +38 -5
- package/src/releases/before.ts +31 -0
- package/src/releases/from-card.ts +105 -20
- package/src/releases/model.ts +100 -18
- package/src/releases/pictures.ts +29 -8
- package/src/repo-config.ts +7 -3
- package/src/review/http.ts +1 -1
- package/src/review/model.ts +12 -2
- package/src/review/stage-card.ts +1 -1
- package/src/review/store.ts +12 -2
- package/src/review/trailer-desk.ts +20 -0
- package/src/review/trailer-model.ts +67 -0
- package/src/review/trailer-routes.ts +98 -1
- package/src/review/trailer-voices.ts +144 -0
- package/src/review/watchers.ts +58 -1
- package/src/secrets/manager.ts +18 -1
- package/src/serve/hub-app/benign-errors.ts +22 -0
- package/src/serve/hub-app/hash-route.ts +27 -15
- package/src/serve/hub-app/home-st.ts +23 -5
- package/src/serve/hub-app/home.html +1 -0
- package/src/serve/hub-app/hub-app.tsx +8 -1
- package/src/serve/hub-app/icons.tsx +2 -0
- package/src/serve/hub-app/palette-st.ts +1 -0
- package/src/serve/hub-app/project-list.css +7 -2
- package/src/serve/hub-app/project-list.tsx +10 -0
- package/src/serve/hub-app/projects-svc.ts +17 -7
- package/src/serve/hub-app/recordings-st.ts +641 -0
- package/src/serve/hub-app/recordings-view.tsx +504 -0
- package/src/serve/hub-app/recordings.css +82 -0
- package/src/serve/hub-app/releases/releases-page.tsx +121 -28
- package/src/serve/hub-app/releases/releases-st.ts +46 -4
- package/src/serve/hub-app/releases.css +30 -3
- package/src/serve/hub-app/repo-sections.ts +1 -1
- package/src/serve/hub-app/repo-st.ts +16 -11
- package/src/serve/hub-app/review/clip-st.ts +3 -1
- package/src/serve/hub-app/review/kit.tsx +3 -3
- package/src/serve/hub-app/review/trailer-page.tsx +32 -6
- package/src/serve/hub-app/review/trailer-st.ts +184 -8
- package/src/serve/hub-app/review/trailer-timeline.tsx +39 -4
- package/src/serve/hub-app/review/trailer-voices-st.ts +370 -0
- package/src/serve/hub-app/review/trailer-voices.tsx +574 -0
- package/src/serve/hub-app/review.css +90 -0
- package/src/serve/hub-app/social/comms-page.tsx +10 -5
- package/src/serve/hub-app/social/next-release.tsx +118 -0
- package/src/serve/hub-app/social/post-card.tsx +31 -3
- package/src/serve/hub-app/social/social-model.ts +6 -0
- package/src/serve/hub-app/social/social-st.ts +50 -1
- package/src/serve/hub-app/social.css +12 -0
- package/src/serve/hub-app/tree-build.ts +2 -0
- package/src/serve/project-gutter-route.ts +8 -0
- package/src/serve/project-gutter.ts +4 -2
- package/src/shipkit.ts +2 -2
- package/src/shot/box.ts +49 -22
- package/src/shot/browser-slot.ts +13 -3
- package/src/shot/client.ts +1 -1
- package/src/shot/film-box.ts +193 -117
- package/src/shot/shooter.ts +20 -8
- package/src/smoke/run-smoke.ts +8 -6
- package/src/social/compose.ts +14 -0
- package/src/social/desk-index.ts +49 -8
- package/src/social/discord-layout.ts +6 -2
- package/src/social/drafts.ts +1 -1
- package/src/social/kinds.ts +3 -2
- package/src/social/load.ts +42 -9
- package/src/social/publish/index.ts +27 -10
- package/src/social/publish/secret.ts +12 -4
- package/src/social/release-card.ts +69 -0
- package/src/social/release-stage.ts +173 -30
- package/src/social/routes.ts +7 -1
- package/src/social/stage.ts +45 -22
- package/src/trailer/bed.ts +111 -0
- package/src/trailer/build.ts +81 -38
- package/src/trailer/ffmpeg.ts +14 -0
- package/src/trailer/grammar.ts +32 -4
- package/src/trailer/graph.ts +12 -1
- package/src/trailer/look.ts +7 -0
- package/src/trailer/shot-plan.ts +75 -0
- package/src/trailer/shot-state.ts +191 -0
- package/src/trailer/sound/audio.ts +28 -0
- package/src/trailer/sound/brand.ts +53 -3
- package/src/trailer/sound/gen.ts +61 -2
- package/src/trailer/sound/index.ts +25 -11
- package/src/trailer/sound/presets.ts +86 -5
- package/src/trailer/sound/run.ts +1 -1
- package/src/trailer/sound/takes.ts +94 -0
- package/src/trailer/titles.ts +36 -0
- package/src/trailer/url.ts +1 -1
- package/src/veille/chrome.ts +6 -3
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
// the recorder app built (wiki tools/shipkit/recordings.md, The recorder): its own Swift source, the same swiftc build
|
|
2
|
+
// as the shipkit app, a menu bar bundle in ~/Applications that answers `shipkit-recorder://` and runs this machine's
|
|
3
|
+
// sk. Rebuilt only when the source or the sk it runs changed: a rebuilt binary may ask for Screen Recording again
|
|
4
|
+
import { join } from 'node:path'
|
|
5
|
+
import { LIB_VERSION } from '../core/lib-version.ts'
|
|
6
|
+
import { appIconOf, identityOfRoot } from '../core/app-icon.ts'
|
|
7
|
+
import { identityOfName } from '../core/identity-color.ts'
|
|
8
|
+
import { houseRoot } from '../home/home-start.ts'
|
|
9
|
+
import { ensureApp, type AppSpec } from '../home/gui-app.ts'
|
|
10
|
+
import { appsDir } from '../home/process-apps.ts'
|
|
11
|
+
import { RECORDER_SCHEME } from './open.ts'
|
|
12
|
+
|
|
13
|
+
export const RECORDER_BUNDLE_ID = 'dev.shipkit.recorder'
|
|
14
|
+
/** the app's Swift module: main.swift (the menu, the hotkeys, the takes), Capture.swift (the overlay), Tray.swift (the
|
|
15
|
+
* recent captures), Editor.swift (the editor window) */
|
|
16
|
+
export const RECORDER_SOURCES = ['main.swift', 'Capture.swift', 'Tray.swift', 'Editor.swift', 'Clip.swift'].map((f) => join(import.meta.dir, 'recorder-app', f))
|
|
17
|
+
export const recorderAppPath = (): string => process.env.SHIPKIT_RECORDER_APP ?? join(appsDir(), 'Shipkit Recorder.app')
|
|
18
|
+
|
|
19
|
+
const xml = (s: string): string => s.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>')
|
|
20
|
+
|
|
21
|
+
/** the argv that runs this sk: the bun running now and the cli source beside this file */
|
|
22
|
+
export const skArgv = (): string[] => [process.execPath, join(import.meta.dir, '..', 'cli.ts')]
|
|
23
|
+
|
|
24
|
+
/** the spec of the bundle: the shipkit app's own icon and colours (the house repo's mark), its own source and scheme */
|
|
25
|
+
export function recorderSpec(o: { app?: string; cli?: string[]; root?: string | null } = {}): AppSpec {
|
|
26
|
+
const cli = o.cli ?? skArgv()
|
|
27
|
+
const root = o.root === undefined ? houseRoot(process.cwd()) : o.root
|
|
28
|
+
return {
|
|
29
|
+
name: 'Shipkit Recorder',
|
|
30
|
+
bundleId: RECORDER_BUNDLE_ID,
|
|
31
|
+
url: null,
|
|
32
|
+
icon: root == null ? null : appIconOf(root),
|
|
33
|
+
identity: root == null ? identityOfName('shipkit') : identityOfRoot(root),
|
|
34
|
+
app: o.app ?? recorderAppPath(),
|
|
35
|
+
version: LIB_VERSION,
|
|
36
|
+
exe: 'shipkit-recorder',
|
|
37
|
+
agent: true,
|
|
38
|
+
micWhy: 'A recording takes your voice when its preset turns the mic on.',
|
|
39
|
+
plist:
|
|
40
|
+
`\t<key>CFBundleURLTypes</key><array><dict><key>CFBundleURLName</key><string>${RECORDER_BUNDLE_ID}</string><key>CFBundleURLSchemes</key><array><string>${RECORDER_SCHEME}</string></array></dict></array>\n` +
|
|
41
|
+
`\t<key>ShipkitCli</key><array>${cli.map((a) => `<string>${xml(a)}</string>`).join('')}</array>\n`,
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export const ensureRecorderApp = (o: { app?: string; cli?: string[]; bins?: string } = {}): Promise<{ app: string; rebuilt: boolean }> =>
|
|
46
|
+
ensureApp(recorderSpec(o), { source: RECORDER_SOURCES, ...(o.bins ? { bins: o.bins } : {}) })
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
// the menu bar recorder as a machine process: pm keeps it running, so its hotkeys answer from any app
|
|
2
|
+
import { join } from 'node:path'
|
|
3
|
+
import { globalProcess } from '../shipkit-files.ts'
|
|
4
|
+
|
|
5
|
+
export default globalProcess({
|
|
6
|
+
id: 'recorder',
|
|
7
|
+
label: 'recorder',
|
|
8
|
+
description: 'menu bar screen recorder: a hotkey per preset, takes filed under a repo, up in the Space (sk rec presets)',
|
|
9
|
+
cmd: ['bun', join(import.meta.dir, 'run-recorder.ts')],
|
|
10
|
+
icon: '⏺️',
|
|
11
|
+
})
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
// the hub's recordings api (wiki tools/shipkit/recordings.md, The hub page): the list, each file (the disk, else a
|
|
2
|
+
// redirect to a signed link), and the actions the page's buttons ask. Every write goes through the same functions
|
|
3
|
+
// as `sk rec`, never a path of its own
|
|
4
|
+
import { existsSync } from 'node:fs'
|
|
5
|
+
import { join } from 'node:path'
|
|
6
|
+
import { mediaKey } from '../media-store/content.ts'
|
|
7
|
+
import { isUp } from '../media-store/remote.ts'
|
|
8
|
+
import { bad, NO_STORE, ranged } from '../review/http.ts'
|
|
9
|
+
import { recorderState } from './load.ts'
|
|
10
|
+
import { currentFile, findRecording, rename, type Recording } from './model.ts'
|
|
11
|
+
import { deleteRecording } from './delete.ts'
|
|
12
|
+
import { recorderUrl } from './open.ts'
|
|
13
|
+
import type { RecPreset } from './presets.ts'
|
|
14
|
+
import { copyRecordingFile, copyText, privateLink, publicLink } from './share.ts'
|
|
15
|
+
import { editText, fileBack, saveEdit } from './take.ts'
|
|
16
|
+
import { reveal } from '../veille/chrome.ts'
|
|
17
|
+
import type { RecList, RecView } from './view.ts'
|
|
18
|
+
|
|
19
|
+
export function viewOf(r: Recording): RecView {
|
|
20
|
+
const current = currentFile(r)
|
|
21
|
+
return {
|
|
22
|
+
id: r.id,
|
|
23
|
+
name: r.name,
|
|
24
|
+
repo: r.repo,
|
|
25
|
+
still: r.still,
|
|
26
|
+
created: r.created,
|
|
27
|
+
preset: r.preset ?? null,
|
|
28
|
+
width: r.width,
|
|
29
|
+
height: r.height,
|
|
30
|
+
seconds: r.seconds,
|
|
31
|
+
audio: r.audio,
|
|
32
|
+
current,
|
|
33
|
+
raw: r.raw,
|
|
34
|
+
edit: editText(r),
|
|
35
|
+
local: existsSync(join(r.dir, current)),
|
|
36
|
+
up: r.files[current] ? isUp(mediaKey('recording', r.files[current]!, current)) : false,
|
|
37
|
+
publicUrl: r.publicUrl ?? null,
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const API = '/api/home/recordings'
|
|
42
|
+
const MEDIA = /^(raw|out)\.(mp4|png)$|^poster\.webp$/
|
|
43
|
+
|
|
44
|
+
/** null when the path is not this route's */
|
|
45
|
+
export async function recordingsRoute(req: Request, path: string): Promise<Response | null> {
|
|
46
|
+
if (!path.startsWith(API)) return null
|
|
47
|
+
try {
|
|
48
|
+
if (path === `${API}.json` && req.method === 'GET') {
|
|
49
|
+
const { listRecordings } = await import('./model.ts')
|
|
50
|
+
const s = await recorderState()
|
|
51
|
+
const list: RecList = {
|
|
52
|
+
recordings: (await listRecordings()).map(viewOf),
|
|
53
|
+
repos: s.repos,
|
|
54
|
+
repo: s.repo,
|
|
55
|
+
presets: s.presets.map((p) => ({ id: p.id, label: p.label ?? p.id, key: p.keys[0] ?? null })),
|
|
56
|
+
problems: s.problems,
|
|
57
|
+
}
|
|
58
|
+
return Response.json(list, { headers: NO_STORE })
|
|
59
|
+
}
|
|
60
|
+
const file = /^\/api\/home\/recordings\/file\/([^/]+)\/([^/]+)\/([^/]+)$/.exec(path)
|
|
61
|
+
if (file && req.method === 'GET') {
|
|
62
|
+
const name = decodeURIComponent(file[3]!)
|
|
63
|
+
if (!MEDIA.test(name)) return bad(`${name}: not a media file of a recording`, 404)
|
|
64
|
+
const r = await findRecording(`${decodeURIComponent(file[1]!)}/${decodeURIComponent(file[2]!)}`)
|
|
65
|
+
const at = join(r.dir, name)
|
|
66
|
+
if (existsSync(at)) return ranged(req, at)
|
|
67
|
+
// purged off the disk: the Space serves it, through a link signed for the hour
|
|
68
|
+
const { signedUrl } = await import('../media-store/remote.ts')
|
|
69
|
+
const f = r.files[name]
|
|
70
|
+
const url = f ? await signedUrl(mediaKey('recording', f, name), f.size) : null
|
|
71
|
+
if (!url) return bad(`${name} is on neither the disk nor the Space`, 404)
|
|
72
|
+
return new Response(null, { status: 302, headers: { Location: url, 'Cache-Control': 'private, max-age=1800' } })
|
|
73
|
+
}
|
|
74
|
+
if (req.method !== 'POST') return null
|
|
75
|
+
const b = (await req.json().catch(() => ({}))) as Record<string, unknown>
|
|
76
|
+
if (path === `${API}/start`) {
|
|
77
|
+
const spec = b.spec as RecPreset | undefined
|
|
78
|
+
const url = recorderUrl({ ...(typeof b.preset === 'string' ? { preset: b.preset } : {}), ...(spec ? { spec } : {}), ...(typeof b.repo === 'string' ? { repo: b.repo } : {}), ...(typeof b.name === 'string' ? { name: b.name } : {}), ...(typeof b.replace === 'string' ? { replace: b.replace } : {}) })
|
|
79
|
+
const p = Bun.spawnSync(['open', url], { stdout: 'ignore', stderr: 'pipe' })
|
|
80
|
+
if (p.exitCode !== 0) return bad(`the recorder did not answer: ${p.stderr.toString().trim()}; sk gp start recorder`)
|
|
81
|
+
return Response.json({ ok: true })
|
|
82
|
+
}
|
|
83
|
+
if (path === `${API}/reshoot`) {
|
|
84
|
+
// a trailer's shot shot again: the take's own preset (or the picker), as long as the shot, the cut's size
|
|
85
|
+
// for a window; the new take replaces the old one under its name
|
|
86
|
+
if (typeof b.ref !== 'string' || typeof b.repo !== 'string') return bad('reshoot wants ref and repo')
|
|
87
|
+
const ref = b.ref
|
|
88
|
+
const old = await findRecording(ref, ref.includes('/') ? {} : { repo: b.repo }).catch(() => findRecording(ref))
|
|
89
|
+
const s = await recorderState()
|
|
90
|
+
const was = s.presets.find((p) => p.id === old.preset)
|
|
91
|
+
const seconds = typeof b.seconds === 'number' && b.seconds > 0 ? Math.ceil(b.seconds) + 1 : undefined
|
|
92
|
+
const size = typeof b.size === 'string' ? b.size : undefined
|
|
93
|
+
const shoot = was && typeof was.shoot === 'object' ? { ...was.shoot, ...(size ? { size } : {}) } : (was?.shoot ?? 'pick')
|
|
94
|
+
const spec: RecPreset = { ...(was ?? {}), shoot, ...(seconds ? { seconds } : {}), after: ['upload'] }
|
|
95
|
+
delete spec.key
|
|
96
|
+
const url = recorderUrl({ spec, repo: old.repo, name: old.name, replace: `${old.repo}/${old.name}` })
|
|
97
|
+
const p = Bun.spawnSync(['open', url], { stdout: 'ignore', stderr: 'pipe' })
|
|
98
|
+
if (p.exitCode !== 0) return bad(`the recorder did not answer: ${p.stderr.toString().trim()}; sk gp start recorder`)
|
|
99
|
+
return Response.json({ said: `the recorder shoots ${old.name} again${seconds ? ` for ${seconds} s` : ''}; build the cut once it is filed` })
|
|
100
|
+
}
|
|
101
|
+
const act = /^\/api\/home\/recordings\/([^/]+)\/([^/]+)\/([a-z-]+)$/.exec(path)
|
|
102
|
+
if (!act) return bad(`no such action ${path}`, 404)
|
|
103
|
+
const r = await findRecording(`${decodeURIComponent(act[1]!)}/${decodeURIComponent(act[2]!)}`)
|
|
104
|
+
const verb = act[3]!
|
|
105
|
+
if (verb === 'link') {
|
|
106
|
+
const url = b.public === true ? await publicLink(r) : (await privateLink(r)).url
|
|
107
|
+
await copyText(url)
|
|
108
|
+
return Response.json({ url, said: b.public === true ? 'public link copied: anyone with it can watch' : 'private link copied: it plays for 7 days' })
|
|
109
|
+
}
|
|
110
|
+
if (verb === 'copy-file') {
|
|
111
|
+
await copyRecordingFile(r)
|
|
112
|
+
return Response.json({ said: 'file copied: paste it in Slack, Discord or a mail' })
|
|
113
|
+
}
|
|
114
|
+
if (verb === 'reveal') {
|
|
115
|
+
await reveal(await fileBack(r, currentFile(r)))
|
|
116
|
+
return Response.json({ said: 'shown in the Finder' })
|
|
117
|
+
}
|
|
118
|
+
if (verb === 'rename') {
|
|
119
|
+
if (typeof b.name !== 'string') return bad('rename wants a name')
|
|
120
|
+
return Response.json({ recording: viewOf(rename(r, b.name)), said: `renamed ${b.name}` })
|
|
121
|
+
}
|
|
122
|
+
if (verb === 'move') {
|
|
123
|
+
if (typeof b.repo !== 'string') return bad('move wants a repo')
|
|
124
|
+
const { moveRecording } = await import('./model.ts')
|
|
125
|
+
return Response.json({ recording: viewOf(moveRecording(r, b.repo)), said: `${r.name} filed under ${b.repo}` })
|
|
126
|
+
}
|
|
127
|
+
if (verb === 'edit') {
|
|
128
|
+
if (typeof b.text !== 'string') return bad('edit wants its text')
|
|
129
|
+
return Response.json({ recording: viewOf(await saveEdit(r, b.text)), said: b.text.trim() === '' ? 'edit removed: the take plays whole' : 'exported' })
|
|
130
|
+
}
|
|
131
|
+
if (verb === 'delete') {
|
|
132
|
+
const res = await deleteRecording(r)
|
|
133
|
+
const pub = r.publicUrl ? '; a public link may play from the CDN for up to an hour' : ''
|
|
134
|
+
return Response.json({ said: res.kept.length ? `${r.name} deleted, but ${res.kept.length} of its files stay up: another recording holds the same content${pub}` : `${r.name} deleted for good: its private links are dead${pub}` })
|
|
135
|
+
}
|
|
136
|
+
return bad(`no action ${verb}`, 404)
|
|
137
|
+
} catch (e) {
|
|
138
|
+
return bad(e)
|
|
139
|
+
}
|
|
140
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
// the recorder as pm runs it: the app built when stale, then opened and waited on, its stderr in pm's log; a stop from
|
|
2
|
+
// pm quits it
|
|
3
|
+
import { appendFileSync, existsSync, mkdirSync, openSync, readSync, statSync } from 'node:fs'
|
|
4
|
+
import { join } from 'node:path'
|
|
5
|
+
import { homeDir } from '../home/home-paths.ts'
|
|
6
|
+
import { quitCmd } from '../home/gui-app.ts'
|
|
7
|
+
import { ensureRecorderApp, RECORDER_BUNDLE_ID } from './recorder-app.ts'
|
|
8
|
+
|
|
9
|
+
if (import.meta.main) {
|
|
10
|
+
const { app, rebuilt } = await ensureRecorderApp()
|
|
11
|
+
if (rebuilt) {
|
|
12
|
+
// the running copy is the old binary: it goes, the new one opens
|
|
13
|
+
Bun.spawnSync(quitCmd(RECORDER_BUNDLE_ID))
|
|
14
|
+
console.log(`shipkit recorder: built ${app}`)
|
|
15
|
+
}
|
|
16
|
+
const quit = (): void => {
|
|
17
|
+
Bun.spawnSync(quitCmd(RECORDER_BUNDLE_ID))
|
|
18
|
+
process.exit(0)
|
|
19
|
+
}
|
|
20
|
+
process.on('SIGTERM', quit)
|
|
21
|
+
process.on('SIGINT', quit)
|
|
22
|
+
// the app's own stdio is LaunchServices', so it writes to a file this process follows
|
|
23
|
+
mkdirSync(homeDir(), { recursive: true })
|
|
24
|
+
const log = join(homeDir(), 'recorder.log')
|
|
25
|
+
if (!existsSync(log)) appendFileSync(log, '')
|
|
26
|
+
let at = statSync(log).size
|
|
27
|
+
const fd = openSync(log, 'r')
|
|
28
|
+
setInterval(() => {
|
|
29
|
+
const size = statSync(log).size
|
|
30
|
+
if (size <= at) return
|
|
31
|
+
const buf = Buffer.alloc(size - at)
|
|
32
|
+
readSync(fd, buf, 0, buf.length, at)
|
|
33
|
+
at = size
|
|
34
|
+
process.stdout.write(buf)
|
|
35
|
+
}, 500)
|
|
36
|
+
const p = Bun.spawn(['open', '-W', '--stdout', log, '--stderr', log, '-a', app], { stdout: 'inherit', stderr: 'inherit' })
|
|
37
|
+
const code = await p.exited
|
|
38
|
+
console.error(`shipkit recorder: the app quit (exit ${code})`)
|
|
39
|
+
process.exit(code)
|
|
40
|
+
}
|
|
@@ -0,0 +1,107 @@
|
|
|
1
|
+
// a recording shared (wiki tools/shipkit/recordings.md, Share): sent up now, a private link (7 days at most), a
|
|
2
|
+
// public one (permanent, published under `public/recording-share/`), the file itself on the clipboard, markdown; and
|
|
3
|
+
// the `after` steps of a preset, run in order. Every link goes through the media store's own verbs
|
|
4
|
+
import { join } from 'node:path'
|
|
5
|
+
import { mediaKey } from '../media-store/content.ts'
|
|
6
|
+
import { linkMedia, putMedia } from '../media-store/put.ts'
|
|
7
|
+
import { machineSpace } from '../media-store/space.ts'
|
|
8
|
+
import { syncKind } from '../media-store/sync.ts'
|
|
9
|
+
import { uploadWaiting } from '../media-store/upload.ts'
|
|
10
|
+
import { copyFile, reveal } from '../veille/chrome.ts'
|
|
11
|
+
import { recordingKindAt, RECORDING_KIND } from './media-kind.ts'
|
|
12
|
+
import { currentFile, readMeta, retireKey, writeMeta, type Recording } from './model.ts'
|
|
13
|
+
import type { RecAfter } from './presets.ts'
|
|
14
|
+
import { fileBack } from './take.ts'
|
|
15
|
+
|
|
16
|
+
export const SHARE_LABEL = 'recording-share'
|
|
17
|
+
|
|
18
|
+
/** the Space key a public URL names: from `public/` on */
|
|
19
|
+
export const publicKeyOf = (url: string): string => url.slice(url.indexOf('/public/') + 1)
|
|
20
|
+
const WEEK = 7 * 86_400
|
|
21
|
+
|
|
22
|
+
/** the name a download saves under: the recording's name and the file's extension */
|
|
23
|
+
const downloadName = (r: Recording, file: string): string => `${r.name}${file.slice(file.lastIndexOf('.'))}`
|
|
24
|
+
|
|
25
|
+
/** every file of the recording up in the Space now, checked by a HEAD each */
|
|
26
|
+
export async function upNow(r: Recording): Promise<void> {
|
|
27
|
+
const store = join(r.root, r.repo)
|
|
28
|
+
await syncKind(recordingKindAt(r.root), store, { fill: Infinity })
|
|
29
|
+
const { space, why } = await machineSpace()
|
|
30
|
+
if (!space) throw new Error(`not up: ${why}`)
|
|
31
|
+
const res = await uploadWaiting(space, { store })
|
|
32
|
+
if (res.failed.length > 0) throw new Error(`${res.failed.length} files did not go up: ${res.failed[0]!.key}: ${res.failed[0]!.error}`)
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/** a private link to what plays, signed for `seconds` (7 days at most) */
|
|
36
|
+
export async function privateLink(r: Recording, seconds = WEEK): Promise<{ url: string; expires: string }> {
|
|
37
|
+
const file = currentFile(r)
|
|
38
|
+
const f = r.files[file]
|
|
39
|
+
if (!f) throw new Error(`${r.repo}/${r.name}: no fact for ${file}`)
|
|
40
|
+
await upNow(r)
|
|
41
|
+
const l = await linkMedia(mediaKey(RECORDING_KIND, f, file), { seconds, name: downloadName(r, file), inline: true })
|
|
42
|
+
return { url: l.url, expires: l.expires }
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/** a permanent URL anyone opens: what plays, published; kept in the meta so the hub shows it */
|
|
46
|
+
export async function publicLink(r: Recording): Promise<string> {
|
|
47
|
+
const file = await fileBack(r, currentFile(r))
|
|
48
|
+
const p = await putMedia(file, { kind: SHARE_LABEL, public: true })
|
|
49
|
+
const m = readMeta(r.dir)
|
|
50
|
+
if (m && p.url && m.publicUrl !== p.url) {
|
|
51
|
+
// the copy published before stays public until a delete takes it: kept in former
|
|
52
|
+
if (m.publicUrl) retireKey(m, publicKeyOf(m.publicUrl))
|
|
53
|
+
writeMeta(r.dir, { ...m, publicUrl: p.url })
|
|
54
|
+
}
|
|
55
|
+
return p.url!
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export async function copyText(text: string): Promise<void> {
|
|
59
|
+
const p = Bun.spawn(['pbcopy'], { stdin: 'pipe', stdout: 'ignore', stderr: 'pipe' })
|
|
60
|
+
p.stdin.write(text)
|
|
61
|
+
await p.stdin.end()
|
|
62
|
+
if ((await p.exited) !== 0) throw new Error(`pbcopy: ${(await new Response(p.stderr).text()).trim()}`)
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** the file itself on the clipboard: a paste in Slack, Discord or a mail attaches it */
|
|
66
|
+
export async function copyRecordingFile(r: Recording): Promise<string> {
|
|
67
|
+
const file = await fileBack(r, currentFile(r))
|
|
68
|
+
await copyFile(file, { image: file.endsWith('.png') })
|
|
69
|
+
return file
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export type AfterResult = { step: RecAfter; said: string }
|
|
73
|
+
|
|
74
|
+
/** a preset's `after` steps in order; a step that fails stops the rest, said with its reason */
|
|
75
|
+
export async function runAfter(r: Recording, steps: readonly RecAfter[], o: { openEditor?: (r: Recording) => Promise<void> } = {}): Promise<AfterResult[]> {
|
|
76
|
+
const out: AfterResult[] = []
|
|
77
|
+
for (const step of steps) {
|
|
78
|
+
let said: string
|
|
79
|
+
if (step === 'upload') {
|
|
80
|
+
await upNow(r)
|
|
81
|
+
said = 'up in the Space'
|
|
82
|
+
} else if (step === 'copy-link') {
|
|
83
|
+
const l = await privateLink(r)
|
|
84
|
+
await copyText(l.url)
|
|
85
|
+
said = `private link copied (until ${l.expires.slice(0, 10)})`
|
|
86
|
+
} else if (step === 'copy-public-link') {
|
|
87
|
+
await copyText(await publicLink(r))
|
|
88
|
+
said = 'public link copied: anyone with it can watch'
|
|
89
|
+
} else if (step === 'copy-markdown') {
|
|
90
|
+
const l = await privateLink(r)
|
|
91
|
+
await copyText(r.still ? `` : `[${r.name}](${l.url})`)
|
|
92
|
+
said = 'markdown copied'
|
|
93
|
+
} else if (step === 'copy-file') {
|
|
94
|
+
await copyRecordingFile(r)
|
|
95
|
+
said = 'file copied: paste it anywhere'
|
|
96
|
+
} else if (step === 'reveal') {
|
|
97
|
+
await reveal(await fileBack(r, currentFile(r)))
|
|
98
|
+
said = 'shown in the Finder'
|
|
99
|
+
} else {
|
|
100
|
+
if (!o.openEditor) throw new Error('open-editor: no hub to open here')
|
|
101
|
+
await o.openEditor(r)
|
|
102
|
+
said = 'opened in the editor'
|
|
103
|
+
}
|
|
104
|
+
out.push({ step, said })
|
|
105
|
+
}
|
|
106
|
+
return out
|
|
107
|
+
}
|
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
// a take filed and an edit exported (wiki tools/shipkit/recordings.md, A recording): the file the recorder wrote
|
|
2
|
+
// becomes `raw.mp4` (remuxed for the web, transcoded only when it must) or `raw.png`, with its poster and its facts;
|
|
3
|
+
// an export renders `edit.txt` into `out.<ext>` in one ffmpeg pass. The take itself is never written again
|
|
4
|
+
import { copyFileSync, existsSync, mkdirSync, readFileSync, renameSync, rmSync, statSync } from 'node:fs'
|
|
5
|
+
import { basename, extname, join } from 'node:path'
|
|
6
|
+
import { factOf, mediaKey } from '../media-store/content.ts'
|
|
7
|
+
import { fetchBack } from '../media-store/remote.ts'
|
|
8
|
+
import { ffmpeg, hasAudio, imageSize, probeMedia } from '../trailer/ffmpeg.ts'
|
|
9
|
+
import { EMPTY_EDIT, editedLength, exportArgs, isEmpty, parseEdit, printEdit, stillArgs, type Edit } from './edit.ts'
|
|
10
|
+
import { markPngs } from './marks-png.ts'
|
|
11
|
+
import { RECORDING_KIND } from './media-kind.ts'
|
|
12
|
+
import { checkName, EDIT, idOf, outOf, POSTER, readMeta, retire, writeMeta, type RecMeta, type Recording } from './model.ts'
|
|
13
|
+
import { ensureDir, rememberRoot, repoDir, writeRoot } from './place.ts'
|
|
14
|
+
|
|
15
|
+
const STILL = new Set(['.png', '.jpg', '.jpeg', '.heic', '.tiff', '.webp'])
|
|
16
|
+
const VIDEO = new Set(['.mp4', '.mov', '.m4v', '.webm', '.mkv'])
|
|
17
|
+
|
|
18
|
+
/** the hardware encoder first (fast on a long take), then x264 when it refuses */
|
|
19
|
+
async function encode(args: (codec: string[]) => string[]): Promise<void> {
|
|
20
|
+
try {
|
|
21
|
+
await ffmpeg(args(['-c:v', 'h264_videotoolbox', '-q:v', '65', '-allow_sw', '1']))
|
|
22
|
+
} catch {
|
|
23
|
+
await ffmpeg(args(['-c:v', 'libx264', '-preset', 'veryfast', '-crf', '20']))
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** a frame of the file as a small webp, the grid's picture */
|
|
28
|
+
async function poster(src: string, seconds: number, out: string): Promise<void> {
|
|
29
|
+
const at = seconds > 0 ? Math.min(0.5, seconds / 2) : 0
|
|
30
|
+
await ffmpeg([...(seconds > 0 ? ['-ss', String(at)] : []), '-i', src, '-frames:v', '1', '-vf', `scale='min(640,iw)':-2`, '-c:v', 'libwebp', '-quality', '80', out])
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export type Filed = { rec: Recording; warn: string | null }
|
|
34
|
+
|
|
35
|
+
/** the recorder's file filed as a recording of the repo: moved (or copied with `copy`), its poster and facts made */
|
|
36
|
+
export async function importTake(file: string, o: { repo: string; name?: string; preset?: string; copy?: boolean; now?: Date }): Promise<Filed> {
|
|
37
|
+
const ext = extname(file).toLowerCase()
|
|
38
|
+
const still = STILL.has(ext)
|
|
39
|
+
if (!still && !VIDEO.has(ext)) throw new Error(`${file}: a recording is a video (${[...VIDEO].join(' ')}) or an image (${[...STILL].join(' ')})`)
|
|
40
|
+
if (!statSync(file, { throwIfNoEntry: false })?.isFile()) throw new Error(`${file}: no such file`)
|
|
41
|
+
const repo = basename(o.repo)
|
|
42
|
+
const { root, warn } = await writeRoot()
|
|
43
|
+
const folder = ensureDir(repoDir(root, repo))
|
|
44
|
+
if (o.name !== undefined) checkName(folder, o.name)
|
|
45
|
+
const at = o.now ?? new Date()
|
|
46
|
+
let id = idOf(at)
|
|
47
|
+
for (let n = 2; existsSync(join(folder, id)); n++) id = `${idOf(at)}-${n}`
|
|
48
|
+
const dir = join(folder, id)
|
|
49
|
+
mkdirSync(dir)
|
|
50
|
+
rememberRoot(root)
|
|
51
|
+
try {
|
|
52
|
+
const raw = still ? 'raw.png' : 'raw.mp4'
|
|
53
|
+
const dst = join(dir, raw)
|
|
54
|
+
if (still) {
|
|
55
|
+
if (ext === '.png') copyFileSync(file, dst)
|
|
56
|
+
else await ffmpeg(['-i', file, '-frames:v', '1', dst])
|
|
57
|
+
} else {
|
|
58
|
+
// the recorder's codec kept: only the box moves, the index up front so a link plays at once
|
|
59
|
+
try {
|
|
60
|
+
await ffmpeg(['-i', file, '-map', '0:v:0', '-map', '0:a?', '-c', 'copy', '-movflags', '+faststart', dst])
|
|
61
|
+
} catch {
|
|
62
|
+
await encode((codec) => ['-i', file, '-map', '0:v:0', '-map', '0:a?', ...codec, '-c:a', 'aac', '-pix_fmt', 'yuv420p', '-movflags', '+faststart', dst])
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
const p = still ? { duration: 0, video: imageSize(dst) } : probeMedia(dst)
|
|
66
|
+
if (!p.video) throw new Error(`${file}: no picture in it`)
|
|
67
|
+
const seconds = p.duration
|
|
68
|
+
await poster(dst, seconds, join(dir, POSTER))
|
|
69
|
+
const m: RecMeta = {
|
|
70
|
+
id,
|
|
71
|
+
name: o.name ?? id,
|
|
72
|
+
repo,
|
|
73
|
+
still,
|
|
74
|
+
created: at.toISOString(),
|
|
75
|
+
...(o.preset ? { preset: o.preset } : {}),
|
|
76
|
+
width: p.video.width,
|
|
77
|
+
height: p.video.height,
|
|
78
|
+
seconds,
|
|
79
|
+
audio: !still && hasAudio(dst),
|
|
80
|
+
raw,
|
|
81
|
+
files: { [raw]: await factOf(dst), [POSTER]: await factOf(join(dir, POSTER)) },
|
|
82
|
+
}
|
|
83
|
+
writeMeta(dir, m)
|
|
84
|
+
if (!o.copy) rmSync(file, { force: true })
|
|
85
|
+
return { rec: { ...m, dir, root }, warn }
|
|
86
|
+
} catch (e) {
|
|
87
|
+
rmSync(dir, { recursive: true, force: true })
|
|
88
|
+
throw e
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/** a media file of the recording on the disk, fetched back from the annex or the Space when the purge took it */
|
|
93
|
+
export async function fileBack(r: Recording, name: string): Promise<string> {
|
|
94
|
+
const path = join(r.dir, name)
|
|
95
|
+
if (existsSync(path)) return path
|
|
96
|
+
const f = r.files[name]
|
|
97
|
+
if (!f) throw new Error(`${r.repo}/${r.name}: no ${name}`)
|
|
98
|
+
const back = await fetchBack(path, mediaKey(RECORDING_KIND, f, name), f, { store: join(r.root, r.repo), item: r.id, name })
|
|
99
|
+
if (back == null) throw new Error(`${r.repo}/${r.name}: ${name} is on neither the disk, an annex, nor the Space`)
|
|
100
|
+
return back
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/** the edit's text, empty when the take has none */
|
|
104
|
+
export const editText = (r: Recording): string => (existsSync(join(r.dir, EDIT)) ? readFileSync(join(r.dir, EDIT), 'utf8') : '')
|
|
105
|
+
|
|
106
|
+
export const readEdit = (r: Recording): Edit => (existsSync(join(r.dir, EDIT)) ? parseEdit(editText(r)) : EMPTY_EDIT)
|
|
107
|
+
|
|
108
|
+
/** the edit saved and exported: `out.<ext>` and its poster made again; an empty edit removes both, the take plays */
|
|
109
|
+
export async function saveEdit(r: Recording, text: string): Promise<Recording> {
|
|
110
|
+
const e = parseEdit(text)
|
|
111
|
+
const m = readMeta(r.dir)
|
|
112
|
+
if (!m) throw new Error(`${r.dir}: no meta.json`)
|
|
113
|
+
const out = outOf(m)
|
|
114
|
+
if (isEmpty(e)) {
|
|
115
|
+
rmSync(join(r.dir, EDIT), { force: true })
|
|
116
|
+
rmSync(join(r.dir, out), { force: true })
|
|
117
|
+
retire(m, out)
|
|
118
|
+
retire(m, POSTER)
|
|
119
|
+
delete m.files[out]
|
|
120
|
+
await poster(await fileBack(r, m.raw), m.seconds, join(r.dir, POSTER))
|
|
121
|
+
m.files[POSTER] = await factOf(join(r.dir, POSTER))
|
|
122
|
+
writeMeta(r.dir, m)
|
|
123
|
+
return { ...m, dir: r.dir, root: r.root }
|
|
124
|
+
}
|
|
125
|
+
const src = await fileBack(r, m.raw)
|
|
126
|
+
const tmp = join(r.dir, `.${out}`)
|
|
127
|
+
const marks = await markPngs(e, m, r.dir)
|
|
128
|
+
try {
|
|
129
|
+
if (m.still) await ffmpeg(stillArgs(e, { file: src, width: m.width, height: m.height }, tmp, marks))
|
|
130
|
+
else {
|
|
131
|
+
editedLength(e, m.seconds)
|
|
132
|
+
await encode((codec) => exportArgs(e, { file: src, seconds: m.seconds, width: m.width, height: m.height, audio: m.audio }, tmp, [...codec, '-f', 'mp4'], marks))
|
|
133
|
+
}
|
|
134
|
+
} finally {
|
|
135
|
+
for (const f of marks) rmSync(f, { force: true })
|
|
136
|
+
}
|
|
137
|
+
renameSync(tmp, join(r.dir, out))
|
|
138
|
+
await Bun.write(join(r.dir, EDIT), printEdit(e))
|
|
139
|
+
retire(m, out)
|
|
140
|
+
retire(m, POSTER)
|
|
141
|
+
m.files[out] = await factOf(join(r.dir, out))
|
|
142
|
+
await poster(join(r.dir, out), m.still ? 0 : editedLength(e, m.seconds), join(r.dir, POSTER))
|
|
143
|
+
m.files[POSTER] = await factOf(join(r.dir, POSTER))
|
|
144
|
+
writeMeta(r.dir, m)
|
|
145
|
+
return { ...m, dir: r.dir, root: r.root }
|
|
146
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
// what the hub's Recordings page reads of one recording: plain data, no node module, so the page imports it
|
|
2
|
+
export type RecView = {
|
|
3
|
+
id: string
|
|
4
|
+
name: string
|
|
5
|
+
repo: string
|
|
6
|
+
still: boolean
|
|
7
|
+
/** ISO time of the take */
|
|
8
|
+
created: string
|
|
9
|
+
preset: string | null
|
|
10
|
+
width: number
|
|
11
|
+
height: number
|
|
12
|
+
seconds: number
|
|
13
|
+
audio: boolean
|
|
14
|
+
/** the file that plays: the export of the edit, else the take */
|
|
15
|
+
current: string
|
|
16
|
+
/** the take's own file */
|
|
17
|
+
raw: string
|
|
18
|
+
/** the edit as text, '' when none */
|
|
19
|
+
edit: string
|
|
20
|
+
/** true while the disk holds what plays; false: the page plays it from the Space */
|
|
21
|
+
local: boolean
|
|
22
|
+
/** what plays is up in the Space (its links work); false while it uploads */
|
|
23
|
+
up: boolean
|
|
24
|
+
publicUrl: string | null
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export type RecList = { recordings: RecView[]; repos: string[]; repo: string; presets: { id: string; label: string; key: string | null }[]; problems: string[] }
|
|
28
|
+
|
|
29
|
+
/** where the page reads a file of a recording: the disk, else a signed link (a redirect) */
|
|
30
|
+
export const fileUrl = (r: Pick<RecView, 'repo' | 'id'>, name: string, stamp = ''): string =>
|
|
31
|
+
`/api/home/recordings/file/${encodeURIComponent(r.repo)}/${encodeURIComponent(r.id)}/${encodeURIComponent(name)}${stamp ? `?v=${encodeURIComponent(stamp)}` : ''}`
|
|
32
|
+
|
|
33
|
+
/** the page's address of one recording, in the editor */
|
|
34
|
+
export const recordingAddress = (r: Pick<RecView, 'repo' | 'id'> | null): string => (r == null ? '/recordings' : `/recordings#${encodeURIComponent(r.repo)}/${encodeURIComponent(r.id)}`)
|
|
35
|
+
|
|
36
|
+
/** the recording an address's hash names */
|
|
37
|
+
export function parseRecordingHash(hash: string): { repo: string; id: string } | null {
|
|
38
|
+
const m = /^#([^/]+)\/([^/]+)$/.exec(hash)
|
|
39
|
+
return m ? { repo: decodeURIComponent(m[1]!), id: decodeURIComponent(m[2]!) } : null
|
|
40
|
+
}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
// the release gate's checkout: the commit that will be tagged, alone, in a
|
|
2
|
+
// linked worktree outside the repo. Several sessions share one working tree
|
|
3
|
+
// and their uncommitted files never reach a tag, so the gate that authorises
|
|
4
|
+
// the tag must not read them either (wiki tools/shipkit/units.md, The gate
|
|
5
|
+
// judges HEAD).
|
|
6
|
+
//
|
|
7
|
+
// kept between releases and brought back to HEAD by throwawayCheckout, the
|
|
8
|
+
// one helper allowed to wipe a tree, so a gate writes only what changed since
|
|
9
|
+
// the last one. node_modules are links to the repo's own entries, never an
|
|
10
|
+
// install: a workspace package links to the checkout's copy, so the gate runs
|
|
11
|
+
// HEAD's code of it, and everything else is the repo's install as it stands.
|
|
12
|
+
import { existsSync, lstatSync, mkdirSync, readdirSync, readFileSync, readlinkSync, rmSync, symlinkSync, writeFileSync } from 'fs'
|
|
13
|
+
import { isAbsolute, join, relative, resolve, sep } from 'path'
|
|
14
|
+
import { throwawayCheckout, throwawayDir } from '../core/throwaway-worktree.ts'
|
|
15
|
+
import { git } from './git-facts.ts'
|
|
16
|
+
|
|
17
|
+
/** where the checkout of `root` lives: one per checkout of a repo, a worktree of it included */
|
|
18
|
+
export const gateCheckoutDir = (root: string): string => throwawayDir(root, 'release-gate')
|
|
19
|
+
|
|
20
|
+
/** every node_modules folder of the repo, from git's own walk of what it ignores (it never descends into one) */
|
|
21
|
+
function moduleDirs(repo: string): string[] {
|
|
22
|
+
const r = git(repo, ['ls-files', '--others', '--ignored', '--exclude-standard', '--directory'])
|
|
23
|
+
if (r.code !== 0) return []
|
|
24
|
+
return r.out.split('\n').map((l) => l.trim().replace(/\/$/, '')).filter((l) => l === 'node_modules' || l.endsWith('/node_modules'))
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** the checkout's node_modules: a link per entry of the repo's, a scope folder walked, a link that points at a
|
|
28
|
+
* workspace package of the repo pointed at the checkout's own copy of it */
|
|
29
|
+
export function linkModules(repo: string, dir: string, nms: string[] = moduleDirs(repo)): void {
|
|
30
|
+
for (const nm of nms) {
|
|
31
|
+
const from = join(repo, nm)
|
|
32
|
+
if (!existsSync(from) || !existsSync(join(dir, nm, '..'))) continue
|
|
33
|
+
mkdirSync(join(dir, nm), { recursive: true })
|
|
34
|
+
for (const name of readdirSync(from)) {
|
|
35
|
+
const entry = join(from, name)
|
|
36
|
+
const st = lstatSync(entry)
|
|
37
|
+
if (name.startsWith('@') && st.isDirectory()) {
|
|
38
|
+
linkModules(repo, dir, [join(nm, name)])
|
|
39
|
+
continue
|
|
40
|
+
}
|
|
41
|
+
const inRepo = st.isSymbolicLink() ? relative(repo, resolve(from, readlinkSync(entry))) : ''
|
|
42
|
+
const workspace = inRepo !== '' && !inRepo.startsWith('..') && !isAbsolute(inRepo) && !inRepo.split(sep).includes('node_modules')
|
|
43
|
+
symlinkSync(workspace ? join(dir, inRepo) : entry, join(dir, nm, name))
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const alive = (pid: number): boolean => {
|
|
49
|
+
try { return process.kill(pid, 0) } catch { return false }
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/** run `fn` in a clean checkout of `root`'s HEAD. One gate per checkout at a time: a second release waits for no
|
|
53
|
+
* one, it refuses, and a lock whose process died is taken over */
|
|
54
|
+
export async function withHeadCheckout<T>(root: string, fn: (dir: string, sha: string) => Promise<T>): Promise<T | { error: string }> {
|
|
55
|
+
const sha = git(root, ['rev-parse', 'HEAD'])
|
|
56
|
+
if (sha.code !== 0) return { error: `no HEAD to gate (${sha.out})` }
|
|
57
|
+
const dir = gateCheckoutDir(root)
|
|
58
|
+
const lock = `${dir}.lock`
|
|
59
|
+
try {
|
|
60
|
+
writeFileSync(lock, String(process.pid), { flag: 'wx' })
|
|
61
|
+
} catch {
|
|
62
|
+
const holder = Number(readFileSync(lock, 'utf8'))
|
|
63
|
+
if (holder !== process.pid && alive(holder)) return { error: `another release gate of this repo runs (pid ${holder}, ${lock})` }
|
|
64
|
+
writeFileSync(lock, String(process.pid))
|
|
65
|
+
}
|
|
66
|
+
try {
|
|
67
|
+
const co = throwawayCheckout(root, 'release-gate', sha.out)
|
|
68
|
+
if ('error' in co) return { error: `the gate's checkout: ${co.error}` }
|
|
69
|
+
linkModules(root, dir)
|
|
70
|
+
return await fn(dir, sha.out)
|
|
71
|
+
} finally {
|
|
72
|
+
rmSync(lock, { force: true })
|
|
73
|
+
}
|
|
74
|
+
}
|