From 623edd7959092bd343c4cc0b2e23eb96d68c40f2 Mon Sep 17 00:00:00 2001 From: JohnsonRan Date: Wed, 16 Jul 2025 15:09:04 +0800 Subject: [PATCH] feat: generate PWA site name dynamically Signed-off-by: JohnsonRan --- package.json | 9 +++--- public/manifest.json | 33 ------------------- scripts/generate-manifest.js | 63 ++++++++++++++++++++++++++++++++++++ 3 files changed, 68 insertions(+), 37 deletions(-) delete mode 100644 public/manifest.json create mode 100644 scripts/generate-manifest.js diff --git a/package.json b/package.json index 6a31d5f..03d919b 100644 --- a/package.json +++ b/package.json @@ -3,8 +3,8 @@ "version": "0.1.0", "private": true, "scripts": { - "dev": "pnpm gen:runtime && next dev -H 0.0.0.0", - "build": "pnpm gen:runtime && next build", + "dev": "pnpm gen:runtime && pnpm gen:manifest && next dev -H 0.0.0.0", + "build": "pnpm gen:runtime && pnpm gen:manifest && next build", "start": "next start", "lint": "next lint", "lint:fix": "eslint src --fix && pnpm format", @@ -15,9 +15,10 @@ "format": "prettier -w .", "format:check": "prettier -c .", "gen:runtime": "node scripts/convert-config.js", + "gen:manifest": "node scripts/generate-manifest.js", "postbuild": "echo 'Build completed - sitemap generation disabled'", "prepare": "husky install", - "pages:build": "pnpm gen:runtime && next build && npx @cloudflare/next-on-pages --experimental-minify" + "pages:build": "pnpm gen:runtime && pnpm gen:manifest && next build && npx @cloudflare/next-on-pages --experimental-minify" }, "dependencies": { "@cloudflare/next-on-pages": "^1.13.12", @@ -87,4 +88,4 @@ ] }, "packageManager": "pnpm@10.12.4+sha512.5ea8b0deed94ed68691c9bad4c955492705c5eeb8a87ef86bc62c74a26b037b08ff9570f108b2e4dbd1dd1a9186fea925e527f141c648e85af45631074680184" -} +} \ No newline at end of file diff --git a/public/manifest.json b/public/manifest.json deleted file mode 100644 index c1f9fed..0000000 --- a/public/manifest.json +++ /dev/null @@ -1,33 +0,0 @@ -{ - "name": "MoonTV", - "short_name": "MoonTV", - "description": "影视聚合", - "start_url": "/", - "scope": "/", - "display": "standalone", - "background_color": "#000000", - "apple-mobile-web-app-capable": "yes", - "apple-mobile-web-app-status-bar-style": "black", - "icons": [ - { - "src": "/icons/icon-192x192.png", - "sizes": "192x192", - "type": "image/png" - }, - { - "src": "/icons/icon-256x256.png", - "sizes": "256x256", - "type": "image/png" - }, - { - "src": "/icons/icon-384x384.png", - "sizes": "384x384", - "type": "image/png" - }, - { - "src": "/icons/icon-512x512.png", - "sizes": "512x512", - "type": "image/png" - } - ] -} diff --git a/scripts/generate-manifest.js b/scripts/generate-manifest.js new file mode 100644 index 0000000..8d6db2b --- /dev/null +++ b/scripts/generate-manifest.js @@ -0,0 +1,63 @@ +#!/usr/bin/env node +/* eslint-disable */ +// 根据 SITE_NAME 动态生成 manifest.json + +const fs = require('fs'); +const path = require('path'); + +// 获取项目根目录 +const projectRoot = path.resolve(__dirname, '..'); +const publicDir = path.join(projectRoot, 'public'); +const manifestPath = path.join(publicDir, 'manifest.json'); + +// 从环境变量获取站点名称 +const siteName = process.env.SITE_NAME || 'MoonTV'; + +// manifest.json 模板 +const manifestTemplate = { + "name": siteName, + "short_name": siteName, + "description": "影视聚合", + "start_url": "/", + "scope": "/", + "display": "standalone", + "background_color": "#000000", + "apple-mobile-web-app-capable": "yes", + "apple-mobile-web-app-status-bar-style": "black", + "icons": [ + { + "src": "/icons/icon-192x192.png", + "sizes": "192x192", + "type": "image/png" + }, + { + "src": "/icons/icon-256x256.png", + "sizes": "256x256", + "type": "image/png" + }, + { + "src": "/icons/icon-384x384.png", + "sizes": "384x384", + "type": "image/png" + }, + { + "src": "/icons/icon-512x512.png", + "sizes": "512x512", + "type": "image/png" + } + ] +}; + +try { + // 确保 public 目录存在 + if (!fs.existsSync(publicDir)) { + fs.mkdirSync(publicDir, { recursive: true }); + } + + // 写入 manifest.json + fs.writeFileSync(manifestPath, JSON.stringify(manifestTemplate, null, 2)); + console.log(`✅ Generated manifest.json with site name: ${siteName}`); +} catch (error) { + console.error('❌ Error generating manifest.json:', error); + process.exit(1); +}