Skip to content

Commit

Permalink
feat(core): add a metadata plugin for project file sizes
Browse files Browse the repository at this point in the history
  • Loading branch information
Cammisuli committed May 9, 2024
1 parent fe98558 commit c2d77c1
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 1 deletion.
38 changes: 38 additions & 0 deletions packages/nx/src/plugins/project-sizes/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import {
CreateMetadata,
CreateMetadataContext,
ProjectsMetadata,
} from '../../project-graph/plugins';
import { ProjectGraph } from '../../config/project-graph';
import { getFileMap } from '../../project-graph/build-project-graph';

export const name = 'nx/core/project-sizes';

export const createMetadata: CreateMetadata = (
graph: ProjectGraph,
options: unknown,
context: CreateMetadataContext
): ProjectsMetadata => {
// TODO: should this be a call to the daemon so that we can work in isolated context?
const {
fileMap: { projectFileMap },
} = getFileMap();

const projectsMetadata: ProjectsMetadata = {};
for (const [projectId, project] of Object.entries(graph.nodes)) {
let totalSize = 0;
let totalFiles = projectFileMap[projectId].length;
for (let i of projectFileMap[projectId]) {
totalSize += i.size ?? 0;
}

projectsMetadata[projectId] = {
metadata: {
totalSize,
totalFiles,
} as any,
};
}

return projectsMetadata;
};
1 change: 1 addition & 0 deletions packages/nx/src/project-graph/plugins/internal-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,5 +154,6 @@ export async function getDefaultPlugins(root: string) {
: []),
join(__dirname, '../../plugins/package-json-workspaces'),
join(__dirname, '../../plugins/project-json/build-nodes/project-json'),
join(__dirname, '../../plugins/project-sizes'),
];
}
6 changes: 5 additions & 1 deletion packages/nx/src/project-graph/plugins/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@ import { AggregateCreateNodesError, CreateNodesError } from '../error-types';
import { performance } from 'perf_hooks';

export function isNxPluginV2(plugin: NxPlugin): plugin is NxPluginV2 {
return 'createNodes' in plugin || 'createDependencies' in plugin;
return (
'createNodes' in plugin ||
'createDependencies' in plugin ||
'createMetadata' in plugin
);
}

export function isNxPluginV1(
Expand Down

0 comments on commit c2d77c1

Please sign in to comment.