UNPKG

3.61 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.createFromJSON = exports.SUPPORTED_SCHEMA_RANGE = void 0;
4const semver = require("semver");
5const graphlib = require("../graphlib");
6const errors_1 = require("./errors");
7const validate_graph_1 = require("./validate-graph");
8const dep_graph_1 = require("./dep-graph");
9exports.SUPPORTED_SCHEMA_RANGE = '^1.0.0';
10/**
11 * Create a DepGraph instance from a JSON representation of a dep graph. This
12 * is typically used after passing the graph over the wire as `DepGraphData`.
13 */
14function createFromJSON(depGraphData) {
15 validateDepGraphData(depGraphData);
16 const graph = new graphlib.Graph({
17 directed: true,
18 multigraph: false,
19 compound: false,
20 });
21 const pkgs = {};
22 const pkgNodes = {};
23 for (const { id, info } of depGraphData.pkgs) {
24 pkgs[id] = info.version ? info : { ...info, version: undefined };
25 }
26 for (const node of depGraphData.graph.nodes) {
27 const pkgId = node.pkgId;
28 if (!pkgNodes[pkgId]) {
29 pkgNodes[pkgId] = new Set();
30 }
31 pkgNodes[pkgId].add(node.nodeId);
32 graph.setNode(node.nodeId, { pkgId, info: node.info });
33 }
34 for (const node of depGraphData.graph.nodes) {
35 for (const depNodeId of node.deps) {
36 graph.setEdge(node.nodeId, depNodeId.nodeId);
37 }
38 }
39 (0, validate_graph_1.validateGraph)(graph, depGraphData.graph.rootNodeId, pkgs, pkgNodes);
40 return new dep_graph_1.DepGraphImpl(graph, depGraphData.graph.rootNodeId, pkgs, pkgNodes, depGraphData.pkgManager);
41}
42exports.createFromJSON = createFromJSON;
43function assert(condition, msg) {
44 if (!condition) {
45 throw new errors_1.ValidationError(msg);
46 }
47}
48function validateDepGraphData(depGraphData) {
49 assert(!!semver.valid(depGraphData.schemaVersion) &&
50 semver.satisfies(depGraphData.schemaVersion, exports.SUPPORTED_SCHEMA_RANGE), `dep-graph schemaVersion not in "${exports.SUPPORTED_SCHEMA_RANGE}"`);
51 assert(depGraphData.pkgManager && !!depGraphData.pkgManager.name, '.pkgManager.name is missing');
52 const pkgsMap = depGraphData.pkgs.reduce((acc, cur) => {
53 assert(!(cur.id in acc), 'more than one pkg with same id');
54 assert(!!cur.info, '.pkgs item missing .info');
55 acc[cur.id] = cur.info;
56 return acc;
57 }, {});
58 const nodesMap = depGraphData.graph.nodes.reduce((acc, cur) => {
59 assert(!(cur.nodeId in acc), 'more than on node with same id');
60 acc[cur.nodeId] = cur;
61 return acc;
62 }, {});
63 const rootNodeId = depGraphData.graph.rootNodeId;
64 const rootNode = nodesMap[rootNodeId];
65 assert(rootNodeId in nodesMap, `.${rootNodeId} root graph node is missing`);
66 const rootPkgId = rootNode.pkgId;
67 assert(rootPkgId in pkgsMap, `.${rootPkgId} root pkg missing`);
68 assert(nodesMap[rootNodeId].pkgId === rootPkgId, `the root node .pkgId should be "${rootPkgId}"`);
69 const pkgIds = Object.keys(pkgsMap);
70 // NOTE: this name@version check is very strict,
71 // we can relax it later, it just makes things easier now
72 assert(pkgIds.filter((pkgId) => pkgId !== dep_graph_1.DepGraphImpl.getPkgId(pkgsMap[pkgId]))
73 .length === 0, 'pkgs ids should be name@version');
74 assert(Object.values(nodesMap).filter((node) => !(node.pkgId in pkgsMap))
75 .length === 0, 'some instance nodes belong to non-existing pkgIds');
76 assert(Object.values(pkgsMap).filter((pkg) => !pkg.name)
77 .length === 0, 'some .pkgs elements have no .name field');
78}
79//# sourceMappingURL=create-from-json.js.map
\No newline at end of file