Files
music/scripts/reindex-music.ts
T
Claude Opus 5 6e07da7a36 music, extracted from the platform into its own repository
Everything the plugin is, moved out of officerdev/platform on 2026-08-15 —
41 files, unchanged from the tree they left.

  manifest.ts   identity, one permission, ffmpeg/ffprobe declared
  api/          the sidecar proxy; the prefix comes from mountPrefix()
  sidecar/      the whole /api/music contract — indexing, streaming, per-user state
  db/           music_favorites, _playlists, _playlist_items, _now_playing
  web/          panels, layout, and the player: engine, bar, lyrics, favourites
  cliamp/       the second playback path, parked — not working, kept deliberately
  widgets/      the dashboard widget, parked — plugins cannot contribute widgets
  assets/       icon.png, the dock tile
  scripts/      the reindex CLI

PLUGIN.md is the design record: what moved, what stayed, what broke, and why.
MUSIC_API.md is the contract the phone and tablet apps speak, and the reason
the sidecar's HTTP shape is not free to change.

── It does not build here, and that is the point ──

The platform resolves `hooks/useClient`, `officerdev`, `officerdb/db` and `@@/*`
through the workspace links in its own node_modules. Measured from this
directory, outside the platform checkout, every one of them fails to resolve —
7 imports in the backend, ~29 in the frontend.

So this repository is the source of truth, not yet a buildable unit. Making it
one means the host API becoming something a plugin can depend on rather than
something it reaches into. That is the next problem, and having the code here
is what makes it unavoidable rather than theoretical.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-15 17:34:51 +00:00

121 lines
3.9 KiB
TypeScript

#!/usr/bin/env bun
/**
* Trigger the music library index on the officer-music sidecar and follow its progress live,
* ending with a summary report. Run from the platform repo: bun scripts/reindex-music.ts
*
* It reads the sidecar's port from DATA_PATH/music/.server (written by the sidecar on startup) and
* consumes its /reindex/stream SSE endpoint — the same stream the app subscribes to.
*/
import { readFileSync } from 'node:fs';
import { join } from 'node:path';
const DATA_PATH = process.env.DATA_PATH ?? join(process.cwd(), 'data');
const PORT_FILE = join(DATA_PATH, 'music', '.server');
type Progress = {
running: boolean;
foldersScanned: number;
albumsBuilt: number;
albumsSkipped: number;
tracksIndexed: number;
coversSaved: number;
currentPath: string;
};
type Report = {
albums: number;
built: number;
skipped: number;
foldersScanned: number;
tracksIndexed: number;
coversSaved: number;
discographies: number;
elapsedSec: number;
error: string | null;
};
function readPort(): number {
try {
const p = parseInt(readFileSync(PORT_FILE, 'utf8').trim(), 10);
if (Number.isInteger(p) && p > 0) return p;
} catch {
/* fall through */
}
console.error(`✗ Could not read the sidecar port from ${PORT_FILE}.`);
console.error(' Is officer-music running? pm2 restart officer-music');
process.exit(1);
}
const isTTY = Boolean(process.stdout.isTTY);
const cols = () => (process.stdout.columns && process.stdout.columns > 0 ? process.stdout.columns : 100);
function printProgress(p: Progress): void {
const line =
`♪ indexing… folders ${p.foldersScanned} · tracks ${p.tracksIndexed} · ` +
`built ${p.albumsBuilt} · skipped ${p.albumsSkipped} · covers ${p.coversSaved}` +
(p.currentPath ? ` · ${p.currentPath}` : '');
if (isTTY) {
const clipped = line.length > cols() - 1 ? line.slice(0, cols() - 2) + '…' : line;
process.stdout.write('\r\x1b[2K' + clipped);
} else {
process.stdout.write(line + '\n');
}
}
function printReport(r: Report): void {
if (isTTY) process.stdout.write('\r\x1b[2K');
const l = (k: string, v: string | number) => console.log(` ${k.padEnd(9)} ${v}`);
console.log('\n─── Music index complete ───');
l('Albums:', `${r.albums} (${r.built} built, ${r.skipped} unchanged)`);
l('Tracks:', `${r.tracksIndexed} indexed`);
l('Covers:', `${r.coversSaved} compressed`);
l('Discogs:', `${r.discographies} artist${r.discographies === 1 ? '' : 's'}`);
l('Folders:', `${r.foldersScanned} scanned`);
l('Elapsed:', `${r.elapsedSec}s`);
if (r.error) l('Error:', r.error);
console.log('────────────────────────────\n');
}
async function main(): Promise<void> {
const port = readPort();
const url = `http://127.0.0.1:${port}/reindex/stream`;
console.log(`Triggering music index via ${url}\n`);
let res: Response;
try {
res = await fetch(url);
} catch (err) {
console.error(`✗ Could not reach the sidecar at 127.0.0.1:${port}: ${String(err)}`);
process.exit(1);
}
if (!res.ok || !res.body) {
console.error(`✗ Sidecar returned ${res.status}`);
process.exit(1);
}
const reader = res.body.getReader();
const decoder = new TextDecoder();
let buf = '';
while (true) {
const { done, value } = await reader.read();
if (done) break;
buf += decoder.decode(value, { stream: true });
let sep: number;
while ((sep = buf.indexOf('\n\n')) >= 0) {
const frame = buf.slice(0, sep);
buf = buf.slice(sep + 2);
let event = 'message';
let data = '';
for (const line of frame.split('\n')) {
if (line.startsWith('event:')) event = line.slice(6).trim();
else if (line.startsWith('data:')) data += line.slice(5).trim();
}
if (!data) continue;
if (event === 'progress') printProgress(JSON.parse(data) as Progress);
else if (event === 'done') printReport(JSON.parse(data) as Report);
}
}
}
main();