Skip to content

Commit

Permalink
feat: add cli command to create admins
Browse files Browse the repository at this point in the history
  • Loading branch information
sgomez committed Mar 7, 2024
1 parent 9831d0e commit af56ccc
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
"storybook": "storybook dev -p 6006",
"build-storybook": "storybook build",
"prisma:migrate": "prisma migrate dev",
"prisma:generate": "prisma generate"
"prisma:generate": "prisma generate",
"cli:user:admin": "npx tsx src/core/user/infrastructure/cli/add-user.cli.ts"
},
"dependencies": {
"@aulasoftwarelibre/next-auth-firewall": "1.2.0",
Expand Down
44 changes: 44 additions & 0 deletions src/core/user/infrastructure/cli/add-user.cli.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#!/usr/bin/env node
import 'dotenv/config'

import { ulid } from 'ulid'

import UserDataMapper from '@/core/user/infrastructure/persistence/user.data-mapper'
import prisma from '@/lib/prisma/prisma'
import gravatar from '@/lib/utils/gravatar'

async function main() {
console.info('Creating admin...')

if (process.argv.length !== 4 || !process.argv[2] || !process.argv[3]) {
console.error(`Error: missing parameters.`)
console.error(`Usage: npm cli:user:admin "Full name" "email"`)

process.exit(1)
}

const name = process.argv[2] || ''
const email = process.argv[3] || ''

const user = UserDataMapper.toModel({
email,
id: ulid().toLowerCase(),
image: gravatar(email),
name,
roles: ['ROLE_USER', 'ROLE_ADMIN'],
version: 0,
})

await prisma.user.create({
data: {
...UserDataMapper.toPrisma(user),
emailVerified: new Date(),
},
})
}

main()
.then(() => console.info('Done'))
.catch((error) => console.error(error))
// eslint-disable-next-line unicorn/prefer-top-level-await
.finally(() => prisma.$disconnect())

0 comments on commit af56ccc

Please sign in to comment.