Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 47 additions & 3 deletions adminforth/commands/createApp/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ import Handlebars from 'handlebars';
import { promisify } from 'util';
import { getVersion } from '../cli.js';

import { URL } from 'url'
import net from 'net'

const execAsync = promisify(exec);

function detectAdminforthVersion() {
Expand Down Expand Up @@ -159,6 +162,48 @@ function checkForExistingPackageJson(options) {
}
}

function checkIfDatabaseLocal(urlString) {
if (urlString.startsWith('sqlite')) {
return true;
}
try {
const url = new URL(urlString)

const host = url.hostname

if (!host) return false

// localhost
if (host === 'localhost') return true

// loopback ipv4
if (host === '127.0.0.1') return true

// loopback ipv6
if (host === '::1') return true

// private IP ranges
if (net.isIP(host)) {
if (
host.startsWith('10.') ||
host.startsWith('192.168.') ||
host.match(/^172\.(1[6-9]|2\d|3[0-1])\./)
) {
return true
}
}

// docker / local network aliases
if (['db', 'postgres', 'mysql', 'mongo'].includes(host)) {
return true
}

return false
} catch {
return false
}
}

async function scaffoldProject(ctx, options, cwd) {
const projectDir = path.join(cwd, options.appName);
await fse.ensureDir(projectDir);
Expand Down Expand Up @@ -253,7 +298,7 @@ async function writeTemplateFiles(dirname, cwd, options) {
{
src: '.env.local.hbs',
dest: '.env.local',
data: { dbUrl, prismaDbUrl },
data: { dbUrl: checkIfDatabaseLocal(dbUrl) ? dbUrl : null, prismaDbUrl },
},
{
src: '.env.prod.hbs',
Expand All @@ -269,8 +314,7 @@ async function writeTemplateFiles(dirname, cwd, options) {
// We'll write .env using the same content as .env.sample
src: '.env.local.hbs',
dest: '.env',
data: {},
empty: true,
data: {dbUrl, prismaDbUrl},
},
{
src: 'adminuser.ts.hbs',
Expand Down