Files
probably-rendering-one-more…/scripts/import-character-packs.mjs
T
codex-bot 72623b75f7
PROMPT Docker CI and deployment / verify-and-deploy (push) Successful in 13m3s
Add street people skill API and two transparent character packs
2026-10-23 01:29:47 +08:00

30 lines
2.2 KiB
JavaScript

import {readFile,readdir} from 'node:fs/promises';
import {fileURLToPath} from 'node:url';
import {join,dirname} from 'node:path';
const root=join(dirname(fileURLToPath(import.meta.url)),'..','seed','characters');
const base=process.env.PROMPT_IMPORT_BASE_URL||'http://127.0.0.1:3000';
const packs=JSON.parse(await readFile(join(root,'packs.json'),'utf8'));
async function verify(pack){
const response=await fetch(`${base}/api/feed?section=images&q=${encodeURIComponent(pack.title)}`);
if(!response.ok)throw new Error(`Cannot verify feed: ${response.status}`);
const post=(await response.json()).posts.find(p=>p.slug===pack.slug);
const images=post?.assets.filter(a=>a.kind==='image');
if(images?.length!==pack.count)throw new Error(`Wrong image count for ${pack.slug}`);
for(const image of images){const media=await fetch(`${base}/api/media/${image.id}`,{method:'HEAD'});if(!media.ok||media.headers.get('content-type')!=='image/png')throw new Error(`Missing PNG ${image.id}`);}
console.log(`Verified: /p/${pack.slug} (${images.length} stored PNGs)`);
}
for(const pack of packs){
const existing=await fetch(`${base}/p/${pack.slug}`);
if(existing.ok){console.log(`Already imported: /p/${pack.slug}`);await verify(pack);continue;}
if(existing.status!==404)throw new Error(`Cannot check pack ${pack.slug}: ${existing.status}`);
const files=(await readdir(join(root,pack.directory))).filter(n=>n.endsWith('.png')).sort();
if(files.length!==pack.count)throw new Error(`Expected ${pack.count} PNGs for ${pack.slug}, got ${files.length}`);
const data=new FormData();data.set('title',pack.title);data.set('slug',pack.slug);data.set('description',pack.description);
for(const file of files)data.append('files',new Blob([await readFile(join(root,pack.directory,file))],{type:'image/png'}),file);
const response=await fetch(`${base}/api/skills/street-people`,{method:'POST',body:data});
if(response.status!==201&&response.status!==409)throw new Error(`Import ${pack.slug}: ${response.status} ${await response.text()}`);
if(response.status===409&&!(await fetch(`${base}/p/${pack.slug}`)).ok)throw new Error(`Conflicting import ${pack.slug}`);
console.log(`Imported: /p/${pack.slug} (${files.length} transparent PNGs)`);
await verify(pack);
}