|
| 1 | +import twDenoJson from "../plugin-tailwindcss/deno.json" with { type: "json" }; |
| 2 | +import * as Marked from "marked"; |
| 3 | +import { ensureDir, walk } from "@std/fs"; |
| 4 | +import { dirname, join, relative } from "@std/path"; |
| 5 | +// import { expect } from "@std/expect/expect"; |
| 6 | +import { withTmpDir } from "../src/test_utils.ts"; |
| 7 | +import { FRESH_VERSION, PREACT_VERSION } from "../update/src/update.ts"; |
| 8 | + |
| 9 | +Deno.test("Docs Code example checks", async () => { |
| 10 | + await using tmp = await withTmpDir(); |
| 11 | + |
| 12 | + for await (const { path, code } of docsMarkdownFiles()) { |
| 13 | + const codePath = join(tmp.dir, path); |
| 14 | + await ensureDir(dirname(codePath)); |
| 15 | + await Deno.writeTextFile(codePath, code); |
| 16 | + } |
| 17 | + |
| 18 | + const denoJson = { |
| 19 | + lock: false, |
| 20 | + imports: { |
| 21 | + fresh: `jsr:@fresh/core@${FRESH_VERSION}`, |
| 22 | + "@fresh/plugin-tailwind-v3": |
| 23 | + `jsr:@fresh/plugin-tailwind@^${twDenoJson.version}`, |
| 24 | + "@fresh/plugin-tailwind": |
| 25 | + `jsr:@fresh/plugin-tailwind@^${twDenoJson.version}`, |
| 26 | + preact: `npm:preact@^${PREACT_VERSION}`, |
| 27 | + "@deno/gfm": "jsr:@deno/gfm@^0.11.0", |
| 28 | + "@std/expect": "jsr:@std/expect@^1.0.16", |
| 29 | + }, |
| 30 | + compilerOptions: { |
| 31 | + lib: ["dom", "dom.asynciterable", "deno.ns", "deno.unstable"], |
| 32 | + jsx: "precompile", |
| 33 | + jsxImportSource: "preact", |
| 34 | + jsxPrecompileSkipElements: ["a", "img", "source", "body", "html", "head"], |
| 35 | + }, |
| 36 | + }; |
| 37 | + await Deno.writeTextFile( |
| 38 | + join(tmp.dir, "deno.json"), |
| 39 | + JSON.stringify(denoJson, undefined, 2), |
| 40 | + ); |
| 41 | + |
| 42 | + // Download and cache all dependencies (reduces `stdout` noise) |
| 43 | + await new Deno.Command(Deno.execPath(), { |
| 44 | + args: ["cache", "**/*"], |
| 45 | + cwd: tmp.dir, |
| 46 | + }).output(); |
| 47 | + |
| 48 | + const { stdout, stderr } = await new Deno.Command(Deno.execPath(), { |
| 49 | + args: ["check", "**/*"], |
| 50 | + cwd: tmp.dir, |
| 51 | + }).output(); |
| 52 | + |
| 53 | + const decoder = new TextDecoder(); |
| 54 | + const output = `${decoder.decode(stdout)}\n${decoder.decode(stderr)}`; |
| 55 | + // Log `deno check` output (can be removed if expects below are enabled) |
| 56 | + // deno-lint-ignore no-console |
| 57 | + console.log(output); |
| 58 | + |
| 59 | + // TODO: Enable after fixing docs check issues |
| 60 | + // expect(code).toBe(0); |
| 61 | + // expect(output).toBe(""); |
| 62 | +}); |
| 63 | + |
| 64 | +async function* docsMarkdownFiles() { |
| 65 | + // Limit to checking Fresh v2 (canary) docs for now |
| 66 | + const docsDir = join(import.meta.dirname!, "..", "docs", "canary"); |
| 67 | + const docsIter = walk(docsDir, { exts: [".md"], includeDirs: false }); |
| 68 | + |
| 69 | + for await (const entry of docsIter) { |
| 70 | + for (const { file, code } of await extractTsCode(entry.path)) { |
| 71 | + const path = join(relative(docsDir, entry.path), file); |
| 72 | + yield { path, code }; |
| 73 | + } |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +async function extractTsCode(path: string) { |
| 78 | + const code: { file: string; code: string }[] = []; |
| 79 | + const input = await Deno.readTextFile(path); |
| 80 | + let index = 0; |
| 81 | + |
| 82 | + const tokens = await Marked.lexer(input, { gfm: true, async: true }); |
| 83 | + |
| 84 | + Marked.walkTokens(tokens, (token) => { |
| 85 | + // Get rid of `Marked.Tokens.Generic` |
| 86 | + const t = token as Marked.MarkedToken; |
| 87 | + if (t.type !== "code") return; |
| 88 | + |
| 89 | + const result = /^([tj]sx?)\s*/.exec(t.lang ?? ""); |
| 90 | + if (!result) return; |
| 91 | + |
| 92 | + // Codeblock must be TS/JS |
| 93 | + index += 1; |
| 94 | + |
| 95 | + // Probably a filename, but perhaps a title, |
| 96 | + // so get rid of non-file safe characters |
| 97 | + const [match, ext] = result; |
| 98 | + let file = t.lang!.slice(match.length) |
| 99 | + .toLocaleLowerCase() |
| 100 | + .replaceAll(/[^a-z0-9._\-\/\\]/g, "_") |
| 101 | + .replaceAll(/_{2,}/g, "_") || String(index); |
| 102 | + |
| 103 | + if (!file.endsWith(ext)) file += `.${ext}`; |
| 104 | + |
| 105 | + code.push({ file, code: t.text }); |
| 106 | + }); |
| 107 | + |
| 108 | + return code; |
| 109 | +} |
0 commit comments