fix: 修复CLI模板路径解析bug

This commit is contained in:
fischer 2026-05-25 10:19:46 +08:00
parent af4de6b86a
commit e053be2c3e
1 changed files with 17 additions and 6 deletions

View File

@ -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 {