Local Build and Deploy / deploy (push) Successful in 1m11s
revert style: clean up code with prettier and eslint
103 lines
3.2 KiB
TypeScript
103 lines
3.2 KiB
TypeScript
import { existsSync, promises as fs } from 'node:fs'
|
|
import path from 'node:path'
|
|
import { downloadFile } from '@/lib/services/import/core/download'
|
|
import { CANONICAL_PETS, resolvePetSwfUrl } from '@/lib/services/pet-source'
|
|
import { convertSwfToNitro } from '@/lib/services/swf-to-nitro'
|
|
|
|
export interface PetListItem {
|
|
lib: string
|
|
nitroExists: boolean
|
|
}
|
|
|
|
export interface ImportPetResult {
|
|
ok: boolean
|
|
lib: string
|
|
warnings: string[]
|
|
error?: string
|
|
}
|
|
|
|
export const PET_NITRO_DIR = path.join(
|
|
/*turbopackIgnore: true*/ process.cwd(),
|
|
'public/nitro-assets/bundled/pet',
|
|
)
|
|
export const PET_SWF_DIR = path.join(/*turbopackIgnore: true*/ process.cwd(), 'public/swf/gordon')
|
|
|
|
export async function ensurePetDirs(): Promise<void> {
|
|
await Promise.all(
|
|
[PET_NITRO_DIR, PET_SWF_DIR]
|
|
.filter((dir) => !existsSync(/*turbopackIgnore: true*/ dir))
|
|
.map((dir) => fs.mkdir(/*turbopackIgnore: true*/ dir, { recursive: true })),
|
|
)
|
|
}
|
|
|
|
function nitroPathFor(lib: string): string {
|
|
return path.join(/*turbopackIgnore: true*/ PET_NITRO_DIR, `${lib}.nitro`)
|
|
}
|
|
|
|
export async function importSinglePet(params: {
|
|
lib: string
|
|
onProgress?: (status: string) => void
|
|
}): Promise<ImportPetResult> {
|
|
const { lib, onProgress } = params
|
|
const warnings: string[] = []
|
|
await ensurePetDirs()
|
|
|
|
onProgress?.('downloading')
|
|
const swfPath = path.join(/*turbopackIgnore: true*/ PET_SWF_DIR, `${lib}.swf`)
|
|
let swfUrl: string
|
|
try {
|
|
swfUrl = await resolvePetSwfUrl(lib)
|
|
} catch (err) {
|
|
return {
|
|
ok: false,
|
|
lib,
|
|
warnings,
|
|
error: `Could not resolve SWF URL: ${(err as Error).message}`,
|
|
}
|
|
}
|
|
const dl = await downloadFile(swfUrl, swfPath, { validate: 'swf' })
|
|
if (!dl.ok) return { ok: false, lib, warnings, error: `SWF download failed: ${swfUrl}` }
|
|
|
|
onProgress?.('converting')
|
|
try {
|
|
const swfBuffer = await fs.readFile(/*turbopackIgnore: true*/ swfPath)
|
|
const result = convertSwfToNitro(swfBuffer, lib)
|
|
await fs.writeFile(/*turbopackIgnore: true*/ nitroPathFor(lib), result.bundle)
|
|
if (result.warnings?.length) warnings.push(...result.warnings)
|
|
} catch (err) {
|
|
return {
|
|
ok: false,
|
|
lib,
|
|
warnings,
|
|
error: `SWF→Nitro conversion failed: ${(err as Error).message}`,
|
|
}
|
|
}
|
|
|
|
return { ok: true, lib, warnings }
|
|
}
|
|
|
|
export async function deleteImportedPet(lib: string): Promise<{ deletedFile: boolean }> {
|
|
const p = nitroPathFor(lib)
|
|
if (existsSync(/*turbopackIgnore: true*/ p)) {
|
|
await fs.unlink(/*turbopackIgnore: true*/ p).catch(() => {})
|
|
return { deletedFile: true }
|
|
}
|
|
return { deletedFile: false }
|
|
}
|
|
|
|
export function getPetList(search: string): PetListItem[] {
|
|
const term = search.trim().toLowerCase()
|
|
const list = CANONICAL_PETS.map((lib) => ({
|
|
lib,
|
|
nitroExists: existsSync(/*turbopackIgnore: true*/ nitroPathFor(lib)),
|
|
}))
|
|
return term ? list.filter((p) => p.lib.toLowerCase().includes(term)) : list
|
|
}
|
|
|
|
export function getPetStats(): { total: number; imported: number; missing: number } {
|
|
const imported = CANONICAL_PETS.filter((lib) =>
|
|
existsSync(/*turbopackIgnore: true*/ nitroPathFor(lib)),
|
|
).length
|
|
return { total: CANONICAL_PETS.length, imported, missing: CANONICAL_PETS.length - imported }
|
|
}
|