Skip to content

Commit 1afec36

Browse files
committed
feat: db port detection
1 parent 2ee36ff commit 1afec36

3 files changed

Lines changed: 38 additions & 9 deletions

File tree

index.js

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { promisify } from "util";
77
import { createWriteStream } from "fs";
88
import AdmZip from "adm-zip";
99
import { randomBytes } from "crypto";
10+
import net from "net";
1011

1112
const streamPipeline = promisify(pipeline);
1213

@@ -19,6 +20,19 @@ function ensureExecutable(filePath) {
1920
}
2021
}
2122

23+
function isPortAvailable(port) {
24+
return new Promise((resolve) => {
25+
const server = net
26+
.createServer()
27+
.once("error", () => resolve(false))
28+
.once("listening", () => {
29+
server.close();
30+
resolve(true);
31+
})
32+
.listen(port);
33+
});
34+
}
35+
2236
function printHelp() {
2337
console.log(`
2438
create-seamless
@@ -36,7 +50,7 @@ Options:
3650
3751
--auth-port <n> Auth server port (default: 5312)
3852
--api-port <n> API server port (default: 3000)
39-
--web-port <n> Web server port (default: 5173)
53+
--web-port <n> Web server port (default: 5001)
4054
4155
-h, --help Show this help message
4256
@@ -350,6 +364,16 @@ async function downloadRepo(repo, dest) {
350364
console.log(`\nCreating Seamless project: ${projectName}\n`);
351365
const API_SERVICE_TOKEN = randomBytes(32).toString("hex");
352366

367+
let dbHostPort = "5432";
368+
369+
if (!(await isPortAvailable(5432))) {
370+
dbHostPort = "5433";
371+
console.log(`
372+
⚠️ Port 5432 is already in use on your machine.
373+
Using 5433 for Docker Postgres instead.
374+
`);
375+
}
376+
353377
if (AUTH) {
354378
const dir = path.join(root, "auth");
355379
fs.mkdirSync(dir);
@@ -374,7 +398,7 @@ async function downloadRepo(repo, dest) {
374398

375399
DB_LOGGING: "false",
376400
DB_HOST: "localhost",
377-
DB_PORT: "5432",
401+
DB_PORT: dbHostPort,
378402
DB_NAME: "seamless_auth",
379403
DB_USER: "myuser",
380404
DB_PASSWORD: "mypassword",
@@ -407,7 +431,7 @@ async function downloadRepo(repo, dest) {
407431
API_SERVICE_TOKEN: API_SERVICE_TOKEN,
408432

409433
DB_HOST: "localhost",
410-
DB_PORT: "5432",
434+
DB_PORT: dbHostPort,
411435
DB_NAME: "seamless_api",
412436
DB_USER: "myuser",
413437
DB_PASSWORD: "mypassword",
@@ -436,10 +460,12 @@ async function downloadRepo(repo, dest) {
436460
}
437461

438462
if (AUTH && API && WEB) {
439-
fs.writeFileSync(
440-
path.join(root, "Docker-compose.yml"),
441-
GENERATED_DOCKER_COMPOSE,
463+
const compose = GENERATED_DOCKER_COMPOSE.replace(
464+
'"5432:5432"',
465+
`"${dbHostPort}:5432"`,
442466
);
467+
468+
fs.writeFileSync(path.join(root, "docker-compose.yml"), compose);
443469
}
444470

445471
if (AUTH) {
@@ -481,6 +507,9 @@ Start development:
481507
482508
docker compose up
483509
510+
If you already have Postgres running locally,
511+
the generator may map Docker to port 5433 instead.
512+
484513
Docs: https://docs.seamlessauth.com/docs
485514
Happy hacking. 🚀
486515
`);

package-lock.json

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

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "create-seamless",
3-
"version": "0.0.9",
3+
"version": "0.0.10",
44
"description": "The starter script for Seamless Auth",
55
"homepage": "https://github.com/fells-code/create-seamless#readme",
66
"bugs": {

0 commit comments

Comments
 (0)