esdoc-plugin-dependency-graphs
Version:
A plugin for ESDoc that adds interactive D3 powered dependency graphs for source code including linking JSPM / NPM managed code and packages.
144 lines (108 loc) • 4.42 kB
JavaScript
/**
* esdoc-plugin-dependency-graphs -- In development / creates a package dependency graph w/ D3 for JSPM / SystemJS.
*/
;
Object.defineProperty(exports, "__esModule", {
value: true
});
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol ? "symbol" : typeof obj; };
// import GraphSourceDep from './GraphSourceDep.js';
exports.onStart = onStart;
exports.onHandleConfig = onHandleConfig;
exports.onHandleHTML = onHandleHTML;
exports.onHandleTag = onHandleTag;
exports.onComplete = onComplete;
var _cheerio = require('cheerio');
var _cheerio2 = _interopRequireDefault(_cheerio);
var _fsExtra = require('fs-extra');
var _fsExtra2 = _interopRequireDefault(_fsExtra);
var _path = require('path');
var _path2 = _interopRequireDefault(_path);
var _taffydb = require('taffydb');
var _GraphPackageDep = require('./GraphPackageDep.js');
var _GraphPackageDep2 = _interopRequireDefault(_GraphPackageDep);
var _GraphDocBuilder = require('./GraphDocBuilder.js');
var _GraphDocBuilder2 = _interopRequireDefault(_GraphDocBuilder);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
// Stores instances of GraphPackageDep and GraphSourceDep which generates the graphs.
var graphPackageDep = void 0; // , graphSourceDep;
// Must store ESDoc configuration file and tags to use later with GraphDocBuilder.
var config = void 0,
options = void 0,
tags = void 0;
var processPackages = false;
// ESDoc plugin callbacks -------------------------------------------------------------------------------------------
/**
* Sanitizes optional parameters and initializes the graph generators.
*
* @param {object} ev - Event from ESDoc containing data field.
*/
function onStart(ev) {
// Stores sanitized option map.
options = _typeof(ev.data.option) === 'object' ? ev.data.option : {};
// Stores option that if true silences logging output.
options.verbose = typeof options.verbose === 'boolean' ? options.verbose : false;
graphPackageDep = new _GraphPackageDep2.default();
// graphSourceDep = new GraphSourceDep(options);
}
/**
* Stores the ESDoc configuration file for later use with GraphDocBuilder and starts the graph generators.
*
* @param {object} ev - Event from ESDoc containing data field.
*/
function onHandleConfig(ev) {
config = ev.data.config;
processPackages = _typeof(global.$$esdoc_plugin_jspm) === 'object' && _typeof(global.$$esdoc_plugin_jspm_package_graph) === 'object';
if (processPackages) {
graphPackageDep.onHandleConfig(ev);
}
// graphSourceDep.onHandleConfig(ev);
}
/**
* Process all HTML files adding a `Graphs` link to the top header navigation bar.
*
* @param {object} ev - Event from ESDoc containing data field
*/
function onHandleHTML(ev) {
if (ev.data.fileName.endsWith('.html')) {
var $ = _cheerio2.default.load(ev.data.html, { decodeEntities: false });
$('<a href="graphs/jspm_packages.html">Graphs</a>').insertAfter('header a[href="identifiers.html"]');
ev.data.html = $.html();
}
}
/**
* Must save tags to eventually feed GraphDocBuilder with taffydb converted data.
*
* @param {object} ev - Event from ESDoc containing data field
*/
function onHandleTag(ev) {
tags = ev.data.tag;
}
/**
* Completes graph generation and then builds the HTML index pages.
*/
function onComplete() {
if (processPackages) {
graphPackageDep.onComplete();
}
try {
// graphSourceDep.onComplete();
new _GraphDocBuilder2.default((0, _taffydb.taffy)(tags), config).exec(s_WRITE_HTML);
} catch (err) {
console.log('!! plugin - onComplete - err: ' + err);
throw err;
}
}
/**
* Provides a callback for GraphDocBuilder to export HTML files delegating to the local `onHandleHTML` function.
*
* @param {string} html - HTML to save.
* @param {string} fileName - name for file output.
*/
var s_WRITE_HTML = function s_WRITE_HTML(html, fileName) {
// Must match plugin event data format.
var ev = { data: { html: html, fileName: fileName } };
onHandleHTML(ev);
var filePath = _path2.default.resolve(config.destination, fileName);
_fsExtra2.default.outputFileSync(filePath, ev.data.html, { encoding: 'utf8' });
};