concise-diagram
Advanced tools
Comparing version 0.3.0 to 0.4.2
192
lib/index.js
@@ -1,2 +0,2 @@ | ||
'use strict'; | ||
"use strict"; | ||
@@ -6,95 +6,76 @@ Object.defineProperty(exports, "__esModule", { | ||
}); | ||
exports.output = undefined; | ||
exports.output = void 0; | ||
var _regenerator = require('babel-runtime/regenerator'); | ||
var _fs = _interopRequireDefault(require("fs")); | ||
var _regenerator2 = _interopRequireDefault(_regenerator); | ||
var _viz = _interopRequireDefault(require("viz.js")); | ||
var _fs = require('fs'); | ||
var _full = require("viz.js/full.render"); | ||
var _fs2 = _interopRequireDefault(_fs); | ||
var _timm = require("timm"); | ||
var _viz = require('viz.js'); | ||
var _viz2 = _interopRequireDefault(_viz); | ||
var _timm = require('timm'); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; } | ||
/* eslint-disable prefer-template */ | ||
/* eslint-disable max-len */ | ||
/* -- | ||
Output-only. | ||
Output options: | ||
* `file?` (`string`): if specified, output will be written to the specified path | ||
* `filterEdges?` (`{ from: ModelName, to: ModelName, as: FieldName, isRequired: boolean } => boolean`): | ||
return `true` if a given edge must be shown. Default: all edges are shown | ||
* `edgeLabels?` (`boolean` = `true`): show edge labels | ||
-- */ | ||
/* eslint-enable max-len */ | ||
var DEFAULT_OPTIONS = { | ||
/* eslint-disable no-console */ | ||
const DEFAULT_OPTIONS = { | ||
edgeLabels: true | ||
}; | ||
// ==================================== | ||
}; // ==================================== | ||
// Main | ||
// ==================================== | ||
var output = function () { | ||
var _ref = _asyncToGenerator(_regenerator2.default.mark(function _callee(schema, options, utils) { | ||
var vizInput, svg; | ||
return _regenerator2.default.wrap(function _callee$(_context) { | ||
while (1) { | ||
switch (_context.prev = _context.next) { | ||
case 0: | ||
vizInput = writeDiagram(utils.preprocessedSchema, (0, _timm.addDefaults)(options, DEFAULT_OPTIONS)); | ||
svg = (0, _viz2.default)(vizInput); | ||
if (options.file) { | ||
_fs2.default.writeFileSync(options.file, svg, 'utf8'); | ||
} | ||
return _context.abrupt('return', svg); | ||
const output = async (schema, options, utils) => { | ||
const vizInput = writeDiagram(utils.preprocessedSchema, (0, _timm.addDefaults)(options, DEFAULT_OPTIONS)); | ||
const viz = new _viz.default({ | ||
Module: _full.Module, | ||
render: _full.render | ||
}); | ||
let svg; | ||
case 4: | ||
case 'end': | ||
return _context.stop(); | ||
} | ||
} | ||
}, _callee, undefined); | ||
})); | ||
try { | ||
svg = await viz.renderString(vizInput); | ||
} catch (err) { | ||
console.error(`Error generating diagram: ${err.message}`); | ||
throw err; | ||
} | ||
return function output(_x, _x2, _x3) { | ||
return _ref.apply(this, arguments); | ||
}; | ||
}(); | ||
if (options.file) { | ||
_fs.default.writeFileSync(options.file, svg, 'utf8'); | ||
} | ||
// ==================================== | ||
return svg; | ||
}; // ==================================== | ||
// Flow writer | ||
// ==================================== | ||
var writeDiagram = function writeDiagram(_ref2, options) { | ||
var models = _ref2.models; | ||
var modelNames = Object.keys(models); | ||
var nodes = modelNames.map(function (modelName) { | ||
var node = modelName; | ||
var description = models[modelName].description; | ||
var props = []; | ||
exports.output = output; | ||
const writeDiagram = ({ | ||
models | ||
}, options) => { | ||
const modelNames = Object.keys(models); | ||
const nodes = modelNames.map(modelName => { | ||
let node = modelName; | ||
const { | ||
description | ||
} = models[modelName]; | ||
const props = []; | ||
if (description) { | ||
props.push('comment="' + description + '"'); | ||
props.push('tooltip="' + description + '"'); | ||
props.push(`comment="${description}"`); | ||
props.push(`tooltip="${description}"`); | ||
} | ||
if (props.length) node += ' [' + props.join(', ') + ']'; | ||
if (props.length) node += ` [${props.join(', ')}]`; | ||
return node; | ||
}); | ||
var edges = []; | ||
modelNames.forEach(function (modelName) { | ||
var relations = models[modelName].relations; | ||
Object.keys(relations).forEach(function (relationName) { | ||
var edge = writeEdge(models, modelName, relationName, options); | ||
const edges = []; | ||
modelNames.forEach(modelName => { | ||
const { | ||
relations | ||
} = models[modelName]; | ||
Object.keys(relations).forEach(relationName => { | ||
const edge = writeEdge(models, modelName, relationName, options); | ||
if (edge) edges.push(edge); | ||
@@ -104,18 +85,16 @@ }); | ||
return 'digraph "" {\n' + // `""` removes the background tooltip | ||
' node [shape=box, fontname="sans-serif"];\n' + ' edge [fontsize=9, fontname="sans-serif"];\n' + nodes.map(function (o) { | ||
return ' ' + o + ';\n'; | ||
}).join('') + edges.map(function (o) { | ||
return ' ' + o + ';\n'; | ||
}).join('') + '}\n'; | ||
' node [shape=box, fontname="sans-serif"];\n' + ' edge [fontsize=9, fontname="sans-serif"];\n' + nodes.map(o => ` ${o};\n`).join('') + edges.map(o => ` ${o};\n`).join('') + '}\n'; | ||
}; | ||
var writeEdge = function writeEdge(models, modelName, relationName, _ref3) { | ||
var filterEdges = _ref3.filterEdges, | ||
edgeLabels = _ref3.edgeLabels; | ||
var relation = models[modelName].relations[relationName]; | ||
const writeEdge = (models, modelName, relationName, { | ||
filterEdges, | ||
edgeLabels | ||
}) => { | ||
const relation = models[modelName].relations[relationName]; | ||
if (relation.isInverse) return null; | ||
var isRequired = relation.isRequired, | ||
inverseName = relation.inverseName, | ||
isPlural = relation.isPlural; | ||
const { | ||
isRequired, | ||
inverseName, | ||
isPlural | ||
} = relation; | ||
@@ -126,38 +105,45 @@ if (filterEdges && !filterEdges({ | ||
as: relationName, | ||
isRequired: isRequired | ||
isRequired | ||
})) { | ||
return null; | ||
} | ||
var props = []; | ||
const props = []; | ||
if (edgeLabels && relationName !== relation.model) { | ||
var expectedRelationName = isPlural ? models[relation.model].plural : relation.model; | ||
const expectedRelationName = isPlural ? models[relation.model].plural : relation.model; | ||
if (relationName !== expectedRelationName) { | ||
props.push('label=" ' + relationName + ' "'); | ||
props.push(`label=" ${relationName} "`); | ||
} | ||
} | ||
var description = relation.description; | ||
const description = relation.description; | ||
if (description) { | ||
props.push('comment="' + description + '"'); | ||
props.push('edgetooltip="' + description + '"'); | ||
props.push('headtooltip="' + description + '"'); | ||
props.push('labeltooltip="' + description + '"'); | ||
props.push(`comment="${description}"`); | ||
props.push(`edgetooltip="${description}"`); | ||
props.push(`headtooltip="${description}"`); | ||
props.push(`labeltooltip="${description}"`); | ||
} | ||
props.push('arrowhead=' + (isPlural ? 'empty' : 'normal')); | ||
props.push(`arrowhead=${isPlural ? 'empty' : 'normal'}`); | ||
if (inverseName != null) { | ||
var inverse = models[relation.model].relations[inverseName]; | ||
const inverse = models[relation.model].relations[inverseName]; | ||
if (!inverse) { | ||
throw new Error('INVERSE_RELATION_NOT_FOUND ' + relation.model + '/' + inverseName); | ||
throw new Error(`INVERSE_RELATION_NOT_FOUND ${relation.model}/${inverseName}`); | ||
} | ||
props.push('dir=both'); | ||
props.push('arrowtail=' + (inverse.isPlural ? 'empty' : 'normal')); | ||
props.push(`arrowtail=${inverse.isPlural ? 'empty' : 'normal'}`); | ||
} | ||
if (!isRequired) props.push('style=dotted'); | ||
var edge = modelName + ' -> ' + relation.model; | ||
if (props.length) edge += ' [' + props.join(', ') + ']'; | ||
let edge = `${modelName} -> ${relation.model}`; | ||
if (props.length) edge += ` [${props.join(', ')}]`; | ||
return edge; | ||
}; | ||
// ==================================== | ||
}; // ==================================== | ||
// Public | ||
// ==================================== | ||
exports.output = output; | ||
// ==================================== |
{ | ||
"name": "concise-diagram", | ||
"version": "0.3.0", | ||
"version": "0.4.2", | ||
"author": "Guillermo Grau Panea", | ||
@@ -16,8 +16,8 @@ "license": "MIT", | ||
"babel-runtime": "^6.23.0", | ||
"timm": "^1.2.5", | ||
"viz.js": "^1.7.1", | ||
"concise-types": "*" | ||
"timm": "^1.7.1", | ||
"viz.js": "^2.1.2", | ||
"concise-types": "^0.4.2" | ||
}, | ||
"devDependencies": { | ||
"prettier": "^1.12.1" | ||
"prettier": "^2.2.1" | ||
}, | ||
@@ -24,0 +24,0 @@ "description": "A tool belt for concise schemas", |
// @flow | ||
/* eslint-disable prefer-template */ | ||
/* eslint-disable no-console */ | ||
import fs from 'fs'; | ||
import Viz from 'viz.js'; | ||
import { Module, render } from 'viz.js/full.render'; | ||
import { addDefaults } from 'timm'; | ||
@@ -54,3 +56,10 @@ import type { | ||
); | ||
const svg = Viz(vizInput); | ||
const viz = new Viz({ Module, render }); | ||
let svg; | ||
try { | ||
svg = await viz.renderString(vizInput); | ||
} catch (err) { | ||
console.error(`Error generating diagram: ${err.message}`); | ||
throw err; | ||
} | ||
if (options.file) { | ||
@@ -67,3 +76,3 @@ fs.writeFileSync(options.file, svg, 'utf8'); | ||
const modelNames = Object.keys(models); | ||
const nodes = modelNames.map(modelName => { | ||
const nodes = modelNames.map((modelName) => { | ||
let node = modelName; | ||
@@ -80,5 +89,5 @@ const { description } = models[modelName]; | ||
const edges = []; | ||
modelNames.forEach(modelName => { | ||
modelNames.forEach((modelName) => { | ||
const { relations } = models[modelName]; | ||
Object.keys(relations).forEach(relationName => { | ||
Object.keys(relations).forEach((relationName) => { | ||
const edge = writeEdge(models, modelName, relationName, options); | ||
@@ -92,4 +101,4 @@ if (edge) edges.push(edge); | ||
' edge [fontsize=9, fontname="sans-serif"];\n' + | ||
nodes.map(o => ` ${o};\n`).join('') + | ||
edges.map(o => ` ${o};\n`).join('') + | ||
nodes.map((o) => ` ${o};\n`).join('') + | ||
edges.map((o) => ` ${o};\n`).join('') + | ||
'}\n' | ||
@@ -96,0 +105,0 @@ ); |
Sorry, the diff of this file is not supported yet
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
Floating dependency
QualityPackage has a dependency with a floating version range. This can cause issues if the dependency publishes a new major version.
Found 1 instance in 1 package
278
1
13647
+ Addedviz.js@2.1.2(transitive)
- Removedviz.js@1.8.2(transitive)
Updatedconcise-types@^0.4.2
Updatedtimm@^1.7.1
Updatedviz.js@^2.1.2