Improve prisma scripts

This commit is contained in:
Isaac 2022-07-23 13:27:32 +01:00
parent 3de9cd8c3a
commit d147fa178b
No known key found for this signature in database
GPG Key ID: F4EAABEB0FFCC06A
7 changed files with 37 additions and 7 deletions

View File

@ -90,7 +90,7 @@ model Category {
requireTopic Boolean @default(false) requireTopic Boolean @default(false)
staffRoles Json staffRoles Json
tickets Ticket[] tickets Ticket[]
totalLimit Int @default(-1) totalLimit Int @default(50)
@@map("categories") @@map("categories")
} }

View File

@ -90,7 +90,7 @@ model Category {
requireTopic Boolean @default(false) requireTopic Boolean @default(false)
staffRoles Json staffRoles Json
tickets Ticket[] tickets Ticket[]
totalLimit Int @default(-1) totalLimit Int @default(50)
@@map("categories") @@map("categories")
} }

View File

@ -90,7 +90,7 @@ model Category {
requireTopic Boolean @default(false) requireTopic Boolean @default(false)
staffRoles String staffRoles String
tickets Ticket[] tickets Ticket[]
totalLimit Int @default(-1) totalLimit Int @default(50)
@@map("categories") @@map("categories")
} }

View File

@ -9,7 +9,8 @@
"contributors:generate": "all-contributors generate", "contributors:generate": "all-contributors generate",
"keygen": "node scripts/keygen", "keygen": "node scripts/keygen",
"lint": "eslint src scripts --ext mjs --fix", "lint": "eslint src scripts --ext mjs --fix",
"prisma": "node scripts/prisma", "preinstall": "node scripts/preinstall",
"postinstall": "node scripts/postinstall",
"start": "npm run prisma && node .", "start": "npm run prisma && node .",
"studio": "npx prisma studio", "studio": "npx prisma studio",
"test": "echo \"There's nothing to test\" && exit 1" "test": "echo \"There's nothing to test\" && exit 1"

View File

@ -90,7 +90,7 @@ model Category {
requireTopic Boolean @default(false) requireTopic Boolean @default(false)
staffRoles Json staffRoles Json
tickets Ticket[] tickets Ticket[]
totalLimit Int @default(-1) totalLimit Int @default(50)
@@map("categories") @@map("categories")
} }

View File

@ -1,11 +1,16 @@
require('dotenv').config(); require('dotenv').config();
const fs = require('fs-extra'); const fs = require('fs-extra');
// const { promisify } = require('util');
// const exec = promisify(require('child_process').exec);
const { spawnSync } = require('child_process'); const { spawnSync } = require('child_process');
const providers = ['mysql', 'postgresql', 'sqlite']; const providers = ['mysql', 'postgresql', 'sqlite'];
const provider = process.env.DB_PROVIDER; const provider = process.env.DB_PROVIDER;
if (!provider) {
console.log('[postinstall] environment not set, exiting.');
process.exit(0);
}
if (!providers.includes(provider)) throw new Error(`DB_PROVIDER must be one of: ${providers}`); if (!providers.includes(provider)) throw new Error(`DB_PROVIDER must be one of: ${providers}`);
if (!fs.existsSync('./prisma')) fs.mkdirSync('./prisma'); if (!fs.existsSync('./prisma')) fs.mkdirSync('./prisma');
@ -15,6 +20,7 @@ npx('prisma generate');
npx('prisma migrate deploy'); npx('prisma migrate deploy');
function npx(cmd) { function npx(cmd) {
console.log(`[postinstall] > ${cmd}`);
const child = spawnSync('npx', cmd.split(/\s/)); const child = spawnSync('npx', cmd.split(/\s/));
if (child.stdout) console.log(child.stdout.toString()); if (child.stdout) console.log(child.stdout.toString());
if (child.stderr) console.log(child.stderr.toString()); if (child.stderr) console.log(child.stderr.toString());

23
scripts/preinstall.js Normal file
View File

@ -0,0 +1,23 @@
const { randomBytes } = require('crypto');
const fs = require('fs');
const env = {
DB_CONNECTION_URL: '',
DB_PROVIDER: '', // don't default to sqlite, postinstall checks if empty
DISCORD_SECRET: '',
DISCORD_TOKEN: '',
ENCRYPTION_KEY: randomBytes(24).toString('hex'),
HTTP_BIND: 8080,
HTTP_EXTERNAL: 'http://localhost:8080',
PORTAL: '',
SUPER: '319467558166069248',
};
// check DISCORD_TOKEN because we don't want to force use of the .env file
if (!process.env.DISCORD_TOKEN && !fs.existsSync('./.env')) {
console.log('[preinstall] Generating ENCRYPTION_KEY');
fs.writeFileSync('./.env', Object.entries(env).map(([k, v]) => `${k}=${v}`).join('\n'));
console.log('[preinstall] Created .env file');
} else {
console.log('[preinstall] Nothing to do');
}