Skip to content

armgit / umami

Public Imported from GitHub
0

Umami is a privacy-first web and product analytics platform. Traffic, campaigns, behavior, conversions, and revenue in one place. No cookies, no surveillance, self-hosted or in the cloud.

Database build script.

Mike Caoc3da37c0b020
diff --git a/.gitignore b/.gitignore
index a2442a921..037f60d35 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 b5f498234..8e0b0bcbc 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 4f494e65f..2933be631 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 000000000..38e284e07
--- /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 000000000..eb070c202
--- /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,
+};