This commit is contained in:
1 parent
8efd032cc6
commit
df38dccbf1
735 files changed
+128321
-120870
No files matched your search
@@ -5,98 +5,125 @@ import { CANONICAL_PETS, resolvePetSwfUrl } from "@/lib/services/pet-source";
|
||||
import { convertSwfToNitro } from "@/lib/services/swf-to-nitro";
|
||||
|
||||
export interface PetListItem {
|
||||
lib: string;
|
||||
nitroExists: boolean;
|
||||
lib: string;
|
||||
nitroExists: boolean;
|
||||
}
|
||||
|
||||
export interface ImportPetResult {
|
||||
ok: boolean;
|
||||
lib: string;
|
||||
warnings: string[];
|
||||
error?: string;
|
||||
ok: boolean;
|
||||
lib: string;
|
||||
warnings: string[];
|
||||
error?: string;
|
||||
}
|
||||
|
||||
export const PET_NITRO_DIR = path.join(
|
||||
/*turbopackIgnore: true*/ process.cwd(),
|
||||
"public/nitro-assets/bundled/pet",
|
||||
/*turbopackIgnore: true*/ process.cwd(),
|
||||
"public/nitro-assets/bundled/pet",
|
||||
);
|
||||
export const PET_SWF_DIR = path.join(
|
||||
/*turbopackIgnore: true*/ process.cwd(),
|
||||
"public/swf/gordon",
|
||||
);
|
||||
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 })),
|
||||
);
|
||||
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`);
|
||||
return path.join(/*turbopackIgnore: true*/ PET_NITRO_DIR, `${lib}.nitro`);
|
||||
}
|
||||
|
||||
export async function importSinglePet(params: {
|
||||
lib: string;
|
||||
onProgress?: (status: string) => void;
|
||||
lib: string;
|
||||
onProgress?: (status: string) => void;
|
||||
}): Promise<ImportPetResult> {
|
||||
const { lib, onProgress } = params;
|
||||
const warnings: string[] = [];
|
||||
await ensurePetDirs();
|
||||
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?.("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}`,
|
||||
};
|
||||
}
|
||||
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 };
|
||||
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 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;
|
||||
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 };
|
||||
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,
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user