Skip to content

Commit 89799d1

Browse files
committed
fix(path): add util function to read multi document yaml file
1 parent b59b1a0 commit 89799d1

File tree

4 files changed

+55
-4
lines changed

4 files changed

+55
-4
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
abc: xyz
2+
---
3+
ijk: lmn

src/deps.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ export { outdent }
1111
import * as crypto from "jsr:@std/crypto@1"
1212
import { moveSync } from "jsr:@std/fs@1"
1313
import { writeAll } from "jsr:@std/io@^0.225.0"
14-
import { parse as parseYaml } from "jsr:@std/yaml@1"
14+
import { parse as parseYaml, parseAll as parseYAMLAll } from "jsr:@std/yaml@1"
1515
import { SEPARATOR as SEP, fromFileUrl } from "jsr:@std/path@1"
1616

1717
const streams = { writeAll }
1818
const fs = { moveSync }
19-
const deno = { crypto, fs, streams, parseYaml, SEP, fromFileUrl }
19+
const deno = { crypto, fs, streams, parseYaml, parseYAMLAll, SEP, fromFileUrl }
2020

2121
export { deno }

src/utils/Path.test.ts

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { assert, assertEquals, assertFalse, assertThrows } from "@std/assert"
1+
import { assert, assertEquals, assertFalse, assertThrows, fail } from "@std/assert"
22
import { SEPARATOR as SEP } from "jsr:@std/path@1"
33
import Path from "./Path.ts"
44

@@ -199,6 +199,42 @@ Deno.test("Path.prettyLocalString()", () => {
199199
assertEquals(new Path("/a/b").prettyLocalString(), `${root}a${SEP}b`)
200200
})
201201

202+
Deno.test("Path.readYAMLAll()", async () => {
203+
const path = Path.cwd().join("./fixtures/pathtests/readYAMLAll.yaml");
204+
205+
try {
206+
const yamlData = await path.readYAMLAll(); // ✅ Use await
207+
208+
assertEquals(Array.isArray(yamlData), true, "Expected yamlData to be an array");
209+
210+
if (!Array.isArray(yamlData)) {
211+
fail("Expected an array");
212+
return;
213+
}
214+
215+
assertEquals(yamlData.length, 2, "Expected exactly 2 YAML documents");
216+
assertEquals(yamlData, [{ abc: "xyz" }, { ijk: "lmn" }], "YAML content mismatch");
217+
218+
} catch (err) {
219+
console.error("Error reading YAML:", err);
220+
fail("Error reading YAML");
221+
}
222+
});
223+
224+
Deno.test("Path.readYAMLAllErr()", async () => {
225+
const path = Path.cwd().join("./fixtures/pathtests/invalid.yaml");
226+
try {
227+
await path.readYAMLAll();
228+
fail("invalid file should not reach here")
229+
} catch (err) {
230+
if (err instanceof Error) {
231+
assertEquals(err.name, "NotFound")
232+
} else{
233+
throw err;
234+
}
235+
}
236+
});
237+
202238
Deno.test("Path.chuzzle()", () => {
203239
const path = Path.mktemp().join("file.txt").touch()
204240
assertEquals(path.chuzzle(), path)

src/utils/Path.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { mkdtempSync } from "node:fs"
44
import * as sys from "node:path"
55
import * as os from "node:os"
66

7-
const { fs, parseYaml, SEP } = deno
7+
const { fs, parseYaml, parseYAMLAll, SEP } = deno
88

99
// modeled after https://github.com/mxcl/Path.swift
1010

@@ -411,6 +411,18 @@ export default class Path {
411411
}
412412
}
413413

414+
async readYAMLAll(): Promise<unknown> {
415+
try {
416+
const txt = await this.read()
417+
return parseYAMLAll(txt)
418+
} catch (err) {
419+
if (err instanceof Error) {
420+
err.cause = this.string
421+
}
422+
throw err
423+
}
424+
}
425+
414426
readJSON(): Promise<unknown> {
415427
return this.read().then(x => JSON.parse(x))
416428
}

0 commit comments

Comments
 (0)