From 17a20b86ff0283eec291fa9bc30e0729fa52f2bd Mon Sep 17 00:00:00 2001 From: Dan Abramov Date: Fri, 10 Oct 2025 11:45:13 +0100 Subject: [PATCH] fix --- packages/cli/src/commands/init.ts | 97 ++++++++++++++++--------------- 1 file changed, 50 insertions(+), 47 deletions(-) diff --git a/packages/cli/src/commands/init.ts b/packages/cli/src/commands/init.ts index aed0ee8..5abc0e0 100644 --- a/packages/cli/src/commands/init.ts +++ b/packages/cli/src/commands/init.ts @@ -1,5 +1,5 @@ import { resolve, relative } from "path"; -import { mkdir, writeFile, readFile, access } from "fs/promises"; +import { mkdir, writeFile, readFile, access, stat } from "fs/promises"; import { spawn } from "child_process"; import { createInterface } from "readline"; @@ -30,7 +30,7 @@ async function promptNamespace(): Promise { }); return new Promise((resolve) => { - rl.question("Namespace (e.g., com.example.*): ", (answer) => { + rl.question("\x1b[36mYour app's namespace (e.g., com.example.*): \x1b[0m", (answer) => { rl.close(); resolve(answer.trim()); }); @@ -47,53 +47,27 @@ export async function initCommand(): Promise { const externalsTspPath = resolve(typelexDir, "externals.tsp"); console.log("Initializing typelex project...\n"); - console.log("Installing dependencies...\n"); + console.log("Installing dependencies..."); - // Detect package manager and lexicons directory by walking up directory tree + // Detect package manager: walk up from cwd let packageManager = "npm"; - let lexiconsDir: string | null = null; - let currentDir = cwd; - const root = resolve("/"); - - // First, move up one directory (we only want lexicons from parent directories) - const parentDir = resolve(currentDir, ".."); - if (parentDir !== currentDir) { - currentDir = parentDir; - - while (currentDir !== root) { - // Check for package manager lockfiles - if (packageManager === "npm") { - try { - await access(resolve(currentDir, "pnpm-lock.yaml")); - packageManager = "pnpm"; - } catch { - try { - await access(resolve(currentDir, "yarn.lock")); - packageManager = "yarn"; - } catch { - // Continue - } - } - } - - // Check for lexicons directory (only in parent directories) - if (!lexiconsDir) { - try { - const lexiconsPath = resolve(currentDir, "lexicons"); - await access(lexiconsPath); - // Calculate relative path from cwd to this lexicons directory - const relativePath = relative(cwd, lexiconsPath); - lexiconsDir = relativePath; - } catch { - // Continue - } - } - - // Move up one directory - const nextParent = resolve(currentDir, ".."); - if (nextParent === currentDir) break; // Reached root - currentDir = nextParent; + let dir = cwd; + while (dir !== resolve(dir, "..") && packageManager === "npm") { + try { + await access(resolve(dir, "pnpm-lock.yaml")); + packageManager = "pnpm"; + break; + } catch { + // Not found } + try { + await access(resolve(dir, "yarn.lock")); + packageManager = "yarn"; + break; + } catch { + // Not found + } + dir = resolve(dir, ".."); } // Install dependencies and only proceed if successful @@ -136,6 +110,33 @@ export async function initCommand(): Promise { // Remove the .* suffix for use in template const namespacePrefix = namespace.slice(0, -2); + // Detect lexicons directory: check cwd first, then walk up parents + let lexiconsDir: string | null = null; + let hasLocalLexicons = false; + + // Check current directory for lexicons/ (will use default, no --out flag needed) + try { + const localPath = resolve(cwd, "lexicons"); + if ((await stat(localPath)).isDirectory()) { + hasLocalLexicons = true; + } + } catch { + // Not found in current directory, check parent directories + let dir = resolve(cwd, ".."); + while (dir !== resolve(dir, "..")) { + try { + const lexPath = resolve(dir, "lexicons"); + if ((await stat(lexPath)).isDirectory()) { + lexiconsDir = relative(cwd, lexPath); + break; + } + } catch { + // Not found, continue up + } + dir = resolve(dir, ".."); + } + } + // Create typelex directory await mkdir(typelexDir, { recursive: true }); @@ -173,7 +174,9 @@ export async function initCommand(): Promise { packageJson.scripts["build:typelex"] = `typelex compile ${namespace}${outFlag}`; await writeFile(packageJsonPath, JSON.stringify(packageJson, null, 2) + "\n", "utf-8"); console.log("✓ Added build:typelex script to package.json"); - if (lexiconsDir) { + if (hasLocalLexicons) { + console.log(` Using existing lexicons directory: ./lexicons`); + } else if (lexiconsDir) { console.log(` Using existing lexicons directory: ${lexiconsDir}`); } } else { -- 2.51.2