Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions fixtures/pathtests/readYAMLAll.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
abc: xyz
---
ijk: lmn
4 changes: 2 additions & 2 deletions src/deps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ export { outdent }
import * as crypto from "jsr:@std/crypto@1"
import { moveSync } from "jsr:@std/fs@1"
import { writeAll } from "jsr:@std/io@^0.225.0"
import { parse as parseYaml } from "jsr:@std/yaml@1"
import { parse as parseYaml, parseAll as parseYamlALL } from "jsr:@std/yaml@1"
import { SEPARATOR as SEP, fromFileUrl } from "jsr:@std/path@1"

const streams = { writeAll }
const fs = { moveSync }
const deno = { crypto, fs, streams, parseYaml, SEP, fromFileUrl }
const deno = { crypto, fs, streams, parseYaml, parseYamlALL, SEP, fromFileUrl }

export { deno }
38 changes: 37 additions & 1 deletion src/utils/Path.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { assert, assertEquals, assertFalse, assertThrows } from "@std/assert"
import { assert, assertEquals, assertFalse, assertThrows, fail } from "@std/assert"
import { SEPARATOR as SEP } from "jsr:@std/path@1"
import Path from "./Path.ts"

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

Deno.test("Path.readYAMLAll()", async () => {
const path = Path.cwd().join("./fixtures/pathtests/readYAMLAll.yaml");

try {
const yamlData = await path.readYAMLAll(); // ✅ Use await

assertEquals(Array.isArray(yamlData), true, "Expected yamlData to be an array");

if (!Array.isArray(yamlData)) {
fail("Expected an array");
return;
}

assertEquals(yamlData.length, 2, "Expected exactly 2 YAML documents");
assertEquals(yamlData, [{ abc: "xyz" }, { ijk: "lmn" }], "YAML content mismatch");

} catch (err) {
console.error("Error reading YAML:", err);
fail("Error reading YAML");
}
});

Deno.test("Path.readYAMLAllErr()", async () => {
const path = Path.cwd().join("./fixtures/pathtests/invalid.yaml");
try {
await path.readYAMLAll();
fail("invalid file should not reach here")
} catch (err) {
if (err instanceof Error) {
assertEquals(err.name, "NotFound")
} else{
throw err;
}
}
});

Deno.test("Path.chuzzle()", () => {
const path = Path.mktemp().join("file.txt").touch()
assertEquals(path.chuzzle(), path)
Expand Down
14 changes: 13 additions & 1 deletion src/utils/Path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { mkdtempSync } from "node:fs"
import * as sys from "node:path"
import * as os from "node:os"

const { fs, parseYaml, SEP } = deno
const { fs, parseYaml, parseYamlALL, SEP } = deno

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

Expand Down Expand Up @@ -411,6 +411,18 @@ export default class Path {
}
}

async readYAMLAll(): Promise<unknown> {
try {
const txt = await this.read()
return parseYamlALL(txt)
} catch (err) {
if (err instanceof Error) {
err.cause = this.string
}
throw err
}
}

readJSON(): Promise<unknown> {
return this.read().then(x => JSON.parse(x))
}
Expand Down