/** * 将邮�?/4 子目录内业务附件平铺到邮件根目录�? * 用法: pnpm exec tsx scripts/flatten-mail-attachments.ts */ import fs from "fs/promises"; import path from "path"; const MAIL_ROOT = path.join(process.cwd(), "docx", "邮件"); async function exists(p: string): Promise { try { await fs.access(p); return true; } catch { return false; } } async function moveUp(subdir: string, mailDir: string): Promise { const moved: string[] = []; if (!(await exists(subdir))) return moved; const entries = await fs.readdir(subdir, { withFileTypes: true }); for (const e of entries) { if (!e.isFile()) continue; const src = path.join(subdir, e.name); const dest = path.join(mailDir, e.name); if (await exists(dest)) { // 同名已在根:删子目录副本 await fs.unlink(src); moved.push(`${e.name} (root exists, removed nested)`); continue; } await fs.rename(src, dest); moved.push(e.name); } // 清空后删子目�? const left = await fs.readdir(subdir); if (!left.length) await fs.rmdir(subdir); return moved; } async function main() { const ops: Array<{ mail: string; nested: string }> = [ { mail: "邮件3", nested: "WHSU5574991 卡转�? }, { mail: "邮件4", nested: "原箱号YT2604021091=FBA199R49LD6-MDW2=76件换标操作指�?, }, ]; for (const op of ops) { const mailDir = path.join(MAIL_ROOT, op.mail); const nested = path.join(mailDir, op.nested); const moved = await moveUp(nested, mailDir); console.log( JSON.stringify({ mail: op.mail, nested: op.nested, moved }, null, 2), ); } } main().catch((e) => { console.error(e); process.exitCode = 1; });