codeburn/src/export.ts
Resham Joshi 7b803b293b
fix: survive prototype-named tools and models, never wipe a config on a syntax error, validate plugin names before removal (#1897)
* fix: keep tool and model names like constructor or toString as plain names

Lookups of provider-supplied tool names and model ids into plain-object maps
returned the inherited Object.prototype member, so one session with a tool
named constructor rejected the whole parse, and a model named toString broke
the models report and the menubar payload. Route those lookups through
Object.hasOwn.

* fix: never overwrite an unparseable config.json

readConfig fell back to {} on any error, so one syntax error in a hand-edited
config.json plus any setter (currency, budget, model-alias, ...) saved {} with
one key and erased every other setting. saveConfig now moves an unparseable
file to config.json.corrupt-<timestamp>, says so on stderr and exits 1; read
paths keep the {} fallback with a one-line warning.

* fix: validate the plugin name before plugin remove, info and verify touch disk

plugin remove joined the raw name onto the plugins dir, so
`plugin remove .. --confirm` deleted the whole codeburn config dir. Check the
name against the manifest name pattern first, as the loader already requires.

* fix: keep a model or tool named __proto__ in every running total

The per-model and per-tool totals behind the models report, the menubar
payload, the JSON report and the export assigned into plain objects, so a
name like __proto__ set the prototype instead of adding a row. Build them
prototype-free, and stop the daily cache load from dropping model rows whose
name happens to be an Object.prototype member: they are written own-only.
2026-11-06 09:27:41 +01:00

572 lines
25 KiB
TypeScript

import { writeFile, mkdir, readdir, open, stat, rm } from 'fs/promises'
import { dirname, join, resolve } from 'path'
import { CATEGORY_LABELS, type ParsedApiCall, type ProjectSummary, type TaskCategory } from './types.js'
import { getCurrency, convertCost, roundForActiveCurrency } from './currency.js'
import { dateKey } from './day-aggregator.js'
import { behavioralCallWeight, behavioralTurnCount } from './behavioral-weight.js'
import { multipleIdentities } from './claude-identity.js'
import { aggregateModelEfficiency } from './model-efficiency.js'
import { callBillableOutputTokens, costBasisShares, countSessions, sessionModelBillableOutputTokens, type CostBasisShares } from './session-output.js'
import { findUnpricedModels, hasPriceDiscounts } from './models.js'
function escCsv(s: string): string {
const sanitized = /^[\t\r=+\-@]/.test(s) ? `'${s}` : s
if (sanitized.includes(',') || sanitized.includes('"') || sanitized.includes('\n')) {
return `"${sanitized.replace(/"/g, '""')}"`
}
return sanitized
}
type Row = Record<string, string | number | boolean | undefined | CostBasisShares>
function csvHeaders(): Record<string, string> {
const { code } = getCurrency()
return {
period: 'Period',
date: 'Date',
cost: `Cost (${code})`,
saved: `Saved (${code})`,
apiCalls: 'API Calls',
sessions: 'Sessions',
inputTokens: 'Input Tokens',
outputTokens: 'Output Tokens',
cacheReadTokens: 'Cache Read Tokens',
cacheWriteTokens: 'Cache Write Tokens',
activity: 'Activity',
share: 'Share (%)',
turns: 'Turns',
editTurns: 'Edit Turns',
oneShotRate: 'One-shot Rate (%)',
retriesPerEdit: 'Retries/Edit',
costPerEdit: `Cost/Edit (${code})`,
avgPerSession: `Avg/Session (${code})`,
project: 'Project',
projects: 'Projects',
sessionId: 'Session ID',
startedAt: 'Started At',
tool: 'Tool',
server: 'Server',
command: 'Command',
calls: 'Calls',
}
}
function rowsToCsv(rows: Row[], names: Record<string, string> = csvHeaders()): string {
if (rows.length === 0) return ''
const keys = Object.keys(rows[0])
const lines = [keys.map(k => escCsv(names[k] ?? k)).join(',')]
for (const row of rows) {
lines.push(keys.map(k => escCsv(csvCell(row[k]))).join(','))
}
return lines.join('\n') + '\n'
}
// Unrounded costs must not reach a spreadsheet as 0.24076000000000003 or 1e-7.
function csvCell(v: Row[string]): string {
if (typeof v !== 'number' || Number.isInteger(v)) return String(v ?? '')
return v.toFixed(8).replace(/\.?0+$/, '')
}
function round2(n: number): number {
return Math.round(n * 100) / 100
}
function pct(n: number, total: number): number {
return total > 0 ? round2((n / total) * 100) : 0
}
type DailyAgg = {
cost: number
savings: number
calls: number
input: number
output: number
cacheRead: number
cacheWrite: number
sessions: Set<string>
served: ParsedApiCall[]
}
function buildDailyRows(projects: ProjectSummary[], period: string, withCostBasis = false): Row[] {
const daily: Record<string, DailyAgg> = {}
for (const project of projects) {
for (const session of project.sessions) {
for (const turn of session.turns) {
const turnTs = turn.timestamp || turn.assistantCalls[0]?.timestamp || ''
for (const call of turn.assistantCalls) {
// Same per-call day rule as aggregateProjectsIntoDays, so the daily
// rows sum to the summary and land on the days the history shows.
const day = dateKey(Number.isNaN(new Date(call.timestamp).getTime()) ? turnTs : call.timestamp)
if (!daily[day]) {
daily[day] = { cost: 0, savings: 0, calls: 0, input: 0, output: 0, cacheRead: 0, cacheWrite: 0, sessions: new Set(), served: [] }
}
daily[day].sessions.add(session.sessionId)
daily[day].cost += call.costUSD
daily[day].savings += call.savingsUSD ?? 0
// Same weight rule as aggregateProjectsIntoDays: a supplementary
// accounting call carries cost/tokens but is not a distinct request,
// so daily.csv call counts must reconcile with summary.csv.
daily[day].calls += behavioralCallWeight(call)
daily[day].input += call.usage.inputTokens
daily[day].output += callBillableOutputTokens(call)
daily[day].cacheRead += call.usage.cacheReadInputTokens
daily[day].cacheWrite += call.usage.cacheCreationInputTokens
daily[day].served.push(call)
}
}
}
}
return Object.entries(daily).sort().map(([date, d]) => ({
period,
date,
cost: roundForActiveCurrency(convertCost(d.cost)),
saved: roundForActiveCurrency(convertCost(d.savings)),
apiCalls: d.calls,
sessions: d.sessions.size,
inputTokens: d.input,
outputTokens: d.output,
cacheReadTokens: d.cacheRead,
cacheWriteTokens: d.cacheWrite,
...(withCostBasis ? { costBasis: costBasisShares(d.served) } : {}),
}))
}
function buildRecordRows(projects: ProjectSummary[], split = false): Row[] {
const rows: Row[] = []
const unpricedModels = new Map<string, boolean>()
const isUnpriced = (model: string, cost: number): boolean => {
if (cost > 0) return false
let unpriced = unpricedModels.get(model)
if (unpriced === undefined) {
unpriced = findUnpricedModels([{ model, calls: 1, cost: 0 }]).length > 0
unpricedModels.set(model, unpriced)
}
return unpriced
}
for (const project of projects) {
for (const session of project.sessions) {
for (const turn of session.turns) {
for (const call of turn.assistantCalls) {
rows.push({
project: project.projectPath,
sessionId: session.sessionId,
timestamp: call.timestamp || turn.timestamp || undefined,
category: turn.category,
provider: call.provider,
subagentType: session.agentType?.trim() || undefined,
model: call.model || undefined,
inputTokens: call.usage.inputTokens,
outputTokens: call.usage.outputTokens,
reasoningTokens: call.usage.reasoningTokens,
cacheWriteTokens: call.usage.cacheCreationInputTokens,
cacheReadTokens: Math.max(call.usage.cacheReadInputTokens, call.usage.cachedInputTokens),
cost: convertCost(call.costUSD),
savings: convertCost(call.savingsUSD ?? 0),
costBasis: call.costBasis,
// Records are the raw serve ledger and keep every supplementary
// accounting row; the marker is how a "one row per API call"
// consumer tells them apart. Key present on every row (undefined
// when false) so rowsToCsv, which reads headers off the first row,
// always emits the column; JSON drops the undefined ones.
supplementary: call.supplementaryAccounting ? true : undefined,
estimated: call.isEstimated ? true : undefined,
unpriced: isUnpriced(call.model, call.costUSD) ? true : undefined,
retained: session.retained ? true : undefined,
...(hasPriceDiscounts() ? { listCost: call.listCostUSD !== undefined ? convertCost(call.listCostUSD) : undefined } : {}),
...(split ? { identity: session.identity?.label } : {}),
})
}
}
}
}
return rows
}
function buildActivityRows(projects: ProjectSummary[], period: string): Row[] {
const catTotals: Record<string, { turns: number; cost: number }> = {}
for (const project of projects) {
for (const session of project.sessions) {
for (const [cat, d] of Object.entries(session.categoryBreakdown)) {
if (!catTotals[cat]) catTotals[cat] = { turns: 0, cost: 0 }
catTotals[cat].turns += d.turns
catTotals[cat].cost += d.costUSD
}
}
}
const totalCost = Object.values(catTotals).reduce((s, d) => s + d.cost, 0)
return Object.entries(catTotals)
.sort(([, a], [, b]) => b.cost - a.cost)
.map(([cat, d]) => ({
period,
activity: CATEGORY_LABELS[cat as TaskCategory] ?? cat,
cost: roundForActiveCurrency(convertCost(d.cost)),
share: pct(d.cost, totalCost),
turns: d.turns,
}))
}
function buildModelRows(projects: ProjectSummary[], period: string): Row[] {
const modelTotals: Record<string, { calls: number; cost: number; savings: number; input: number; output: number; cacheRead: number; cacheWrite: number }> = Object.create(null)
const modelEfficiency = aggregateModelEfficiency(projects)
const ensure = (model: string) => {
if (!Object.hasOwn(modelTotals, model)) modelTotals[model] = { calls: 0, cost: 0, savings: 0, input: 0, output: 0, cacheRead: 0, cacheWrite: 0 }
return modelTotals[model]
}
for (const project of projects) {
for (const session of project.sessions) {
for (const [model, d] of Object.entries(session.modelBreakdown)) {
const acc = ensure(model)
acc.calls += d.calls
acc.cost += d.costUSD
acc.savings += d.savingsUSD
acc.input += d.tokens.inputTokens
acc.cacheRead += d.tokens.cacheReadInputTokens ?? 0
acc.cacheWrite += d.tokens.cacheCreationInputTokens ?? 0
}
// Output must be billed per call while provider identity is still known.
// Join on the same key as parser modelBreakdown (getShortModelName), not raw call.model.
for (const [model, output] of Object.entries(sessionModelBillableOutputTokens(session))) {
ensure(model).output += output
}
}
}
const totalCost = Object.values(modelTotals).reduce((s, d) => s + d.cost, 0)
return Object.entries(modelTotals)
.filter(([name]) => name !== '<synthetic>')
.sort(([, a], [, b]) => (b.cost + b.savings) - (a.cost + a.savings))
.map(([model, d]) => {
const efficiency = modelEfficiency.get(model)
return {
period,
model,
cost: roundForActiveCurrency(convertCost(d.cost)),
saved: roundForActiveCurrency(convertCost(d.savings)),
share: pct(d.cost, totalCost),
apiCalls: d.calls,
editTurns: efficiency?.editTurns ?? 0,
oneShotRate: efficiency?.oneShotRate ?? '',
retriesPerEdit: efficiency?.retriesPerEdit ?? '',
costPerEdit: efficiency?.costPerEditUSD !== null && efficiency?.costPerEditUSD !== undefined
? roundForActiveCurrency(convertCost(efficiency.costPerEditUSD))
: '',
inputTokens: d.input,
outputTokens: d.output,
cacheReadTokens: d.cacheRead,
cacheWriteTokens: d.cacheWrite,
}
})
}
function buildToolRows(projects: ProjectSummary[]): Row[] {
const toolTotals: Record<string, number> = Object.create(null)
for (const project of projects) {
for (const session of project.sessions) {
for (const [tool, d] of Object.entries(session.toolBreakdown)) {
toolTotals[tool] = (toolTotals[tool] ?? 0) + d.calls
}
}
}
const total = Object.values(toolTotals).reduce((s, n) => s + n, 0)
return Object.entries(toolTotals)
.sort(([, a], [, b]) => b - a)
.map(([tool, calls]) => ({
tool,
calls,
share: pct(calls, total),
}))
}
function buildMcpRows(projects: ProjectSummary[]): Row[] {
const mcpTotals: Record<string, number> = Object.create(null)
for (const project of projects) {
for (const session of project.sessions) {
for (const [server, d] of Object.entries(session.mcpBreakdown)) {
mcpTotals[server] = (mcpTotals[server] ?? 0) + d.calls
}
}
}
const total = Object.values(mcpTotals).reduce((s, n) => s + n, 0)
return Object.entries(mcpTotals)
.sort(([, a], [, b]) => b - a)
.map(([server, calls]) => ({
server,
calls,
share: pct(calls, total),
}))
}
function buildBashRows(projects: ProjectSummary[]): Row[] {
const bashTotals: Record<string, number> = Object.create(null)
for (const project of projects) {
for (const session of project.sessions) {
for (const [cmd, d] of Object.entries(session.bashBreakdown)) {
bashTotals[cmd] = (bashTotals[cmd] ?? 0) + d.calls
}
}
}
const total = Object.values(bashTotals).reduce((s, n) => s + n, 0)
return Object.entries(bashTotals)
.sort(([, a], [, b]) => b - a)
.map(([cmd, calls]) => ({
command: cmd,
calls,
share: pct(calls, total),
}))
}
function buildProjectRows(projects: ProjectSummary[]): Row[] {
const total = projects.reduce((s, p) => s + p.totalCostUSD, 0)
return projects
.slice()
.sort((a, b) => (b.totalCostUSD + b.totalSavingsUSD) - (a.totalCostUSD + a.totalSavingsUSD))
.map(p => ({
project: p.projectPath,
cost: roundForActiveCurrency(convertCost(p.totalCostUSD)),
saved: roundForActiveCurrency(convertCost(p.totalSavingsUSD)),
avgPerSession: p.sessions.length > 0 ? roundForActiveCurrency(convertCost(p.totalCostUSD / p.sessions.length)) : '',
share: pct(p.totalCostUSD, total),
apiCalls: p.totalApiCalls,
sessions: p.sessions.length,
}))
}
function buildSessionRows(projects: ProjectSummary[], withCostBasis = false, split = false): Row[] {
const rows: Row[] = []
for (const p of projects) {
for (const s of p.sessions) {
const models = new Set(
s.turns.flatMap(turn => turn.assistantCalls.map(call => call.model).filter(Boolean)),
)
rows.push({
project: p.projectPath,
sessionId: s.sessionId,
startedAt: s.firstTimestamp ?? '',
cost: roundForActiveCurrency(convertCost(s.totalCostUSD)),
saved: roundForActiveCurrency(convertCost(s.totalSavingsUSD)),
apiCalls: s.apiCalls,
turns: behavioralTurnCount(s.turns),
subagentType: s.agentType?.trim() || undefined,
model: models.size === 1 ? [...models][0] : undefined,
...(withCostBasis ? { costBasis: costBasisShares(s.turns.flatMap(turn => turn.assistantCalls)) } : {}),
retained: s.retained ? true : undefined,
...(split ? { identity: s.identity?.label } : {}),
})
}
}
return rows.sort((a, b) => ((b.cost as number) + (b.saved as number)) - ((a.cost as number) + (a.saved as number)))
}
export type PeriodExport = {
label: string
projects: ProjectSummary[]
}
function buildSummaryRows(periods: PeriodExport[]): Row[] {
return periods.map(p => {
const cost = p.projects.reduce((s, proj) => s + proj.totalCostUSD, 0)
const savings = p.projects.reduce((s, proj) => s + proj.totalSavingsUSD, 0)
const calls = p.projects.reduce((s, proj) => s + proj.totalApiCalls, 0)
const sessions = countSessions(p.projects)
const projectCount = p.projects.filter(proj => proj.totalCostUSD > 0 || proj.totalSavingsUSD > 0).length
return {
period: p.label,
cost: roundForActiveCurrency(convertCost(cost)),
saved: roundForActiveCurrency(convertCost(savings)),
apiCalls: calls,
sessions,
projects: projectCount,
}
})
}
function buildReadme(periods: PeriodExport[]): string {
const { code } = getCurrency()
const generated = new Date().toISOString()
const lines = [
'CodeBurn Usage Export',
'====================',
'',
`Generated: ${generated}`,
`Currency: ${code}`,
`Periods: ${periods.map(p => p.label).join(', ')}`,
'',
'Files',
'-----',
' summary.csv One row per period. Headline totals.',
' daily.csv Day-by-day breakdown, Period column distinguishes the window.',
' activity.csv Time spent per task category (Coding, Debugging, Exploration, etc.).',
' models.csv Spend per model with token totals and cache usage.',
' records.csv One row per served call, with optional subagentType and model.',
' supplementary marks accounting-only rows: recovered tokens/cost that',
' are not distinct requests. estimated marks costs priced from',
' estimated or missing token counts; unpriced marks $0 rows whose',
' model has no known price. cost is unrounded. costBasis says where the',
' cost came from: recorded (the tool\'s own figure), calculated (measured',
' tokens at published prices), estimated (guessed tokens), included',
' (covered by a subscription, $0) or unpriced (no known price, $0).',
' retained marks a session kept from the cache after its tool removed the transcript;',
' sessions.csv carries the same column.',
' listCost (only with price-discount set) is the list-price cost of a discounted call.',
' identity (only once codeburn has seen two or more Claude identities) is the organization',
' (or account) active when the session started; sessions.csv carries it too.',
' projects.csv Spend per project folder for the selected detail period.',
' sessions.csv One row per session for the selected detail period.',
' tools.csv Tool invocations and share for the selected detail period.',
' mcp.csv MCP server invocations and share for the selected detail period.',
' shell-commands.csv Shell commands executed via Bash tool for the selected detail period.',
'',
'Notes',
'-----',
' Every cost column is already converted to the active currency. Tokens are raw integer',
' counts from provider telemetry. Share (%) is relative to the period/table total.',
' CSV headers are unchanged. New columns can be added; read them by header.',
' The JSON export (codeburn.export.v3) uses fixed camelCase keys (cost, saved, apiCalls,',
' inputTokens, ...) and states the currency once in currency.code. v2 consumers must rename',
' keys. New keys can still appear under v3; read them by name.',
'',
]
return lines.join('\n')
}
/// Sentinel file dropped into every folder we create so we can safely overwrite an older
/// codeburn export without ever deleting a user's unrelated files by accident.
const EXPORT_MARKER_FILE = '.codeburn-export'
async function isCodeburnExportFolder(path: string): Promise<boolean> {
const markerStat = await stat(join(path, EXPORT_MARKER_FILE)).catch(() => null)
return markerStat?.isFile() ?? false
}
async function clearCodeburnExportFolder(path: string): Promise<void> {
const entries = await readdir(path)
for (const entry of entries) {
await rm(join(path, entry), { recursive: true, force: true })
}
}
/// Writes a folder of one-table-per-file CSVs. The outputPath is treated as a directory. If it
/// ends in `.csv` the extension is stripped to form the folder name. Refuses to delete a
/// pre-existing file or a non-codeburn folder, so a typo like `-o ~/.ssh/id_ed25519` can't
/// wipe a sensitive file (prior versions did `rm(path, { force: true })` unconditionally).
export async function exportCsv(periods: PeriodExport[], outputPath: string): Promise<string> {
const thirtyDays = periods.find(p => p.label === '30 Days')
const thirtyDayProjects = thirtyDays?.projects ?? periods[periods.length - 1]?.projects ?? []
const split = await multipleIdentities()
let folder = resolve(outputPath)
if (folder.toLowerCase().endsWith('.csv')) {
folder = folder.slice(0, -4)
}
// The desktop app (and any `-o <existing dir>`) hands us a real folder like the
// Desktop, not a slot to fill: writing our ~11 files straight in would trip the
// reuse guard below. Nest the export in a dated subfolder of our own and let the
// guard apply to that. A folder we made earlier (marker present) is reused as-is.
const targetStat = await stat(folder).catch(() => null)
if (targetStat?.isDirectory() && !(await isCodeburnExportFolder(folder))) {
folder = join(folder, `codeburn-export-${new Date().toISOString().slice(0, 10)}`)
}
const existingStat = await stat(folder).catch(() => null)
if (existingStat?.isFile()) {
throw new Error(`Refusing to overwrite existing file at ${folder}. Pass a directory path instead.`)
}
if (existingStat?.isDirectory()) {
if (!(await isCodeburnExportFolder(folder))) {
throw new Error(
`Refusing to reuse non-empty directory ${folder}: no ${EXPORT_MARKER_FILE} marker. ` +
`Delete it manually or pick a different -o path.`
)
}
await clearCodeburnExportFolder(folder)
}
await mkdir(folder, { recursive: true })
await writeFile(join(folder, EXPORT_MARKER_FILE), '', 'utf-8')
const dailyRows = periods.flatMap(p => buildDailyRows(p.projects, p.label))
const activityRows = periods.flatMap(p => buildActivityRows(p.projects, p.label))
const modelRows = periods.flatMap(p => buildModelRows(p.projects, p.label))
await writeFile(join(folder, 'README.txt'), buildReadme(periods), 'utf-8')
await writeFile(join(folder, 'summary.csv'), rowsToCsv(buildSummaryRows(periods)), 'utf-8')
await writeFile(join(folder, 'daily.csv'), rowsToCsv(dailyRows), 'utf-8')
await writeFile(join(folder, 'activity.csv'), rowsToCsv(activityRows), 'utf-8')
await writeFile(join(folder, 'models.csv'), rowsToCsv(modelRows, { ...csvHeaders(), model: 'Model' }), 'utf-8')
await writeFile(join(folder, 'records.csv'), rowsToCsv(buildRecordRows(thirtyDayProjects, split), {}), 'utf-8')
await writeFile(join(folder, 'projects.csv'), rowsToCsv(buildProjectRows(thirtyDayProjects)), 'utf-8')
await writeFile(join(folder, 'sessions.csv'), rowsToCsv(buildSessionRows(thirtyDayProjects, false, split)), 'utf-8')
await writeFile(join(folder, 'tools.csv'), rowsToCsv(buildToolRows(thirtyDayProjects)), 'utf-8')
await writeFile(join(folder, 'mcp.csv'), rowsToCsv(buildMcpRows(thirtyDayProjects)), 'utf-8')
await writeFile(join(folder, 'shell-commands.csv'), rowsToCsv(buildBashRows(thirtyDayProjects)), 'utf-8')
return folder
}
export async function exportJson(periods: PeriodExport[], outputPath: string): Promise<string> {
const thirtyDays = periods.find(p => p.label === '30 Days')
const thirtyDayProjects = thirtyDays?.projects ?? periods[periods.length - 1]?.projects ?? []
const split = await multipleIdentities()
const { code, rate, symbol } = getCurrency()
const data = {
schema: 'codeburn.export.v3',
generated: new Date().toISOString(),
currency: { code, rate, symbol },
summary: buildSummaryRows(periods),
periods: periods.map(p => ({
label: p.label,
daily: buildDailyRows(p.projects, p.label, true),
activity: buildActivityRows(p.projects, p.label),
models: buildModelRows(p.projects, p.label),
})),
projects: buildProjectRows(thirtyDayProjects),
sessions: buildSessionRows(thirtyDayProjects, true, split),
records: buildRecordRows(thirtyDayProjects, split),
tools: buildToolRows(thirtyDayProjects),
mcp: buildMcpRows(thirtyDayProjects),
shellCommands: buildBashRows(thirtyDayProjects),
}
// The desktop app (and any `-o <existing dir>`) hands us a real folder, not a slot to
// fill: appending `.json` to it wrote a sibling *of* the folder. Name a dated file
// inside it instead, the same name exportCsv gives its dated subfolder. A path written
// with a trailing separator says folder just as plainly, whether or not it exists yet.
const namedAsFolder = /[\\/]$/.test(outputPath)
let target = resolve(outputPath)
if (namedAsFolder || (await stat(target).catch(() => null))?.isDirectory()) {
target = join(target, `codeburn-export-${new Date().toISOString().slice(0, 10)}.json`)
} else if (!target.toLowerCase().endsWith('.json')) {
target = `${target}.json`
}
// Refuse to overwrite an existing file that wasn't produced by codeburn
// export. CSV path has the same guard via the .codeburn-export marker; JSON
// was missing it, so a stray `-o ~/important.json` would silently clobber.
const existing = await stat(target).catch(() => null)
if (existing?.isFile()) {
// Read just the first 4KB to look for the schema marker. The schema key
// is the first field in the JSON object so a partial read is enough;
// loading the whole file (potentially gigabytes) into memory could OOM
// on Node's ~512MB string limit.
const fh = await open(target, 'r')
try {
const buf = Buffer.alloc(4096)
const { bytesRead } = await fh.read(buf, 0, buf.length, 0)
const head = buf.toString('utf-8', 0, bytesRead)
if (!head.includes('"schema": "codeburn.export.v')) {
throw new Error(
`Refusing to overwrite ${target}: file does not look like a codeburn export. ` +
`Delete it manually or pick a different -o path.`
)
}
} finally {
await fh.close()
}
}
if (existing?.isDirectory()) {
throw new Error(`Refusing to overwrite directory at ${target}. Pass a file path instead.`)
}
await mkdir(dirname(target), { recursive: true })
await writeFile(target, JSON.stringify(data, null, 2), 'utf-8')
return target
}