Skip to content

Commit

Permalink
Integrate jsdoc-to-markdown as a build-docs command.
Browse files Browse the repository at this point in the history
  • Loading branch information
eaviles committed Oct 5, 2021
1 parent c17c9dc commit 00432f6
Show file tree
Hide file tree
Showing 5 changed files with 613 additions and 6 deletions.
31 changes: 31 additions & 0 deletions lib/commands/build-docs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
'use strict';

const jsdocToMd = require('jsdoc-to-markdown');
const path = require('path');
const { $ } = require('zx');
const { readFile, writeFile } = require('fs').promises;

const { loadConfig } = require('../utils');

module.exports = {
command: path.parse(__filename).name,
describe: 'Builds the README.md file from the JSDoc in the codebase.',
handler: async () => {
$.verbose = false;
const cwd = process.cwd();
const readmePath = path.resolve(cwd, 'README.md');
const { docs } = await loadConfig();
const { files, headingDepth, noGfm, template } = docs || {};
const templatePath = template || path.resolve(cwd, 'docs', 'templates', 'README.hbs');
const templateData = await readFile(templatePath, 'utf8');
const cfg = {
files: (Array.isArray(files) && files) || ['./lib/**/*.js'],
'heading-depth': headingDepth ?? 2,
'no-gfm': noGfm ?? false,
template: templateData
};
const output = await jsdocToMd.render(cfg);
await writeFile(readmePath, output, 'utf8');
await $`git add ./README.md`;
}
};
4 changes: 2 additions & 2 deletions lib/commands/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ const merge = require('merge-options');
const path = require('path');
const { $ } = require('zx');

const { readConfig } = require('../utils');
const { loadConfig } = require('../utils');

module.exports = {
command: path.parse(__filename).name,
describe: 'Run tests with Jest.',
handler: async () => {
$.verbose = false;
await $`mkdir -p ./reports`;
const { test } = await readConfig();
const { test } = await loadConfig();
const baseCfg = {
collectCoverage: true,
collectCoverageFrom: ['**/*.js'],
Expand Down
8 changes: 4 additions & 4 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@ const { readFile } = require('fs').promises;
const { name } = require('../package.json');

/**
* Reads the configuration for this tool from the target package.json file.
* Loads the configuration for this tool from the target package.json file.
*
* @returns {Promise<Object>}
*/
async function readConfig() {
async function loadConfig() {
try {
const { filepath } = await cosmiconfig(name, {
searchPlaces: ['package.json']
}).search();
const pkg = JSON.parse(await readFile(filepath, 'utf8'));
return pkg[name];
return pkg[name] || {};
} catch {
return {};
}
Expand All @@ -33,4 +33,4 @@ function pathFromCwd(...args) {
return path.relative(process.cwd(), path.resolve(...args));
}

module.exports = { pathFromCwd, readConfig };
module.exports = { loadConfig, pathFromCwd };
Loading

0 comments on commit 00432f6

Please sign in to comment.