0.3.0
Publish npm package / publish (push) Canceled after 0s

This commit is contained in:
2026-10-07 15:30:39 +02:00
parent 452d872954
commit ed5a666ad3
11 changed files with 148 additions and 19 deletions
+70 -1
View File
@@ -1,5 +1,5 @@
import { expect, test, beforeEach, afterEach } from 'bun:test'
import { initRenderer, reverseDir, SchemaMismatchError } from "./index";
import { initRenderer, reverseDir, SchemaMismatchError, TargetExistsError } from "./index";
import { z } from "zod"
import { mkdtempSync, rmSync, existsSync, readFileSync, writeFileSync, mkdirSync } from "node:fs"
import { basename, join } from "node:path"
@@ -655,3 +655,72 @@ test("and or in path", () => {
render(tmp,{enabled:false, kind:"docs"})
expect(existsSync(join(tmp,"match"))).toBe(false)
})
test("rename maps output names and records them in the reverse map", () => {
const templateDir = join(tmp, "template")
mkdirSync(join(templateDir, "<@if(context.web)>web"), { recursive: true })
writeFileSync(join(templateDir, "_gitignore"), "node_modules\n")
writeFileSync(join(templateDir, "<@if(context.web)>web", "_gitignore"), "dist\n")
const render = initRenderer(templateDir)(z.object({ web: z.boolean() }))
const out = join(tmp, "out")
render(out, { web: true }, { reverseMap: true, rename: { _gitignore: ".gitignore" } })
expect(readFileSync(join(out, ".gitignore"), "utf-8")).toBe("node_modules\n")
expect(readFileSync(join(out, "web", ".gitignore"), "utf-8")).toBe("dist\n")
expect(existsSync(join(out, "_gitignore"))).toBe(false)
const manifest = JSON.parse(readFileSync(join(out, ".tdir-map.json"), "utf-8"))
expect(manifest.files).toContainEqual(expect.objectContaining({ outputPath: ".gitignore", templatePath: "_gitignore" }))
expect(manifest.files).toContainEqual(expect.objectContaining({
outputPath: join("web", ".gitignore"),
templatePath: join("<@if(context.web)>web", "_gitignore"),
}))
const reversedDir = join(tmp, "reversed")
reverseDir(out, reversedDir)
expect(readFileSync(join(reversedDir, "_gitignore"), "utf-8")).toBe("node_modules\n")
})
test("rename applies to directories", () => {
const templateDir = join(tmp, "template")
mkdirSync(join(templateDir, "_github"), { recursive: true })
writeFileSync(join(templateDir, "_github", "ci.yml"), "on: push\n")
const render = initRenderer(templateDir)(z.object({}))
const out = join(tmp, "out")
render(out, {}, { rename: { _github: ".github" } })
expect(readFileSync(join(out, ".github", "ci.yml"), "utf-8")).toBe("on: push\n")
})
test("overwrite false refuses a non-empty target and leaves it untouched", () => {
const render = initRenderer("./testdata/if_example")(ifExampleSchema)
const out = join(tmp, "out")
mkdirSync(out)
writeFileSync(join(out, "keep.txt"), "important")
expect(() => render(out, { web: true, header: { render: true, text: "T" } }, { overwrite: false })).toThrow(TargetExistsError)
expect(readFileSync(join(out, "keep.txt"), "utf-8")).toBe("important")
})
test("overwrite false renders into a missing or empty target", () => {
const render = initRenderer("./testdata/if_example")(ifExampleSchema)
const missing = join(tmp, "missing")
const empty = join(tmp, "empty")
mkdirSync(empty)
render(missing, { web: true, header: { render: true, text: "T" } }, { overwrite: false })
render(empty, { web: true, header: { render: true, text: "T" } }, { overwrite: false })
expect(existsSync(join(missing, "web", "if_example.html"))).toBe(true)
expect(existsSync(join(empty, "web", "if_example.html"))).toBe(true)
})
test("overwrite false refuses when the target is a file", () => {
const render = initRenderer("./testdata/if_example")(ifExampleSchema)
const out = join(tmp, "out")
writeFileSync(out, "not a directory")
expect(() => render(out, { web: true, header: { render: true, text: "T" } }, { overwrite: false })).toThrow(TargetExistsError)
expect(readFileSync(out, "utf-8")).toBe("not a directory")
})