Skip to content

The ultimate harness for coding agents and beyond. All your agents. All your machines. One command center. Start with code, then follow your curiosity and build across disciplines: CAD, circuits, robots, games and music.

6.0 KB
Raw Blame
#!/usr/bin/env node
/**
 * Writes src/lib/connectors/generated/*.ts from src/lib/connectors/assets: the catalog, the page's files
 * and the services' icons as data URLs, so cli.js carries them with no loader or asset file of its own.
 *
 *   node scripts/connector-assets.mjs          # after changing anything in assets/
 *   node scripts/connector-assets.mjs --check  # what the spec runs: fails when they differ
 *
 * src/lib/connectors/generated.spec.ts keeps the two in step.
 */
import { readFileSync, readdirSync, writeFileSync, mkdirSync, rmSync } from 'node:fs'
import { fileURLToPath } from 'node:url'
import { join, extname, basename } from 'node:path'

const root = fileURLToPath(new URL('../src/lib/connectors/', import.meta.url))
const assets = join(root, 'assets')
const out = join(root, 'generated')
const header = '// Generated by scripts/connector-assets.mjs from src/lib/connectors/assets. Do not edit.\n'

export function render() {
  const catalog = JSON.parse(readFileSync(join(assets, 'catalog.json'), 'utf8')).connectors
  const icons = {}
  for (const name of readdirSync(join(assets, 'web/icons')).sort()) {
    const type = { '.png': 'image/png', '.svg': 'image/svg+xml' }[extname(name)]
    if (!type) continue
    icons[basename(name, extname(name))] = `data:${type};base64,${readFileSync(join(assets, 'web/icons', name)).toString('base64')}`
  }
  const page = Object.fromEntries(['index.html', 'page.js', 'style.css'].map(name => [name, readFileSync(join(assets, 'web', name), 'utf8')]))
  return {
    'catalog.ts': `${header}import type { CatalogItem } from '../catalog.js'\n\nexport const CATALOG_ITEMS: CatalogItem[] = ${JSON.stringify(catalog, null, 1)}\n`,
    'icons.ts': `${header}/** Each service's icon as a data URL, by code. */\nexport const ICONS: Record<string, string> = ${JSON.stringify(icons, null, 1)}\n`,
    'page.ts': `${header}/** The Connectors page's files, served by page.ts. */\nexport const PAGE_FILES: Record<string, string> = ${JSON.stringify(page, null, 1)}\n`,
  }
}

/** The desktop app's copy: the same icons as assets, and their names by code. */
const desktop = fileURLToPath(new URL('../../desktop/', import.meta.url))
export function renderDesktop() {
  const names = readdirSync(join(assets, 'web/icons')).filter(name => ['.png', '.svg'].includes(extname(name))).sort()
  const map = names.map(name => `  '${basename(name, extname(name))}': 'assets/connector-icons/${name}',`).join('\n')
  return {
    icons: Object.fromEntries(names.map(name => [name, readFileSync(join(assets, 'web/icons', name))])),
    dart: `// Generated by cli/scripts/connector-assets.mjs from cli/src/lib/connectors/assets. Do not edit.\n\n/// Each connected service's icon, bundled with the app, by its code.\nconst connectorIcons = <String, String>{\n${map}\n};\n`,
  }
}

/**
 * The phone app's copy. It draws no SVG (it carries no SVG package), so a service whose icon is an SVG
 * takes its PNG rendering from assets/mobile-icons: 64 px, transparent, made once from the SVG beside it
 * (headless Chrome, or `rsvg-convert -w 64 -h 64 --keep-aspect-ratio`) when that icon changes.
 */
const mobile = fileURLToPath(new URL('../../mobile/', import.meta.url))
export function renderMobile() {
  const icons = {}
  for (const name of readdirSync(join(assets, 'web/icons')).sort()) {
    const code = basename(name, extname(name))
    if (extname(name) === '.png') icons[`${code}.png`] = readFileSync(join(assets, 'web/icons', name))
    else if (extname(name) === '.svg') icons[`${code}.png`] = readFileSync(join(assets, 'mobile-icons', `${code}.png`))
  }
  const map = Object.keys(icons).sort().map(name => `  '${basename(name, '.png')}': 'assets/connector-icons/${name}',`).join('\n')
  return {
    icons,
    dart: `// Generated by cli/scripts/connector-assets.mjs from cli/src/lib/connectors/assets. Do not edit.\n\n/// Each connected service's icon, bundled with the app, by its code. PNG only: this app draws no SVG.\nconst connectorIcons = <String, String>{\n${map}\n};\n`,
  }
}

const NOTICE = [
  'OpenHarness: service icons for Settings ▸ Connectors', '',
  'Copied by cli/scripts/connector-assets.mjs from cli/src/lib/connectors/assets/web/icons; do not edit here.', '',
  "Sources: Autonomous's connector icons (cdn.autonomous.ai), Simple Icons (CC0) and the services' own",
  'favicons, resized to 64 px PNG or coloured SVG. Names and marks identify the services; they do not imply',
  'sponsorship or endorsement.', ''].join('\n')

if (process.argv[1] === fileURLToPath(import.meta.url)) {
  const files = render()
  const app = renderDesktop()
  const phone = renderMobile()
  if (process.argv.includes('--check')) {
    const stale = Object.entries(files).filter(([name, body]) => {
      try { return readFileSync(join(out, name), 'utf8') !== body } catch { return true }
    }).map(([name]) => name)
    if (stale.length) { console.error(`Out of date: ${stale.join(', ')}. Run node scripts/connector-assets.mjs`); process.exit(1) }
  } else {
    mkdirSync(out, { recursive: true })
    for (const [name, body] of Object.entries(files)) writeFileSync(join(out, name), body)
    rmSync(join(desktop, 'assets/connector-icons'), { recursive: true, force: true })
    mkdirSync(join(desktop, 'assets/connector-icons'), { recursive: true })
    for (const [name, data] of Object.entries(app.icons)) writeFileSync(join(desktop, 'assets/connector-icons', name), data)
    writeFileSync(join(desktop, 'assets/connector-icons/NOTICE.txt'), NOTICE)
    mkdirSync(join(desktop, 'lib/connectors'), { recursive: true })
    writeFileSync(join(desktop, 'lib/connectors/connector_icons.dart'), app.dart)
    rmSync(join(mobile, 'assets/connector-icons'), { recursive: true, force: true })
    mkdirSync(join(mobile, 'assets/connector-icons'), { recursive: true })
    for (const [name, data] of Object.entries(phone.icons)) writeFileSync(join(mobile, 'assets/connector-icons', name), data)
    writeFileSync(join(mobile, 'assets/connector-icons/NOTICE.txt'), NOTICE)
    writeFileSync(join(mobile, 'lib/phone/connector_icons.dart'), phone.dart)
  }
}