|
| 1 | +/** |
| 2 | + * 构建 homepage、文档、playground-web,合并到仓库根目录 _site/,供 GitHub Pages 上传。。 |
| 3 | + * |
| 4 | + * 本地调试: |
| 5 | + * export ROOT_BASE=/ VITE_GENUI_DOCS_BASE=/docs VITE_GENUI_PLAYGROUND_HREF=/playground && node scripts/build-github-pages.mjs |
| 6 | + */ |
| 7 | +import { spawnSync } from 'node:child_process' |
| 8 | +import { copyFile, cp, rm } from 'node:fs/promises' |
| 9 | +import path from 'node:path' |
| 10 | +import { fileURLToPath } from 'node:url' |
| 11 | + |
| 12 | +const __dirname = path.dirname(fileURLToPath(import.meta.url)) |
| 13 | +const ROOT = path.resolve(__dirname, '..') |
| 14 | + |
| 15 | + |
| 16 | +function withTrailingSlash(s) { |
| 17 | + return s.endsWith('/') ? s : `${s}/` |
| 18 | +} |
| 19 | + |
| 20 | +function run(cmd, args, options = {}) { |
| 21 | + const r = spawnSync(cmd, args, { |
| 22 | + cwd: ROOT, |
| 23 | + stdio: 'inherit', |
| 24 | + env: process.env, |
| 25 | + ...options, |
| 26 | + }) |
| 27 | + if (r.error) throw r.error |
| 28 | + if (r.status !== 0) process.exit(r.status ?? 1) |
| 29 | +} |
| 30 | + |
| 31 | +function runPnpm(args, options = {}) { |
| 32 | + run('pnpm', args, options) |
| 33 | +} |
| 34 | + |
| 35 | +async function buildAllPages() { |
| 36 | + const rootBase = process.env.ROOT_BASE || '/genui-sdk' |
| 37 | + const siteBase = withTrailingSlash(`${rootBase}`) |
| 38 | + const playgroundBase = withTrailingSlash(`${siteBase}playground`) |
| 39 | + const docsBase = withTrailingSlash(`${siteBase}docs`) |
| 40 | + |
| 41 | + console.log(`site base: ${siteBase}`) |
| 42 | + console.log(`playground base: ${playgroundBase}`) |
| 43 | + console.log(`docs base: ${docsBase}`) |
| 44 | + |
| 45 | + |
| 46 | + console.log('>>> prebuild') |
| 47 | + runPnpm(['run', 'prebuild:playground']) |
| 48 | + |
| 49 | + console.log('>>> build:playground-web') |
| 50 | + runPnpm(['exec', 'vite', 'build', `--base=${playgroundBase}`], { |
| 51 | + cwd: path.join(ROOT, 'sites/playground/web'), |
| 52 | + }) |
| 53 | + |
| 54 | + console.log('>>> build:docs') |
| 55 | + runPnpm(['exec', 'pnpm', 'build', `--base=${docsBase}`], { |
| 56 | + cwd: path.join(ROOT, 'docs'), |
| 57 | + }) |
| 58 | + |
| 59 | + console.log('>>> build:homepage') |
| 60 | + runPnpm(['exec', 'pnpm', 'build:web', `--base=${siteBase}`], { |
| 61 | + cwd: path.join(ROOT, 'sites/homepage/web'), |
| 62 | + }) |
| 63 | + |
| 64 | + const pgDist = path.join(ROOT, 'sites/playground/web/dist') |
| 65 | + await copyFile(path.join(pgDist, 'index.html'), path.join(pgDist, '404.html')) |
| 66 | + |
| 67 | + console.log('>>> assemble _site') |
| 68 | + const out = path.join(ROOT, '_site') |
| 69 | + await rm(out, { recursive: true, force: true }) |
| 70 | + // 目标不存在时 cp 会把 dist 整棵拷成 _site,等价于 bash 的 dist/. → _site/ |
| 71 | + await cp(path.join(ROOT, 'sites/homepage/web/dist'), out, { recursive: true }) |
| 72 | + await cp(path.join(ROOT, 'docs/.vitepress/dist'), path.join(out, 'docs'), { recursive: true }) |
| 73 | + await cp(path.join(ROOT, 'sites/playground/web/dist'), path.join(out, 'playground'), { |
| 74 | + recursive: true, |
| 75 | + }) |
| 76 | + |
| 77 | + console.log(`Done. Output: ${out}`) |
| 78 | +} |
| 79 | + |
| 80 | +buildAllPages().catch((err) => { |
| 81 | + console.error(err) |
| 82 | + process.exit(1) |
| 83 | +}) |
0 commit comments