#!/usr/bin/env node
'use strict'
// 站点生成器:用 site/_home.tpl.html 与 site/_plugins.tpl.html 渲染首页 / 插件页,
// 数据来自 site/releases.json(版本)与 market/index.json(插件)。
const fs = require('fs')
const path = require('path')
const root = path.resolve(__dirname, '..')
const site = path.join(root, 'site')
const esc = (s) => String(s == null ? '' : s).replace(/[&<>"']/g, (c) => ({ '&': '&', '<': '<', '>': '>', '"': '"', "'": ''' }[c]))
const mb = (n) => (Number(n) / 1048576).toFixed(1) + ' MB'
const data = JSON.parse(fs.readFileSync(path.join(site, 'releases.json'), 'utf8'))
const rel = Array.isArray(data.releases) ? data.releases.slice() : (Array.isArray(data) ? data.slice() : [])
rel.sort((a, b) => String(b.version).localeCompare(String(a.version), undefined, { numeric: true }))
const latest = rel[0] || { version: '', platforms: {} }
const V = esc(latest.version)
const STAMP = encodeURIComponent(V + '-' + Date.now().toString(36))
const marketFile = path.join(root, 'market', 'index.json')
const market = fs.existsSync(marketFile) ? JSON.parse(fs.readFileSync(marketFile, 'utf8')) : { plugins: [] }
const plugins = Array.isArray(market.plugins) ? market.plugins : []
const nav = (active) => [
'',
].join('\n')
const footer = [
'',
].join('\n')
const card = (p) => [
'
',
'
' + esc(p.icon || '◻') + '',
'
' + esc(p.name || p.id) + '
v' + esc(p.version || '') + ' · ' + esc(p.author === 'official' ? '官方' : (p.author || '')) + '
',
'
' + esc(p.desc || '') + '
',
'
' + (p.tags || []).map((t) => '' + esc(t) + '').join('') + '
',
'
',
].join('\n')
const feats = [
['💻', '#eef2ff', '本地优先', '图片与数据保存在 ~/Documents/ImageWorkbench,不经服务器中转;可整目录备份、迁移到新电脑。'],
['🕸️', '#fff7ed', '节点式无限画布', '参考图 → 提示词 → 生成 → 图库,用连线把工作流串起来;一张画布容纳整条生产线。'],
['🧩', '#ecfdf5', '插件按需安装', '应用本体不含插件,需要什么装什么;可随时启停,关闭后入口自动隐藏。'],
]
const featCards = feats.map((f) => [
' ' + f[0] + '
',
'
' + f[2] + '
' + f[3] + '
',
].join('\n')).join('\n')
const dlCards = [
['darwin-aarch64', 'macOS · Apple Silicon', 'M1 / M2 / M3 / M4'],
['darwin-x86_64', 'macOS · Intel', 'x86_64'],
['windows-x86_64', 'Windows · x64', '需要 WebView2'],
].map(([key, label, hint]) => {
const p = (latest.platforms || {})[key]
if (!p) return ''
return '' + label + '
' + hint + ' · ' + mb(p.size) + ' · .' + esc(String(p.file).split('.').pop()) + '
'
}).join('\n ')
const tags = []
for (const p of plugins) for (const t of (p.tags || [])) if (tags.indexOf(t) < 0) tags.push(t)
const tagChips = tags.map((t) => '').join('\n ')
function render(tpl, map) {
let out = fs.readFileSync(path.join(site, tpl), 'utf8')
for (const k of Object.keys(map)) out = out.split('').join(map[k])
return out
}
let home = render('_home.tpl.html', {
NAV: nav('home'),
FOOTER: footer,
VERSION: V,
PLUGIN_COUNT: String(plugins.length),
FEAT_CARDS: featCards,
PLUGIN_CARDS: plugins.slice(0, 6).map((p) => card(p).replace(/^\s+/, ' ')).join('\n'),
DL_CARDS: dlCards,
})
home = home.replace('/assets/style.css', '/assets/style.css?v=' + STAMP).replace('/assets/painting.jpg', '/assets/painting.jpg?v=' + STAMP)
fs.writeFileSync(path.join(site, 'index.html'), home)
let pluginsPage = render('_plugins.tpl.html', {
NAV: nav('plugins'),
FOOTER: footer,
PLUGIN_COUNT: String(plugins.length),
TAG_CHIPS: tagChips,
PLUGIN_CARDS: plugins.map(card).join('\n'),
})
fs.mkdirSync(path.join(site, 'plugins'), { recursive: true })
pluginsPage = pluginsPage.replace('/assets/style.css', '/assets/style.css?v=' + STAMP)
fs.writeFileSync(path.join(site, 'plugins', 'index.html'), pluginsPage)
fs.writeFileSync(path.join(site, 'plugins-index.json'), JSON.stringify(market, null, 2) + '\n')
console.log('站点已生成:首页 / 插件页(' + plugins.length + ' 个插件,' + tags.length + ' 个标签,版本 v' + V + ')')