Procházet zdrojové kódy

fix: support inkos init without arguments for current directory

Previously `inkos init` always created a subdirectory, requiring an
extra `cd` step. Now omitting the name argument initializes the
current directory directly, deriving the project name from basename.
majx_mac před 8 měsíci
rodič
revize
31c91ab41e
1 změnil soubory, kde provedl 9 přidání a 10 odebrání
  1. 9 10
      packages/cli/src/commands/init.ts

+ 9 - 10
packages/cli/src/commands/init.ts

@@ -1,6 +1,6 @@
 import { Command } from "commander";
 import { Command } from "commander";
 import { readFile, writeFile, mkdir } from "node:fs/promises";
 import { readFile, writeFile, mkdir } from "node:fs/promises";
-import { join } from "node:path";
+import { join, basename } from "node:path";
 import { log, logError, GLOBAL_ENV_PATH } from "../utils.js";
 import { log, logError, GLOBAL_ENV_PATH } from "../utils.js";
 
 
 async function hasGlobalConfig(): Promise<boolean> {
 async function hasGlobalConfig(): Promise<boolean> {
@@ -13,10 +13,11 @@ async function hasGlobalConfig(): Promise<boolean> {
 }
 }
 
 
 export const initCommand = new Command("init")
 export const initCommand = new Command("init")
-  .description("Initialize a new InkOS project")
-  .argument("[name]", "Project name", "my-novel-project")
-  .action(async (name: string) => {
-    const projectDir = join(process.cwd(), name);
+  .description("Initialize an InkOS project (current directory by default)")
+  .argument("[name]", "Project name (creates subdirectory). Omit to init current directory.")
+  .action(async (name?: string) => {
+    const projectDir = name ? join(process.cwd(), name) : process.cwd();
+    const projectName = name ?? basename(projectDir);
 
 
     try {
     try {
       await mkdir(projectDir, { recursive: true });
       await mkdir(projectDir, { recursive: true });
@@ -24,7 +25,7 @@ export const initCommand = new Command("init")
       await mkdir(join(projectDir, "radar"), { recursive: true });
       await mkdir(join(projectDir, "radar"), { recursive: true });
 
 
       const config = {
       const config = {
-        name,
+        name: projectName,
         version: "0.1.0",
         version: "0.1.0",
         llm: {
         llm: {
           provider: process.env.INKOS_LLM_PROVIDER ?? "openai",
           provider: process.env.INKOS_LLM_PROVIDER ?? "openai",
@@ -51,7 +52,6 @@ export const initCommand = new Command("init")
       const global = await hasGlobalConfig();
       const global = await hasGlobalConfig();
 
 
       if (global) {
       if (global) {
-        // Global config exists — project .env only for overrides
         await writeFile(
         await writeFile(
           join(projectDir, ".env"),
           join(projectDir, ".env"),
           [
           [
@@ -66,7 +66,6 @@ export const initCommand = new Command("init")
           "utf-8",
           "utf-8",
         );
         );
       } else {
       } else {
-        // No global config — full .env template
         await writeFile(
         await writeFile(
           join(projectDir, ".env"),
           join(projectDir, ".env"),
           [
           [
@@ -105,11 +104,11 @@ export const initCommand = new Command("init")
         log("Global LLM config detected. Ready to go!");
         log("Global LLM config detected. Ready to go!");
         log("");
         log("");
         log("Next steps:");
         log("Next steps:");
-        log(`  cd ${name}`);
+        if (name) log(`  cd ${name}`);
         log("  inkos book create --title '我的小说' --genre xuanhuan --platform tomato");
         log("  inkos book create --title '我的小说' --genre xuanhuan --platform tomato");
       } else {
       } else {
         log("Next steps:");
         log("Next steps:");
-        log(`  cd ${name}`);
+        if (name) log(`  cd ${name}`);
         log("  # Option 1: Set global config (recommended, one-time):");
         log("  # Option 1: Set global config (recommended, one-time):");
         log("  inkos config set-global --provider openai --base-url https://api.openai.com/v1 --api-key sk-xxx --model gpt-4o");
         log("  inkos config set-global --provider openai --base-url https://api.openai.com/v1 --api-key sk-xxx --model gpt-4o");
         log("  # Option 2: Edit .env for this project only");
         log("  # Option 2: Edit .env for this project only");