Skip to content

Commit

Permalink
feat: bundle engines
Browse files Browse the repository at this point in the history
  • Loading branch information
ilharp committed Mar 7, 2024
1 parent 3611c74 commit 6c5404e
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 12 deletions.
54 changes: 45 additions & 9 deletions packages/shell/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ interface EngineInfo {
filename: string
type: string
path: string
hidden: boolean
}

export const chronocat = async () => {
Expand Down Expand Up @@ -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
Expand All @@ -89,13 +92,46 @@ 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(
Boolean as unknown as (x: EngineInfo | undefined) => x is EngineInfo,
)

if (!engines.length)
l.warn('没有找到任何引擎。Chronocat 服务仍将启动。', { code: 2156 })

Expand All @@ -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) {
Expand Down
19 changes: 16 additions & 3 deletions scripts/packengine.cts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`),
)
}),
)

0 comments on commit 6c5404e

Please sign in to comment.