feat: add branded tutorials and display-only plan device settings

This commit is contained in:
hellcat committed 2026-10-08 18:01:02 +08:00
1 parent 4f48e61a2c
commit cd117604a3
126 files changed
+1197 -9

No files matched your search

+44
View File
@@ -0,0 +1,44 @@
import fs from 'node:fs/promises'
import path from 'node:path'
import { fileURLToPath } from 'node:url'
const root = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '../..')
const source = path.join(root, 'content/tutorials')
const output = path.join(root, 'public/tutorials')
let layout = await fs.readFile(path.join(root, 'resources/tutorials/layout.html'), 'utf8')
if (process.argv.includes('--live-reload')) {
layout = layout.replace('</body>', `@if(in_array(request()->getHost(), ['localhost', '127.0.0.1']))
<script src="/tutorials/assets/live-reload.js" defer></script>
@endif
</body>`)
}
const registry = {}
const views = path.join(root, 'resources/views/tutorials')
await fs.mkdir(path.join(output, 'assets'), {recursive: true})
await fs.copyFile(path.join(root, 'resources/tutorials/tutorial.css'), path.join(output, 'assets/tutorial.css'))
if (process.argv.includes('--live-reload')) await fs.copyFile(path.join(root, 'resources/tutorials/live-reload.js'), path.join(output, 'assets/live-reload.js'))
for (const platform of await fs.readdir(source, {withFileTypes: true})) {
if (!platform.isDirectory()) continue
for (const client of await fs.readdir(path.join(source, platform.name), {withFileTypes: true})) {
if (!client.isDirectory()) continue
const folder = path.join(source, platform.name, client.name)
const meta = JSON.parse(await fs.readFile(path.join(folder, 'meta.json'), 'utf8'))
if (meta.platform !== platform.name || meta.client !== client.name || !/^[a-z0-9-]+$/.test(platform.name) || !/^[a-z0-9-]+$/.test(client.name)) throw new Error('Invalid tutorial location')
const prefix = `/tutorials/${platform.name}/${client.name}`
let body = await fs.readFile(path.join(folder, `${meta.language}.html`), 'utf8')
for (const match of body.matchAll(/src="\.\/images\/([^"]+)"/g)) await fs.access(path.join(folder, 'images', match[1]))
body = body.replaceAll('src="./images/', `src="${prefix}/images/`)
const escape = value => value.replaceAll('&','&amp;').replaceAll('<','&lt;').replaceAll('"','&quot;')
const html = layout.replace('{{language}}', escape(meta.language)).replace('{{title}}', escape(meta.title)).replace('{{body}}', body).replaceAll('{{brand}}', '{{ $brand }}')
await fs.mkdir(path.join(output, platform.name), {recursive:true})
await fs.mkdir(path.join(views, platform.name), {recursive:true})
await fs.writeFile(path.join(views, platform.name, `${client.name}.blade.php`), html)
// Remove legacy static HTML so Laravel can inject the current configured name.
await fs.rm(path.join(output, platform.name, `${client.name}.html`), {force:true})
await fs.cp(path.join(folder,'images'), path.join(output,platform.name,client.name,'images'),{recursive:true})
registry[platform.name] = prefix + '.html'
}
}
const generated = path.join(root, 'stellar/src/generated')
await fs.mkdir(generated,{recursive:true})
await fs.writeFile(path.join(generated,'tutorials.ts'), '// Generated by scripts/tutorials/build.mjs\nexport const tutorialPages: Record<string, string> = '+JSON.stringify(registry,null,2)+'\n')
console.log(`Built ${Object.keys(registry).length} independent tutorial page(s).`)
+75
View File
@@ -0,0 +1,75 @@
import { watch } from 'node:fs'
import http from 'node:http'
import path from 'node:path'
import { fileURLToPath } from 'node:url'
import { execFile } from 'node:child_process'
import { promisify } from 'node:util'
const root = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '../..')
const run = promisify(execFile)
const clients = new Set()
const allowedOrigins = new Set(['http://127.0.0.1:8587', 'http://localhost:8587'])
let revision = Date.now().toString()
let dirty = false
let building = false
let timer
function send(response, event) {
response.write(`event: ${event}\ndata: ${JSON.stringify({ revision })}\n\n`)
}
async function build() {
if (building) { dirty = true; return }
building = true
do {
dirty = false
try {
const { stdout } = await run(process.execPath, ['scripts/tutorials/build.mjs', '--live-reload'], { cwd: root })
process.stdout.write(stdout)
revision = Date.now().toString()
for (const response of clients) send(response, 'update')
} catch (error) {
console.error('Tutorial build failed; keeping the current preview.', error.stderr || error.message)
}
} while (dirty)
building = false
}
const watchers = ['content/tutorials', 'resources/tutorials'].map(folder => {
const watcher = watch(path.join(root, folder), { recursive: true }, (_, filename) => {
if (!filename || /(^|[\/])\./.test(filename)) return
clearTimeout(timer)
timer = setTimeout(build, 180)
})
watcher.on('error', error => console.error('Tutorial watcher error:', error.message))
return watcher
})
await build()
const server = http.createServer((request, response) => {
if (request.url !== '/events') { response.writeHead(404).end(); return }
const origin = request.headers.origin
if (origin && !allowedOrigins.has(origin)) { response.writeHead(403).end(); return }
response.writeHead(200, {
'Content-Type': 'text/event-stream',
'Cache-Control': 'no-cache',
'Connection': 'keep-alive',
...(origin ? { 'Access-Control-Allow-Origin': origin, 'Vary': 'Origin' } : {}),
})
response.flushHeaders()
clients.add(response)
send(response, 'ready')
request.on('close', () => clients.delete(response))
})
server.on('error', error => { console.error('Live reload server:', error.message); process.exit(1) })
server.listen(8588, '127.0.0.1', () => console.log('Tutorial watch enabled: save content, images or shared styles to refresh the local preview.'))
const heartbeat = setInterval(() => { for (const response of clients) response.write(': keepalive\n\n') }, 20000)
function shutdown() {
clearInterval(heartbeat)
clearTimeout(timer)
for (const watcher of watchers) watcher.close()
for (const response of clients) response.end()
server.close(() => process.exit(0))
}
process.on('SIGINT', shutdown)
process.on('SIGTERM', shutdown)