Skip to content

Commit

Permalink
feat: added test script and validation
Browse files Browse the repository at this point in the history
  • Loading branch information
lmammino committed Mar 21, 2024
1 parent c33f6c9 commit 6254e55
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 5 deletions.
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
"main": "index.js",
"type": "module",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"lint:biome": "biome lint .",
"lint:biome:format": "biome format .",
"validate": "tsx scripts/validate-books.ts",
"test": "pnpm run lint:biome && pnpm run lint:biome:format && pnpm run validate",
"build": "rm -rf dist && tsx scripts/build.ts"
},
"keywords": [],
Expand All @@ -16,6 +19,7 @@
"@biomejs/biome": "1.6.1",
"@types/node": "^20.11.28",
"@types/turndown": "^5.0.4",
"ajv": "^8.12.0",
"cheerio": "1.0.0-rc.12",
"marked": "^12.0.1",
"mkdirp": "^3.0.1",
Expand Down
36 changes: 36 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 1 addition & 3 deletions scripts/build.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { cp, readFile, writeFile } from 'node:fs/promises'
import { dirname, join } from 'node:path'
import { fileURLToPath } from 'node:url'
// biome-ignore lint/nursery/noUndeclaredDependencies: <explanation>
import { marked } from 'marked'
import { mkdirp } from 'mkdirp'
import slugify from 'slugify'
Expand All @@ -12,7 +10,7 @@ const REPO_URL = 'https://github.com/FullStackBulletin/fullstack-books'
const GH_PAGES_URL = 'https://fullStackbulletin.github.io/fullstack-books'
const baseUrl = process.env.BASE_URL ?? GH_PAGES_URL

const __dirname = dirname(fileURLToPath(import.meta.url))
const __dirname = dirname(new URL(import.meta.url).pathname)
const destPath = join(__dirname, '..', 'dist')
const booksPath = join(__dirname, '..', 'dist', 'books')
const authorsPath = join(__dirname, '..', 'dist', 'authors')
Expand Down
17 changes: 17 additions & 0 deletions scripts/validate-books.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { readFile } from 'node:fs/promises'
import { dirname, join } from 'node:path'
import Ajv from 'ajv'
import { parse } from 'yaml'

const __dirname = dirname(new URL(import.meta.url).pathname)
const sourcePath = join(__dirname, '..', 'src', 'books.yml')
const schemaPath = join(__dirname, '..', 'src', 'raw-books.schema.json')
const data = parse(await readFile(sourcePath, 'utf-8'))
const schema = parse(await readFile(schemaPath, 'utf-8'))

const ajv = new Ajv.default()

if (!ajv.validate(schema, data)) {
console.error(ajv.errors)
process.exit(1)
}
6 changes: 5 additions & 1 deletion src/raw-books.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
"subtitle": {
"type": "string"
},
"cover": {
"type": "string"
},
"edition": {
"type": "number"
},
Expand Down Expand Up @@ -45,6 +48,7 @@
"type": "string"
}
},
"required": ["title", "authors", "links", "description"]
"required": ["title", "authors", "links", "description"],
"additionalProperties": false
}
}

0 comments on commit 6254e55

Please sign in to comment.