feat: bundle engines

This commit is contained in:
Il Harper 2024-03-07 19:23:11 +08:00
parent 3611c745ec
commit 6c5404e508
No known key found for this signature in database
GPG Key ID: 4B71FCA698E7E8EC
2 changed files with 61 additions and 12 deletions

View File

@ -29,6 +29,7 @@ interface EngineInfo {
filename: string
type: string
path: string
hidden: boolean
}
export const chronocat = async () => {
@ -60,27 +61,29 @@ export const chronocat = async () => {
},
}
const enginesPath = join(baseDir, 'engines')
const engines: EngineInfo[] = []
mkdirSync(enginesPath, {
const externalEnginesPath = join(baseDir, 'engines')
mkdirSync(externalEnginesPath, {
recursive: true,
})
const engines = readdirSync(enginesPath)
readdirSync(externalEnginesPath)
.map((filename) => {
let valid = false
let name = filename
let type = 'js'
if (name.endsWith('.jsc')) {
if (name.endsWith('.engine.jsc')) {
valid = true
name = name.slice(0, name.length - 4)
name = name.slice(0, name.length - 11)
type = 'jsc'
}
if (name.endsWith('.js')) {
if (name.endsWith('.engine.js')) {
valid = true
name = name.slice(0, name.length - 3)
name = name.slice(0, name.length - 10)
}
if (!valid) return undefined
@ -89,7 +92,40 @@ export const chronocat = async () => {
name,
filename,
type,
path: join(enginesPath, filename),
path: join(externalEnginesPath, filename),
hidden: false,
}
})
.filter(
Boolean as unknown as (x: EngineInfo | undefined) => x is EngineInfo,
)
if (!engines.length)
readdirSync(__dirname)
.map((filename) => {
let valid = false
let name = filename
let type = 'js'
if (name.endsWith('.engine.jsc')) {
valid = true
name = name.slice(0, name.length - 11)
type = 'jsc'
}
if (name.endsWith('.engine.js')) {
valid = true
name = name.slice(0, name.length - 10)
}
if (!valid) return undefined
return {
name,
filename,
type,
path: join(__dirname, filename),
hidden: true,
}
})
.filter(
@ -110,7 +146,7 @@ export const chronocat = async () => {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const engine = require(engineInfo.path) as unknown as Engine
l.info(
`使用引擎 ${engine.name} v${engine.version}${styles.grey.open},来自 ${engineInfo.filename}${styles.grey.close}`,
`使用引擎 ${styles.green.open}${engine.name}${styles.green.close} v${engine.version}${engineInfo.hidden ? '' : `${styles.grey.open},来自 ${engineInfo.filename}${styles.grey.close}`}`,
)
engine.apply(ctx)
} catch (e) {

View File

@ -5,12 +5,25 @@ void Promise.all(
['engine-chronocat-api', 'engine-chronocat-event'].map(async (x) => {
const srcPath = resolve(__dirname, `../packages/${x}/lib/index.js`)
const distPath = resolve(__dirname, `../build/dist/${x}`)
const distDir = resolve(__dirname, `../build/dist/${x}`)
await mkdir(distPath, {
await mkdir(distDir, {
recursive: true,
})
await cp(srcPath, join(distPath, `${x}.js`))
const filename = `${x.slice(7)}.engine.js`
const distPath = join(distDir, filename)
await cp(srcPath, distPath)
await cp(
distPath,
resolve(__dirname, `../build/dist/llqqntv0/src/${filename}`),
)
await cp(
distPath,
resolve(__dirname, `../build/dist/llqqntv1/src/${filename}`),
)
}),
)