dsh-think-skin 1.1.0 → 1.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -1,5 +1,7 @@
1
1
  # dsh-think-skin
2
2
 
3
+ ![ThinkSkin 宣传片封面](assets/screenshots/1-cover.png)
4
+
3
5
  **Give the DSH thinking row your own face and your own words.**
4
6
 
5
7
  The row that says `深度求索中,用时 3秒 ···` next to a whale tail is the one thing you look at
@@ -9,6 +11,23 @@ more than the answer. This replaces its icon and its copy, and keeps the timer.
9
11
 
10
12
  ▶ **[宣传片 · B 站](https://www.bilibili.com/video/BV1Layaz2Em7)** (1 分 45 秒,三套皮肤 + 安装全过程)
11
13
 
14
+ ## Screenshots / 截图
15
+
16
+ **设置页** —— 套组卡片、字形(字符转圈)、文字渐变、音乐,都在这一页里。
17
+
18
+ ![设置页](assets/screenshots/2-settings.png)
19
+
20
+ **音乐池** —— 每个套组一个,每张图还能单独配;导入的音频会随套组 zip 一起走。
21
+
22
+ ![通用音乐池](assets/screenshots/6-music.png)
23
+
24
+ **三套皮肤的那一行** —— 图和句分开抽,同一任务跑两次不重样。
25
+
26
+ | 蓝色大肥鱼 | 酒狐 | ClaudeCode |
27
+ |---|---|---|
28
+ | ![蓝色大肥鱼](assets/screenshots/3-fish.png) | ![酒狐](assets/screenshots/4-jjfox.png) | ![ClaudeCode](assets/screenshots/5-claude.png) |
29
+
30
+
12
31
  ---
13
32
 
14
33
  ## What it does
@@ -91,7 +110,30 @@ $DSH_HOME/storages/think_skin/
91
110
  The browser fetches `pack.json` once and each image with `immutable` caching, so the bundle
92
111
  stays around 19 KB no matter how many images a pack holds.
93
112
 
113
+ ### 装完就有(1.1.2)
114
+
115
+ 三个内置套的图**随包一起发**,第一次启动铺进 `storages`,不需要你导入任何东西:
116
+
117
+ | 套 | 图 | 文案 |
118
+ |---|---|---|
119
+ | 蓝色大肥鱼 | 39 张 GIF | 27 条兜底 + 每张自己的 |
120
+ | ClaudeCode | 1 张占位图 | 184 个 -ing 动词 |
121
+ | 酒狐 | 1 张 GIF | 4 条兜底 + 歌词,另带三段《朋友的酒》 |
122
+
123
+ ```
124
+ 素材 assets/compiled-128/蓝色大肥鱼/ · assets/ClaudeCode/ · assets/酒狐/
125
+ 文案 assets/texts/<套名>.json
126
+ 拼装 seed.js(纯函数,可单测)→ index.js 里 seedBuiltinPacks()
127
+ 规则 只补缺的套;你改过、删过的不覆盖
128
+ 体积 首次启动会拷 13.4 MB,之后就只是读
129
+ ```
130
+
131
+ > **1.1.1 的坑**:那个版本只有音乐会被铺进来,图不会 —— 装完看到的是空列表。
132
+ > 1.1.2 修了。测这个不能靠开发机(开发机上早就导入过),要清空 `storages/think_skin`
133
+ > 再重启:`node tools/test-seed.mjs` 就是干这个的。
134
+
94
135
  **导入图片:** 设置 → 思考状态 → 选图片 / 选文件夹。文件会被**复制**进上面的目录,原文件随便处置。
136
+
95
137
  **Import from the CLI:**
96
138
 
97
139
  ```bash
@@ -128,6 +170,17 @@ binary is recorded in [assets/ClaudeCode/FINDINGS.md](assets/ClaudeCode/FINDINGS
128
170
 
129
171
  ---
130
172
 
173
+ ## Security / 安全
174
+
175
+ 插件在 DSH 的 web server 上挂了 8 条路由(读写套组、上传图片与音乐、导出导入)。
176
+ 每条都先过 DSH 自己的访问检查 `ctx.connection.requestRejection` ——
177
+ 它要求请求来自**已认证的活跃 Peer**,跨站的网页过不去。
178
+ 老版本 DSH 里没有这个服务时,退回同源的 `Sec-Fetch-Site` / `Origin` 判断。
179
+
180
+ 导入套组那条 `POST /import` 会去下载用户给的 URL:只允许 http/https,
181
+ 拒绝回环、内网、链路本地、CGNAT、`*.local`,跟随重定向后复查一次,
182
+ 30 秒超时,192 MB 上限。
183
+
131
184
  ## Credits
132
185
 
133
186
  - **Mechanism reference:** [`huanlinoto/dsh-plugin-prts-neuron`](https://github.com/huanlinoto/dsh-plugin-prts-neuron).
Binary file
Binary file
Binary file
Binary file
Binary file
package/index.js CHANGED
@@ -19,6 +19,8 @@ import zlib from 'node:zlib'
19
19
  import os from 'node:os'
20
20
  import path from 'node:path'
21
21
 
22
+ import { BUILTIN_PACKS, planBuiltinSeeds } from './seed.js'
23
+
22
24
  const ROUTE = '/think-skin'
23
25
  const STORE = 'think_skin'
24
26
 
@@ -80,6 +82,38 @@ function originAllowed(get) {
80
82
  return true
81
83
  }
82
84
 
85
+ /**
86
+ * DSH 自己的访问检查:`ctx.connection.requestRejection` 走 Host/Origin 围栏,
87
+ * 并要求请求来自**已认证的活跃 Peer**。返回状态码表示拒绝,undefined 表示放行。
88
+ *
89
+ * 老版本 DSH 没有这个服务时退回上面的同源检查——不会比现在更宽。
90
+ */
91
+ function requestRejection(context, request, get) {
92
+ let connection = null
93
+ try {
94
+ connection = context.connection
95
+ } catch {
96
+ connection = null
97
+ }
98
+ if (connection === undefined || connection === null) {
99
+ try {
100
+ if (typeof context.get === 'function') connection = context.get('connection')
101
+ } catch {
102
+ connection = null
103
+ }
104
+ }
105
+ if (connection !== undefined && connection !== null
106
+ && typeof connection.requestRejection === 'function') {
107
+ try {
108
+ const status = connection.requestRejection(request)
109
+ return status === undefined || status === null ? undefined : status
110
+ } catch {
111
+ return 403
112
+ }
113
+ }
114
+ return originAllowed(get) ? undefined : 403
115
+ }
116
+
83
117
  /** 本机回环、内网、云元数据地址——导入下载器一律不碰。 */
84
118
  function isPrivateHost(hostname) {
85
119
  const host = String(hostname).toLowerCase().replace(/^\[|\]$/g, '')
@@ -404,9 +438,10 @@ export function apply(ctx) {
404
438
  kind: 'prefix',
405
439
  path: ROUTE,
406
440
  async resource(request) {
407
- if (!originAllowed((name) => request.headers.get(name))) {
441
+ const blocked = requestRejection(ctx, request, (name) => request.headers.get(name))
442
+ if (blocked !== undefined) {
408
443
  return new Response('forbidden', {
409
- status: 403,
444
+ status: blocked,
410
445
  headers: { 'content-type': 'text/plain; charset=utf-8' },
411
446
  })
412
447
  }
@@ -460,7 +495,8 @@ export function apply(ctx) {
460
495
  res.writeHead(status, { 'content-type': 'text/plain; charset=utf-8' })
461
496
  res.end(message)
462
497
  }
463
- if (!originAllowed((name) => req.headers[name])) return fail(403, 'forbidden')
498
+ const blocked = requestRejection(ctx, req, (name) => req.headers[name])
499
+ if (blocked !== undefined) return fail(blocked, 'forbidden')
464
500
  if (req.method !== 'POST') return fail(405, 'method not allowed')
465
501
  try {
466
502
  const patch = JSON.parse((await readBody(req, MAX_BODY)).toString('utf8'))
@@ -566,7 +602,8 @@ export function apply(ctx) {
566
602
  res.writeHead(status, { 'content-type': 'text/plain; charset=utf-8' })
567
603
  res.end(message)
568
604
  }
569
- if (!originAllowed((name) => req.headers[name])) return fail(403, 'forbidden')
605
+ const blocked = requestRejection(ctx, req, (name) => req.headers[name])
606
+ if (blocked !== undefined) return fail(blocked, 'forbidden')
570
607
  if (req.method !== 'POST') return fail(405, 'method not allowed')
571
608
  try {
572
609
  const body = JSON.parse((await readBody(req, MAX_BODY)).toString('utf8'))
@@ -630,7 +667,8 @@ export function apply(ctx) {
630
667
  res.writeHead(status, { 'content-type': 'text/plain; charset=utf-8' })
631
668
  res.end(message)
632
669
  }
633
- if (!originAllowed((name) => req.headers[name])) return fail(403, 'forbidden')
670
+ const blocked = requestRejection(ctx, req, (name) => req.headers[name])
671
+ if (blocked !== undefined) return fail(blocked, 'forbidden')
634
672
  if (req.method !== 'POST') return fail(405, 'method not allowed')
635
673
  try {
636
674
  const url = new URL(req.url, 'http://local')
@@ -730,7 +768,8 @@ export function apply(ctx) {
730
768
  res.writeHead(status, { 'content-type': 'text/plain; charset=utf-8' })
731
769
  res.end(message)
732
770
  }
733
- if (!originAllowed((name) => req.headers[name])) return fail(403, 'forbidden')
771
+ const blocked = requestRejection(ctx, req, (name) => req.headers[name])
772
+ if (blocked !== undefined) return fail(blocked, 'forbidden')
734
773
  if (req.method !== 'POST') return fail(405, 'method not allowed')
735
774
  try {
736
775
  const url = new URL(req.url, 'http://local')
@@ -805,7 +844,8 @@ export function apply(ctx) {
805
844
  res.writeHead(status, { 'content-type': 'text/plain; charset=utf-8' })
806
845
  res.end(message)
807
846
  }
808
- if (!originAllowed((name) => req.headers[name])) return fail(403, 'forbidden')
847
+ const blocked = requestRejection(ctx, req, (name) => req.headers[name])
848
+ if (blocked !== undefined) return fail(blocked, 'forbidden')
809
849
  if (req.method !== 'POST') return fail(405, 'method not allowed')
810
850
  try {
811
851
  const body = JSON.parse((await readBody(req, MAX_BODY)).toString('utf8'))
@@ -833,6 +873,62 @@ export function apply(ctx) {
833
873
  })
834
874
  ctx.effect(() => disposeMusicDel)
835
875
 
876
+ /**
877
+ * 把内置套组(蓝色大肥鱼 / ClaudeCode / 酒狐)铺进 storages。
878
+ *
879
+ * 之前只有音乐会被铺进来,图没有 —— 所以别人装完看到的是空列表。
880
+ * 素材在 assets/ 下,文案在 assets/texts/<套名>.json,
881
+ * 拼装逻辑在 seed.js(纯函数,能直接用 node 测,不用起 DSH)。
882
+ * 只补缺的套,用户改过的不覆盖。
883
+ */
884
+ async function seedBuiltinPacks() {
885
+ let touched = false
886
+ try {
887
+ const document = await load()
888
+ const material = {}
889
+ for (const entry of BUILTIN_PACKS) {
890
+ for (const dir of entry.dirs) {
891
+ let fileNames = []
892
+ try {
893
+ const names = await fs.readdir(new URL('./assets/' + dir + '/', import.meta.url))
894
+ fileNames = names.filter((n) => /\.(gif|png|webp|jpe?g|avif)$/i.test(n))
895
+ } catch {
896
+ continue
897
+ }
898
+ if (fileNames.length === 0) continue
899
+ let texts = {}
900
+ try {
901
+ texts = JSON.parse(
902
+ await fs.readFile(
903
+ new URL('./assets/texts/' + entry.id + '.json', import.meta.url),
904
+ 'utf8',
905
+ ),
906
+ )
907
+ } catch {
908
+ /* 没有文案表就退化成「按文件名当名字」 */
909
+ }
910
+ material[entry.id] = { dir, fileNames, texts }
911
+ break
912
+ }
913
+ }
914
+
915
+ for (const item of planBuiltinSeeds(document.packs.map((p) => p.id), material)) {
916
+ const dir = path.join(root, 'images', item.id)
917
+ await fs.mkdir(dir, { recursive: true })
918
+ for (const copy of item.copies) {
919
+ const bytes = await fs.readFile(new URL('./assets/' + copy.from, import.meta.url))
920
+ await fs.writeFile(path.join(dir, copy.to.slice(item.id.length + 1)), bytes)
921
+ }
922
+ document.packs.push(item.pack)
923
+ if (!document.active) document.active = item.id
924
+ touched = true
925
+ }
926
+ if (touched) await save(document)
927
+ } catch {
928
+ /* 内置套组是送分项,铺不进去也不该挡住插件启动 */
929
+ }
930
+ }
931
+
836
932
  /**
837
933
  * 把内置套组自带的音乐铺进 storages。
838
934
  *
@@ -876,7 +972,10 @@ export function apply(ctx) {
876
972
  }
877
973
  }
878
974
 
879
- seedBuiltinMusic()
975
+ // 先铺套组(音乐只给「已存在的套」铺,所以顺序不能反)
976
+ seedBuiltinPacks()
977
+ .then(seedBuiltinMusic)
978
+ .catch(() => {})
880
979
 
881
980
  /**
882
981
  * Export one pack as a self-contained JSON document.
@@ -892,7 +991,8 @@ export function apply(ctx) {
892
991
  res.writeHead(status, { 'content-type': 'text/plain; charset=utf-8' })
893
992
  res.end(message)
894
993
  }
895
- if (!originAllowed((name) => req.headers[name])) return fail(403, 'forbidden')
994
+ const blocked = requestRejection(ctx, req, (name) => req.headers[name])
995
+ if (blocked !== undefined) return fail(blocked, 'forbidden')
896
996
  if (req.method !== 'POST') return fail(405, 'method not allowed')
897
997
  try {
898
998
  const body = JSON.parse((await readBody(req, MAX_BODY)).toString('utf8'))
@@ -1022,7 +1122,8 @@ export function apply(ctx) {
1022
1122
  res.writeHead(status, { 'content-type': 'text/plain; charset=utf-8' })
1023
1123
  res.end(message)
1024
1124
  }
1025
- if (!originAllowed((name) => req.headers[name])) return fail(403, 'forbidden')
1125
+ const blocked = requestRejection(ctx, req, (name) => req.headers[name])
1126
+ if (blocked !== undefined) return fail(blocked, 'forbidden')
1026
1127
  if (req.method !== 'POST') return fail(405, 'method not allowed')
1027
1128
  let dir = null
1028
1129
  try {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "dsh-think-skin",
3
- "version": "1.1.0",
3
+ "version": "1.1.2",
4
4
  "description": "把 DSH 的思考状态行换成你自己的图、自己的话:每张图带自己的文案池,逐字打出、高光扫过、渐变可调。支持套组导入导出与字符转圈。",
5
5
  "type": "module",
6
6
  "license": "MIT",
@@ -42,8 +42,12 @@
42
42
  "tools/compile-pack.py",
43
43
  "assets/texts",
44
44
  "assets/music",
45
+ "assets/screenshots",
46
+ "screenshots.json",
45
47
  "tools/export-texts.py",
46
- "locale"
48
+ "locale",
49
+ "seed.js",
50
+ "tools/test-seed.mjs"
47
51
  ],
48
52
  "dsh": {
49
53
  "bundle": {
@@ -0,0 +1,8 @@
1
+ [
2
+ "assets/screenshots/1-cover.png",
3
+ "assets/screenshots/2-settings.png",
4
+ "assets/screenshots/3-fish.png",
5
+ "assets/screenshots/4-jjfox.png",
6
+ "assets/screenshots/5-claude.png",
7
+ "assets/screenshots/6-music.png"
8
+ ]
package/seed.js ADDED
@@ -0,0 +1,126 @@
1
+ /**
2
+ * 内置套组的「出厂内容」—— 装完就该有的那三个套。
3
+ *
4
+ * 之前只有 seedBuiltinMusic:音乐铺进去了,图没铺,所以别人装完看到的是空列表。
5
+ * 这里把「一个套由什么组成」写成纯数据 + 纯函数,index.js 只负责读盘和拷文件,
6
+ * 逻辑本身能直接用 node 跑测试(不用起 DSH)。
7
+ *
8
+ * 约定:
9
+ * · 图片的 file 字段是「<套名>/fNNN.<ext>」,相对 storages/think_skin/images/
10
+ * · texts 来自 assets/texts/<套名>.json,按图片名(文件名词干)对齐
11
+ * · 只补缺的套,不覆盖用户已经改过的
12
+ */
13
+
14
+ /** 出厂套组的清单。dirs 是素材可能所在的目录(按顺序找,找到就用)。 */
15
+ export const BUILTIN_PACKS = [
16
+ { id: '蓝色大肥鱼', dirs: ['compiled-128/蓝色大肥鱼', '蓝色大肥鱼'] },
17
+ { id: 'ClaudeCode', dirs: ['ClaudeCode'] },
18
+ { id: '酒狐', dirs: ['酒狐'] },
19
+ ]
20
+
21
+ /** 每个套的配色/转圈样式。图能扫出来,但这些是设计决定,写死在这里。 */
22
+ const APPEARANCE = {
23
+ 蓝色大肥鱼: { gradient: ['#a8cdff', '#5b8dff'], mode: 'sweep', speed: 3 },
24
+ ClaudeCode: {
25
+ gradient: ['#f0a882', '#d97757'],
26
+ mode: 'sweep',
27
+ speed: 3,
28
+ spinner: { mode: 'glyph', interval: 165, color: '#eb720f', period: 2000 },
29
+ },
30
+ 酒狐: { gradient: ['#ffd6a5', '#ff9e6d'] },
31
+ }
32
+
33
+ const IMAGE_EXT = new Set(['.gif', '.png', '.webp', '.jpg', '.jpeg', '.avif'])
34
+
35
+ /** 文件名去掉扩展名 —— texts 里就是用这个名字当键。 */
36
+ export function stem(fileName) {
37
+ return String(fileName).replace(/\.[^.]+$/, '')
38
+ }
39
+
40
+ /**
41
+ * 把「素材目录里有哪些文件」+「texts 里的文案」拼成一个套的完整数据。
42
+ *
43
+ * @param {string} packId 套名,也是目录名
44
+ * @param {{ fallbackTexts?: string[], images?: Record<string, {texts?: string[], useFallback?: boolean}> }} texts
45
+ * @param {string[]} fileNames 素材目录下的文件名列表(只留图片)
46
+ * @param {string} [assetDir] 素材目录(相对 assets/),用于生成拷贝来源
47
+ * @returns {{ pack: object, copies: {from: string, to: string}[] }}
48
+ */
49
+ export function buildBuiltinPack(packId, texts, fileNames, assetDir = packId) {
50
+ const imagesByName = new Map()
51
+ for (const name of texts?.images && Object.keys(texts.images).length > 0
52
+ ? Object.keys(texts.images)
53
+ : []) {
54
+ imagesByName.set(name, texts.images[name] || {})
55
+ }
56
+
57
+ const usable = (fileNames || [])
58
+ .filter((n) => IMAGE_EXT.has(String(n).slice(String(n).lastIndexOf('.')).toLowerCase()))
59
+ // 文案表里有名字的优先,没名字的按文件名排序跟在后面(别丢图)
60
+ .sort((a, b) => {
61
+ const ia = imagesByName.has(stem(a)) ? 0 : 1
62
+ const ib = imagesByName.has(stem(b)) ? 0 : 1
63
+ return ia - ib || String(a).localeCompare(String(b), 'zh')
64
+ })
65
+
66
+ const images = []
67
+ const copies = []
68
+ let index = 0
69
+ for (const fileName of usable) {
70
+ const name = stem(fileName)
71
+ const meta = imagesByName.get(name) || {}
72
+ const ext = String(fileName).slice(String(fileName).lastIndexOf('.')).toLowerCase()
73
+ index += 1
74
+ const id = 'f' + String(index).padStart(3, '0')
75
+ const stored = id + ext
76
+ images.push({
77
+ id,
78
+ file: packId + '/' + stored,
79
+ name,
80
+ texts: Array.isArray(meta.texts) ? meta.texts : [],
81
+ useFallback: meta.useFallback !== false,
82
+ music: [],
83
+ useFallbackMusic: true,
84
+ })
85
+ copies.push({ from: assetDir + '/' + fileName, to: packId + '/' + stored })
86
+ }
87
+
88
+ const appearance = APPEARANCE[packId] || { gradient: ['#a8cdff', '#5b8dff'] }
89
+ const pack = {
90
+ id: packId,
91
+ name: packId,
92
+ cover: images.length > 0 ? images[0].file : null,
93
+ fallbackTexts:
94
+ Array.isArray(texts?.fallbackTexts) && texts.fallbackTexts.length > 0
95
+ ? texts.fallbackTexts
96
+ : ['正在思考'],
97
+ appearance: { ...appearance },
98
+ images,
99
+ builtin: true,
100
+ }
101
+ if (appearance.spinner !== undefined) pack.spinner = { ...appearance.spinner }
102
+
103
+ return { pack, copies }
104
+ }
105
+
106
+ /**
107
+ * 决定这次要补哪几个套。
108
+ *
109
+ * @param {string[]} existingIds 文档里已有的套名
110
+ * @param {Record<string, {dir?: string, fileNames: string[], texts: object}>} material 每个套能用的素材
111
+ * @returns {{ id: string, pack: object, copies: {from: string, to: string}[] }[]}
112
+ */
113
+ export function planBuiltinSeeds(existingIds, material) {
114
+ const have = new Set(existingIds || [])
115
+ const out = []
116
+ for (const entry of BUILTIN_PACKS) {
117
+ const id = entry.id
118
+ if (have.has(id)) continue
119
+ const m = material[id]
120
+ if (m === undefined || !Array.isArray(m.fileNames) || m.fileNames.length === 0) continue
121
+ const { pack, copies } = buildBuiltinPack(id, m.texts || {}, m.fileNames, m.dir || id)
122
+ if (pack.images.length === 0) continue
123
+ out.push({ id, pack, copies })
124
+ }
125
+ return out
126
+ }
@@ -0,0 +1,124 @@
1
+ /**
2
+ * 跑一遍「从零安装」时内置套组的铺装流程,不依赖 DSH。
3
+ *
4
+ * node tools/test-seed.mjs → 用临时目录做 storage,跑完打印结果
5
+ * node tools/test-seed.mjs --keep → 保留临时目录,方便自己进去看
6
+ *
7
+ * 覆盖的是 index.js 里 seedBuiltinPacks 的同一套逻辑(素材扫描 → planBuiltinSeeds
8
+ * → 拷文件),差别只在 storage 根是临时的。
9
+ */
10
+
11
+ import fs from 'node:fs/promises'
12
+ import os from 'node:os'
13
+ import path from 'node:path'
14
+ import { fileURLToPath } from 'node:url'
15
+
16
+ import { BUILTIN_PACKS, planBuiltinSeeds } from '../seed.js'
17
+
18
+ const here = path.dirname(fileURLToPath(import.meta.url))
19
+ const assetsRoot = path.join(here, '..', 'assets')
20
+ const keep = process.argv.includes('--keep')
21
+
22
+ const isImage = (n) => /\.(gif|png|webp|jpe?g|avif)$/i.test(n)
23
+
24
+ async function readMaterial() {
25
+ const material = {}
26
+ for (const entry of BUILTIN_PACKS) {
27
+ for (const dir of entry.dirs) {
28
+ let fileNames = []
29
+ try {
30
+ fileNames = (await fs.readdir(path.join(assetsRoot, dir))).filter(isImage)
31
+ } catch {
32
+ continue
33
+ }
34
+ if (fileNames.length === 0) continue
35
+ let texts = {}
36
+ try {
37
+ texts = JSON.parse(await fs.readFile(path.join(assetsRoot, 'texts', entry.id + '.json'), 'utf8'))
38
+ } catch {
39
+ /* 没有文案表也能跑 */
40
+ }
41
+ material[entry.id] = { dir, fileNames, texts }
42
+ break
43
+ }
44
+ }
45
+ return material
46
+ }
47
+
48
+ async function main() {
49
+ const storage = await fs.mkdtemp(path.join(os.tmpdir(), 'ts-seed-'))
50
+ const material = await readMaterial()
51
+
52
+ console.log('素材扫描:')
53
+ for (const [id, m] of Object.entries(material)) {
54
+ console.log(' %s ← assets/%s (%d 张图, 文案 %d 条)', id, m.dir, m.fileNames.length,
55
+ Array.isArray(m.texts?.fallbackTexts) ? m.texts.fallbackTexts.length : 0)
56
+ }
57
+
58
+ // 空文档 = 全新安装的样子
59
+ const document = { packs: [], active: null, settings: { musicEnabled: true, musicVolume: 42, musicMode: 'random' } }
60
+ const plan = planBuiltinSeeds(document.packs.map((p) => p.id), material)
61
+
62
+ for (const item of plan) {
63
+ const dir = path.join(storage, 'images', item.id)
64
+ await fs.mkdir(dir, { recursive: true })
65
+ for (const copy of item.copies) {
66
+ const bytes = await fs.readFile(path.join(assetsRoot, copy.from))
67
+ await fs.writeFile(path.join(dir, copy.to.slice(item.id.length + 1)), bytes)
68
+ }
69
+ document.packs.push(item.pack)
70
+ if (!document.active) document.active = item.id
71
+ }
72
+ await fs.writeFile(path.join(storage, 'pack.json'), JSON.stringify(document, null, 2), 'utf8')
73
+
74
+ // ── 校验 ────────────────────────────────────────────────────────────
75
+ const problems = []
76
+ const onDisk = JSON.parse(await fs.readFile(path.join(storage, 'pack.json'), 'utf8'))
77
+ for (const pack of onDisk.packs) {
78
+ if (!pack.builtin) problems.push(pack.id + ': 没标 builtin')
79
+ if (!pack.cover) problems.push(pack.id + ': 没有 cover')
80
+ if (pack.images.length === 0) problems.push(pack.id + ': 一张图都没有')
81
+ for (const image of pack.images) {
82
+ const p = path.join(storage, 'images', image.file)
83
+ try {
84
+ const st = await fs.stat(p)
85
+ if (st.size === 0) problems.push(pack.id + '/' + image.id + ': 文件是空的')
86
+ } catch {
87
+ problems.push(pack.id + '/' + image.id + ': 文件不存在 ' + image.file)
88
+ }
89
+ }
90
+ }
91
+
92
+ console.log('\n铺装结果:')
93
+ for (const pack of onDisk.packs) {
94
+ const stats = await Promise.all(pack.images.map((i) => fs.stat(path.join(storage, 'images', i.file))))
95
+ const bytes = stats.reduce((sum, s) => sum + s.size, 0)
96
+ const line = ' ' + pack.id.padEnd(10) +
97
+ ' images=' + String(pack.images.length).padEnd(4) +
98
+ ' cover=' + String(pack.cover).padEnd(26) +
99
+ ' fallback=' + String(pack.fallbackTexts.length).padEnd(4) +
100
+ Math.round(bytes / 1024) + ' KB'
101
+ console.log(line)
102
+ }
103
+ console.log(' active =', onDisk.active)
104
+
105
+ const withTexts = onDisk.packs.flatMap((p) => p.images).filter((i) => i.texts.length > 0).length
106
+ const totalImages = onDisk.packs.flatMap((p) => p.images).length
107
+ console.log(' 有独立文案的图: %d / %d(其余走 fallback)', withTexts, totalImages)
108
+
109
+ if (problems.length > 0) {
110
+ console.log('\n❌ 问题:')
111
+ for (const p of problems.slice(0, 12)) console.log(' ' + p)
112
+ process.exitCode = 1
113
+ } else {
114
+ console.log('\n✅ 三个内置套全部铺好,文件都在、非空、cover 有效')
115
+ }
116
+
117
+ if (keep) console.log('\n临时目录: ' + storage)
118
+ else await fs.rm(storage, { recursive: true, force: true })
119
+ }
120
+
121
+ main().catch((error) => {
122
+ console.error(error)
123
+ process.exitCode = 1
124
+ })