OverviewFilesCommitsIssuesDocsReleasesPeers

cthuloid / webui / scripts / gen-assets.js

22 lines · javascript ref:
// Embed the page's files into a JS module, for a packed worklet (the Android app),
// where there are no files on disk to read. Used by scripts/gen-assets-cli.js and
// tested in test/app.test.js against the files themselves.
import fs from 'bare-fs'
import path from 'bare-path'
import { ASSET_FILES } from '../lib/asset-files.js'

const STATIC_DIR = path.join(path.dirname(new URL(import.meta.url).pathname), '..', 'static')

export function buildAssets (dir = STATIC_DIR) {
  const out = {}
  for (const [url, [file, type]] of Object.entries(ASSET_FILES)) {
    out[url] = { type, body: fs.readFileSync(path.join(dir, file), 'utf8') }
  }
  return out
}

export function assetsModule (assets) {
  return '// GENERATED by scripts/gen-assets-cli.js from static/ -- do not edit.\n' +
    'export default ' + JSON.stringify(assets, null, 1) + '\n'
}