-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathcli.ts
More file actions
executable file
·103 lines (86 loc) · 3.99 KB
/
cli.ts
File metadata and controls
executable file
·103 lines (86 loc) · 3.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#!/usr/bin/env bun
import { mkdir, writeFile, access, readFile, appendFile } from "node:fs/promises"
import { spawn } from "node:child_process"
const EXAMPLE_TEST = `import { expect, test } from "bun:test"
const testSvg = \`<svg width="100" height="100" xmlns="http://www.w3.org/2000/svg">
<circle cx="50" cy="50" r="40" stroke="black" stroke-width="3" fill="red" />
</svg>\`
test("svg snapshot example", async () => {
// First run will create the snapshot
// Subsequent runs will compare against the saved snapshot
await expect(testSvg).toMatchSvgSnapshot(import.meta.path)
})
`
const PRELOAD_FILE = `import "bun-match-svg"`
const BUNFIG = `[test]
preload = ["./tests/fixtures/preload.ts"]`
export async function installDependency() {
return new Promise((resolve, reject) => {
const install = spawn('bun', ['add', '-d', 'bun-match-svg'], {
stdio: 'inherit'
})
install.on('close', (code) => {
if (code === 0) {
resolve(undefined)
} else {
reject(new Error(`Installation failed with code ${code}`))
}
})
})
}
export async function fileExists(path: string): Promise<boolean> {
try {
await access(path)
return true
} catch {
return false
}
}
export async function init() {
try {
console.log("📦 Installing bun-match-svg...")
await installDependency()
await mkdir("tests/fixtures", { recursive: true })
if (!await fileExists("tests/svg.test.ts")) {
await writeFile("tests/svg.test.ts", EXAMPLE_TEST)
console.log("✅ Created example test in tests/svg.test.ts")
} else {
console.log("🔒 tests/svg.test.ts already exists, skipping.")
}
if (!await fileExists("tests/fixtures/preload.ts")) {
await writeFile("tests/fixtures/preload.ts", PRELOAD_FILE)
console.log("✅ Created preload file in tests/fixtures/preload.ts")
} else {
console.log("🔒 tests/fixtures/preload.ts already exists, skipping.")
}
if (!await fileExists("bunfig.toml")) {
await writeFile("bunfig.toml", BUNFIG)
console.log("✅ Created bunfig.toml")
} else {
const bunfigContent = await readFile("bunfig.toml", "utf-8")
if (bunfigContent.includes('preload = ["./tests/fixtures/preload.ts"]')) {
console.log("🔒 bunfig.toml already has preload configuration, skipping.")
} else if (bunfigContent.match(/^\s*\[test\]\s*$/m)) {
const updatedContent = bunfigContent.replace(
/(^\s*\[test\]\s*$)/m,
`$1\npreload = ["./tests/fixtures/preload.ts"]`,
)
await writeFile("bunfig.toml", updatedContent)
console.log("✅ Updated bunfig.toml with preload configuration.")
} else {
await appendFile("bunfig.toml", `\n\n${BUNFIG}`)
console.log("✅ Added preload configuration to bunfig.toml.")
}
}
console.log("\n🎉 You can now run: bun test")
} catch (error) {
console.error("❌ Error during initialization:", error)
process.exit(1)
}
}
const command = process.argv[2]
if (command === "init") {
init().catch(console.error)
} else {
console.log("Usage: bunx bun-match-svg init")
}