diff --git a/.gitignore b/.gitignore index a2442a921e..037f60d356 100644 --- a/.gitignore +++ b/.gitignore @@ -11,6 +11,7 @@ # next.js /.next/ /out/ +/prisma/schema.prisma # production /build diff --git a/README.md b/README.md index b5f4982344..8e0b0bcbc4 100644 --- a/README.md +++ b/README.md @@ -13,21 +13,11 @@ A detailed getting started guide can be found at [https://umami.is/docs/](https: - A server with Node.js 10.13 or newer - A database (MySQL or Postgresql) -### Get the source code +### Get the source code and install packages ``` git clone https://github.com/mikecao/umami.git -``` - -### Go into the repo folder - -``` cd umami -``` - -### Install packages - -``` npm install ``` @@ -70,18 +60,8 @@ The `HASH_SALT` is used to generate unique values for your installation. ### Generate database client -Depending on your database type, run the appropriate script. - -For MySQL: - -``` -npm run build-mysql-client -``` - -For Postgresql: - ``` -npm run build-postgresql-client +npm run build-db-client ``` ### Create a production build diff --git a/package.json b/package.json index 4f494e65fc..2933be631c 100644 --- a/package.json +++ b/package.json @@ -14,10 +14,9 @@ "build": "next build", "start": "next start", "build-tracker": "rollup -c rollup.tracker.config.js", - "build-mysql-schema": "dotenv prisma introspect -- --schema=./prisma/schema.mysql.prisma", - "build-mysql-client": "dotenv prisma generate -- --schema=./prisma/schema.mysql.prisma", - "build-postgresql-schema": "dotenv prisma introspect -- --schema=./prisma/schema.postgresql.prisma", - "build-postgresql-client": "dotenv prisma generate -- --schema=./prisma/schema.postgresql.prisma" + "build-db-client": "node ./scripts/build-db-client.js", + "prisma-introspect": "dotenv prisma introspect", + "prisma-generate": "dotenv prisma generate" }, "lint-staged": { "**/*.js": [ diff --git a/scripts/build-db-client.js b/scripts/build-db-client.js new file mode 100644 index 0000000000..38e284e078 --- /dev/null +++ b/scripts/build-db-client.js @@ -0,0 +1,22 @@ +require('dotenv').config(); +const fs = require('fs'); +const path = require('path'); +const { getDatabase, getNpmCommand, runCommand } = require('./common'); + +(async () => { + const db = getDatabase(); + + if (!db) { + throw new Error('Database not specified'); + } + + const src = path.resolve(__dirname, `../prisma/schema.${db}.prisma`); + const dest = path.resolve(__dirname, '../prisma/schema.prisma'); + + fs.copyFileSync(src, dest); + + await runCommand(getNpmCommand(), ['run', 'prisma-generate']).catch(err => { + console.error(err); + process.exit(1); + }); +})(); diff --git a/scripts/common.js b/scripts/common.js new file mode 100644 index 0000000000..eb070c2021 --- /dev/null +++ b/scripts/common.js @@ -0,0 +1,29 @@ +const { spawn } = require('child_process'); + +function getDatabase() { + return process.env.DATABASE_URL.split(':')[0]; +} + +function runCommand(cmd, args = []) { + return new Promise((resolve, reject) => { + const child = spawn(cmd, args); + + child.stdout.on('data', data => process.stdout.write(data)); + + child.stderr.on('data', data => process.stdout.write(data)); + + child.on('error', err => reject(err)); + + child.on('exit', (code, signal) => resolve({ code, signal })); + }); +} + +function getNpmCommand() { + return /^win/.test(process.platform) ? 'npm.cmd' : 'npm'; +} + +module.exports = { + getDatabase, + runCommand, + getNpmCommand, +};