feat(core): add prune lock file hashing based on the input (#13167)

This commit is contained in:
Miroslav Jonaš 2022-11-15 12:31:50 +01:00 committed by GitHub
parent af0a18ff4e
commit b924f1a620
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 38 additions and 5 deletions

View File

@ -1,6 +1,11 @@
import { satisfies } from 'semver'; import { satisfies } from 'semver';
import { LockFileData, PackageDependency } from './lock-file-type'; import { LockFileData, PackageDependency } from './lock-file-type';
import { sortObject, hashString, TransitiveLookupFunctionInput } from './utils'; import {
sortObject,
hashString,
TransitiveLookupFunctionInput,
generatePrunnedHash,
} from './utils';
type PackageMeta = { type PackageMeta = {
path: string; path: string;
@ -460,7 +465,11 @@ Returning entire lock file.`
...pruneRootPackage(lockFileData, packages, projectName), ...pruneRootPackage(lockFileData, packages, projectName),
}; };
let prunedLockFileData: LockFileData; let prunedLockFileData: LockFileData;
prunedLockFileData = { dependencies, lockFileMetadata, hash: '' }; prunedLockFileData = {
dependencies,
lockFileMetadata,
hash: generatePrunnedHash(lockFileData.hash, packages, projectName),
};
return prunedLockFileData; return prunedLockFileData;
} }

View File

@ -5,6 +5,7 @@ import {
hashString, hashString,
isRootVersion, isRootVersion,
TransitiveLookupFunctionInput, TransitiveLookupFunctionInput,
generatePrunnedHash,
} from './utils'; } from './utils';
import { satisfies } from 'semver'; import { satisfies } from 'semver';
@ -345,7 +346,7 @@ export function prunePnpmLockFile(
const prunedLockFileData = { const prunedLockFileData = {
lockFileMetadata: lockFileData.lockFileMetadata, lockFileMetadata: lockFileData.lockFileMetadata,
dependencies, dependencies,
hash: '', hash: generatePrunnedHash(lockFileData.hash, packages, projectName),
}; };
return prunedLockFileData; return prunedLockFileData;
} }

View File

@ -245,3 +245,22 @@ function traverseExternalNodesDependencies(
} }
}); });
} }
/**
* 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,
packages: string[],
projectName?: string
) {
const hashingInput = [originalHash, ...packages];
if (projectName) {
hashingInput.push(projectName);
}
return defaultHashing.hashArray(hashingInput);
}

View File

@ -10,6 +10,7 @@ import {
hashString, hashString,
isRootVersion, isRootVersion,
TransitiveLookupFunctionInput, TransitiveLookupFunctionInput,
generatePrunnedHash,
} from './utils'; } from './utils';
type LockFileDependencies = Record< type LockFileDependencies = Record<
@ -204,10 +205,13 @@ export function pruneYarnLockFile(
), ),
}, },
dependencies: prunedDependencies, dependencies: prunedDependencies,
hash: '', hash: generatePrunnedHash(lockFileData.hash, packages, projectName),
}; };
} else { } else {
prunedLockFileData = { dependencies: prunedDependencies, hash: '' }; prunedLockFileData = {
dependencies: prunedDependencies,
hash: generatePrunnedHash(lockFileData.hash, packages, projectName),
};
} }
prunedLockFileData.hash = hashString(JSON.stringify(prunedLockFileData)); prunedLockFileData.hash = hashString(JSON.stringify(prunedLockFileData));