From 8b55c351adeef3e0ff04df5db004c595d8e56aed Mon Sep 17 00:00:00 2001 From: Isaac Date: Wed, 5 Oct 2022 17:09:08 +0100 Subject: [PATCH] why --- scripts/postinstall.js | 29 +++++++++++++++++++---------- src/stdin/npx.js | 18 ++++++++++++++++++ 2 files changed, 37 insertions(+), 10 deletions(-) create mode 100644 src/stdin/npx.js diff --git a/scripts/postinstall.js b/scripts/postinstall.js index 3b541d1..6e1c473 100644 --- a/scripts/postinstall.js +++ b/scripts/postinstall.js @@ -1,6 +1,17 @@ require('dotenv').config(); 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 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}`); +console.log(`[postinstall] provider=${provider}`); +console.log(`[postinstall] copying ${provider} schema & migrations`); + if (!fs.existsSync('./prisma')) fs.mkdirSync('./prisma'); fs.copySync(`./db/${provider}`, './prisma'); // copy schema & migrations -npx('prisma generate'); -npx('prisma migrate deploy'); +(async () => { + 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); -} \ No newline at end of file diff --git a/src/stdin/npx.js b/src/stdin/npx.js new file mode 100644 index 0000000..992eebc --- /dev/null +++ b/src/stdin/npx.js @@ -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 [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()); + } +}; \ No newline at end of file