bigint-crypto-utils/build/build.docs.js

107 lines
4.0 KiB
JavaScript
Raw Normal View History

'use strict'
const fs = require('fs')
2021-03-25 12:40:04 +00:00
const TypeDoc = require('typedoc')
const path = require('path')
const pkgJson = require('../package.json')
const rootDir = path.join(__dirname, '..')
function camelise (str) {
return str.replace(/-([a-z])/g,
function (m, w) {
return w.toUpperCase()
})
}
2021-03-25 12:40:04 +00:00
async function typedoc () {
const app = new TypeDoc.Application()
// If you want TypeDoc to load tsconfig.json / typedoc.json files
app.options.addReader(new TypeDoc.TSConfigReader())
app.options.addReader(new TypeDoc.TypeDocReader())
app.bootstrap({
// typedoc options here
entryPoints: ['src/index.ts'],
plugin: ['typedoc-plugin-markdown'],
2021-04-21 18:05:45 +00:00
includeVersion: false,
2021-03-25 12:40:04 +00:00
entryDocument: 'API.md',
2021-04-21 18:05:45 +00:00
readme: 'none',
hideBreadcrumbs: true
2021-03-25 12:40:04 +00:00
})
const project = app.convert()
if (project) {
// Project may not have converted correctly
const output = path.join(rootDir, './docs')
// Rendered docs
await app.generateDocs(project, output)
}
}
function getRepositoryData () {
if (typeof pkgJson.repository === 'string') {
const repodata = pkgJson.repository.split(/[:/]/)
const repoProvider = repodata[0]
if (repoProvider === 'github' || repoProvider === 'gitlab' || repoProvider === 'bitbucket') {
return {
repoProvider,
repoUsername: repodata[1],
repoName: repodata[2]
}
} else return null
}
}
const { repoProvider, repoUsername, repoName } = getRepositoryData() || { repoProvider: null, repoUsername: null, repoName: null }
2021-03-25 12:40:04 +00:00
const regex = /^(?:(?<scope>@.*?)\/)?(?<name>.*)/ // We are going to take only the package name part if there is a scope, e.g. @my-org/package-name
const { name } = pkgJson.name.match(regex).groups
const camelCaseName = camelise(name)
2021-04-21 18:05:45 +00:00
const iifeBundlePath = path.relative('.', pkgJson.exports['./iife-browser-bundle'])
const esmBundlePath = path.relative('.', pkgJson.exports['./esm-browser-bundle'])
const umdBundlePath = path.relative('.', pkgJson.exports['./umd-browser-bundle'])
2021-03-25 12:40:04 +00:00
let iifeBundle, esmBundle, umdBundle, workflowBadget, coverallsBadge
if (repoProvider) {
switch (repoProvider) {
case 'github':
2021-04-21 18:05:45 +00:00
iifeBundle = `[IIFE bundle](https://raw.githubusercontent.com/${repoUsername}/${repoName}/master/${iifeBundlePath})`
esmBundle = `[ESM bundle](https://raw.githubusercontent.com/${repoUsername}/${repoName}/master/${esmBundlePath})`
umdBundle = `[UMD bundle](https://raw.githubusercontent.com/${repoUsername}/${repoName}/master/${umdBundlePath})`
2021-03-25 12:40:04 +00:00
workflowBadget = `[![Node CI](https://github.com/${repoUsername}/${repoName}/workflows/Node%20CI/badge.svg)](https://github.com/${repoUsername}/${repoName}/actions?query=workflow%3A%22Node+CI%22)`
coverallsBadge = `[![Coverage Status](https://coveralls.io/repos/github/${repoUsername}/${repoName}/badge.svg?branch=master)](https://coveralls.io/github/${repoUsername}/${repoName}?branch=master)`
break
case 'gitlab':
2021-04-21 18:05:45 +00:00
iifeBundle = `[IIFE bundle](https://gitlab.com/${repoUsername}/${repoName}/-/raw/master/dist/${iifeBundlePath}?inline=false)`
esmBundle = `[ESM bundle](https://gitlab.com/${repoUsername}/${repoName}/-/raw/master/dist/${esmBundlePath}?inline=false)`
umdBundle = `[UMD bundle](https://gitlab.com/${repoUsername}/${repoName}/-/raw/master/dist/${umdBundlePath}?inline=false)`
2021-03-25 12:40:04 +00:00
break
default:
break
}
}
2021-03-25 12:40:04 +00:00
const templateFile = path.join(rootDir, pkgJson.directories.src, 'docs/index.md')
let template = fs.readFileSync(templateFile, { encoding: 'UTF-8' })
.replace(/\{\{PKG_NAME\}\}/g, pkgJson.name)
2021-03-25 12:40:04 +00:00
.replace(/\{\{PKG_CAMELCASE\}\}/g, camelCaseName)
.replace(/\{\{IIFE_BUNDLE\}\}/g, iifeBundle || 'IIFE bundle')
.replace(/\{\{ESM_BUNDLE\}\}/g, esmBundle || 'ESM bundle')
2021-03-25 12:40:04 +00:00
.replace(/\{\{UMD_BUNDLE\}\}/g, umdBundle || 'UMD bundle')
if (repoProvider && repoProvider === 'github') {
template = template.replace(/\{\{GITHUB_ACTIONS_BADGES\}\}/g, workflowBadget + '\n' + coverallsBadge)
}
2021-03-25 12:40:04 +00:00
const readmeFile = path.join(rootDir, 'README.md')
fs.writeFileSync(readmeFile, template)
2021-03-25 12:40:04 +00:00
typedoc()