Skip to content

Commit

Permalink
feat(core): add hash to lock file data
Browse files Browse the repository at this point in the history
  • Loading branch information
meeroslav committed Sep 27, 2022
1 parent 3771b7d commit 951fbd1
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 6 deletions.
1 change: 1 addition & 0 deletions packages/nx/src/utils/lock-file/lock-file-type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ export type PackageVersions = Record<string, PackageDependency>;
export type LockFileData = {
dependencies: Record<string, PackageVersions>;
lockFileMetadata?: Record<string, any>;
hash: string;
};
23 changes: 23 additions & 0 deletions packages/nx/src/utils/lock-file/lock-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,29 @@ import {
stringifyPnpmLockFile,
} from './pnpm';
import { LockFileData } from './lock-file-type';
import { hashLockFile } from './utils';

export function lockFileHash(
packageManager: PackageManager = detectPackageManager()
): string {
let file: string;
if (packageManager === 'yarn') {
file = readFileSync('yarn.lock', 'utf8');
}
if (packageManager === 'pnpm') {
file = readFileSync('pnpm-lock.yaml', 'utf8');
}
if (packageManager === 'npm') {
file = readFileSync('package-lock.json', 'utf8');
}
if (file) {
return hashLockFile(file);
} else {
throw Error(
`Unknown package manager ${packageManager} or lock file missing`
);
}
}

/**
* Parses lock file and maps dependencies and metadata to {@link LockFileData}
Expand Down
5 changes: 4 additions & 1 deletion packages/nx/src/utils/lock-file/npm.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { defaultHashing } from '../../hasher/hashing-impl';
import { LockFileData, PackageDependency } from './lock-file-type';
import { sortObject } from './utils';
import { hashLockFile, sortObject } from './utils';

type PackageMeta = {
path: string;
Expand Down Expand Up @@ -36,12 +37,14 @@ type NpmLockFile = {
*/
export function parseNpmLockFile(lockFile: string): LockFileData {
const { packages, dependencies, ...metadata } = JSON.parse(lockFile);
const hash = hashLockFile(lockFile);
return {
dependencies: mapPackages(packages),
lockFileMetadata: {
metadata,
rootPackage: packages[''],
},
hash,
};
}

Expand Down
6 changes: 4 additions & 2 deletions packages/nx/src/utils/lock-file/pnpm.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { LockFileData, PackageDependency } from './lock-file-type';
import { load, dump } from '@zkochan/js-yaml';
import { sortObject } from './utils';
import { transpileModule } from 'typescript';
import { hashLockFile, sortObject } from './utils';
import { defaultHashing } from '../../hasher/hashing-impl';

type PackageMeta = {
key: string;
Expand Down Expand Up @@ -50,6 +50,7 @@ export function parsePnpmLockFile(lockFile: string): LockFileData {
const data: PnpmLockFile = load(lockFile);
const { dependencies, devDependencies, packages, specifiers, ...metadata } =
data;
const hash = hashLockFile(lockFile);

return {
dependencies: mapPackages(
Expand All @@ -60,6 +61,7 @@ export function parsePnpmLockFile(lockFile: string): LockFileData {
metadata.lockfileVersion.toString().endsWith('inlineSpecifiers')
),
lockFileMetadata: { ...metadata },
hash,
};
}

Expand Down
6 changes: 6 additions & 0 deletions packages/nx/src/utils/lock-file/utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { defaultHashing } from '../../hasher/hashing-impl';

/**
* Simple sort function to ensure keys are ordered alphabetically
* @param obj
Expand All @@ -18,3 +20,7 @@ export function sortObject<T = string>(
});
return result;
}

export function hashLockFile(fileContent: string): string {
return defaultHashing.hashArray([fileContent]);
}
12 changes: 9 additions & 3 deletions packages/nx/src/utils/lock-file/yarn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
PackageDependency,
PackageVersions,
} from './lock-file-type';
import { sortObject } from './utils';
import { hashLockFile, sortObject } from './utils';

type LockFileDependencies = Record<
string,
Expand All @@ -22,6 +22,7 @@ const BERRY_LOCK_FILE_DISCLAIMER = `# This file was generated by Nx. Do not edit
*/
export function parseYarnLockFile(lockFile: string): LockFileData {
const { __metadata, ...dependencies } = parseSyml(lockFile);
const hash = hashLockFile(lockFile);

// Yarn Berry has workspace packages includes, so we need to extract those to metadata
const [mappedPackages, workspacePackages] = mapPackages(dependencies);
Expand All @@ -33,9 +34,10 @@ export function parseYarnLockFile(lockFile: string): LockFileData {
__metadata,
workspacePackages,
},
hash,
};
} else {
return { dependencies: mappedPackages };
return { dependencies: mappedPackages, hash };
}
}

Expand Down Expand Up @@ -148,6 +150,9 @@ export function pruneYarnLockFile(
packages
);

// for prunned packages let's check for combo of original hash and input packages
const hash = hashLockFile(lockFileData.hash + packages.sort().join(':'));

if (isBerry) {
const { __metadata, workspacePackages } = lockFileData.lockFileMetadata;
return {
Expand All @@ -160,9 +165,10 @@ export function pruneYarnLockFile(
),
},
dependencies: prunedDependencies,
hash,
};
} else {
return { dependencies: prunedDependencies };
return { dependencies: prunedDependencies, hash };
}
}

Expand Down

0 comments on commit 951fbd1

Please sign in to comment.