From aeba95bb4cd276ac0beeee33e6708a7fbd55e441 Mon Sep 17 00:00:00 2001 From: Sebastian Pravda Date: Wed, 16 Aug 2023 20:21:47 +0200 Subject: [PATCH] feat: usersplit cli options --- packages/usersplit/index.ts | 63 +++++++++++++++++++++++++++++---- packages/usersplit/package.json | 7 ++-- pnpm-lock.yaml | 12 +++++++ 3 files changed, 73 insertions(+), 9 deletions(-) diff --git a/packages/usersplit/index.ts b/packages/usersplit/index.ts index 8cd7b21..9ba96ce 100644 --- a/packages/usersplit/index.ts +++ b/packages/usersplit/index.ts @@ -1,14 +1,28 @@ +/* eslint-disable @typescript-eslint/ban-ts-comment */ import fs from 'fs'; import path from 'path'; import prisma from './prisma'; import bcrypt from 'bcrypt'; +import { program } from 'commander'; + +async function main({ + usersFile, + passwordsFile, + outputFile, + superadminFile +}: { + usersFile: string; + passwordsFile: string; + outputFile: string; + superadminFile: string; +}) { + const superadminPassword = fs.readFileSync(path.join(__dirname, superadminFile), 'utf8'); + const lines = fs.readFileSync(path.join(__dirname, usersFile), 'utf8').split('\n'); + const passwords = fs.readFileSync(path.join(__dirname, passwordsFile), 'utf8').split('\n'); -async function main() { - const lines = fs.readFileSync(path.join(__dirname, 'users.txt'), 'utf8').split('\n'); - const passwords = fs.readFileSync(path.join(__dirname, 'passwords.txt'), 'utf8').split('\n'); let passIndex = 0; - let adminLogins: string[] = []; + const adminLogins: string[] = []; await prisma.$executeRaw`TRUNCATE TABLE "User" CASCADE`; await prisma.$executeRaw`TRUNCATE TABLE "Group" CASCADE`; @@ -26,7 +40,7 @@ async function main() { surname: 'Superadmin', email: 'superadmin', username: 'superadmin', - password: bcrypt.hashSync('REDACTED', 12) + password: bcrypt.hashSync(superadminPassword, 12) } }); @@ -91,9 +105,46 @@ async function main() { console.log(dbUser); } + + // if file doesn't exist, it will be created + fs.writeFileSync(path.join(__dirname, outputFile), adminLogins.join('\n')); console.log(adminLogins); } +console.log(process.argv); +program + .option('-u, --usersfile ', 'Path to users file (default users.txt)', 'users.txt') + .option( + '-p, --passwordsfile ', + 'Path to passwords file (default passwords.txt)', + 'passwords.txt' + ) + .option( + '-o, --output ', + 'Path to output file with admin logins (default: admins.txt)', + 'admins.txt' + ) + .option( + '-s, --superadminfile ', + 'Path to superadmin password (default: superadmin.txt)', + 'superadmin.txt' + ) + .parse(process.argv); + +const options = program.opts(); + +const { + usersfile: usersFile, + passwordsfile: passwordsFile, + output: outputFile, + superadminfile: superadminFile +} = options; + +if (!usersFile || !passwordsFile || !outputFile || !superadminFile) { + console.error('Missing arguments'); + process.exit(1); +} -main().then(() => { +main({ usersFile, passwordsFile, outputFile, superadminFile }).then(() => { console.log('done'); + process.exit(0); }); diff --git a/packages/usersplit/package.json b/packages/usersplit/package.json index 3c18342..10976ce 100644 --- a/packages/usersplit/package.json +++ b/packages/usersplit/package.json @@ -3,16 +3,17 @@ "main": "./index.ts", "types": "./index.ts", "scripts": { - "main": "ts-node index.ts" + "main": "prisma generate --schema=../database/prisma/schema.prisma && ts-node index.ts" }, "dependencies": { "@testy/database": "workspace:*", "bcrypt": "^5.1.0", + "commander": "^11.0.0", "ts-node": "^10.9.1" }, "devDependencies": { + "@types/bcrypt": "^5.0.0", "prisma": "^4.13.0", - "ts-node": "^10.9.1", - "@types/bcrypt": "^5.0.0" + "ts-node": "^10.9.1" } } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index b00117a..0f652d4 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -1,5 +1,9 @@ lockfileVersion: '6.0' +settings: + autoInstallPeers: true + excludeLinksFromLockfile: false + importers: .: @@ -249,6 +253,9 @@ importers: bcrypt: specifier: ^5.1.0 version: 5.1.0 + commander: + specifier: ^11.0.0 + version: 11.0.0 ts-node: specifier: ^10.9.1 version: 10.9.1(@types/node@18.15.5)(typescript@5.0.4) @@ -2137,6 +2144,11 @@ packages: hasBin: true dev: false + /commander@11.0.0: + resolution: {integrity: sha512-9HMlXtt/BNoYr8ooyjjNRdIilOTkVJXB+GhxMTtOKwk0R4j4lS4NpjuqmRxroBfnfTSHQIHQB7wryHhXarNjmQ==} + engines: {node: '>=16'} + dev: false + /commondir@1.0.1: resolution: {integrity: sha512-W9pAhw0ja1Edb5GVdIF1mjZw/ASI0AlShXM83UUGe2DVr5TdAPEA1OA8m/g8zWp9x6On7gqufY+FatDbC3MDQg==}