You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
mail-yubao/scripts/flatten-mail-attachments.ts

63 lines
1.7 KiB

/**
* 将邮<E5B086>?/4 子目录内业务附件平铺到邮件根目录<E79BAE>? * 用法: 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<boolean> {
try {
await fs.access(p);
return true;
} catch {
return false;
}
}
async function moveUp(subdir: string, mailDir: string): Promise<string[]> {
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);
}
// 清空后删子目<E5AD90>? 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 卡转<E58DA1>? },
{
mail: "邮件4",
nested: "原箱号YT2604021091=FBA199R49LD6-MDW2=76件换标操作指<EFBFBD>?,
},
];
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;
});