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

View File

@@ -16,6 +16,7 @@ const changesUrl = 'https://raw.githubusercontent.com/misode/technical-changes'
const fixesUrl = 'https://raw.githubusercontent.com/misode/mcfixes'
const versionDiffUrl = 'https://mcmeta-diff.misode.workers.dev'
const whatsNewUrl = 'https://whats-new.misode.workers.dev'
const vanillaMcdocUrl = 'https://proxy.misode.workers.dev/mcdoc'
type McmetaTypes = 'summary' | 'data' | 'data-json' | 'assets' | 'assets-json' | 'registries' | 'atlas'
@@ -39,6 +40,15 @@ async function validateCache(version: RefInfo) {
}
}
export async function fetchVanillaMcdoc() {
try {
// TODO: enable refresh
return cachedFetch(vanillaMcdocUrl, { decode: res => res.arrayBuffer(), refresh: false })
} catch (e) {
throw new Error(`Error occured while fetching vanilla-mcdoc: ${message(e)}`)
}
}
export async function fetchRegistries(versionId: VersionId) {
console.debug(`[fetchRegistries] ${versionId}`)
const version = config.versions.find(v => v.id === versionId)!
@@ -51,7 +61,7 @@ export async function fetchRegistries(versionId: VersionId) {
}
return result
} catch (e) {
throw new Error(`Error occurred while fetching registries (2): ${message(e)}`)
throw new Error(`Error occurred while fetching registries: ${message(e)}`)
}
}

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 }
})
}