• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

hiddentao / lcoview / 18675491117

21 Oct 2025 06:54AM UTC coverage: 99.721% (-0.3%) from 100.0%
18675491117

push

github

hiddentao
fix: resolve template loading path for global npm installations

Replace hardcoded relative path (../../templates) with dynamic package root detection that searches upward for package.json. This fixes template not found errors when the package is installed globally via npm/bun.

11 of 12 new or added lines in 1 file covered. (91.67%)

358 of 359 relevant lines covered (99.72%)

34.61 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

97.44
/src/utils/template.ts
1
import { existsSync } from "node:fs"
37✔
2
import { readFile } from "node:fs/promises"
44✔
3
import { dirname, join } from "node:path"
42✔
4
import { fileURLToPath } from "node:url"
41✔
5

6
const __filename = fileURLToPath(import.meta.url)
50✔
7
const __dirname = dirname(__filename)
37✔
8

9
function findPackageRoot(startDir: string): string {
14✔
10
  let currentDir = startDir
28✔
11

12
  while (currentDir !== dirname(currentDir)) {
47✔
13
    const packageJsonPath = join(currentDir, "package.json")
61✔
14
    if (existsSync(packageJsonPath)) {
39✔
15
      return currentDir
17✔
16
    }
4✔
17
    currentDir = dirname(currentDir)
35✔
NEW
18
  }
×
19

20
  throw new Error("Could not find package.json in any parent directory")
1✔
21
}
22

23
export async function loadTemplate(templateName: string): Promise<string> {
22✔
24
  const packageRoot = findPackageRoot(__dirname)
49✔
25
  const paths = [
19✔
26
    join(packageRoot, "templates", templateName),
49✔
27
    join(process.cwd(), "templates", templateName),
48✔
28
  ]
4✔
29

30
  for (const path of paths) {
30✔
31
    if (existsSync(path)) {
28✔
32
      return await readFile(path, "utf-8")
36✔
33
    }
2✔
34
  }
2✔
35

36
  throw new Error(
16✔
37
    `Template not found: ${templateName} (searched: ${paths.join(", ")})`,
69✔
38
  )
2✔
39
}
40

41
export function renderTemplate(
6✔
42
  template: string,
10✔
43
  replacements: Record<string, string>,
14✔
44
): string {
3✔
45
  let result = template
24✔
46
  for (const [key, value] of Object.entries(replacements)) {
61✔
47
    result = result.replaceAll(`{{${key}}}`, value)
50✔
48
  }
2✔
49
  return result
13✔
50
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc