2026-05-25 14:31:58 +08:00

48 lines
1.2 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { PrismaClient as MainPrismaClient } from '.prisma/main-client';
import bcrypt from 'bcryptjs';
const prisma = new MainPrismaClient();
async function main() {
const username = process.argv[2] || 'admin';
const password = process.argv[3] || process.env.ADMIN_INITIAL_PASSWORD;
if (!password) {
throw new Error('请通过命令参数或 ADMIN_INITIAL_PASSWORD 提供管理员初始密码');
}
// 检查是否已存在
const existing = await prisma.admin.findUnique({
where: { username },
});
if (existing) {
console.log(`管理员 "${username}" 已存在`);
return;
}
const passwordHash = await bcrypt.hash(password, 10);
await prisma.admin.create({
data: {
username,
passwordHash,
isSuperAdmin: true,
},
});
console.log(`管理员创建成功!`);
console.log(`用户名: ${username}`);
console.log(`密码: 已按输入值写入哈希,不在日志中输出明文`);
console.log(`\n请使用提供的初始密码登录后立即修改密码!`);
}
main()
.catch((e) => {
console.error('创建管理员失败:', e);
process.exit(1);
})
.finally(async () => {
await prisma.$disconnect();
});