From 9879365e9b356ba03706f9886d5c1fa0af023b4a Mon Sep 17 00:00:00 2001 From: Jannis R Date: Thu, 20 Apr 2023 00:12:19 +0200 Subject: [PATCH] use JS build script db-rest equivalent of https://github.com/derhuerst/vbb-rest/commit/605085229e7c1e79ab530963af2a5e45f19576a3 --- api-docs.js => build/api-docs.js | 66 ++++++++++++++++---------------- build/index.js | 62 ++++++++++++++++++++++++++++++ package.json | 5 +-- 3 files changed, 98 insertions(+), 35 deletions(-) rename api-docs.js => build/api-docs.js (93%) create mode 100755 build/index.js diff --git a/api-docs.js b/build/api-docs.js similarity index 93% rename from api-docs.js rename to build/api-docs.js index 5004323..14f7cf9 100644 --- a/api-docs.js +++ b/build/api-docs.js @@ -1,5 +1,5 @@ import {generateApiDocs} from 'hafas-rest-api/tools/generate-docs.js' -import {api} from './api.js' +import {api} from '../api.js' const HEAD = `\ # \`v6.db.transport.rest\` API documentation @@ -416,40 +416,42 @@ curl "https://v6.db.transport.rest/radar?$bbox&results=10" -s | jq `, } -const { - listOfRoutes, - routes, - tail, -} = generateApiDocs(api.routes) +const generateMarkdownApiDocs = async function* () { + const { + listOfRoutes, + routes, + tail, + } = generateApiDocs(api.routes) -const sortedRoutes = Object.entries(routes) -.sort(([routeA], [routeB]) => { - const iA = order.indexOf(routeA) - const iB = order.indexOf(routeB) - if (iA >= 0 && iB >= 0) return iA - iB - if (iA < 0 && iB >= 0) return 1 - if (iB < 0 && iA >= 0) return -1 - return 0 -}) + const sortedRoutes = Object.entries(routes) + .sort(([routeA], [routeB]) => { + const iA = order.indexOf(routeA) + const iB = order.indexOf(routeB) + if (iA >= 0 && iB >= 0) return iA - iB + if (iA < 0 && iB >= 0) return 1 + if (iB < 0 && iA >= 0) return -1 + return 0 + }) -const write = process.stdout.write.bind(process.stdout) + yield HEAD + yield `\n\n` -write(HEAD) -write(`\n\n`) + yield listOfRoutes + yield `\n\n` -write(listOfRoutes) -write(`\n\n`) - -for (const [route, params] of sortedRoutes) { - write(`## \`GET ${route}\`\n\n`) - write(descriptions[route] || '') - write(` -### Query Parameters -`) - write(params) - if (examples[route]) { - write('\n' + examples[route]) + for (const [route, params] of sortedRoutes) { + yield `## \`GET ${route}\`\n\n` + yield descriptions[route] || '' + yield `\n### Query Parameters\n` + yield params + if (examples[route]) { + yield '\n' + examples[route] + } + yield `\n\n` } - write(`\n\n`) + yield tail } -write(tail) + +export { + generateMarkdownApiDocs, +} \ No newline at end of file diff --git a/build/index.js b/build/index.js new file mode 100755 index 0000000..610781d --- /dev/null +++ b/build/index.js @@ -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) +} diff --git a/package.json b/package.json index 59b6d04..ee78d9d 100644 --- a/package.json +++ b/package.json @@ -36,7 +36,7 @@ "serve-static": "^1.14.1" }, "devDependencies": { - "@derhuerst/technical-docs-cli": "^1.1.0", + "@derhuerst/technical-docs-cli": "^1.5.0", "axios": "~1.1", "eslint": "^8.12.0", "get-port": "^6.1.2", @@ -46,8 +46,7 @@ "tape": "^5.5.2" }, "scripts": { - "docs": "node api-docs.js >docs/api.md && build-technical-doc --syntax-stylesheet-url /syntax.css docs/index.html && build-technical-doc --syntax-stylesheet-url /syntax.css docs/getting-started.html && build-technical-doc --syntax-stylesheet-url /syntax.css docs/api.html && build-technical-doc --syntax-stylesheet github >docs/syntax.css", - "build": "npm run docs", + "build": "REDIS_URL='' ./build/index.js", "start": "node index.js", "lint": "eslint .", "test": "node test/index.js | tap-min"