Skip to content

Commit 170489c

Browse files
authored
Merge pull request #49 from Lemoncode/Feature/add-postinstall-to-db
Feature/add postinstall to db
2 parents f9496ec + fddeec4 commit 170489c

File tree

4 files changed

+29
-0
lines changed

4 files changed

+29
-0
lines changed

package-lock.json

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/db/.env.example

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
MONGODB_CONNECTION_STRING: mongodb://localhost:27017/embalse-info

packages/db/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
"main": "./dist/index.js",
1313
"types": "./dist/index.d.ts",
1414
"scripts": {
15+
"postinstall": "node setup.js",
1516
"build": "run-p clean type-check build:db",
1617
"build:db": "tsc",
1718
"clean": "rimraf dist",

packages/db/setup.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import fs from "fs";
2+
import path from "path";
3+
import { fileURLToPath } from "url";
4+
5+
const __filename = fileURLToPath(import.meta.url);
6+
const __dirname = path.dirname(__filename);
7+
8+
const mongoDataPath = path.resolve(__dirname, "mongo-data");
9+
const envExamplePath = path.resolve(__dirname, ".env.example");
10+
const envPath = path.resolve(__dirname, ".env");
11+
12+
try {
13+
// Create directory mongo-data
14+
if (!fs.existsSync(mongoDataPath)) {
15+
fs.mkdirSync(mongoDataPath, { recursive: true });
16+
}
17+
18+
// Copy .env.example a .env
19+
if (!fs.existsSync(envPath)) {
20+
if (fs.existsSync(envExamplePath)) {
21+
fs.copyFileSync(envExamplePath, envPath);
22+
}
23+
}
24+
} catch (error) {
25+
process.exit(1);
26+
}

0 commit comments

Comments
 (0)