update-readme.js 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. #!/usr/bin/env node
  2. const fs = require('fs')
  3. const jsdoc2md = require('jsdoc-to-markdown')
  4. const toc = require('markdown-toc')
  5. const README = 'README.md'
  6. const HEADER = '## OError API Reference'
  7. const FOOTER = '<!-- END API REFERENCE -->'
  8. async function main() {
  9. const apiDocs = await jsdoc2md.render({ files: 'index.js' })
  10. const apiDocLines = apiDocs.trim().split(/\r?\n/g)
  11. // The first few lines don't make much sense when included in the README.
  12. const apiDocStart = apiDocLines.indexOf('* [OError](#OError)')
  13. if (apiDocStart === -1) {
  14. console.error('API docs not in expected format for insertion.')
  15. process.exit(1)
  16. }
  17. apiDocLines.splice(1, apiDocStart - 1)
  18. apiDocLines.unshift(HEADER, '')
  19. const readme = await fs.promises.readFile(README, { encoding: 'utf8' })
  20. const readmeLines = readme.split(/\r?\n/g)
  21. const apiStart = readmeLines.indexOf(HEADER)
  22. const apiEnd = readmeLines.indexOf(FOOTER)
  23. if (apiStart === -1 || apiEnd === -1) {
  24. console.error('Could not find the API Reference section.')
  25. process.exit(1)
  26. }
  27. Array.prototype.splice.apply(
  28. readmeLines,
  29. [apiStart, apiEnd - apiStart].concat(apiDocLines)
  30. )
  31. const readmeWithApi = readmeLines.join('\n')
  32. let readmeWithApiAndToc = toc.insert(readmeWithApi)
  33. // Unfortunately, the ⇒ breaks the generated TOC links.
  34. readmeWithApiAndToc = readmeWithApiAndToc.replace(/-%E2%87%92-/g, '--')
  35. await fs.promises.writeFile(README, readmeWithApiAndToc)
  36. }
  37. main()