Skip to content

Commit

Permalink
Merge pull request #85 from admisio/usersplit_options
Browse files Browse the repository at this point in the history
feat: usersplit cli options
  • Loading branch information
starvy authored Feb 18, 2024
2 parents 107ce37 + aeba95b commit 04ae0b8
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 9 deletions.
63 changes: 57 additions & 6 deletions packages/usersplit/index.ts
Original file line number Diff line number Diff line change
@@ -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`;
Expand All @@ -26,7 +40,7 @@ async function main() {
surname: 'Superadmin',
email: 'superadmin',
username: 'superadmin',
password: bcrypt.hashSync('REDACTED', 12)
password: bcrypt.hashSync(superadminPassword, 12)
}
});

Expand Down Expand Up @@ -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 <usersfile>', 'Path to users file (default users.txt)', 'users.txt')
.option(
'-p, --passwordsfile <passwordsfile>',
'Path to passwords file (default passwords.txt)',
'passwords.txt'
)
.option(
'-o, --output <output>',
'Path to output file with admin logins (default: admins.txt)',
'admins.txt'
)
.option(
'-s, --superadminfile <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);
});
7 changes: 4 additions & 3 deletions packages/usersplit/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
12 changes: 12 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 04ae0b8

Please sign in to comment.