From 5aac3fe9f9ccd18d86f7b82130da6d79211a8eae Mon Sep 17 00:00:00 2001 From: Kyle Carberry Date: Tue, 26 Sep 2023 09:11:11 -0500 Subject: [PATCH] Add linting --- .github/workflows/ci.yaml | 3 +- bun.lockb | Bin 1269 -> 1638 bytes lint.ts | 61 ++++++++++++++++++++++++++++++++++++++ package.json | 6 ++-- tsconfig.json | 1 + 5 files changed, 68 insertions(+), 3 deletions(-) create mode 100644 lint.ts diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 9ee005c..96bb4e5 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -21,7 +21,7 @@ jobs: with: bun-version: latest - run: bun test - fmt: + pretty: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 @@ -29,3 +29,4 @@ jobs: with: bun-version: latest - run: bun fmt:ci + - run: bun lint diff --git a/bun.lockb b/bun.lockb index dfed91948022558cca9901420988b9f7b8ce4291..8a29fd78271797e5e89e0faa97f21da167f2e17d 100755 GIT binary patch delta 588 zcmey$`HW|Ro@UvT3#I01t9MIy8CPo+d^@#5V|lddv|yI*rAa2C@#Z1dSQ)^8d1AP{ zbvZMH!@!W6pHiBWTFj7}Sd^Wb!oa`HFlJZ3Nk7{m z^je8=G9#n-#2*@hOi&XPfV2rv1Lwq#`kXMqkcls?SwVI%Py8s%3uZu-Gf%c;Eb&>= z|Kw54LFwQltAom80@;s7KkhvKc>1Xa8OK%?Md?W@^SAk3xZ`pA=9I$Y-vTU;n11(n zKN}^+5vKL3>A{wLj7x!rF*5w)|HsI{z%+RwW4MXS@8WH%Rzd6lIRfS#VIZ3q=sJ)L zy4d7EMuW*7Oh%Iv7!A1&KttpT^W+vL2U(!q!sKh77&YpfrDl8Jtj3ikdOsb z$OR;%Bn6TM=>bUrUB*xWl|IHY*@;&VDl!sGU`wKz#<5f1gQnFnI_L;HcS2A0Vi8I324FdK0lfEsXxX>ud8gVewO5CBpP)(&+a z3-jcA%xYW)P_6}tt0V+c3sRK<6`sR9*^fnQat({R5ZqnQpmNV9H!zD%e!`-|B>)Y^ jSeD6BtO`OUl?AEA$wiq3CBVSZ1@R`wv&u|f&$=A|LHlIX diff --git a/lint.ts b/lint.ts new file mode 100644 index 0000000..235cf35 --- /dev/null +++ b/lint.ts @@ -0,0 +1,61 @@ +import { readFile, readdir, stat } from "fs/promises"; +import * as path from "path"; +import * as marked from "marked"; + +const files = await readdir(".", { withFileTypes: true }); +const dirs = files.filter( + (f) => f.isDirectory() && !f.name.startsWith(".") && f.name !== "node_modules" +); + +let badExit = false + +// Ensures that each README has the proper format. +// Exits with 0 if all is good! +for (const dir of dirs) { + const readme = path.join(dir.name, "README.md"); + // Ensure exists + try { + await stat(readme); + } catch (ex) { + throw new Error(`Missing README.md in ${dir.name}`); + } + const content = await readFile(readme, "utf8"); + const tokens = marked.lexer(content); + // Ensure there is an h1 and some text, then a code block + + let h1 = false; + let code = false; + let paragraph = false; + + for (const token of tokens) { + if (token.type === "heading" && token.depth === 1) { + h1 = true; + continue; + } + if (h1 && token.type === "heading") { + break; + } + if (token.type === "paragraph") { + paragraph = true; + continue; + } + if (token.type === "code") { + code = true; + continue; + } + } + if (!h1) { + console.error(dir.name, "missing h1") + } + if (!paragraph) { + console.error(dir.name, "missing paragraph after h1") + } + if (!code) { + console.error(dir.name, "missing example code block after paragraph") + } + badExit = true +} + +if (badExit) { + process.exit(1) +} diff --git a/package.json b/package.json index c42b502..9dacc26 100644 --- a/package.json +++ b/package.json @@ -3,10 +3,12 @@ "scripts": { "test": "bun test", "fmt": "bun x prettier -w **/*.ts **/*.md *.md && terraform fmt **/*.tf", - "fmt:ci": "bun x prettier --check **/*.ts **/*.md *.md && terraform fmt -check **/*.tf" + "fmt:ci": "bun x prettier --check **/*.ts **/*.md *.md && terraform fmt -check **/*.tf", + "lint": "bun run lint.ts" }, "devDependencies": { - "bun-types": "^1.0.3" + "bun-types": "^1.0.3", + "marked": "^9.0.3" }, "peerDependencies": { "typescript": "^5.0.0" diff --git a/tsconfig.json b/tsconfig.json index 86140a5..dfa1fff 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -2,6 +2,7 @@ "compilerOptions": { "target": "esnext", "module": "esnext", + "moduleResolution": "nodenext", "types": ["bun-types"] } }