Skip to content

Commit

Permalink
cleanup(core): additional cleanup of the code
Browse files Browse the repository at this point in the history
  • Loading branch information
meeroslav committed Dec 2, 2022
1 parent 2830122 commit bd196b3
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 55 deletions.
1 change: 1 addition & 0 deletions packages/nx/src/lock-file/lock-file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ export async function pruneLockFile(
}

const packageJson = createPackageJson(projectName, projectGraph, {});
// cleanup irrelevant fields from the generated pacakge.json
const normalizedPackageJson = normalizePackageJson(
packageJson,
isProduction,
Expand Down
23 changes: 16 additions & 7 deletions packages/nx/src/lock-file/npm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { output } from '../utils/output';
import { joinPathFragments } from '../utils/path';
import { workspaceRoot } from '../utils/workspace-root';
import { LockFileData, PackageDependency } from './utils/lock-file-type';
import { sortObject } from './utils/sorting';
import { TransitiveLookupFunctionInput } from './utils/mapping';
import { hashString, generatePrunnedHash } from './utils/hashing';
import { PackageJsonDeps } from './utils/pruning';
Expand Down Expand Up @@ -256,19 +255,29 @@ export function stringifyNpmLockFile(lockFileData: LockFileData): string {
const lockFileJson: NpmLockFile = {
...lockFileData.lockFileMetadata.metadata,
...(notV1 && {
packages: sortObject(
packages,
(value) => value,
false,
(a, b) => a.localeCompare(b)
),
packages: sortObject(packages),
}),
...(notV3 && { dependencies: sortDependencies(dependencies) }),
};

return JSON.stringify(lockFileJson, null, 2) + '\n';
}

function sortObject(packages: Dependencies): Dependencies | undefined {
const keys = Object.keys(packages);
if (keys.length === 0) {
return;
}

keys.sort((a, b) => a.localeCompare(b));

const result: Dependencies = {};
keys.forEach((key) => {
result[key] = packages[key];
});
return result;
}

// remapping the package back to package-lock format
function unmapPackage(packages: Dependencies, dependency: PackageDependency) {
const {
Expand Down
19 changes: 14 additions & 5 deletions packages/nx/src/lock-file/pnpm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import {
PackageVersions,
} from './utils/lock-file-type';
import { load, dump } from '@zkochan/js-yaml';
import { sortObject } from './utils/sorting';
import { TransitiveLookupFunctionInput, isRootVersion } from './utils/mapping';
import { hashString, generatePrunnedHash } from './utils/hashing';
import { satisfies } from 'semver';
import { PackageJsonDeps } from './utils/pruning';
import { sortObjectByKeys } from '../utils/object-sort';

type PackageMeta = {
key: string;
Expand Down Expand Up @@ -119,6 +119,7 @@ function mapPackages(
};
}
});

Object.keys(mappedPackages).forEach((packageName) => {
const versions = mappedPackages[packageName];
const versionKeys = Object.keys(versions);
Expand Down Expand Up @@ -316,10 +317,18 @@ function unmapLockFile(lockFileData: LockFileData): PnpmLockFile {

return {
...(lockFileMetatada as { lockfileVersion: number }),
specifiers: sortObject(specifiers),
dependencies: sortObject(dependencies),
devDependencies: sortObject(devDependencies),
packages: sortObject(packages),
...(Object.keys(specifiers).length && {
specifiers: sortObjectByKeys(specifiers),
}),
...(Object.keys(dependencies).length && {
dependencies: sortObjectByKeys(dependencies),
}),
...(Object.keys(devDependencies).length && {
devDependencies: sortObjectByKeys(devDependencies),
}),
...(Object.keys(packages).length && {
packages: sortObjectByKeys(packages),
}),
time,
};
}
Expand Down
9 changes: 9 additions & 0 deletions packages/nx/src/lock-file/utils/hashing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ 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) {
Expand All @@ -25,10 +30,13 @@ export function hashExternalNodes(projectGraph: 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());
}
Expand All @@ -42,6 +50,7 @@ function traverseExternalNodesDependencies(
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]) {
Expand Down
41 changes: 26 additions & 15 deletions packages/nx/src/lock-file/utils/mapping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,17 @@ import {
PackageVersions,
} from './lock-file-type';

export type TransitiveLookupFunctionInput = {
packageName: string;
parentPackages: string[];
versions: PackageVersions;
version: string;
};

type TransitiveLookupFunction = (
data: TransitiveLookupFunctionInput
) => PackageDependency;

/**
* Checks whether the package is a root dependency
* @param packageName
Expand All @@ -15,6 +26,7 @@ import {
*/
export function isRootVersion(packageName: string, version: string): boolean {
const fullPath = `${workspaceRoot}/node_modules/${packageName}/package.json`;

if (existsSync(fullPath)) {
const content = readFileSync(fullPath, 'utf-8');
return JSON.parse(content).version === version;
Expand All @@ -23,17 +35,6 @@ export function isRootVersion(packageName: string, version: string): boolean {
}
}

export type TransitiveLookupFunctionInput = {
packageName: string;
parentPackages: string[];
versions: PackageVersions;
version: string;
};

type TransitiveLookupFunction = (
data: TransitiveLookupFunctionInput
) => PackageDependency;

// Finds the maching version of each dependency of the package and
// maps each {package}:{versionRange} pair to "npm:{package}@{version}" (when transitive) or "npm:{package}" (when root)
function mapTransitiveDependencies(
Expand All @@ -46,6 +47,7 @@ function mapTransitiveDependencies(
if (!dependencies) {
return [];
}

const result: string[] = [];

Object.keys(dependencies).forEach((packageName) => {
Expand Down Expand Up @@ -91,7 +93,7 @@ function mapTransitiveDependencies(
}

/**
* Returns node name depending whether it's root version or nested
* Returns node name depending on whether it's root version or nested
*/
export function getNodeName(
dep: string,
Expand All @@ -101,6 +103,14 @@ export function getNodeName(
return rootVersion ? `npm:${dep}` : `npm:${dep}@${version}`;
}

/**
* Maps the lockfile data to the partial project graph
* using package manager specific {@link TransitiveLookupFunction}
*
* @param lockFileData
* @param transitiveLookupFn
* @returns
*/
export function mapExternalNodes(
lockFileData: LockFileData,
transitiveLookupFn: TransitiveLookupFunction
Expand All @@ -127,20 +137,21 @@ export function mapExternalNodes(
},
};

const combinedDependencies =
// combine dependencies and peerDependencies
const allDependencies =
dependencies || peerDependencies
? {
...(dependencies || {}),
...(peerDependencies || {}),
}
: undefined;

if (combinedDependencies) {
if (allDependencies) {
const nodeDependencies = [];
const transitiveDeps = mapTransitiveDependencies(
[packageName],
lockFileData.dependencies,
combinedDependencies,
allDependencies,
versionCache,
transitiveLookupFn
);
Expand Down
25 changes: 0 additions & 25 deletions packages/nx/src/lock-file/utils/sorting.ts

This file was deleted.

12 changes: 9 additions & 3 deletions packages/nx/src/lock-file/yarn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import {
PackageDependency,
PackageVersions,
} from './utils/lock-file-type';
import { sortObject } from './utils/sorting';
import { TransitiveLookupFunctionInput, isRootVersion } from './utils/mapping';
import { hashString, generatePrunnedHash } from './utils/hashing';
import { PackageJsonDeps } from './utils/pruning';
import { sortObjectByKeys } from '../utils/object-sort';

type LockFileDependencies = Record<
string,
Expand All @@ -27,9 +27,10 @@ export function parseYarnLockFile(lockFile: string): LockFileData {
const { __metadata, ...dependencies } = parseSyml(lockFile);

// Yarn Berry has workspace packages includes, so we need to extract those to metadata
const [mappedPackages, workspacePackages] = mapPackages(dependencies);
const isBerry = !!__metadata;
const hash = hashString(lockFile);
const [mappedPackages, workspacePackages] = mapPackages(dependencies);

if (isBerry) {
return {
dependencies: mappedPackages,
Expand Down Expand Up @@ -104,12 +105,14 @@ export function stringifyYarnLockFile(lockFileData: LockFileData): string {
// this is an easy way to distinguish it from the classic
const isBerry = !!lockFileData.lockFileMetadata?.__metadata;
const dependencies = unmapPackages(lockFileData.dependencies, isBerry);

if (isBerry) {
const lockFile = {
__metadata: lockFileData.lockFileMetadata.__metadata,
...lockFileData.lockFileMetadata.workspacePackages,
...dependencies,
};

// berry's stringifySyml doesn't generate comment
return BERRY_LOCK_FILE_DISCLAIMER + stringifySyml(lockFile);
} else {
Expand Down Expand Up @@ -182,6 +185,7 @@ export function pruneYarnLockFile(
normalizedPackageJson: PackageJsonDeps
): LockFileData {
const isBerry = !!lockFileData.lockFileMetadata?.__metadata;

const prunedDependencies = pruneDependencies(
lockFileData.dependencies,
normalizedPackageJson
Expand Down Expand Up @@ -321,7 +325,7 @@ function pruneWorkspacePackages(
resolution: workspaceProjKey,
languageName: 'unknown',
linkType: 'soft',
dependencies: sortObject(prunedWorkspaceDependencies),
dependencies: sortObjectByKeys(prunedWorkspaceDependencies),
};
}

Expand Down Expand Up @@ -401,10 +405,12 @@ function findDependencyTriplet(

for (let i = 0; i < entries.length; i++) {
const [key, value] = entries[i];

let metaVersion = `${packageName}@${version}`;
if (value.packageMeta.includes(metaVersion)) {
return [key, value, metaVersion];
}

// for berry, meta version starts with 'npm:'
metaVersion = `${packageName}@npm:${version}`;
if (value.packageMeta.includes(metaVersion)) {
Expand Down

0 comments on commit bd196b3

Please sign in to comment.