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');
|
2024-01-11 02:08:57 +02:00
|
|
|
const {
|
|
|
|
resolve, join,
|
|
|
|
} = require('path');
|
2023-02-01 23:19:48 +02:00
|
|
|
|
2023-08-25 01:36:03 +03:00
|
|
|
const fallback = { prisma: './node_modules/prisma/build/index.js' };
|
|
|
|
|
2024-01-11 02:08:57 +02:00
|
|
|
function pathify(path) {
|
|
|
|
return resolve(__dirname, '../', path);
|
|
|
|
}
|
|
|
|
|
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,
|
2024-01-11 02:08:57 +02:00
|
|
|
} = await exec(cmd, { cwd: pathify('./') }); // { env } = process.env
|
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
|
|
|
|
2024-01-11 02:08:57 +02:00
|
|
|
if (fs.existsSync(pathify('./prisma'))) {
|
2023-08-25 01:36:03 +03:00
|
|
|
fs.rmSync('./prisma', {
|
|
|
|
force: true,
|
|
|
|
recursive: true,
|
|
|
|
});
|
|
|
|
} else {
|
2024-01-11 02:08:57 +02:00
|
|
|
fs.mkdirSync(pathify('./prisma'));
|
|
|
|
}
|
|
|
|
fs.copySync(pathify(`./db/${provider}`), pathify('./prisma')); // copy schema & migrations
|
|
|
|
|
|
|
|
if (provider === 'sqlite' && !process.env.DB_CONNECTION_URL) {
|
|
|
|
process.env.DB_CONNECTION_URL = 'file:' + join(process.cwd(), './user/database.db');
|
|
|
|
log(`set DB_CONNECTION_URL=${process.env.DB_CONNECTION_URL}`);
|
2023-08-25 01:36:03 +03:00
|
|
|
}
|
2022-07-23 03:44:03 +03:00
|
|
|
|
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
|
|
|
})();
|