Skip to content

Commit

Permalink
fix(core): remove edge types
Browse files Browse the repository at this point in the history
  • Loading branch information
meeroslav committed Jan 3, 2023
1 parent 569c68a commit 7da33cc
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 131 deletions.
22 changes: 12 additions & 10 deletions packages/nx/src/lock-file/npm-v2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,15 @@ export function parseNpmLockFile(
}

// adds edge out to node
function addEdge(builder, node, depName, depSpec, type) {
function addEdge(
builder: LockFileBuilder,
node,
depName,
depSpec,
isOptional?: boolean
) {
if (!node.edgesOut || !node.edgesOut.has(depName)) {
builder.addEdgeOut(node, depName, depSpec, type);
builder.addEdgeOut(node, depName, depSpec, isOptional);
}
}

Expand Down Expand Up @@ -140,7 +146,7 @@ function parseV1Dependencies(
) {
if (value.requires) {
Object.entries(value.requires).forEach(([depName, depSpec]) => {
addEdge(builder, node, depName, depSpec, 'unknown');
addEdge(builder, node, depName, depSpec);
});
}
}
Expand Down Expand Up @@ -194,21 +200,17 @@ function parseV3Dependencies(
if (value.peerDependencies) {
const peerMeta = value.peerDependenciesMeta || {};
Object.entries(value.peerDependencies).forEach(([depName, depSpec]) => {
if (peerMeta[depName]?.optional) {
addEdge(builder, node, depName, depSpec, 'peerOptional');
} else {
addEdge(builder, node, depName, depSpec, 'peer');
}
addEdge(builder, node, depName, depSpec, peerMeta[depName]?.optional);
});
}
if (value.dependencies) {
Object.entries(value.dependencies).forEach(([depName, depSpec]) => {
addEdge(builder, node, depName, depSpec, 'prod');
addEdge(builder, node, depName, depSpec);
});
}
if (value.optionalDependencies) {
Object.entries(value.optionalDependencies).forEach(([depName, depSpec]) => {
addEdge(builder, node, depName, depSpec, 'optional');
addEdge(builder, node, depName, depSpec, true);
});
}
}
115 changes: 31 additions & 84 deletions packages/nx/src/lock-file/utils/lock-file-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,45 +16,22 @@ export type LockFileNode = {
path: string;
resolved?: string;
integrity?: string;
dev?: boolean;
optional?: boolean;
peer?: boolean;
devOptional?: boolean; // tree.devOptional && !tree.dev && !tree.optional
edgesOut?: Map<string, LockFileEdge>;
children?: Map<string, LockFileNode>;
edgesIn?: Set<LockFileEdge>;
isProjectRoot?: true;
};

type LockFileEdgeType =
| 'prod'
| 'dev'
| 'optional'
| 'peer'
| 'peerOptional'
| 'workspace'
| 'unknown';

const VALID_TYPES = new Set([
'prod',
'dev',
'optional',
'peer',
'peerOptional',
'workspace',
]);

export type LockFileEdge = {
name: string;
versionSpec: string;
type: LockFileEdgeType;
from: LockFileNode; // path from
from?: LockFileNode;
to?: LockFileNode;
error?:
| 'MISSING_TARGET'
| 'MISSING_SOURCE'
// | 'DETACHED'
| 'UNRESOLVED_TYPE';
// some optional dependencies might be missing
// we want to keep track of that to avoid false positives
optional?: boolean;
// error type if source or target is missing
error?: 'MISSING_TARGET' | 'MISSING_SOURCE';
};

// TODO 1: Check Arborist for links? Perhaps those should be workspace files
Expand Down Expand Up @@ -112,22 +89,28 @@ export class LockFileBuilder {
const skipEdgeInCheck = true;
if (dependencies) {
Object.entries(dependencies).forEach(([name, versionSpec]) => {
this.addEdgeOut(node, name, versionSpec, 'prod', skipEdgeInCheck);
this.addEdgeOut(node, name, versionSpec, false, skipEdgeInCheck);
});
}
if (devDependencies) {
Object.entries(devDependencies).forEach(([name, versionSpec]) => {
this.addEdgeOut(node, name, versionSpec, 'dev', skipEdgeInCheck);
this.addEdgeOut(node, name, versionSpec, false, skipEdgeInCheck);
});
}
if (optionalDependencies) {
Object.entries(optionalDependencies).forEach(([name, versionSpec]) => {
this.addEdgeOut(node, name, versionSpec, 'optional', skipEdgeInCheck);
this.addEdgeOut(node, name, versionSpec, true, skipEdgeInCheck);
});
}
if (peerDependencies) {
Object.entries(peerDependencies).forEach(([name, versionSpec]) => {
this.addEdgeOut(node, name, versionSpec, 'peer', skipEdgeInCheck);
this.addEdgeOut(
node,
name,
versionSpec,
packageJson.peerDependenciesMeta?.[name]?.optional,
skipEdgeInCheck
);
});
}
return node;
Expand All @@ -137,10 +120,10 @@ export class LockFileBuilder {
node: LockFileNode,
name: string,
versionSpec: string,
type: LockFileEdgeType,
isOptional?: boolean,
skipEdgeInCheck?: boolean
) {
this.validateEdgeCreation(node, name, versionSpec, type);
this.validateEdgeCreation(node, name, versionSpec);

if (!this.isVersionSpecSupported(versionSpec)) {
return;
Expand All @@ -156,9 +139,11 @@ export class LockFileBuilder {
const edge: LockFileEdge = {
name,
versionSpec,
type,
from: node,
};
if (isOptional) {
edge.optional = true;
}
this.updateEdgeTypeAndError(edge);
node.edgesOut.set(name, edge);
if (!skipEdgeInCheck) {
Expand Down Expand Up @@ -231,13 +216,8 @@ export class LockFileBuilder {
}
}

addEdgeIn(
node: LockFileNode,
type: LockFileEdgeType,
versionSpec: string,
parent?: LockFileNode
) {
this.validateEdgeCreation(node, node.name, versionSpec, type);
addEdgeIn(node: LockFileNode, versionSpec: string, parent?: LockFileNode) {
this.validateEdgeCreation(node, node.name, versionSpec);

const existingEdges = this.findEdgesOut(node.name, versionSpec);
if (existingEdges.size > 0) {
Expand All @@ -248,7 +228,6 @@ export class LockFileBuilder {
const edge: LockFileEdge = {
name: node.name,
versionSpec,
type,
from: parent,
to: node,
};
Expand Down Expand Up @@ -315,37 +294,16 @@ export class LockFileBuilder {
}

private updateEdgeTypeAndError(edge: LockFileEdge) {
if (edge.to && edge.type === 'unknown') {
edge.type = this.getValidEdgeType(edge.to);
}
if (!edge.to && edge.type !== 'optional' && edge.type !== 'peerOptional') {
if (!edge.to && !edge.optional) {
edge.error = 'MISSING_TARGET';
}
if (!edge.from) {
edge.error = 'MISSING_SOURCE';
} else if (edge.to && edge.type === 'unknown') {
edge.error = 'UNRESOLVED_TYPE';
} else {
delete edge.error;
}
}

private getValidEdgeType(node: LockFileNode): LockFileEdgeType {
if (node.peer && node.optional) {
return 'peerOptional';
}
if (node.optional) {
return 'optional';
}
if (node.peer) {
return 'peer';
}
if (node.dev) {
return 'dev';
}
return 'prod';
}

// private detachEdge(edge: LockFileEdge) {
// if (edge['to']) {
// edge['to'].edgesIn.delete(edge);
Expand Down Expand Up @@ -432,8 +390,7 @@ export class LockFileBuilder {
private validateEdgeCreation(
node: LockFileNode,
name: string,
versionSpec: string,
type: LockFileEdgeType
versionSpec: string
) {
if (!node) {
throw new TypeError(`Edge must be bound to a node`);
Expand All @@ -446,16 +403,6 @@ export class LockFileBuilder {
`Edge must have a valid version specification: ${versionSpec}`
);
}
if (!type) {
throw new TypeError(`Edge must have a valid type: ${type}`);
}
if (!type || (!VALID_TYPES.has(type) && type !== 'unknown')) {
throw new TypeError(
`Edge must have a valid type: ${type}\nValid types: ${Array.from(
VALID_TYPES
).join(', ')}`
);
}
}

private verifyNode(node: LockFileNode) {
Expand Down Expand Up @@ -494,12 +441,12 @@ export class LockFileBuilder {

getLockFileGraph(): LockFileGraph {
let isValid = true;
if (!this.isGraphConsistent()) {
console.error(
`Graph is not consistent. Please report this issue via github`
);
isValid = false;
}
// if (!this.isGraphConsistent()) {
// console.error(
// `Graph is not consistent. Please report this issue via github`
// );
// isValid = false;
// }

return {
root: this.root,
Expand Down
21 changes: 0 additions & 21 deletions packages/nx/src/lock-file/utils/parsing.ts

This file was deleted.

Loading

0 comments on commit 7da33cc

Please sign in to comment.