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

View File

@ -5,6 +5,7 @@ import {
hashString,
isRootVersion,
TransitiveLookupFunctionInput,
generatePrunnedHash,
} from './utils';
import { satisfies } from 'semver';
@ -345,7 +346,7 @@ export function prunePnpmLockFile(
const prunedLockFileData = {
lockFileMetadata: lockFileData.lockFileMetadata,
dependencies,
hash: '',
hash: generatePrunnedHash(lockFileData.hash, packages, projectName),
};
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,
isRootVersion,
TransitiveLookupFunctionInput,
generatePrunnedHash,
} from './utils';
type LockFileDependencies = Record<
@ -204,10 +205,13 @@ export function pruneYarnLockFile(
),
},
dependencies: prunedDependencies,
hash: '',
hash: generatePrunnedHash(lockFileData.hash, packages, projectName),
};
} else {
prunedLockFileData = { dependencies: prunedDependencies, hash: '' };
prunedLockFileData = {
dependencies: prunedDependencies,
hash: generatePrunnedHash(lockFileData.hash, packages, projectName),
};
}
prunedLockFileData.hash = hashString(JSON.stringify(prunedLockFileData));