-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
hashing.ts
75 lines (68 loc) · 2.17 KB
/
hashing.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import { defaultHashing } from '../../hasher/hashing-impl';
import {
ProjectGraph,
ProjectGraphExternalNode,
} from '../../config/project-graph';
import { PackageJsonDeps } from './pruning';
/**
* Apply simple hashing of the content using the default hashing implementation
* @param fileContent
* @returns
*/
export function hashString(fileContent: string): string {
return defaultHashing.hashArray([fileContent]);
}
/**
* Hash partial graph's external nodes
* for task graph caching
* @param projectGraph
*/
export function hashExternalNodes(projectGraph: ProjectGraph) {
Object.keys(projectGraph.externalNodes).forEach((key) => {
if (!projectGraph.externalNodes[key].data.hash) {
// hash it using it's dependencies
hashExternalNode(projectGraph.externalNodes[key], projectGraph);
}
});
}
function hashExternalNode(node: ProjectGraphExternalNode, graph: ProjectGraph) {
const hashKey = `${node.data.packageName}@${node.data.version}`;
if (!graph.dependencies[node.name]) {
node.data.hash = hashString(hashKey);
} else {
const hashingInput = [hashKey];
// collect all dependencies' hashes
traverseExternalNodesDependencies(node.name, graph, hashingInput);
node.data.hash = defaultHashing.hashArray(hashingInput.sort());
}
}
function traverseExternalNodesDependencies(
projectName: string,
graph: ProjectGraph,
visited: string[]
) {
graph.dependencies[projectName].forEach((d) => {
const target = graph.externalNodes[d.target];
const targetKey = `${target.data.packageName}@${target.data.version}`;
if (visited.indexOf(targetKey) === -1) {
visited.push(targetKey);
if (graph.dependencies[d.target]) {
traverseExternalNodesDependencies(d.target, graph, visited);
}
}
});
}
/**
* Generate new hash based on the original hash and pruning input parameters - packages and project name
* @param originalHash
* @param packages
* @param projectName
* @returns
*/
export function generatePrunnedHash(
originalHash: string,
normalizedPackageJson: PackageJsonDeps
) {
const hashingInput = [originalHash, JSON.stringify(normalizedPackageJson)];
return defaultHashing.hashArray(hashingInput);
}