2022-07-23 03:44:03 +03:00
|
|
|
require('dotenv').config();
|
|
|
|
const fs = require('fs-extra');
|
|
|
|
const { spawnSync } = require('child_process');
|
|
|
|
|
2022-07-23 15:27:32 +03:00
|
|
|
|
2022-07-23 03:44:03 +03:00
|
|
|
const providers = ['mysql', 'postgresql', 'sqlite'];
|
|
|
|
const provider = process.env.DB_PROVIDER;
|
2022-07-23 15:27:32 +03:00
|
|
|
|
|
|
|
if (!provider) {
|
|
|
|
console.log('[postinstall] environment not set, exiting.');
|
|
|
|
process.exit(0);
|
|
|
|
}
|
|
|
|
|
2022-07-23 03:44:03 +03:00
|
|
|
if (!providers.includes(provider)) throw new Error(`DB_PROVIDER must be one of: ${providers}`);
|
|
|
|
|
|
|
|
if (!fs.existsSync('./prisma')) fs.mkdirSync('./prisma');
|
|
|
|
fs.copySync(`./db/${provider}`, './prisma'); // copy schema & migrations
|
|
|
|
|
|
|
|
npx('prisma generate');
|
|
|
|
npx('prisma migrate deploy');
|
|
|
|
|
|
|
|
function npx(cmd) {
|
2022-07-23 15:27:32 +03:00
|
|
|
console.log(`[postinstall] > ${cmd}`);
|
2022-07-23 03:44:03 +03:00
|
|
|
const child = spawnSync('npx', cmd.split(/\s/));
|
|
|
|
if (child.stdout) console.log(child.stdout.toString());
|
|
|
|
if (child.stderr) console.log(child.stderr.toString());
|
|
|
|
if (child.status) process.exit(child.status);
|
|
|
|
}
|