feat: add docker compose, use @fennec/configuration.

This commit is contained in:
2021-10-31 14:53:11 +08:00
parent ece8ccf27a
commit 4b8bb47800
10 changed files with 599 additions and 469 deletions

View File

@@ -0,0 +1,20 @@
version: "3.9"
services:
postgres:
image: 'postgres:14'
restart: always
environment:
- POSTGRES_HOST_AUTH_METHOD=trust
ports:
- '${PG_PORT}:5432'
redis:
image: 'redis:6'
restart: always
ports:
- '${REDIS_PORT}:6379'
networks:
default:
name: 'blog-dev'
driver: bridge

61
docker/init-dev.mjs Normal file
View File

@@ -0,0 +1,61 @@
import execa from 'execa';
import { URL } from 'url';
import { findFreePorts } from 'find-free-ports';
import YAML from 'js-yaml';
import { readFile, writeFile } from 'fs/promises';
const [PG_PORT, REDIS_PORT] = await findFreePorts(2);
await execa(
'docker-compose',
[
'-f',
new URL('./docker-compose.dev.yml', import.meta.url).pathname,
'up',
'-d',
],
{
env: {
PG_PORT,
REDIS_PORT,
},
stdout: process.stdout,
stderr: process.stderr,
},
);
console.log(`✅ Postgres is running on port ${PG_PORT}`);
console.log(`✅ Redis is running on port ${REDIS_PORT}`);
const config = await YAML.load(
await readFile(
new URL('../config.yml.example', import.meta.url).pathname,
'utf-8',
),
);
config.db.postgres = {
host: 'localhost',
port: PG_PORT,
database: 'postgres',
username: 'postgres',
password: '',
};
config.db.redis = {
host: 'localhost',
port: REDIS_PORT,
};
const configOutputPath = new URL('../config.yml', import.meta.url).pathname;
await writeFile(configOutputPath, YAML.dump(config), 'utf-8');
console.log(`✅ Config file is written to ${configOutputPath}`);
await execa.command('npm run typeorm -- migration:run', {
cwd: new URL('../', import.meta.url).pathname,
stdout: process.stdout,
stderr: process.stderr,
});
console.log(`✅ Database Initiated!`);