@rushstack/lockfile-explorer
Version:
Rush Lockfile Explorer: The UI for solving version conflicts quickly in a large monorepo
123 lines • 5.38 kB
JavaScript
"use strict";
// Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT license.
// See LICENSE in the project root for license information.
Object.defineProperty(exports, "__esModule", { value: true });
exports.serializeToJson = serializeToJson;
exports.deserializeFromJson = deserializeFromJson;
const LfxGraph_1 = require("./LfxGraph");
function serializeToJson(graph) {
const jsonLfxEntries = [];
const jsonIdByEntry = new Map();
function toJsonId(entry) {
const result = jsonIdByEntry.get(entry);
if (result === undefined) {
throw new Error('Attempt to serialize disconnected object');
}
return result;
}
// First create the jsonId mapping
for (const entry of graph.entries) {
const nextIndex = jsonLfxEntries.length;
const jsonLfxEntry = {
jsonId: nextIndex,
kind: entry.kind,
entryId: entry.entryId,
rawEntryId: entry.rawEntryId,
packageJsonFolderPath: entry.packageJsonFolderPath,
entryPackageName: entry.entryPackageName,
displayText: entry.displayText,
entryPackageVersion: entry.entryPackageVersion,
entrySuffix: entry.entrySuffix,
// Lists will be added in the second loop
dependencies: [],
transitivePeerDependencies: [],
referrerJsonIds: []
};
jsonLfxEntries.push(jsonLfxEntry);
jsonIdByEntry.set(entry, jsonLfxEntry.jsonId);
}
// Use the jsonId mapping to serialize the lists
for (let i = 0; i < jsonLfxEntries.length; ++i) {
const jsonLfxEntry = jsonLfxEntries[i];
const entry = graph.entries[i];
for (const dependency of entry.dependencies) {
const jsonLfxDependency = {
name: dependency.name,
versionPath: dependency.versionPath,
entryId: dependency.entryId,
originalSpecifier: dependency.originalSpecifier,
dependencyKind: dependency.dependencyKind,
peerDependencyMeta: {
name: dependency.peerDependencyMeta.name,
version: dependency.peerDependencyMeta.version,
optional: dependency.peerDependencyMeta.optional
}
};
if (dependency.resolvedEntry) {
jsonLfxDependency.resolvedEntryJsonId = toJsonId(dependency.resolvedEntry);
}
jsonLfxEntry.dependencies.push(jsonLfxDependency);
}
jsonLfxEntry.transitivePeerDependencies = Array.from(entry.transitivePeerDependencies);
jsonLfxEntry.referrerJsonIds = entry.referrers.map((x) => toJsonId(x));
}
return { workspace: graph.workspace, entries: jsonLfxEntries };
}
function deserializeFromJson(jsonLfxGraph) {
const graph = new LfxGraph_1.LfxGraph(jsonLfxGraph.workspace);
const entries = graph.entries;
function fromJsonId(jsonId) {
const result = entries[jsonId];
if (result === undefined) {
throw new Error('Invalid jsonId');
}
return result;
}
const jsonLfxEntries = jsonLfxGraph.entries;
// First create the jsonId mapping
for (const jsonLfxEntry of jsonLfxEntries) {
const options = {
kind: jsonLfxEntry.kind,
entryId: jsonLfxEntry.entryId,
rawEntryId: jsonLfxEntry.rawEntryId,
packageJsonFolderPath: jsonLfxEntry.packageJsonFolderPath,
entryPackageName: jsonLfxEntry.entryPackageName,
displayText: jsonLfxEntry.displayText,
entryPackageVersion: jsonLfxEntry.entryPackageVersion,
entrySuffix: jsonLfxEntry.entrySuffix
};
entries.push(new LfxGraph_1.LfxGraphEntry(options));
}
// Use the jsonId mapping to deserialize the lists
for (let i = 0; i < jsonLfxEntries.length; ++i) {
const jsonLfxEntry = jsonLfxEntries[i];
const entry = graph.entries[i];
for (const jsonLfxDependency of jsonLfxEntry.dependencies) {
const dependency = new LfxGraph_1.LfxGraphDependency({
name: jsonLfxDependency.name,
versionPath: jsonLfxDependency.versionPath,
entryId: jsonLfxDependency.entryId,
originalSpecifier: jsonLfxDependency.originalSpecifier,
dependencyKind: jsonLfxDependency.dependencyKind,
peerDependencyMeta: {
name: jsonLfxDependency.peerDependencyMeta.name,
version: jsonLfxDependency.peerDependencyMeta.version,
optional: jsonLfxDependency.peerDependencyMeta.optional
},
containingEntry: entry
});
if (jsonLfxDependency.resolvedEntryJsonId) {
dependency.resolvedEntry = fromJsonId(jsonLfxDependency.resolvedEntryJsonId);
}
entry.dependencies.push(dependency);
}
for (const item of jsonLfxEntry.transitivePeerDependencies) {
entry.transitivePeerDependencies.add(item);
}
for (const referrerJsonId of jsonLfxEntry.referrerJsonIds) {
entry.referrers.push(fromJsonId(referrerJsonId));
}
}
return graph;
}
//# sourceMappingURL=lfxGraphSerializer.js.map