Skip to content

Commit

Permalink
feat(#157): add package list generator (#163)
Browse files Browse the repository at this point in the history
  • Loading branch information
Decipher authored Aug 28, 2021
1 parent 245b4c0 commit 7aca43f
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 21 deletions.
10 changes: 0 additions & 10 deletions docs/content/api/README.md

This file was deleted.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"build:docs": "./packages/docgen/bin/druxt-docgen.js",
"changeset": "changeset",
"clean": "yarn clean:dist && yarn clean:docs",
"clean:docs": "rimraf docs/content/api/packages || true",
"clean:docs": "rimraf docs/content/api || true",
"clean:dist": "rimraf packages/*/dist || true",
"dev": "siroc dev",
"lint": "eslint --ext .js,.vue packages/*/src",
Expand Down
87 changes: 77 additions & 10 deletions packages/docgen/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,33 @@ const cwd = path.join(__dirname, '..')
* DruxtDocgen class.
*/
class DruxtDocgen {
constructor() {
this.destination = 'docs/content/api/'
}

/**
* Generate documentation for source JS and Vue files.
*/
async generateDocs () {
async generateDocs() {
consola.info('Generating docs')

/**
* Generate list of packages.
*/
await this.generatePackageList()

/**
* Generate API documents.
*/
await this.generateApiDocs()
}

/**
* Generate API documents.
*/
async generateApiDocs() {
consola.info('Generating API documents')

const files = await globby([
// Javascript.
'packages/**/src/**/*.js',
Expand All @@ -27,30 +48,76 @@ class DruxtDocgen {
// Exclude fixtures, mocks and tests.
'!**/__*__/**/*',
])

for (const file of files) {
// Get JSDoc template data from file.
const templateData = jsdoc2md.getTemplateDataSync({
configure: path.resolve(__dirname, '../jsdoc.json'),
files: file,
})

// Process file based on extension.
if (file.match(/\.js$/)) {
await this.processJs(file, templateData)
}
else {
await this.processVue(file, templateData)
}
this.writeData(file, templateData)

this.writeTemplateData(file, templateData)
}
}

/**
* Generate list of packages.
*/
async generatePackageList() {
consola.info('Generating package list')

const packageFiles = await globby('packages/**/package.json')
const packages = packageFiles
// Load package data.
.map((file) => ({
...require(path.resolve(__dirname, '../../..', file)),
__dir: file.split('/')[1],
}))
// Filter and sort data
.filter((o) => !o.private)
.sort((a, b) => a.__dir === 'druxt' ? -1 : a < b)
// Process package data.
.map((pkg) =>
`## ${pkg.name}
<div class="text-sm">
Version: \`${pkg.version}\`
</div>
${pkg.description}
[Read the docs](/api/packages/${pkg.__dir}/)
`)

const destination = this.destination + 'README.md'
mkdirp.sync(path.dirname(destination))

fs.writeFileSync(destination, `---
title: Packages
---
${packages.join('\n* * *\n\n')}
* * *
_Note: The contents of this file where automatically generated by the [Druxt Docgen](https://github.com/druxt/druxt.js/tree/develop/packages/docgen)._
`)
}

/**
* Process Javascript files.
*/
async processJs (file, templateData) {
async processJs(file, templateData) {
templateData.map(item => {
// Vuex state scope fix.
if (item.name === 'state' && item.scope === 'inner') {
Expand All @@ -69,7 +136,7 @@ class DruxtDocgen {
/**
* Process Vue.js files.
*/
async processVue (file, templateData) {
async processVue(file, templateData) {
// Get data from vue-docgen-api.
const data = await vueDocs.parse(file)

Expand Down Expand Up @@ -106,12 +173,12 @@ class DruxtDocgen {
}

/**
* Write data to file system.
* Write template data to file system.
*
* @param {string} file - The file name.
* @param {*} data - The JSDoc / Vuedoc generated data.
*/
writeData (file, templateData) {
writeTemplateData(file, templateData) {
if (!templateData) return

const content = dmd(templateData, {
Expand All @@ -124,7 +191,7 @@ class DruxtDocgen {
})
if (!content) return

const destination = 'docs/content/api/' + file.replace('src/', '').replace(/\.[^/.]+$/, '.md')
const destination = this.destination + file.replace('src/', '').replace(/\.[^/.]+$/, '.md')
mkdirp.sync(path.dirname(destination))

// Build frontmatter.
Expand Down

0 comments on commit 7aca43f

Please sign in to comment.