weather_app/scripts/inline-assets.js
hermes-explorigin 897f49ae22
All checks were successful
Test, Build & Deploy / test-and-build (push) Successful in 1m1s
Test, Build & Deploy / deploy (push) Successful in 22s
Add installable PWA (manifest, service worker, icons) + per-type notification toggles
- manifest.webmanifest + apple/maskable PNG icons; relative paths so the
  app works both at dev root and under /weather
- plain-JS service worker (sw.js) with notificationclick/push handlers and
  an SW-mediated showNotification() path for native Android notifications
- src/lib/pwa.js bootstraps registration and routes alerts to the SW
- Every alert kind (storm/precip/heat/cold/uv/wind) is now a per-type
  preference toggle in Settings; removed the old alertThresholds.thunderstorm
  boolean in favor of notificationTypes.storm
- Store dedups native notifications per forecast day and fires best-effort
  native notifications for newly-appeared alerts
- inline-assets keeps manifest/sw/icons in dist; deploy.yml uploads + verifies
  all PWA files via WebDAV
2026-09-08 00:59:19 +00:00

59 lines
2.1 KiB
JavaScript

/**
* Post-build script: inline remaining external assets (favicon) into index.html
* and remove leftover files so only a single HTML file remains.
*/
import { readFileSync, writeFileSync, rmSync, existsSync } from 'fs'
import { join, dirname } from 'path'
import { fileURLToPath } from 'url'
const __dirname = dirname(fileURLToPath(import.meta.url))
const distDir = join(__dirname, '..', 'dist')
// Read favicon SVG and encode as data URI
const faviconPath = join(distDir, 'favicon.svg')
if (existsSync(faviconPath)) {
const svgContent = readFileSync(faviconPath, 'utf8')
const encoded = Buffer.from(svgContent).toString('base64')
const dataUri = `data:image/svg+xml;base64,${encoded}`
// Replace the favicon link in index.html
const indexPath = join(distDir, 'index.html')
let html = readFileSync(indexPath, 'utf8')
html = html.replace(
/<link rel="icon"[^>]*href="[^"]*favicon\.svg"[^>]*\/?>/i,
`<link rel="icon" type="image/svg+xml" href="${dataUri}" />`
)
writeFileSync(indexPath, html)
// Remove the standalone SVG
rmSync(faviconPath)
console.log('[inline-assets] Inlined favicon.svg into index.html')
}
// Remove any other leftover asset files (e.g. icons.svg from Svelte compiler,
// and the source pwa-icon.svg which is only needed to regenerate the PNGs).
for (const leftover of ['icons.svg', 'pwa-icon.svg']) {
const p = join(distDir, leftover)
if (existsSync(p)) {
rmSync(p)
console.log(`[inline-assets] Removed ${leftover}`)
}
}
// Remove assets directory if it exists
const assetsDir = join(distDir, 'assets')
if (existsSync(assetsDir)) {
rmSync(assetsDir, { recursive: true })
console.log('[inline-assets] Removed assets/ directory')
}
// Keep the PWA companion files that MUST ship alongside index.html so a user
// can install the app: the web manifest, the service worker, and the icon set.
// (These are public/ files Vite copied into dist/; do not delete them.)
for (const keep of ['manifest.webmanifest', 'sw.js', 'icons']) {
const p = join(distDir, keep)
if (existsSync(p)) console.log(`[inline-assets] Preserved PWA ${keep}: ${p}`)
}
console.log('[inline-assets] Done — dist/ contains index.html + PWA files')