This commit is contained in:
Isaac 2022-10-05 17:09:08 +01:00
parent c2fe7f73d1
commit 8b55c351ad
No known key found for this signature in database
GPG Key ID: 0DE40AE37BBA5C33
2 changed files with 37 additions and 10 deletions

View File

@ -1,6 +1,17 @@
require('dotenv').config(); require('dotenv').config();
const fs = require('fs-extra'); const fs = require('fs-extra');
const { spawnSync } = require('child_process'); const util = require('util');
const exec = util.promisify(require('child_process').exec);
async function npx(cmd) {
console.log(`[postinstall] > ${cmd}`);
const {
stderr,
stdout,
} = await exec('npx ' + cmd);
if (stdout) console.log(stdout.toString());
if (stderr) console.log(stderr.toString());
}
const providers = ['mysql', 'postgresql', 'sqlite']; const providers = ['mysql', 'postgresql', 'sqlite'];
const provider = process.env.DB_PROVIDER; const provider = process.env.DB_PROVIDER;
@ -12,16 +23,14 @@ if (!provider) {
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}`);
console.log(`[postinstall] provider=${provider}`);
console.log(`[postinstall] copying ${provider} schema & migrations`);
if (!fs.existsSync('./prisma')) fs.mkdirSync('./prisma'); if (!fs.existsSync('./prisma')) fs.mkdirSync('./prisma');
fs.copySync(`./db/${provider}`, './prisma'); // copy schema & migrations fs.copySync(`./db/${provider}`, './prisma'); // copy schema & migrations
npx('prisma generate'); (async () => {
npx('prisma migrate deploy'); await npx('prisma generate');
// await npx('prisma migrate deploy');
})();
function npx(cmd) {
console.log(`[postinstall] > ${cmd}`);
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);
}

18
src/stdin/npx.js Normal file
View File

@ -0,0 +1,18 @@
const { StdinCommand } = require('@eartharoid/dbf');
const { spawn } = require('child_process');
module.exports = class extends StdinCommand {
constructor(client, options) {
super(client, {
...options,
id: 'npx',
});
}
async run(input) {
if (!input[0]) return this.client.log.warn('Usage: npx <command> [args]');
const child = spawn('npx', input, { shell: true });
for await (const data of child.stdout) this.client.log.info('npx:', data.toString());
for await (const data of child.stderr) this.client.warn.info('npx:', data.toString());
}
};