dsh-sansheng-liubu/scripts/build-combined.mjs

60 lines
2.3 KiB
JavaScript

import { cp, mkdir, readFile, rm, writeFile } from 'node:fs/promises'
import { join } from 'node:path'
const root = process.cwd()
const stage = join(root, '.scratch', 'combined')
const ds = join(root, 'ds-bot')
const ss = join(root, 'sansheng')
await rm(stage, { recursive: true, force: true })
await mkdir(stage, { recursive: true })
for (const item of ['index.js', 'src', 'office.js', 'web-search.js', 'cordis.patch.yml', 'README.md', 'LICENSE', 'NOTICE', 'package.json']) {
await cp(join(ds, item), join(stage, item), { recursive: true })
}
await mkdir(join(stage, 'sansheng'), { recursive: true })
await cp(join(ss, 'index.js'), join(stage, 'sansheng', 'index.js'))
await cp(join(ss, 'src'), join(stage, 'sansheng', 'src'), { recursive: true })
const manifest = JSON.parse(await readFile(join(stage, 'package.json'), 'utf8'))
manifest.name = 'ds-bot-sansheng'
manifest.version = '0.2.8'
manifest.description = 'DS Bot 底座 + 三省六部治理层的单包版本。'
manifest.files = [...new Set([...(manifest.files ?? []), 'sansheng', 'client.js'])]
await writeFile(join(stage, 'package.json'), JSON.stringify(manifest, null, 2) + '\n', 'utf8')
await writeFile(join(stage, 'cordis.patch.yml'), `- insert:
- id: bot
name: 'ds-bot-sansheng'
- id: web-search-auto
name: './web-search.js'
- id: sansheng
name: './sansheng/index.js'
- id: web
config:
searchProvider: auto
fetchProvider: http
`, 'utf8')
const dsClient = (await readFile(join(ds, 'client.js'), 'utf8')).replace("id: 'ds-bot'", "id: 'ds-bot-internal'")
const ssClient = (await readFile(join(ss, 'client.js'), 'utf8')).replace("id: 'dsh-sansheng-sansheng'", "id: 'sansheng-internal'")
await writeFile(join(stage, 'client.js'), `${dsClient}\n${ssClient}\nwindow.__ModuleLoader__.load({
id: 'ds-bot-sansheng',
factory: (require) => {
const ds = require('ds-bot-internal')
const ss = require('sansheng-internal')
const apply = (ctx) => {
const first = ss.apply?.(ctx)
const second = ds.apply?.(ctx)
return () => {
try { second?.() } catch { /* ignore */ }
try { first?.() } catch { /* ignore */ }
}
}
const inject = [...new Set([...(ds.inject ?? []), ...(ss.inject ?? [])])]
return { ...ds, ...ss, apply, ...(inject.length > 0 ? { inject } : {}) }
},
})
`, 'utf8')
console.log(stage)