Initialize spyglass project and load vanilla-mcdoc

This commit is contained in:
Misode
2024-10-15 07:24:12 +02:00
parent ccdcf9e7e3
commit 60aab0c6b9
7 changed files with 943 additions and 78 deletions
+63
View File
@@ -0,0 +1,63 @@
import * as core from '@spyglassmc/core'
import { BrowserExternals } from '@spyglassmc/core/lib/browser.js'
import * as mcdoc from '@spyglassmc/mcdoc'
import * as zip from '@zip.js/zip.js'
import { fetchVanillaMcdoc } from './index.js'
const externals: core.Externals = {
...BrowserExternals,
archive: {
...BrowserExternals.archive,
async decompressBall(buffer, { stripLevel } = {}) {
const reader = new zip.ZipReader(new zip.BlobReader(new Blob([buffer])))
const entries = await reader.getEntries()
return await Promise.all(entries.map(async e => {
const data = await e.getData?.(new zip.Uint8ArrayWriter())
const path = stripLevel === 1 ? e.filename.substring(e.filename.indexOf('/') + 1) : e.filename
const type = e.directory ? 'dir' : 'file'
return { data, path, mtime: '', type, mode: 0 }
}))
},
},
}
export async function setupSpyglass() {
const logger: core.Logger = console
const profilers = new core.ProfilerFactory(logger, [
'project#init',
'project#ready',
'misode#setup',
])
const profiler = profilers.get('misode#setup')
const service = new core.Service({
logger,
profilers,
project: {
cacheRoot: 'file:cache/',
projectRoots: ['file:project/'],
externals: externals,
defaultConfig: core.ConfigService.merge(core.VanillaConfig, {
env: { dependencies: ['@vanilla-mcdoc'] },
}),
initializers: [mcdoc.initialize, initialize],
},
})
await service.project.ready()
profiler.task('Project ready')
await service.project.cacheService.save()
profiler.task('Save cache')
profiler.finalize()
service.logger.info(service.project.symbols.global)
}
const initialize: core.ProjectInitializer = async (ctx) => {
const { meta, externals, cacheRoot } = ctx
meta.registerDependencyProvider('@vanilla-mcdoc', async () => {
const uri: string = new core.Uri('downloads/vanilla-mcdoc.tar.gz', cacheRoot).toString()
const buffer = await fetchVanillaMcdoc()
await core.fileUtil.writeFile(externals, uri, new Uint8Array(buffer))
return { info: { startDepth: 1 }, uri }
})
}