Skip to content

Router that virtually distributes inference across connected devices in the home.

Go
2.8 KB
Raw Blame
// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

import { mkdir, writeFile } from 'node:fs/promises'
import { join } from 'node:path'

interface CssAsset {
    name: string
    url: string
    outputPath: string
}

const version = '1.0.0'
const outputDir = join(process.cwd(), 'src/ui/lib/kaizen-ui-foundations')
const assets: CssAsset[] = [
    {
        name: 'base-external.css',
        url: `https://webassets.nvidia.com/kaizen-ui-foundations/${version}/base-external.css`,
        outputPath: join(outputDir, 'base-external.css')
    },
    {
        name: 'components.css',
        url: `https://webassets.nvidia.com/kaizen-ui-foundations/${version}/components.css`,
        outputPath: join(outputDir, 'components.css')
    }
]

function removeRemoteFontFaces(css: string): string {
    return css.replace(/@font-face\{[^{}]*https?:\/\/[^{}]*\}/g, '')
}

function remoteLoadPattern(): RegExp {
    return /(?:@import\s+(?:url\()?["']?https?:\/\/|url\(["']?https?:\/\/)/g
}

function remoteLoadIndex(css: string): number {
    const match = remoteLoadPattern().exec(css)
    return match?.index ?? -1
}

function remoteUrlSnippet(css: string): string {
    const index = remoteLoadIndex(css)
    if (index === -1) return ''
    const start = Math.max(0, index - 80)
    const end = Math.min(css.length, index + 180)
    return css.slice(start, end)
}

function ensureOfflineCss(css: string, assetName: string): string {
    const offlineCss = removeRemoteFontFaces(css)
    if (remoteLoadIndex(offlineCss) !== -1) {
        throw new Error(
            `${assetName} still contains a remote network load after vendoring: ${remoteUrlSnippet(offlineCss)}`
        )
    }
    return [
        `/* Vendored from Kaizen UI Foundations ${version}.`,
        ' * Generated by scripts/vendor-kaizen-ui-foundations-css.ts.',
        ' * Remote @font-face rules are stripped so the Electron app works offline.',
        ' */',
        offlineCss
    ].join('\n')
}

async function fetchText(asset: CssAsset): Promise<string> {
    const response = await fetch(asset.url)
    if (!response.ok) {
        throw new Error(`Failed to fetch ${asset.url}: HTTP ${response.status}`)
    }
    return response.text()
}

async function vendorAsset(asset: CssAsset): Promise<void> {
    const css = await fetchText(asset)
    const offlineCss = ensureOfflineCss(css, asset.name)
    await writeFile(asset.outputPath, offlineCss, 'utf8')
    console.log(`Vendored ${asset.name} (${offlineCss.length} bytes)`)
}

async function main(): Promise<void> {
    await mkdir(outputDir, { recursive: true })
    for (const asset of assets) {
        await vendorAsset(asset)
    }
}

main().catch(err => {
    const message = err instanceof Error ? err.message : String(err)
    console.error(message)
    process.exitCode = 1
})