use JS build script

db-rest equivalent of 605085229e
This commit is contained in:
Jannis R 2023-04-20 00:12:19 +02:00
parent ba256397d0
commit 9879365e9b
No known key found for this signature in database
GPG key ID: 0FE83946296A88A5
3 changed files with 98 additions and 35 deletions

View file

@ -1,5 +1,5 @@
import {generateApiDocs} from 'hafas-rest-api/tools/generate-docs.js' import {generateApiDocs} from 'hafas-rest-api/tools/generate-docs.js'
import {api} from './api.js' import {api} from '../api.js'
const HEAD = `\ const HEAD = `\
# \`v6.db.transport.rest\` API documentation # \`v6.db.transport.rest\` API documentation
@ -416,40 +416,42 @@ curl "https://v6.db.transport.rest/radar?$bbox&results=10" -s | jq
`, `,
} }
const { const generateMarkdownApiDocs = async function* () {
listOfRoutes, const {
routes, listOfRoutes,
tail, routes,
} = generateApiDocs(api.routes) tail,
} = generateApiDocs(api.routes)
const sortedRoutes = Object.entries(routes) const sortedRoutes = Object.entries(routes)
.sort(([routeA], [routeB]) => { .sort(([routeA], [routeB]) => {
const iA = order.indexOf(routeA) const iA = order.indexOf(routeA)
const iB = order.indexOf(routeB) const iB = order.indexOf(routeB)
if (iA >= 0 && iB >= 0) return iA - iB if (iA >= 0 && iB >= 0) return iA - iB
if (iA < 0 && iB >= 0) return 1 if (iA < 0 && iB >= 0) return 1
if (iB < 0 && iA >= 0) return -1 if (iB < 0 && iA >= 0) return -1
return 0 return 0
}) })
const write = process.stdout.write.bind(process.stdout) yield HEAD
yield `\n\n`
write(HEAD) yield listOfRoutes
write(`\n\n`) yield `\n\n`
write(listOfRoutes) for (const [route, params] of sortedRoutes) {
write(`\n\n`) yield `## \`GET ${route}\`\n\n`
yield descriptions[route] || ''
for (const [route, params] of sortedRoutes) { yield `\n### Query Parameters\n`
write(`## \`GET ${route}\`\n\n`) yield params
write(descriptions[route] || '') if (examples[route]) {
write(` yield '\n' + examples[route]
### Query Parameters }
`) yield `\n\n`
write(params)
if (examples[route]) {
write('\n' + examples[route])
} }
write(`\n\n`) yield tail
}
export {
generateMarkdownApiDocs,
} }
write(tail)

62
build/index.js Executable file
View file

@ -0,0 +1,62 @@
#!/usr/bin/env node
import {dirname, join} from 'node:path'
import {pipeline} from 'node:stream/promises'
import {createReadStream, createWriteStream} from 'node:fs'
import {copyFile} from 'node:fs/promises'
import _technicalDocsCli from '@derhuerst/technical-docs-cli'
import {config} from '../api.js'
const {
createMarkdownRenderer,
determineSyntaxStylesheetPath,
} = _technicalDocsCli
import {generateMarkdownApiDocs} from './api-docs.js'
const BASE_URL = new URL('..', import.meta.url).href
const API_DOCS_DEST = 'docs/api.md'
const DOCS_TO_RENDER = [
['docs/readme.md', 'docs/index.html'],
['docs/getting-started.md', 'docs/getting-started.html'],
['docs/api.md', 'docs/api.html'],
]
const SYNTAX_STYLESHEET_URL = '/syntax.css'
const SYNTAX_STYLESHEET_SRC = determineSyntaxStylesheetPath('github')
const SYNTAX_STYLESHEET_DEST = 'docs/syntax.css'
{
console.info('writing Markdown API docs to ' + API_DOCS_DEST)
const destPath = new URL(API_DOCS_DEST, BASE_URL).pathname
await pipeline(
generateMarkdownApiDocs(),
createWriteStream(destPath),
)
}
const markdownRenderingCfg = {
syntaxStylesheetUrl: SYNTAX_STYLESHEET_URL,
}
for (const [src, dest] of DOCS_TO_RENDER) {
console.info(`rendering Markdown file ${src} to HTML file ${dest}`)
const srcPath = new URL(src, BASE_URL).pathname
const destPath = new URL(dest, BASE_URL).pathname
// unfortunately, we can't use stream.pipeline right now
// see https://github.com/unifiedjs/unified-stream/issues/1
await new Promise((resolve, reject) => {
createReadStream(srcPath)
.once('error', reject)
.pipe(createMarkdownRenderer(markdownRenderingCfg))
.once('error', reject)
.pipe(createWriteStream(destPath))
.once('error', reject)
.once('finish', resolve)
})
}
{
const srcPath = SYNTAX_STYLESHEET_SRC
const destPath = new URL(SYNTAX_STYLESHEET_DEST, BASE_URL).pathname
console.info(`copying syntax stylesheet from ${srcPath} to ${destPath}`)
await copyFile(srcPath, destPath)
}

View file

@ -36,7 +36,7 @@
"serve-static": "^1.14.1" "serve-static": "^1.14.1"
}, },
"devDependencies": { "devDependencies": {
"@derhuerst/technical-docs-cli": "^1.1.0", "@derhuerst/technical-docs-cli": "^1.5.0",
"axios": "~1.1", "axios": "~1.1",
"eslint": "^8.12.0", "eslint": "^8.12.0",
"get-port": "^6.1.2", "get-port": "^6.1.2",
@ -46,8 +46,7 @@
"tape": "^5.5.2" "tape": "^5.5.2"
}, },
"scripts": { "scripts": {
"docs": "node api-docs.js >docs/api.md && build-technical-doc --syntax-stylesheet-url /syntax.css <docs/readme.md >docs/index.html && build-technical-doc --syntax-stylesheet-url /syntax.css <docs/getting-started.md >docs/getting-started.html && build-technical-doc --syntax-stylesheet-url /syntax.css <docs/api.md >docs/api.html && build-technical-doc --syntax-stylesheet github >docs/syntax.css", "build": "REDIS_URL='' ./build/index.js",
"build": "npm run docs",
"start": "node index.js", "start": "node index.js",
"lint": "eslint .", "lint": "eslint .",
"test": "node test/index.js | tap-min" "test": "node test/index.js | tap-min"