Skip to content

Commit

Permalink
Database build script.
Browse files Browse the repository at this point in the history
  • Loading branch information
mikecao committed Aug 20, 2020
1 parent b905824 commit c3da37c
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 26 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
# next.js
/.next/
/out/
/prisma/schema.prisma

# production
/build
Expand Down
24 changes: 2 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```

Expand Down Expand Up @@ -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
Expand Down
7 changes: 3 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand Down
22 changes: 22 additions & 0 deletions scripts/build-db-client.js
Original file line number Diff line number Diff line change
@@ -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);
});
})();
29 changes: 29 additions & 0 deletions scripts/common.js
Original file line number Diff line number Diff line change
@@ -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,
};

0 comments on commit c3da37c

Please sign in to comment.