2023-02-01 23:19:48 +02:00
|
|
|
/* eslint-disable no-console */
|
2022-07-23 03:44:03 +03:00
|
|
|
require('dotenv').config();
|
|
|
|
const fs = require('fs-extra');
|
2022-10-05 19:09:08 +03:00
|
|
|
const util = require('util');
|
|
|
|
const exec = util.promisify(require('child_process').exec);
|
2023-02-01 23:19:48 +02:00
|
|
|
const { short } = require('leeks.js');
|
|
|
|
|
|
|
|
function log(...strings) {
|
|
|
|
console.log(short('&9[postinstall]&r'), ...strings);
|
|
|
|
}
|
2022-10-05 19:09:08 +03:00
|
|
|
|
|
|
|
async function npx(cmd) {
|
2023-02-01 23:19:48 +02:00
|
|
|
log(`> ${cmd}`);
|
2022-10-05 19:09:08 +03:00
|
|
|
const {
|
|
|
|
stderr,
|
|
|
|
stdout,
|
|
|
|
} = await exec('npx ' + cmd);
|
|
|
|
if (stdout) console.log(stdout.toString());
|
|
|
|
if (stderr) console.log(stderr.toString());
|
|
|
|
}
|
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) {
|
2023-02-01 23:19:48 +02:00
|
|
|
log('environment not set, exiting.');
|
2022-07-23 15:27:32 +03:00
|
|
|
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}`);
|
|
|
|
|
2023-02-01 23:19:48 +02:00
|
|
|
log(`provider=${provider}`);
|
|
|
|
log(`copying ${provider} schema & migrations`);
|
2022-10-05 19:09:08 +03:00
|
|
|
|
2022-07-23 03:44:03 +03:00
|
|
|
if (!fs.existsSync('./prisma')) fs.mkdirSync('./prisma');
|
|
|
|
fs.copySync(`./db/${provider}`, './prisma'); // copy schema & migrations
|
|
|
|
|
2022-10-05 19:09:08 +03:00
|
|
|
(async () => {
|
|
|
|
await npx('prisma generate');
|
2023-01-30 23:21:48 +02:00
|
|
|
await npx('prisma migrate deploy');
|
2022-10-05 19:09:08 +03:00
|
|
|
})();
|
2022-07-23 03:44:03 +03:00
|
|
|
|