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');
|
|
|
|
|
2023-08-25 01:36:03 +03:00
|
|
|
const fallback = { prisma: './node_modules/prisma/build/index.js' };
|
|
|
|
|
2023-02-01 23:19:48 +02:00
|
|
|
function log(...strings) {
|
|
|
|
console.log(short('&9[postinstall]&r'), ...strings);
|
|
|
|
}
|
2022-10-05 19:09:08 +03:00
|
|
|
|
|
|
|
async function npx(cmd) {
|
2023-08-25 01:36:03 +03:00
|
|
|
const parts = cmd.split(' ');
|
|
|
|
// fallback for environments with no symlink/npx support (PebbleHost)
|
|
|
|
if (!fs.existsSync(`./node_modules/.bin/${parts[0]}`)) {
|
|
|
|
const x = parts.shift();
|
|
|
|
cmd = 'node ' + fallback[x] + ' ' + parts.join(' ');
|
|
|
|
} else {
|
|
|
|
cmd = 'npx ' + cmd;
|
|
|
|
}
|
2023-02-01 23:19:48 +02:00
|
|
|
log(`> ${cmd}`);
|
2022-10-05 19:09:08 +03:00
|
|
|
const {
|
|
|
|
stderr,
|
|
|
|
stdout,
|
2023-08-25 01:36:03 +03:00
|
|
|
} = await exec(cmd);
|
2022-10-05 19:09:08 +03:00
|
|
|
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
|
|
|
|
2023-08-25 01:36:03 +03:00
|
|
|
if (fs.existsSync('./prisma')) {
|
|
|
|
fs.rmSync('./prisma', {
|
|
|
|
force: true,
|
|
|
|
recursive: true,
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
fs.mkdirSync('./prisma');
|
|
|
|
}
|
2022-07-23 03:44:03 +03:00
|
|
|
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
|
|
|
})();
|