Skip to content

Instantly share code, notes, and snippets.

@pjlamb12
Last active September 14, 2020 12:42
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pjlamb12/6c77e8357294a69d4edd1d4069259556 to your computer and use it in GitHub Desktop.
Save pjlamb12/6c77e8357294a69d4edd1d4069259556 to your computer and use it in GitHub Desktop.
Generate Compodoc Documentation for each App and Library in an Nx workspace from a script
const angularJson = require('./angular.json');
const exec = require('child_process').exec;
const fs = require('fs');
const mainProjects = Object.keys(angularJson.projects).filter(proj => !proj.includes('e2e'));
const tsConfigPaths = parseTsconfigPaths();
function parseTsconfigPaths() {
const pathsArray = [];
for (const projectName of mainProjects) {
const project = angularJson.projects[projectName];
const tsConfigPath =
project.projectType === 'library'
? `${project.root}/tsconfig.lib.json`
: `${project.root}/tsconfig.app.json`;
const nameParts = project.root.split('/');
const name = nameParts[nameParts.length - 1];
pathsArray.push({ tsConfigPath, name });
}
return pathsArray;
}
async function runCompodoc() {
for (const path of tsConfigPaths) {
await exec(`npx compodoc -p ${path.tsConfigPath} --output documentation/${path.name}/`);
}
}
runCompodoc();
generateRootIndexHtml();
function generateRootIndexHtml() {
const fileName = './documentation/index.html';
const stream = fs.createWriteStream(fileName);
stream.once('open', function() {
const html = generateAllHtml();
stream.end(html);
});
}
function generateAllHtml() {
const topHalf = getFirstHtmlPart();
const mainText = getMainDocumentText();
const listItems = buildListItems();
const bottomHalf = getLastHtmlPart();
return `${topHalf}${mainText}${listItems}${bottomHalf}`;
}
function buildListItems() {
let itemsHtml = '';
for (const path of tsConfigPaths) {
itemsHtml = `${itemsHtml}${buildNavItem(path.name)}`;
}
return itemsHtml;
}
function buildNavItem(sectionName) {
return `
<li>
<a href="./${sectionName}/index.html"><span class="fa fa-file-text-o"></span>${sectionName}</a>
</li>
`;
}
function getFirstHtmlPart() {
const oneProject = tsConfigPaths[0].name;
return `
<!doctype html>
<html class="no-js" lang="">
<head>
<meta charset="utf-8">
<meta http-equiv="x-ua-compatible" content="ie=edge">
<title>motiv-health-angular documentation</title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" type="image/x-icon" href="./${oneProject}/images/favicon.ico">
<link rel="stylesheet" href="./${oneProject}/styles/style.css">
</head>
<body>
`;
}
function getMainDocumentText() {
return `
<div class="container-fluid main">
<div class="row main">
<div class="content readme">
<div class="content-data">
<h1 id="motiv-health-angular-monorepo-main">Motiv Health Angular Monorepo</h1>
<p>
The documentation here covers each project and library. You can navigate to the main page of each of those apps or libraries by using the menu.
</p>
<ul>
`;
}
function getLastHtmlPart() {
const oneProject = tsConfigPaths[0].name;
return `
</ul>
</div>
</div>
</div>
</div>
<script src="./${oneProject}/js/libs/bootstrap-native.js"></script>
<script src="./${oneProject}/js/menu.js"></script>
</body>
</html>
`;
}
@pjlamb12
Copy link
Author

This also now creates an index.html file in the documentation folder that links to each subfolder's index.html file. It uses the style sheets from one of the subfolders so that it looks similar to the other pages, but there can be some work done to make it look a little better as well.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment