diff --git a/packages/cli/src/commands/index.ts b/packages/cli/src/commands/init.ts similarity index 85% rename from packages/cli/src/commands/index.ts rename to packages/cli/src/commands/init.ts index ea28880..56aa97c 100644 --- a/packages/cli/src/commands/index.ts +++ b/packages/cli/src/commands/init.ts @@ -1,14 +1,16 @@ -import path from "node:path"; import fs from "node:fs/promises"; -import * as p from "@clack/prompts"; +import path from "node:path"; + import * as c from "yoctocolors"; -import { generateEnvFile } from "../generators/env.js"; +import * as p from "@clack/prompts"; + import { generateSecret } from "../utils/crypto.js"; +import { generateEnvFile } from "../utils/env.js"; /** * Normalizes a URL to use https:// if no protocol is specified. * @param value - The URL to normalize. - * @returns The normalized URL. + * @returns {string} The normalized URL. */ function normalizeHttpUrl(value: string): string { const trimmed = value.trim(); @@ -22,6 +24,12 @@ function normalizeHttpUrl(value: string): string { return hasProtocol ? trimmed : `https://${trimmed}`; } +/** + * Validates a URL or host, returning the normalized URL or undefined if valid. + * @param value - The URL or host to validate. + * @param label - The label to use in error messages. + * @returns {string | undefined} The normalized URL or undefined if valid. + */ function validateHttpUrlOrHost( value: string | undefined, label: string, @@ -42,6 +50,11 @@ function validateHttpUrlOrHost( return undefined; } +/** + * Validates a Postgres URL, returning undefined if valid. + * @param value - The Postgres URL to validate. + * @returns {string | undefined} The error message or undefined if valid. + */ function validatePostgresUrl(value: string | undefined): string | undefined { if (!value?.trim()) { return "Database URL is required"; @@ -59,6 +72,12 @@ function validatePostgresUrl(value: string | undefined): string | undefined { return undefined; } +/** + * Writes a private file to disk, throwing an error if the file already exists. + * @param filePath - The path to the file to write. + * @param content - The content to write to the file. + * @returns {Promise} A promise that resolves when the file is written. + */ async function writePrivateFile( filePath: string, content: string, @@ -80,6 +99,11 @@ async function writePrivateFile( } } +/** + * Preflight check for write targets, throwing an error if any target file already exists. + * @param filePaths - The paths of the files to write. + * @returns {Promise} A promise that resolves when all targets are checked. + */ async function preflightWriteTargets(filePaths: string[]): Promise { for (const filePath of filePaths) { try { @@ -96,6 +120,11 @@ async function preflightWriteTargets(filePaths: string[]): Promise { } } +/** + * Initializes a Myst project by creating the necessary configuration files. + * @param _args - The command arguments. + * @returns {Promise} A promise that resolves when the initialization is complete. + */ export async function initCommand(_args: string[]): Promise { p.intro(c.bold("myst init")); p.note( diff --git a/packages/cli/src/generators/examples.ts b/packages/cli/src/generators/examples.ts deleted file mode 100644 index e3935df..0000000 --- a/packages/cli/src/generators/examples.ts +++ /dev/null @@ -1,22 +0,0 @@ -export function generateExampleCaddyfile(domain: string): string { - return `${domain} { - reverse_proxy /_myst/* 127.0.0.1:4000 - reverse_proxy 127.0.0.1:3000 -} -`; -} - -export function generateExampleNginxConf(domain: string): string { - return `server { - server_name ${domain}; - - location /_myst/ { - proxy_pass http://127.0.0.1:4000/; - } - - location / { - proxy_pass http://127.0.0.1:3000/; - } -} -`; -} diff --git a/packages/cli/src/generators/index.ts b/packages/cli/src/generators/index.ts deleted file mode 100644 index 7b37266..0000000 --- a/packages/cli/src/generators/index.ts +++ /dev/null @@ -1,2 +0,0 @@ -export * from "./env.js"; -export * from "./examples.js"; diff --git a/packages/cli/src/index.ts b/packages/cli/src/index.ts index 49f55c9..88a6a00 100644 --- a/packages/cli/src/index.ts +++ b/packages/cli/src/index.ts @@ -2,9 +2,7 @@ import * as c from "yoctocolors"; import pkg from "../package.json" with { type: "json" }; -import { - initCommand, -} from "./commands/index.js"; +import { initCommand } from "./commands/init.js"; const PADDING = 4; const INDENT = " "; diff --git a/packages/cli/src/generators/env.ts b/packages/cli/src/utils/env.ts similarity index 86% rename from packages/cli/src/generators/env.ts rename to packages/cli/src/utils/env.ts index 6a8b050..38712f9 100644 --- a/packages/cli/src/generators/env.ts +++ b/packages/cli/src/utils/env.ts @@ -15,6 +15,11 @@ function escapeEnvValue(value: string): string { .replace(/\r?\n/g, "\\n")}"`; // Escape newlines } +/** + * Generates an .env file for Myst with the given options. + * @param options - The options for generating the .env file. + * @returns {string} The .env file formatted as a string. + */ export function generateEnvFile(options: EnvOptions): string { return `# Public URL used for share links. MYST_PUBLIC_URL=${escapeEnvValue(options.publicUrl)}