From e053be2c3e45445877ac1772b8e80e04ae18bd1c Mon Sep 17 00:00:00 2001 From: fischer Date: Mon, 25 May 2026 10:19:46 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8DCLI=E6=A8=A1=E6=9D=BF?= =?UTF-8?q?=E8=B7=AF=E5=BE=84=E8=A7=A3=E6=9E=90bug?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tools/cli/src/utils/index.ts | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/tools/cli/src/utils/index.ts b/tools/cli/src/utils/index.ts index 8dde1d8..3671343 100644 --- a/tools/cli/src/utils/index.ts +++ b/tools/cli/src/utils/index.ts @@ -1,17 +1,28 @@ import fs from 'fs-extra'; import path from 'path'; -import { fileURLToPath } from 'url'; import type { TemplateVariables } from '../types.js'; -const __filename = fileURLToPath(import.meta.url); -const __dirname = path.dirname(__filename); - export function getTemplatesDir(): string { - return path.resolve(__dirname, '../../templates'); + const cliRoot = findCliRoot(); + return path.join(cliRoot, 'templates'); +} + +function findCliRoot(): string { + let current = path.resolve(process.argv[1] || __dirname); + while (current !== path.dirname(current)) { + if (fs.existsSync(path.join(current, 'package.json'))) { + const pkg = fs.readJsonSync(path.join(current, 'package.json')); + if (pkg.name === '@fischerx/cli') { + return current; + } + } + current = path.dirname(current); + } + return path.resolve(__dirname, '..'); } export function getPackageRoot(): string { - return path.resolve(__dirname, '../..'); + return findCliRoot(); } export function getProjectRoot(): string {