refactor: simplify .env generation and remove unnecessary escaping
- Remove escapeEnvValue function (no longer needed) - Simplify generateEnvFile to write plain KEY=value format - Remove quote-stripping from parseEnvFile - Move helper functions back to module level for cleaner code - Use DATABASE_PROTOCOLS in validateDatabaseUrl
This commit is contained in:
parent
a4ee1d95dc
commit
099d521288
2 changed files with 67 additions and 79 deletions
|
|
@ -5,70 +5,58 @@ import * as c from "yoctocolors";
|
|||
import * as p from "@clack/prompts";
|
||||
|
||||
import { generateSecret } from "../../utils/crypto.js";
|
||||
import { generateEnvFile, DATABASE_PROTOCOLS } from "../../utils/config.js";
|
||||
import { generateEnvFile } from "../../utils/config.js";
|
||||
import { CommandConfig } from "../../cli-router.js";
|
||||
|
||||
function normalizeHttpUrl(value: string): string {
|
||||
const trimmed = value.trim();
|
||||
const hasProtocol = /^[a-z]+:\/\//i.test(trimmed);
|
||||
const isNotHttp = !/^https?:\/\//i.test(trimmed);
|
||||
|
||||
if (hasProtocol && isNotHttp) {
|
||||
throw new Error("URLs must use http:// or https://");
|
||||
}
|
||||
|
||||
return hasProtocol ? trimmed : `https://${trimmed}`;
|
||||
}
|
||||
|
||||
function validateHttpUrlOrHost(
|
||||
value: string | undefined,
|
||||
label: string,
|
||||
): string | undefined {
|
||||
if (!value?.trim()) return `${label} is required`;
|
||||
|
||||
try {
|
||||
const normalized = normalizeHttpUrl(value);
|
||||
const url = new URL(normalized);
|
||||
if (!url.hostname) {
|
||||
return `${label} must include a domain or hostname`;
|
||||
}
|
||||
} catch {
|
||||
return `${label} must be a valid URL or hostname`;
|
||||
}
|
||||
|
||||
return undefined;
|
||||
}
|
||||
|
||||
function validatePostgresUrl(value: string | undefined): string | undefined {
|
||||
if (!value?.trim()) {
|
||||
return "Database URL is required";
|
||||
}
|
||||
|
||||
try {
|
||||
const url = new URL(value);
|
||||
if (url.protocol !== "postgres:" && url.protocol !== "postgresql:") {
|
||||
return "Database URL must start with postgres:// or postgresql://";
|
||||
}
|
||||
} catch {
|
||||
return "Database URL must be a valid Postgres connection string";
|
||||
}
|
||||
|
||||
return undefined;
|
||||
}
|
||||
|
||||
async function init(opts: { output?: string }) {
|
||||
// Helper functions scoped to init
|
||||
function normalizeHttpUrl(value: string): string {
|
||||
const trimmed = value.trim();
|
||||
const hasProtocol = /^[a-z]+:\/\//i.test(trimmed);
|
||||
const isNotHttp = !/^https?:\/\//i.test(trimmed);
|
||||
|
||||
if (hasProtocol && isNotHttp) {
|
||||
throw new Error("URLs must use http:// or https://");
|
||||
}
|
||||
|
||||
return hasProtocol ? trimmed : `https://${trimmed}`;
|
||||
}
|
||||
|
||||
function validateHttpUrlOrHost(
|
||||
value: string | undefined,
|
||||
label: string,
|
||||
): string | undefined {
|
||||
if (!value?.trim()) return `${label} is required`;
|
||||
|
||||
try {
|
||||
const normalized = normalizeHttpUrl(value);
|
||||
const url = new URL(normalized);
|
||||
if (!url.hostname) {
|
||||
return `${label} must include a domain or hostname`;
|
||||
}
|
||||
} catch {
|
||||
return `${label} must be a valid URL or hostname`;
|
||||
}
|
||||
|
||||
return undefined;
|
||||
}
|
||||
|
||||
function validatePostgresUrl(value: string | undefined): string | undefined {
|
||||
if (!value?.trim()) {
|
||||
return "Database URL is required";
|
||||
}
|
||||
|
||||
try {
|
||||
const url = new URL(value);
|
||||
if (!DATABASE_PROTOCOLS.includes(url.protocol as "postgres:" | "postgresql:")) {
|
||||
return "Database URL must start with postgres:// or postgresql://";
|
||||
}
|
||||
} catch {
|
||||
return "Database URL must be a valid Postgres connection string";
|
||||
}
|
||||
|
||||
return undefined;
|
||||
}
|
||||
|
||||
async function writePrivateFile(
|
||||
filePath: string,
|
||||
content: string,
|
||||
): Promise<void> {
|
||||
await fs.writeFile(filePath, content, {
|
||||
encoding: "utf8",
|
||||
flag: "wx",
|
||||
mode: 0o600,
|
||||
});
|
||||
}
|
||||
|
||||
p.intro(c.bold("myst config init"));
|
||||
p.note(
|
||||
[
|
||||
|
|
@ -92,6 +80,7 @@ async function init(opts: { output?: string }) {
|
|||
const outDir = opts.output ? path.dirname(opts.output) : process.cwd();
|
||||
const envPath = opts.output || path.join(outDir, ".env");
|
||||
|
||||
// Check if .env file already exists
|
||||
let envExists = false;
|
||||
try {
|
||||
await fs.stat(envPath);
|
||||
|
|
@ -100,6 +89,7 @@ async function init(opts: { output?: string }) {
|
|||
// .env file does not exist
|
||||
}
|
||||
|
||||
// Handle existing .env file
|
||||
if (envExists) {
|
||||
while (true) {
|
||||
const choice = await p.select({
|
||||
|
|
@ -209,7 +199,11 @@ async function init(opts: { output?: string }) {
|
|||
publicUrl,
|
||||
});
|
||||
|
||||
await writePrivateFile(envPath, envContent);
|
||||
await fs.writeFile(envPath, envContent, {
|
||||
encoding: "utf8",
|
||||
flag: "wx",
|
||||
mode: 0o600,
|
||||
});
|
||||
|
||||
s.stop(c.yellow(`.env created at ${envPath}`));
|
||||
p.note(
|
||||
|
|
@ -240,4 +234,4 @@ export const commandConfig: CommandConfig = {
|
|||
output: { type: "string", short: "o" },
|
||||
},
|
||||
handler: init,
|
||||
};
|
||||
};
|
||||
|
|
|
|||
|
|
@ -57,38 +57,32 @@ export function validateDatabaseUrl(url: string | undefined): boolean {
|
|||
if (!url) return false;
|
||||
try {
|
||||
const parsed = new URL(url);
|
||||
return DATABASE_PROTOCOLS.includes(parsed.protocol as "postgres:" | "postgresql:");
|
||||
return DATABASE_PROTOCOLS.includes(
|
||||
parsed.protocol as "postgres:" | "postgresql:",
|
||||
);
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
function escapeEnvValue(value: string): string {
|
||||
return `"${value
|
||||
.replace(/\\/g, "\\\\")
|
||||
.replace(/"/g, '\\"')
|
||||
.replace(/\r?\n/g, "\\n")
|
||||
.replace(/\$/g, "\\$")}"`;
|
||||
}
|
||||
|
||||
export function generateEnvFile(options: MystEnvironmentVariables): string {
|
||||
const lines = [
|
||||
`# Public URL used for share links.`,
|
||||
`${ENV_MAPPING.MYST_PUBLIC_URL}=${escapeEnvValue(options.publicUrl)}`,
|
||||
`MYST_PUBLIC_URL=${options.publicUrl}`,
|
||||
``,
|
||||
`# Private operator URL for the admin UI. Protect this upstream.`,
|
||||
`${ENV_MAPPING.MYST_ADMIN_URL}=${escapeEnvValue(options.adminUrl)}`,
|
||||
`MYST_ADMIN_URL=${options.adminUrl}`,
|
||||
``,
|
||||
`# Myst's own Postgres database.`,
|
||||
`${ENV_MAPPING.DATABASE_URL}=${escapeEnvValue(options.databaseUrl)}`,
|
||||
`DATABASE_URL=${options.databaseUrl}`,
|
||||
``,
|
||||
`# Existing Forgejo instance and operator-created PAT for the dedicated Forgejo user.`,
|
||||
`${ENV_MAPPING.FORGEJO_BASE_URL}=${escapeEnvValue(options.forgejoBaseUrl)}`,
|
||||
`${ENV_MAPPING.FORGEJO_BOT_USERNAME}=${escapeEnvValue(options.forgejoBotUsername)}`,
|
||||
`${ENV_MAPPING.FORGEJO_PAT}=${escapeEnvValue(options.forgejoPat)}`,
|
||||
`FORGEJO_BASE_URL=${options.forgejoBaseUrl}`,
|
||||
`FORGEJO_BOT_USERNAME=${options.forgejoBotUsername}`,
|
||||
`FORGEJO_PAT=${options.forgejoPat}`,
|
||||
``,
|
||||
`# Secret used to sign or derive grant tokens.`,
|
||||
`${ENV_MAPPING.GRANT_TOKEN_SECRET}=${escapeEnvValue(options.grantTokenSecret)}`,
|
||||
`GRANT_TOKEN_SECRET=${options.grantTokenSecret}`,
|
||||
];
|
||||
|
||||
return lines.join("\n");
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue