Files
BrowserOS/scripts/build/server/metadata.ts
Nikhil 9fdb361d67 feat: build prod server resource artifact pipeline (#417)
* feat: build prod server resource artifacts with cloudflare r2

* fix: address PR review comments for prod_server_resources_cloudflare

* feat: fix prod server build setup and CLI ergonomics

* fix: prevent build env secret inlining and template fallback

* fix: read from fileenv

* feat: add ripgrep

* feat: upload prod artifacts to latest and version prefixes
2026-03-05 14:26:46 -08:00

75 lines
1.7 KiB
TypeScript

import { createHash } from 'node:crypto'
import { readdir, readFile, writeFile } from 'node:fs/promises'
import { join, relative, sep } from 'node:path'
import type { BuildTarget } from './types'
interface MetadataFile {
path: string
sha256: string
size: number
}
async function collectFiles(
rootDir: string,
currentDir: string,
): Promise<string[]> {
const entries = await readdir(currentDir, { withFileTypes: true })
const files: string[] = []
for (const entry of entries) {
const absolutePath = join(currentDir, entry.name)
if (entry.isDirectory()) {
const nested = await collectFiles(rootDir, absolutePath)
files.push(...nested)
continue
}
files.push(relative(rootDir, absolutePath))
}
return files
}
async function toMetadataFile(
rootDir: string,
filePath: string,
): Promise<MetadataFile> {
const absolutePath = join(rootDir, filePath)
const content = await readFile(absolutePath)
return {
path: filePath.split(sep).join('/'),
sha256: createHash('sha256').update(content).digest('hex'),
size: content.byteLength,
}
}
export async function writeArtifactMetadata(
artifactRoot: string,
target: BuildTarget,
version: string,
): Promise<string> {
const fileList = await collectFiles(artifactRoot, artifactRoot)
const files: MetadataFile[] = []
for (const filePath of fileList.sort()) {
files.push(await toMetadataFile(artifactRoot, filePath))
}
const metadataPath = join(artifactRoot, 'artifact-metadata.json')
await writeFile(
metadataPath,
JSON.stringify(
{
version,
target: target.id,
generatedAt: new Date().toISOString(),
files,
},
null,
2,
),
)
return metadataPath
}