bun-match-svg 0.0.8 → 0.0.10

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. package/bun.lockb +0 -0
  2. package/cli.ts +33 -14
  3. package/index.ts +12 -4
  4. package/package.json +1 -1
package/bun.lockb CHANGED
Binary file
package/cli.ts CHANGED
@@ -1,10 +1,9 @@
1
1
  #!/usr/bin/env bun
2
2
 
3
- import { mkdir, writeFile } from "node:fs/promises"
3
+ import { mkdir, writeFile, access } from "node:fs/promises"
4
4
  import { spawn } from "node:child_process"
5
5
 
6
6
  const EXAMPLE_TEST = `import { expect, test } from "bun:test"
7
- import "bun-match-svg"
8
7
 
9
8
  const testSvg = \`<svg width="100" height="100" xmlns="http://www.w3.org/2000/svg">
10
9
  <circle cx="50" cy="50" r="40" stroke="black" stroke-width="3" fill="red" />
@@ -13,7 +12,7 @@ const EXAMPLE_TEST = `import { expect, test } from "bun:test"
13
12
  test("svg snapshot example", async () => {
14
13
  // First run will create the snapshot
15
14
  // Subsequent runs will compare against the saved snapshot
16
- await expect(testSvg).toMatchSvgSnapshot(import.meta.path, "example")
15
+ await expect(testSvg).toMatchSvgSnapshot(import.meta.path)
17
16
  })
18
17
  `
19
18
 
@@ -38,23 +37,43 @@ async function installDependency() {
38
37
  })
39
38
  }
40
39
 
40
+ async function fileExists(path: string): Promise<boolean> {
41
+ try {
42
+ await access(path)
43
+ return true
44
+ } catch {
45
+ return false
46
+ }
47
+ }
48
+
41
49
  async function init() {
42
50
  try {
43
51
  console.log("šŸ“¦ Installing bun-match-svg...")
44
52
  await installDependency()
45
53
 
46
54
  await mkdir("tests/fixtures", { recursive: true })
47
-
48
- await writeFile("tests/svg.test.ts", EXAMPLE_TEST)
49
-
50
- await writeFile("tests/fixtures/preload.ts", PRELOAD_FILE)
51
-
52
- await writeFile("bunfig.toml", BUNFIG)
53
-
54
- console.log("āœ… Installed bun-match-svg")
55
- console.log("āœ… Created example test in tests/svg.test.ts")
56
- console.log("āœ… Created preload file in tests/fixtures/preload.ts")
57
- console.log("āœ… Created bunfig.toml")
55
+
56
+ if (!await fileExists("tests/svg.test.ts")) {
57
+ await writeFile("tests/svg.test.ts", EXAMPLE_TEST)
58
+ console.log("āœ… Created example test in tests/svg.test.ts")
59
+ } else {
60
+ console.log("šŸ”’ tests/svg.test.ts already exists, skipping.")
61
+ }
62
+
63
+ if (!await fileExists("tests/fixtures/preload.ts")) {
64
+ await writeFile("tests/fixtures/preload.ts", PRELOAD_FILE)
65
+ console.log("āœ… Created preload file in tests/fixtures/preload.ts")
66
+ } else {
67
+ console.log("šŸ”’ tests/fixtures/preload.ts already exists, skipping.")
68
+ }
69
+
70
+ if (!await fileExists("bunfig.toml")) {
71
+ await writeFile("bunfig.toml", BUNFIG)
72
+ console.log("āœ… Created bunfig.toml")
73
+ } else {
74
+ console.log("šŸ”’ bunfig.toml already exists, skipping.")
75
+ }
76
+
58
77
  console.log("\nšŸŽ‰ You can now run: bun test")
59
78
  } catch (error) {
60
79
  console.error("āŒ Error during initialization:", error)
package/index.ts CHANGED
@@ -28,10 +28,14 @@ async function toMatchSvgSnapshot(
28
28
  Boolean(process.env["BUN_UPDATE_SNAPSHOTS"])
29
29
 
30
30
  if (!fs.existsSync(filePath) || updateSnapshot) {
31
- console.log("Writing snapshot to", filePath)
31
+ if (updateSnapshot && fs.existsSync(filePath)) {
32
+ console.log("Updating snapshot at", filePath)
33
+ } else {
34
+ console.log("Writing snapshot to", filePath)
35
+ }
32
36
  fs.writeFileSync(filePath, received)
33
37
  return {
34
- message: () => `Snapshot created at ${filePath}`,
38
+ message: () => `Snapshot ${updateSnapshot ? 'updated' : 'created'} at ${filePath}`,
35
39
  pass: true,
36
40
  }
37
41
  }
@@ -97,10 +101,14 @@ async function toMatchMultipleSvgSnapshots(
97
101
  Boolean(process.env["BUN_UPDATE_SNAPSHOTS"])
98
102
 
99
103
  if (!fs.existsSync(filePath) || updateSnapshot) {
100
- console.log("Writing snapshot to", filePath)
104
+ if (updateSnapshot && fs.existsSync(filePath)) {
105
+ console.log("Updating snapshot at", filePath)
106
+ } else {
107
+ console.log("Writing snapshot to", filePath)
108
+ }
101
109
  fs.writeFileSync(filePath, received[index] as any)
102
110
  passed.push({
103
- message: `Snapshot ${svgName} created at ${filePath}`,
111
+ message: `Snapshot ${svgName} ${updateSnapshot ? 'updated' : 'created'} at ${filePath}`,
104
112
  pass: true,
105
113
  })
106
114
  continue
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "bun-match-svg",
3
3
  "module": "index.ts",
4
- "version": "0.0.8",
4
+ "version": "0.0.10",
5
5
  "type": "module",
6
6
  "bin": {
7
7
  "bun-match-svg": "./cli.ts"