first commit 🎉
This commit is contained in:
@@ -0,0 +1,73 @@
|
||||
#!/usr/bin/env bun
|
||||
import { setTimeout } from 'node:timers/promises'
|
||||
import { createInterface } from 'readline'
|
||||
import UserCommandService from './src/modules/user/commands/service'
|
||||
import { passwordHash } from './src/helpers/utils'
|
||||
|
||||
const rl = createInterface({
|
||||
input: process.stdin,
|
||||
output: process.stdout,
|
||||
})
|
||||
|
||||
function question(prompt: string): Promise<string> {
|
||||
return new Promise((resolve) => {
|
||||
rl.question(prompt, resolve)
|
||||
})
|
||||
}
|
||||
|
||||
async function main() {
|
||||
try {
|
||||
console.log('Setting up admin user...')
|
||||
|
||||
const name = await question('Enter admin name (min 3 characters): ')
|
||||
if (name.length < 3) {
|
||||
console.error('Name must be at least 3 characters long.')
|
||||
process.exit(1)
|
||||
}
|
||||
|
||||
const emailOrPhone = await question('Enter email or phone number: ')
|
||||
if (!emailOrPhone) {
|
||||
console.error('Either email or phone number must be provided.')
|
||||
process.exit(1)
|
||||
}
|
||||
|
||||
const password = await question('Enter password (min 8 characters): ')
|
||||
if (password.length < 8) {
|
||||
console.error('Password must be at least 8 characters long.')
|
||||
process.exit(1)
|
||||
}
|
||||
|
||||
const confirmPassword = await question('Confirm password: ')
|
||||
if (password !== confirmPassword) {
|
||||
console.error('Passwords do not match.')
|
||||
process.exit(1)
|
||||
}
|
||||
|
||||
// Determine if it's email or phone
|
||||
const isEmail = emailOrPhone.includes('@')
|
||||
const userData = {
|
||||
name,
|
||||
password: passwordHash(password),
|
||||
role: 'admin' as const,
|
||||
status: true,
|
||||
...(isEmail ? { email: emailOrPhone } : { phone: emailOrPhone }),
|
||||
}
|
||||
|
||||
const user = await UserCommandService.createUser(userData)
|
||||
|
||||
console.log('Admin user created successfully!')
|
||||
console.log(`ID: ${user.id}`)
|
||||
console.log(`Name: ${user.name}`)
|
||||
console.log(`Email/Phone: ${user.email || user.phone}`)
|
||||
console.log(`Role: ${user.role}`)
|
||||
await setTimeout(3000)
|
||||
process.exit()
|
||||
} catch (error) {
|
||||
console.error('Error creating admin user:', error instanceof Error ? error.message : String(error))
|
||||
process.exit(1)
|
||||
} finally {
|
||||
rl.close()
|
||||
}
|
||||
}
|
||||
|
||||
main()
|
||||
Reference in New Issue
Block a user