UNPKG

bit-bin

Version:

<a href="https://opensource.org/licenses/Apache-2.0"><img alt="apache" src="https://img.shields.io/badge/License-Apache%202.0-blue.svg"></a> <a href="https://github.com/teambit/bit/blob/master/CONTRIBUTING.md"><img alt="prs" src="https://img.shields.io/b

301 lines (234 loc) 7.05 kB
"use strict"; var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard"); var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; function _bluebird() { const data = require("bluebird"); _bluebird = function () { return data; }; return data; } function _defineProperty2() { const data = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty")); _defineProperty2 = function () { return data; }; return data; } function path() { const data = _interopRequireWildcard(require("path")); path = function () { return data; }; return data; } function _fsExtra() { const data = _interopRequireDefault(require("fs-extra")); _fsExtra = function () { return data; }; return data; } function _execa() { const data = _interopRequireDefault(require("execa")); _execa = function () { return data; }; return data; } function _graphviz() { const data = _interopRequireDefault(require("graphviz")); _graphviz = function () { return data; }; return data; } function _logger() { const data = _interopRequireDefault(require("../../logger/logger")); _logger = function () { return data; }; return data; } function _bitIds() { const data = _interopRequireDefault(require("../../bit-id/bit-ids")); _bitIds = function () { return data; }; return data; } function _utils() { const data = require("../../utils"); _utils = function () { return data; }; return data; } const defaultConfig = { layout: 'dot', fontName: 'Arial', fontSize: '14px', backgroundColor: '#000000', nodeColor: '#c6c5fe', noDependencyColor: '#cfffac', // @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX! devDependencyColor: '#ff0000', edgeColor: '#757575', highlightColor: 'green' }; class VisualDependencyGraph { // @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX! // @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX! // @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX! // @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX! constructor(graphlib, graph, config) { (0, _defineProperty2().default)(this, "graphlib", void 0); (0, _defineProperty2().default)(this, "graph", void 0); (0, _defineProperty2().default)(this, "config", void 0); this.graph = graph; this.graphlib = graphlib; this.config = config; } // @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX! static loadFromGraphlib(graphlib, config = {}) { return (0, _bluebird().coroutine)(function* () { const mergedConfig = Object.assign({}, defaultConfig, config); checkGraphvizInstalled(config.graphVizPath); // @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX! const graph = VisualDependencyGraph.buildDependenciesGraph(graphlib, mergedConfig); return new VisualDependencyGraph(graphlib, graph, mergedConfig); })(); } /** * Creates the graphviz graph */ // @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX! // @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX! static buildDependenciesGraph(graphlib, config) { const graph = _graphviz().default.digraph('G'); if (config.graphVizPath) { graph.setGraphVizPath(config.graphVizPath); } const nodes = graphlib.nodes(); const edges = graphlib.edges(); nodes.forEach(node => { graph.addNode(node); }); edges.forEach(edge => { const edgeType = graphlib.edge(edge); const vizEdge = graph.addEdge(edge.v, edge.w); if (edgeType !== 'dependencies') { // @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX! setEdgeColor(vizEdge, config.devDependencyColor); } }); return graph; } getNode(id) { if (id.hasVersion()) { return this.graph.getNode(id.toString()); } // if there is no version, search for the component with the latest version const allIds = this.graphlib.nodes().map(n => this.graphlib.node(n)); const bitIds = _bitIds().default.fromArray(allIds); const latestId = (0, _utils().getLatestVersionNumber)(bitIds, id); return this.graph.getNode(latestId.toString()); } highlightId(id) { const node = this.getNode(id); setNodeColor(node, this.config.highlightColor); } /** * Creates an image from the module dependency graph. * @param {String} imagePath * @return {Promise} */ image(imagePath) { var _this = this; return (0, _bluebird().coroutine)(function* () { const options = createGraphvizOptions(_this.config); const type = path().extname(imagePath).replace('.', '') || 'png'; // @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX! options.type = type; const outputP = new Promise((resolve, reject) => { _this.graph.output(options, resolve, (code, out, err) => { _logger().default.debug('Error during viz graph output function'); _logger().default.debug(code, out, err); reject(new Error(err)); }); }); const image = yield outputP; yield _fsExtra().default.writeFile(imagePath, image); return path().resolve(imagePath); })(); } /** * Return the module dependency graph as DOT output. * @return {dot} */ dot() { return this.graph.to_dot(); } } /** * Set color on a node. * @param {Object} node * @param {String} color */ exports.default = VisualDependencyGraph; function setNodeColor(node, color) { node.set('color', color); node.set('fontcolor', color); } /** * Set color on an edge. * @param {Object} edge * @param {String} color */ function setEdgeColor(edge, color) { edge.set('color', color); } /** * Check if Graphviz is installed on the system. * @param {Object} config * @return {Promise} */ function checkGraphvizInstalled(graphVizPath) { const options = { shell: true }; if (graphVizPath) { // @ts-ignore AUTO-ADDED-AFTER-MIGRATION-PLEASE-FIX! options.cwd = graphVizPath; } const childProcess = (0, _execa().default)('gvpr', ['-V'], options); return childProcess.catch(e => { _logger().default.debug(`Graphviz could not be found in path: ${graphVizPath || 'default path'}`); throw new Error(`Graphviz could not be found. Ensure that "gvpr" is in your $PATH.\n${e}`); }); } /** * Return options to use with graphviz digraph. * @param {Object} config * @return {Object} */ function createGraphvizOptions(config) { const graphVizOptions = config.graphVizOptions || {}; return { G: Object.assign({ overlap: false, pad: 0.111, layout: config.layout, bgcolor: config.backgroundColor }, graphVizOptions.G), E: Object.assign({ color: config.edgeColor }, graphVizOptions.E), N: Object.assign({ fontname: config.fontName, fontsize: config.fontSize, color: config.nodeColor, fontcolor: config.nodeColor }, graphVizOptions.N) }; }