Skip to content

Commit

Permalink
feat(core): refactor most of js graph work into its own folder (#15365)
Browse files Browse the repository at this point in the history
  • Loading branch information
FrozenPandaz committed Mar 28, 2023
1 parent 16b3fa0 commit 8cf8f18
Show file tree
Hide file tree
Showing 101 changed files with 551 additions and 462 deletions.
2 changes: 1 addition & 1 deletion .prettierignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ packages/express/src/schematics/**/files/**/*.json
packages/nest/src/schematics/**/files/**/*.json
packages/react/src/schematics/**/files/**/*.json
packages/jest/src/schematics/**/files/**/*.json
packages/nx/src/lock-file/__fixtures__/**/*.*
packages/nx/src/plugins/js/lock-file/__fixtures__/**/*.*
packages/**/schematics/**/files/**/*.html
packages/**/generators/**/files/**/*.html
packages/nx/src/native/
Expand Down
2 changes: 1 addition & 1 deletion e2e/node/src/node.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import {
} from '@nrwl/e2e/utils';
import { exec, execSync } from 'child_process';
import * as http from 'http';
import { getLockFileName } from 'nx/src/lock-file/lock-file';
import { getLockFileName } from 'nx/src/plugins/js/lock-file/lock-file';
import { satisfies } from 'semver';

function getData(port, path = '/api'): Promise<any> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { DependencyType } from '@nrwl/devkit';
import * as parser from '@typescript-eslint/parser';
import { TSESLint } from '@typescript-eslint/utils';
import { vol } from 'memfs';
import { TargetProjectLocator } from 'nx/src/utils/target-project-locator';
import { TargetProjectLocator } from 'nx/src/plugins/js/project-graph/build-dependencies/target-project-locator';
import enforceModuleBoundaries, {
RULE_NAME as enforceModuleBoundariesRuleName,
} from '../../src/rules/enforce-module-boundaries';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import {
isComboDepConstraint,
} from '../utils/runtime-lint-utils';
import { AST_NODE_TYPES, TSESTree } from '@typescript-eslint/utils';
import { TargetProjectLocator } from 'nx/src/utils/target-project-locator';
import { TargetProjectLocator } from 'nx/src/plugins/js/project-graph/build-dependencies/target-project-locator';
import { basename, dirname, relative } from 'path';
import {
getBarrelEntryPointByImportScope,
Expand Down
2 changes: 1 addition & 1 deletion packages/eslint-plugin-nx/src/utils/runtime-lint-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import {
} from '@nrwl/devkit';
import { getPath, pathExists } from './graph-utils';
import { readFileIfExisting } from 'nx/src/utils/fileutils';
import { TargetProjectLocator } from 'nx/src/utils/target-project-locator';
import { TargetProjectLocator } from 'nx/src/plugins/js/project-graph/build-dependencies/target-project-locator';
import {
findProjectForPath,
ProjectRootMappings,
Expand Down
4 changes: 2 additions & 2 deletions packages/js/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ export * from './utils/assets';
export * from './utils/package-json/update-package-json';
export { libraryGenerator } from './generators/library/library';
export { initGenerator } from './generators/init/init';
export { createLockFile } from 'nx/src/lock-file/lock-file';
export { createPackageJson } from 'nx/src/utils/create-package-json';
export { createLockFile } from 'nx/src/plugins/js/lock-file/lock-file';
export { createPackageJson } from 'nx/src/plugins/js/package-json/create-package-json';
7 changes: 5 additions & 2 deletions packages/js/src/utils/package-json/update-package-json.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { createLockFile, getLockFileName } from 'nx/src/lock-file/lock-file';
import { createPackageJson } from 'nx/src/utils/create-package-json';
import {
createLockFile,
getLockFileName,
} from 'nx/src/plugins/js/lock-file/lock-file';
import { createPackageJson } from 'nx/src/plugins/js/package-json/create-package-json';
import {
ExecutorContext,
getOutputsForTargetAndConfiguration,
Expand Down
2 changes: 1 addition & 1 deletion packages/next/src/executors/build/build.impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { createNextConfigFile } from './lib/create-next-config-file';
import { checkPublicDirectory } from './lib/check-project';
import { NextBuildBuilderOptions } from '../../utils/types';

import { getLockFileName } from 'nx/src/lock-file/lock-file';
import { getLockFileName } from 'nx/src/plugins/js/lock-file/lock-file';

export default async function buildExecutor(
options: NextBuildBuilderOptions,
Expand Down
4 changes: 2 additions & 2 deletions packages/nx/src/devkit-exports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -226,8 +226,8 @@ export { Hash, Hasher } from './hasher/hasher';
*/
export { cacheDir } from './utils/cache-directory';

import { createLockFile as _createLockFile } from './lock-file/lock-file';
import { createPackageJson as _createPackageJson } from './utils/create-package-json';
import { createLockFile as _createLockFile } from './plugins/js/lock-file/lock-file';
import { createPackageJson as _createPackageJson } from './plugins/js/package-json/create-package-json';

/**
* @category Package Manager
Expand Down
79 changes: 78 additions & 1 deletion packages/nx/src/plugins/js/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,88 @@
import { ProjectGraphProcessor } from '../../config/project-graph';
import { ProjectGraphBuilder } from '../../project-graph/project-graph-builder';
import { buildNpmPackageNodes } from './project-graph/build-nodes/build-npm-package-nodes';
import { buildExplicitDependencies } from './project-graph/build-dependencies/build-dependencies';
import { readNxJson } from '../../config/configuration';
import { fileExists, readJsonFile } from '../../utils/fileutils';
import { PackageJson } from '../../utils/package-json';
import {
lockFileExists,
lockFileHash,
parseLockFile,
} from './lock-file/lock-file';
import { NrwlJsPluginConfig, NxJsonConfiguration } from '../../config/nx-json';
import { dirname, join } from 'path';
import { projectGraphCacheDirectory } from '../../utils/cache-directory';
import { readFileSync, writeFileSync } from 'fs';
import { workspaceRoot } from '../../utils/workspace-root';
import { ensureDirSync } from 'fs-extra';
import { removeNpmNodes } from 'nx/src/plugins/js/lock-file/remove-npm-nodes';

export const processProjectGraph: ProjectGraphProcessor = (graph) => {
export const processProjectGraph: ProjectGraphProcessor = (graph, context) => {
const builder = new ProjectGraphBuilder(graph);

const lockHash = lockFileHash() ?? 'n/a';
// during the create-nx-workspace lock file might not exists yet
if (lockFileExists()) {
if (lockFileNeedsReprocessing(lockHash)) {
removeNpmNodes(graph, builder);
parseLockFile(builder);
}
writeLastProcessedLockfileHash(lockHash);
}

buildNpmPackageNodes(builder);

buildExplicitDependencies(jsPluginConfig(readNxJson()), context, builder);

return builder.getUpdatedProjectGraph();
};

const lockFileHashFile = join(projectGraphCacheDirectory, 'lockfile.hash');
function lockFileNeedsReprocessing(lockHash: string) {
try {
return readFileSync(lockFileHashFile).toString() !== lockHash;
} catch {
return true;
}
}

function writeLastProcessedLockfileHash(hash: string) {
ensureDirSync(dirname(lockFileHashFile));
writeFileSync(lockFileHashFile, hash);
}

function jsPluginConfig(nxJson: NxJsonConfiguration): NrwlJsPluginConfig {
if (nxJson?.pluginsConfig?.['@nrwl/js']) {
return nxJson?.pluginsConfig?.['@nrwl/js'];
}

if (!fileExists(join(workspaceRoot, 'package.json'))) {
return {
analyzePackageJson: false,
analyzeSourceFiles: false,
};
}

const packageJson = readJsonFile<PackageJson>(
join(workspaceRoot, 'package.json')
);

const packageJsonDeps = {
...packageJson.dependencies,
...packageJson.devDependencies,
};
if (
packageJsonDeps['@nrwl/workspace'] ||
packageJsonDeps['@nrwl/js'] ||
packageJsonDeps['@nrwl/node'] ||
packageJsonDeps['@nrwl/next'] ||
packageJsonDeps['@nrwl/react'] ||
packageJsonDeps['@nrwl/angular'] ||
packageJsonDeps['@nrwl/web']
) {
return { analyzePackageJson: true, analyzeSourceFiles: true };
} else {
return { analyzePackageJson: true, analyzeSourceFiles: false };
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ const packageNames = [];

function processNodeModules(path = '.') {
if (existsSync(`${path}/node_modules`)) {
readdirSync(`${path}/node_modules`).forEach(folder => {
readdirSync(`${path}/node_modules`).forEach((folder) => {
if (folder.startsWith('@')) {
readdirSync(`${path}/node_modules/${folder}`).forEach(subfolder => {
readdirSync(`${path}/node_modules/${folder}`).forEach((subfolder) => {
packageNames.push(`${path}/node_modules/${folder}/${subfolder}`);
processNodeModules(`${path}/node_modules/${folder}/${subfolder}`);
});
Expand All @@ -29,15 +29,15 @@ function processNodeModules(path = '.') {

processNodeModules();

packageNames.forEach(path => {
packageNames.forEach((path) => {
const filePath = `${path}/package.json`;
if (existsSync(filePath)) {
const content = readFileSync(filePath, 'utf-8');
const peerDependencies = JSON.parse(content).peerDependencies;
const peerDependenciesMeta = JSON.parse(content).peerDependenciesMeta;
const output = JSON.stringify({
...peerDependencies && { peerDependencies },
...peerDependenciesMeta && { peerDependenciesMeta },
...(peerDependencies && { peerDependencies }),
...(peerDependenciesMeta && { peerDependenciesMeta }),
});
if (output === '{}') return;
report += `'${filePath.slice(2)}': '${output}',\n`;
Expand All @@ -59,18 +59,18 @@ const existsSync = require('fs').existsSync;
let report = '';

const packageNames = [];
readdirSync('node_modules').forEach(folder => {
readdirSync('node_modules').forEach((folder) => {
if (folder === '.pnpm') return;
if (folder.startsWith('@')) {
readdirSync(`node_modules/${folder}`).forEach(subfolder => {
readdirSync(`node_modules/${folder}`).forEach((subfolder) => {
packageNames.push(`${folder}/${subfolder}`);
});
} else {
packageNames.push(folder);
}
});

packageNames.forEach(packageName => {
packageNames.forEach((packageName) => {
const path = `node_modules/${packageName}/package.json`;
if (existsSync(path)) {
const content = readFileSync(path, 'utf-8');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1103,9 +1103,7 @@
"integrity": "sha512-xiqMQR4xAeHTuB9uWm+fFRcIOgKBMiOBP+eXiyT7jsgVCq1bkVygt00oASowB7EdtpOHaaPgKt812P9ab+DDKA==",
"hasInstallScript": true,
"optional": true,
"os": [
"darwin"
],
"os": ["darwin"],
"peer": true,
"engines": {
"node": "^8.16.0 || ^10.6.0 || >=11.0.0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1103,9 +1103,7 @@
"integrity": "sha512-xiqMQR4xAeHTuB9uWm+fFRcIOgKBMiOBP+eXiyT7jsgVCq1bkVygt00oASowB7EdtpOHaaPgKt812P9ab+DDKA==",
"hasInstallScript": true,
"optional": true,
"os": [
"darwin"
],
"os": ["darwin"],
"peer": true,
"engines": {
"node": "^8.16.0 || ^10.6.0 || >=11.0.0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,22 @@
import { readFileSync, existsSync } from 'fs';
import { join } from 'path';

import { detectPackageManager, PackageManager } from '../utils/package-manager';
import { workspaceRoot } from '../utils/workspace-root';
import { ProjectGraph } from '../config/project-graph';
import { PackageJson } from '../utils/package-json';
import { defaultHashing } from '../hasher/hashing-impl';
import {
detectPackageManager,
PackageManager,
} from '../../../utils/package-manager';
import { workspaceRoot } from '../../../utils/workspace-root';
import { ProjectGraph } from '../../../config/project-graph';
import { ProjectGraphBuilder } from '../../../project-graph/project-graph-builder';
import { PackageJson } from '../../../utils/package-json';
import { defaultHashing } from '../../../hasher/hashing-impl';
import { output } from '../../../utils/output';

import { parseNpmLockfile, stringifyNpmLockfile } from './npm-parser';
import { parsePnpmLockfile, stringifyPnpmLockfile } from './pnpm-parser';
import { parseYarnLockfile, stringifyYarnLockfile } from './yarn-parser';
import { pruneProjectGraph } from './project-graph-pruning';
import { normalizePackageJson } from './utils/package-json';
import { output } from '../utils/output';

const YARN_LOCK_FILE = 'yarn.lock';
const NPM_LOCK_FILE = 'package-lock.json';
Expand Down Expand Up @@ -76,20 +80,24 @@ export function lockFileHash(
* Parses lock file and maps dependencies and metadata to {@link LockFileGraph}
*/
export function parseLockFile(
builder: ProjectGraphBuilder,
packageManager: PackageManager = detectPackageManager(workspaceRoot)
): ProjectGraph {
try {
if (packageManager === 'yarn') {
const content = readFileSync(YARN_LOCK_PATH, 'utf8');
return parseYarnLockfile(content);
parseYarnLockfile(content, builder);
return builder.getUpdatedProjectGraph();
}
if (packageManager === 'pnpm') {
const content = readFileSync(PNPM_LOCK_PATH, 'utf8');
return parsePnpmLockfile(content);
parsePnpmLockfile(content, builder);
return builder.getUpdatedProjectGraph();
}
if (packageManager === 'npm') {
const content = readFileSync(NPM_LOCK_PATH, 'utf8');
return parseNpmLockfile(content);
parseNpmLockfile(content, builder);
return builder.getUpdatedProjectGraph();
}
} catch (e) {
if (!isPostInstallProcess()) {
Expand Down Expand Up @@ -138,19 +146,26 @@ export function createLockFile(
const normalizedPackageJson = normalizePackageJson(packageJson);
const content = readFileSync(getLockFileName(packageManager), 'utf8');

const builder = new ProjectGraphBuilder();

try {
if (packageManager === 'yarn') {
const graph = parseYarnLockfile(content);
parseYarnLockfile(content, builder);
const graph = builder.getUpdatedProjectGraph();
const prunedGraph = pruneProjectGraph(graph, packageJson);
return stringifyYarnLockfile(prunedGraph, content, normalizedPackageJson);
}
if (packageManager === 'pnpm') {
const graph = parsePnpmLockfile(content);
parsePnpmLockfile(content, builder);

const graph = builder.getUpdatedProjectGraph();
const prunedGraph = pruneProjectGraph(graph, packageJson);
return stringifyPnpmLockfile(prunedGraph, content, normalizedPackageJson);
}
if (packageManager === 'npm') {
const graph = parseNpmLockfile(content);
parseNpmLockfile(content, builder);

const graph = builder.getUpdatedProjectGraph();
const prunedGraph = pruneProjectGraph(graph, packageJson);
return stringifyNpmLockfile(prunedGraph, content, normalizedPackageJson);
}
Expand Down
Loading

1 comment on commit 8cf8f18

@vercel
Copy link

@vercel vercel bot commented on 8cf8f18 Mar 28, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

nx-dev – ./

nx.dev
nx-dev-nrwl.vercel.app
nx-dev-git-master-nrwl.vercel.app
nx-five.vercel.app

Please sign in to comment.