mirror of
https://github.com/hanzo-apps/hanzo.ai.git
synced 2026-10-29 07:20:43 +00:00
77 lines
2.4 KiB
JavaScript
77 lines
2.4 KiB
JavaScript
import { createServer } from 'node:http'
|
|
import { createReadStream, existsSync, statSync } from 'node:fs'
|
|
import { extname, join, normalize } from 'node:path'
|
|
import { homedir } from 'node:os'
|
|
import { chromium } from '@playwright/test'
|
|
|
|
const TYPES = {
|
|
'.html': 'text/html; charset=utf-8',
|
|
'.css': 'text/css; charset=utf-8',
|
|
'.js': 'text/javascript; charset=utf-8',
|
|
'.json': 'application/json',
|
|
'.svg': 'image/svg+xml',
|
|
'.woff2': 'font/woff2',
|
|
'.txt': 'text/plain; charset=utf-8',
|
|
'.xml': 'application/xml',
|
|
'.ico': 'image/x-icon',
|
|
'.png': 'image/png',
|
|
}
|
|
|
|
const OUT = join(process.cwd(), 'out')
|
|
const DESKTOP_DIR = join(homedir(), 'Desktop')
|
|
|
|
const pages = [
|
|
{ url: '/', filename: 'hanzo-home.png', title: 'Home' },
|
|
{ url: '/login', filename: 'hanzo-login.png', title: 'Login' },
|
|
{ url: '/signup', filename: 'hanzo-signup.png', title: 'Signup' },
|
|
{ url: '/pricing', filename: 'hanzo-pricing.png', title: 'Pricing' },
|
|
]
|
|
|
|
async function main() {
|
|
const server = createServer((req, res) => {
|
|
const rel = normalize(decodeURIComponent((req.url ?? '/').split('?')[0])).replace(/^(\.\.[/\\])+/, '')
|
|
const base = join(OUT, rel)
|
|
const file = [base, base + '.html', join(base, 'index.html')].find(
|
|
(p) => existsSync(p) && statSync(p).isFile()
|
|
)
|
|
if (!file) {
|
|
res.writeHead(404).end('not found')
|
|
return
|
|
}
|
|
const type = TYPES[extname(file)] ?? 'application/octet-stream'
|
|
res.writeHead(200, { 'content-type': type })
|
|
createReadStream(file).pipe(res)
|
|
})
|
|
|
|
await new Promise((ok) => server.listen(0, '127.0.0.1', ok))
|
|
const { port } = server.address()
|
|
console.log(`Export server listening at http://127.0.0.1:${port}`)
|
|
|
|
const browser = await chromium.launch({ headless: true })
|
|
const context = await browser.newContext({
|
|
viewport: { width: 1440, height: 960 },
|
|
deviceScaleFactor: 2,
|
|
colorScheme: 'dark',
|
|
})
|
|
|
|
for (const p of pages) {
|
|
const page = await context.newPage()
|
|
await page.goto(`http://127.0.0.1:${port}${p.url}`, { waitUntil: 'networkidle' })
|
|
await page.waitForTimeout(1000)
|
|
|
|
const dest = join(DESKTOP_DIR, p.filename)
|
|
await page.screenshot({ path: dest, fullPage: false })
|
|
console.log(`Saved screenshot for ${p.title} -> ${dest}`)
|
|
await page.close()
|
|
}
|
|
|
|
await browser.close()
|
|
server.close()
|
|
console.log('Finished capturing all desktop screenshots successfully.')
|
|
}
|
|
|
|
main().catch((err) => {
|
|
console.error(err)
|
|
process.exit(1)
|
|
})
|