Skip to content

Commit

Permalink
chore(js): convert typescript dependency processing to a createDepend… (
Browse files Browse the repository at this point in the history
  • Loading branch information
FrozenPandaz authored Aug 25, 2023
1 parent 182a0d0 commit 3ebb772
Show file tree
Hide file tree
Showing 6 changed files with 262 additions and 305 deletions.
48 changes: 39 additions & 9 deletions packages/nx/src/plugins/js/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,10 @@ import {
ProjectGraph,
ProjectGraphProcessor,
} from '../../config/project-graph';
import { ProjectGraphBuilder } from '../../project-graph/project-graph-builder';
import {
ProjectGraphBuilder,
ProjectGraphDependencyWithFile,
} from '../../project-graph/project-graph-builder';
import { buildExplicitDependencies } from './project-graph/build-dependencies/build-dependencies';
import { readNxJson } from '../../config/configuration';
import { fileExists, readJsonFile } from '../../utils/fileutils';
Expand All @@ -19,6 +22,24 @@ import { readFileSync, writeFileSync } from 'fs';
import { workspaceRoot } from '../../utils/workspace-root';
import { ensureDirSync } from 'fs-extra';
import { performance } from 'perf_hooks';
import {
CreateDependencies,
CreateDependenciesContext,
} from '../../utils/nx-plugin';

const createDependencies: CreateDependencies = (context) => {
const pluginConfig = jsPluginConfig(context.nxJsonConfiguration);

performance.mark('build typescript dependencies - start');
const dependencies = buildExplicitDependencies(pluginConfig, context);
performance.mark('build typescript dependencies - end');
performance.measure(
'build typescript dependencies',
'build typescript dependencies - start',
'build typescript dependencies - end'
);
return dependencies;
};

export const processProjectGraph: ProjectGraphProcessor = async (
graph,
Expand All @@ -42,14 +63,23 @@ export const processProjectGraph: ProjectGraphProcessor = async (
}
}

performance.mark('build typescript dependencies - start');
await buildExplicitDependencies(pluginConfig, context, builder);
performance.mark('build typescript dependencies - end');
performance.measure(
'build typescript dependencies',
'build typescript dependencies - start',
'build typescript dependencies - end'
);
const createDependenciesContext: CreateDependenciesContext = {
...context,
graph,
};

const dependencies = createDependencies(
createDependenciesContext
) as ProjectGraphDependencyWithFile[];

for (const dep of dependencies) {
builder.addDependency(
dep.source,
dep.target,
dep.dependencyType,
dep.sourceFile
);
}

return builder.getUpdatedProjectGraph();
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,18 @@
import {
DependencyType,
ProjectGraphProcessorContext,
} from '../../../../config/project-graph';
import { ProjectGraphBuilder } from '../../../../project-graph/project-graph-builder';
import {
buildExplicitTypeScriptDependencies,
ExplicitDependency,
} from './explicit-project-dependencies';
import { buildExplicitTypeScriptDependencies } from './explicit-project-dependencies';
import { buildExplicitPackageJsonDependencies } from './explicit-package-json-dependencies';
import { CreateDependenciesContext } from '../../../../utils/nx-plugin';
import { ProjectGraphDependencyWithFile } from '../../../../project-graph/project-graph-builder';

export function buildExplicitDependencies(
jsPluginConfig: {
analyzeSourceFiles?: boolean;
analyzePackageJson?: boolean;
},
ctx: ProjectGraphProcessorContext,
builder: ProjectGraphBuilder
) {
if (totalNumberOfFilesToProcess(ctx) === 0) return;
ctx: CreateDependenciesContext
): ProjectGraphDependencyWithFile[] {
if (totalNumberOfFilesToProcess(ctx) === 0) return [];

let dependencies: ExplicitDependency[] = [];
let dependencies: ProjectGraphDependencyWithFile[] = [];

if (
jsPluginConfig.analyzeSourceFiles === undefined ||
Expand All @@ -32,7 +25,7 @@ export function buildExplicitDependencies(
} catch {}
if (tsExists) {
dependencies = dependencies.concat(
buildExplicitTypeScriptDependencies(builder.graph, ctx.filesToProcess)
buildExplicitTypeScriptDependencies(ctx)
);
}
}
Expand All @@ -41,40 +34,17 @@ export function buildExplicitDependencies(
jsPluginConfig.analyzePackageJson === true
) {
dependencies = dependencies.concat(
buildExplicitPackageJsonDependencies(
ctx.nxJsonConfiguration,
ctx.projectsConfigurations,
builder.graph,
ctx.filesToProcess
)
buildExplicitPackageJsonDependencies(ctx)
);
}

dependencies.forEach((r) => addDependency(builder, r));
return dependencies;
}

function totalNumberOfFilesToProcess(ctx: ProjectGraphProcessorContext) {
function totalNumberOfFilesToProcess(ctx: CreateDependenciesContext) {
let totalNumOfFilesToProcess = 0;
Object.values(ctx.filesToProcess).forEach(
(t) => (totalNumOfFilesToProcess += t.length)
);
return totalNumOfFilesToProcess;
}
function addDependency(
builder: ProjectGraphBuilder,
dependency: ExplicitDependency
) {
if (dependency.type === DependencyType.static) {
builder.addStaticDependency(
dependency.sourceProjectName,
dependency.targetProjectName,
dependency.sourceProjectFile
);
} else {
builder.addDynamicDependency(
dependency.sourceProjectName,
dependency.targetProjectName,
dependency.sourceProjectFile
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,14 @@ const tempFs = new TempFs('explicit-package-json');

import { buildExplicitPackageJsonDependencies } from './explicit-package-json-dependencies';

import {
ProjectGraphProcessorContext,
ProjectGraphProjectNode,
} from '../../../../config/project-graph';
import { ProjectGraphProjectNode } from '../../../../config/project-graph';
import { ProjectGraphBuilder } from '../../../../project-graph/project-graph-builder';
import { createProjectFileMap } from '../../../../project-graph/file-map-utils';
import { fileHasher } from '../../../../hasher/file-hasher';
import { CreateDependenciesContext } from '../../../../utils/nx-plugin';

describe('explicit package json dependencies', () => {
let ctx: ProjectGraphProcessorContext;
let ctx: CreateDependenciesContext;
let projects: Record<string, ProjectGraphProjectNode>;

beforeEach(async () => {
Expand Down Expand Up @@ -55,15 +53,6 @@ describe('explicit package json dependencies', () => {

await fileHasher.init();

ctx = {
projectsConfigurations,
nxJsonConfiguration,
filesToProcess: createProjectFileMap(
projectsConfigurations as any,
fileHasher.allFileData()
).projectFileMap,
} as any;

projects = {
proj: {
name: 'proj',
Expand All @@ -83,14 +72,13 @@ describe('explicit package json dependencies', () => {
data: { root: 'libs/proj4' },
},
};
});

afterEach(() => {
tempFs.cleanup();
});
const projectFileMap = createProjectFileMap(
projectsConfigurations as any,
fileHasher.allFileData()
).projectFileMap;

it(`should add dependencies for projects based on deps in package.json`, () => {
const builder = new ProjectGraphBuilder(undefined, ctx.fileMap);
const builder = new ProjectGraphBuilder(undefined, projectFileMap);
Object.values(projects).forEach((p) => {
builder.addNode(p);
});
Expand All @@ -103,31 +91,40 @@ describe('explicit package json dependencies', () => {
},
});

const res = buildExplicitPackageJsonDependencies(
ctx.nxJsonConfiguration,
ctx.projectsConfigurations,
builder.graph,
ctx.filesToProcess
);
ctx = {
fileMap: projectFileMap,
graph: builder.getUpdatedProjectGraph(),
projectsConfigurations: projectsConfigurations as any,
nxJsonConfiguration,
filesToProcess: projectFileMap,
};
});

afterEach(() => {
tempFs.cleanup();
});

it(`should add dependencies for projects based on deps in package.json`, () => {
const res = buildExplicitPackageJsonDependencies(ctx);

expect(res).toEqual([
{
sourceProjectName: 'proj',
targetProjectName: 'proj2',
sourceProjectFile: 'libs/proj/package.json',
type: 'static',
source: 'proj',
target: 'proj2',
sourceFile: 'libs/proj/package.json',
dependencyType: 'static',
},
{
sourceProjectFile: 'libs/proj/package.json',
sourceProjectName: 'proj',
targetProjectName: 'npm:external',
type: 'static',
sourceFile: 'libs/proj/package.json',
source: 'proj',
target: 'npm:external',
dependencyType: 'static',
},
{
sourceProjectName: 'proj',
targetProjectName: 'proj3',
sourceProjectFile: 'libs/proj/package.json',
type: 'static',
source: 'proj',
target: 'proj3',
sourceFile: 'libs/proj/package.json',
dependencyType: 'static',
},
]);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,27 @@ import { defaultFileRead } from '../../../../project-graph/file-utils';
import { join } from 'path';
import {
DependencyType,
ProjectFileMap,
ProjectGraph,
ProjectGraphProjectNode,
} from '../../../../config/project-graph';
import { parseJson } from '../../../../utils/json';
import { joinPathFragments } from '../../../../utils/path';
import { ProjectsConfigurations } from '../../../../config/workspace-json-project-json';
import { NxJsonConfiguration } from '../../../../config/nx-json';
import { ExplicitDependency } from './explicit-project-dependencies';
import { PackageJson } from '../../../../utils/package-json';
import { CreateDependenciesContext } from '../../../../utils/nx-plugin';
import {
ProjectGraphDependencyWithFile,
validateDependency,
} from '../../../../project-graph/project-graph-builder';

export function buildExplicitPackageJsonDependencies(
nxJsonConfiguration: NxJsonConfiguration,
projectsConfigurations: ProjectsConfigurations,
graph: ProjectGraph,
filesToProcess: ProjectFileMap
) {
const res = [] as any;
export function buildExplicitPackageJsonDependencies({
nxJsonConfiguration,
projectsConfigurations,
graph,
filesToProcess,
}: CreateDependenciesContext): ProjectGraphDependencyWithFile[] {
const res: ProjectGraphDependencyWithFile[] = [];
let packageNameMap = undefined;
const nodes = Object.values(graph.nodes);
Object.keys(filesToProcess).forEach((source) => {
Expand Down Expand Up @@ -82,7 +85,7 @@ function processPackageJson(
sourceProject: string,
fileName: string,
graph: ProjectGraph,
collectedDeps: ExplicitDependency[],
collectedDeps: ProjectGraphDependencyWithFile[],
packageNameMap: { [packageName: string]: string }
) {
try {
Expand All @@ -91,19 +94,23 @@ function processPackageJson(
deps.forEach((d) => {
// package.json refers to another project in the monorepo
if (packageNameMap[d]) {
collectedDeps.push({
sourceProjectName: sourceProject,
targetProjectName: packageNameMap[d],
sourceProjectFile: fileName,
type: DependencyType.static,
});
const dependency = {
source: sourceProject,
target: packageNameMap[d],
sourceFile: fileName,
dependencyType: DependencyType.static,
};
validateDependency(graph, dependency);
collectedDeps.push(dependency);
} else if (graph.externalNodes[`npm:${d}`]) {
collectedDeps.push({
sourceProjectName: sourceProject,
targetProjectName: `npm:${d}`,
sourceProjectFile: fileName,
type: DependencyType.static,
});
const dependency = {
source: sourceProject,
target: `npm:${d}`,
sourceFile: fileName,
dependencyType: DependencyType.static,
};
validateDependency(graph, dependency);
collectedDeps.push(dependency);
}
});
} catch (e) {
Expand Down
Loading

0 comments on commit 3ebb772

Please sign in to comment.