UNPKG

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.

220 lines (172 loc) 8.55 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); var _get = function get(object, property, receiver) { if (object === null) object = Function.prototype; var desc = Object.getOwnPropertyDescriptor(object, property); if (desc === undefined) { var parent = Object.getPrototypeOf(object); if (parent === null) { return undefined; } else { return get(parent, property, receiver); } } else if ("value" in desc) { return desc.value; } else { var getter = desc.get; if (getter === undefined) { return undefined; } return getter.call(receiver); } }; 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 _iceCap = require('ice-cap'); var _iceCap2 = _interopRequireDefault(_iceCap); var _DocBuilder2 = require('esdoc/out/src/Publisher/Builder/DocBuilder.js'); var _DocBuilder3 = _interopRequireDefault(_DocBuilder2); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; } function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; } /** * Graph Output Builder class. */ var GraphDocBuilder = function (_DocBuilder) { _inherits(GraphDocBuilder, _DocBuilder); function GraphDocBuilder() { _classCallCheck(this, GraphDocBuilder); return _possibleConstructorReturn(this, Object.getPrototypeOf(GraphDocBuilder).apply(this, arguments)); } _createClass(GraphDocBuilder, [{ key: '_buildGraph', /** * Simply wraps the actual graph in an iframe. * * @param {object} item - target graph config item. * * @return {string} built graph. * @private */ value: function _buildGraph(item) { return '<iframe src="' + item.graphPath + '" frameBorder="0" style="display: block; width: 100%; height: 100%;" />'; } /** * Creates the left hand navigation list for the various graphs supported. * * @param {Array<{fileName: string, graphPath: string, label: string, safeLabel: string}>} graphConfig - * target manual config. * * @return {object} IceCap instance * @private */ }, { key: '_buildGraphNav', value: function _buildGraphNav(graphConfig) { var ice = new _iceCap2.default(this._readTemplate('graphIndex.html')); ice.loop('manual', graphConfig, function (i, item, ice) { ice.attr('manual', 'data-toc-name', item.safeLabel); ice.text('title', item.label); ice.attr('title', 'href', item.fileName); }); return ice; } /** * Execute building output. * * @param {function(html: string, filePath: string)} callback - is called each manual. */ }, { key: 'exec', value: function exec(callback) { var graphConfig = this._getGraphConfig(); var ice = this._buildLayoutDoc(); ice.autoDrop = false; ice.attr('rootContainer', 'class', ' manual-root'); { var fileName = 'graphs/index.html'; var baseUrl = this._getBaseUrl(fileName); ice.load('nav', this._buildGraphNav(graphConfig), _iceCap2.default.MODE_WRITE); ice.text('title', 'Graphs', _iceCap2.default.MODE_WRITE); ice.attr('baseUrl', 'href', baseUrl, _iceCap2.default.MODE_WRITE); callback(ice.html, fileName); } // To get the iFrame holding the graph styles to display with no padding and 100% height the following needs // to be overridden: 'html', 'body', '.content'. This is a bit of a hack as a new IceCap instance is created. var $ = _cheerio2.default.load(ice.html); $('html').attr('style', 'height: calc(100% - 40px)'); $('body').attr('style', 'height: 100%'); $('.content').attr('style', 'padding: 0; height: 100%; position: relative;'); ice = new _iceCap2.default($.html()); var _iteratorNormalCompletion = true; var _didIteratorError = false; var _iteratorError = undefined; try { for (var _iterator = graphConfig[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) { var item = _step.value; var _fileName = item.fileName; var _baseUrl = this._getBaseUrl(_fileName); ice.load('content', this._buildGraph(item), _iceCap2.default.MODE_WRITE); ice.load('nav', this._buildGraphNav(graphConfig), _iceCap2.default.MODE_WRITE); ice.text('title', item.label, _iceCap2.default.MODE_WRITE); ice.attr('baseUrl', 'href', _baseUrl, _iceCap2.default.MODE_WRITE); callback(this._removeFooter(ice), _fileName); } } catch (err) { _didIteratorError = true; _iteratorError = err; } finally { try { if (!_iteratorNormalCompletion && _iterator.return) { _iterator.return(); } } finally { if (_didIteratorError) { throw _iteratorError; } } } } /** * Get graph config for all entries for HTML page generation. * * @returns {Array<{}>} built graph config. * @private */ }, { key: '_getGraphConfig', value: function _getGraphConfig() { var graphConfig = []; graphConfig.push({ fileName: 'graphs/jspm_packages.html', graphPath: 'graphs/jspm_packages/index.html', label: 'JSPM Packages', safeLabel: 'jspm_packages' }); return graphConfig; } /** * Attempts to first read local html template before deferring to ESDoc for loading templates. * * @param {string} fileName - template file name. * * @return {string} html of template. * @private */ }, { key: '_readTemplate', value: function _readTemplate(fileName) { try { var filePath = _path2.default.resolve(__dirname, '../template/esdoc/' + fileName); return _fsExtra2.default.readFileSync(filePath, { encoding: 'utf-8' }); } catch (err) {/* ... */} return _get(Object.getPrototypeOf(GraphDocBuilder.prototype), '_readTemplate', this).call(this, fileName); } /** * Removes the footer from ESDoc layout template which is useful for full screen graphs. * * @param {object} ice - An instance of IceCap * * @return {string} * @private */ }, { key: '_removeFooter', value: function _removeFooter(ice) { var $ = _cheerio2.default.load(ice.html); $('.footer').remove(); return $.html(); } }]); return GraphDocBuilder; }(_DocBuilder3.default); exports.default = GraphDocBuilder;