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 {