Skip to content

Commit 66a3e7d

Browse files
committed
fix: resolve packaged template path
1 parent e7ef69f commit 66a3e7d

3 files changed

Lines changed: 50 additions & 6 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@fba/cli",
3-
"version": "0.1.9",
3+
"version": "0.1.10",
44
"description": "Super CLI tool for creating, managing, and running fastapi-best-architecture projects",
55
"type": "module",
66
"license": "MIT",

src/lib/template.ts

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// template.ts — 插件模板引擎
22
import { readdir, readFile, writeFile, cp, rename, rm } from 'fs/promises'
3-
import { join, resolve, dirname } from 'path'
3+
import { existsSync } from 'fs'
4+
import { join, dirname } from 'path'
45
import { fileURLToPath } from 'url'
56

67
export interface TemplateVars {
@@ -16,10 +17,33 @@ export interface TemplateVars {
1617
* 获取 CLI 包内置的 templates 目录路径
1718
*/
1819
export function getTemplatesDir(): string {
19-
// 支持 bun 直接运行和 build 后运行
20-
const currentDir = dirname(fileURLToPath(import.meta.url))
21-
// src/lib/template.ts -> ../../templates
22-
return resolve(currentDir, '..', '..', 'templates')
20+
return resolveTemplatesDir(import.meta.url)
21+
}
22+
23+
/**
24+
* 从运行模块位置向上查找包内 templates 目录。
25+
* 支持源码运行 (src/lib/template.ts) 和打包后运行 (dist/*.js)。
26+
*/
27+
export function resolveTemplatesDir(moduleUrl: string): string {
28+
let currentDir = dirname(fileURLToPath(moduleUrl))
29+
30+
while (true) {
31+
const candidate = join(currentDir, 'templates')
32+
if (
33+
existsSync(join(candidate, 'web'))
34+
&& existsSync(join(candidate, 'server'))
35+
) {
36+
return candidate
37+
}
38+
39+
const parentDir = dirname(currentDir)
40+
if (parentDir === currentDir) break
41+
currentDir = parentDir
42+
}
43+
44+
throw new Error(
45+
`Unable to locate templates directory from ${fileURLToPath(moduleUrl)}`,
46+
)
2347
}
2448

2549
/**

tests/template.test.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { describe, expect, test } from 'bun:test'
2+
import { resolve } from 'path'
3+
import { pathToFileURL } from 'url'
4+
import { resolveTemplatesDir } from '../src/lib/template.ts'
5+
6+
const repoRoot = resolve(import.meta.dir, '..')
7+
8+
describe('resolveTemplatesDir', () => {
9+
test('resolves templates from the source module location', () => {
10+
const sourceUrl = pathToFileURL(resolve(repoRoot, 'src/lib/template.ts')).href
11+
12+
expect(resolveTemplatesDir(sourceUrl)).toBe(resolve(repoRoot, 'templates'))
13+
})
14+
15+
test('resolves templates from the bundled dist module location', () => {
16+
const bundledUrl = pathToFileURL(resolve(repoRoot, 'dist/create-EXAMPLE.js')).href
17+
18+
expect(resolveTemplatesDir(bundledUrl)).toBe(resolve(repoRoot, 'templates'))
19+
})
20+
})

0 commit comments

Comments
 (0)