Skip to content

Commit

Permalink
chore(core): wip
Browse files Browse the repository at this point in the history
  • Loading branch information
FrozenPandaz committed Nov 28, 2024
1 parent b018b94 commit 41d0324
Show file tree
Hide file tree
Showing 20 changed files with 238 additions and 117 deletions.
8 changes: 5 additions & 3 deletions packages/nx/bin/post-install.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@ import { setupWorkspaceContext } from '../src/utils/workspace-context';
if (isMainNxPackage() && fileExists(join(workspaceRoot, 'nx.json'))) {
assertSupportedPlatform();
setupWorkspaceContext(workspaceRoot);
try {
await daemonClient.stop();
} catch (e) {}
if (daemonClient.enabled()) {
try {
await daemonClient.stop();
} catch (e) {}
}
const tasks: Array<Promise<any>> = [
buildProjectGraphAndSourceMapsWithoutDaemon(),
];
Expand Down
43 changes: 40 additions & 3 deletions packages/nx/plugins/package-json.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,50 @@
import type { NxPluginV2 } from '../src/project-graph/plugins';
import { createNodesFromFiles, NxPluginV2 } from '../src/project-graph/plugins';
import { workspaceRoot } from '../src/utils/workspace-root';
import { createNodeFromPackageJson } from '../src/plugins/package-json';
import { workspaceDataDirectory } from '../src/utils/cache-directory';
import { join } from 'path';
import { ProjectConfiguration } from '../src/config/workspace-json-project-json';
import { readJsonFile, writeJsonFile } from '../src/utils/fileutils';

export type PackageJsonConfigurationCache = {
[hash: string]: ProjectConfiguration;
};

const cachePath = join(workspaceDataDirectory, 'package-json.hash');

export function readPackageJsonConfigurationCache() {
try {
return readJsonFile<PackageJsonConfigurationCache>(cachePath);
} catch (e) {
return {};
}
}

function writeCache(cache: PackageJsonConfigurationCache) {
writeJsonFile(cachePath, cache);
}

const plugin: NxPluginV2 = {
name: 'nx-all-package-jsons-plugin',
createNodes: [
createNodesV2: [
'*/**/package.json',
(f) => createNodeFromPackageJson(f, workspaceRoot),
(configFiles, options, context) => {
const cache = readPackageJsonConfigurationCache();

const result = createNodesFromFiles(
(f) => createNodeFromPackageJson(f, workspaceRoot, cache),
configFiles,
options,
context
);

writeCache(cache);

return result;
},
],
};

module.exports = plugin;
module.exports.readPackageJsonConfigurationCache =
readPackageJsonConfigurationCache;
21 changes: 17 additions & 4 deletions packages/nx/src/command-line/affected/affected.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ import {
splitArgsIntoNxArgsAndOverrides,
} from '../../utils/command-line-utils';
import { performance } from 'perf_hooks';
import { createProjectGraphAsync } from '../../project-graph/project-graph';
import {
createProjectGraphAndSourceMapsAsync,
createProjectGraphAsync,
} from '../../project-graph/project-graph';
import {
ProjectGraph,
ProjectGraphProjectNode,
Expand All @@ -21,6 +24,7 @@ import { readNxJson } from '../../config/configuration';
import { findMatchingProjects } from '../../utils/find-matching-projects';
import { generateGraph } from '../graph/graph';
import { allFileData } from '../../utils/all-file-data';
import { ConfigurationSourceMaps } from '../../project-graph/utils/project-configuration-utils';

export async function affected(
command: 'graph' | 'print-affected' | 'affected',
Expand Down Expand Up @@ -53,8 +57,15 @@ export async function affected(

await connectToNxCloudIfExplicitlyAsked(nxArgs);

const projectGraph = await createProjectGraphAsync({ exitOnError: true });
const projects = await getAffectedGraphNodes(nxArgs, projectGraph);
const { projectGraph, sourceMaps } =
await createProjectGraphAndSourceMapsAsync({
exitOnError: true,
});
const projects = await getAffectedGraphNodes(
nxArgs,
projectGraph,
sourceMaps
);

try {
switch (command) {
Expand Down Expand Up @@ -100,12 +111,14 @@ export async function affected(

export async function getAffectedGraphNodes(
nxArgs: NxArgs,
projectGraph: ProjectGraph
projectGraph: ProjectGraph,
sourceMaps: ConfigurationSourceMaps
): Promise<ProjectGraphProjectNode[]> {
let affectedGraph = nxArgs.all
? projectGraph
: await filterAffected(
projectGraph,
sourceMaps,
calculateFileChanges(
parseFiles(nxArgs).files,
await allFileData(),
Expand Down
11 changes: 9 additions & 2 deletions packages/nx/src/command-line/format/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ import {
getRootTsConfigPath,
} from '../../plugins/js/utils/typescript';
import { filterAffected } from '../../project-graph/affected/affected-project-graph';
import { createProjectGraphAsync } from '../../project-graph/project-graph';
import {
createProjectGraphAndSourceMapsAsync,
createProjectGraphAsync,
} from '../../project-graph/project-graph';
import { allFileData } from '../../utils/all-file-data';
import { chunkify } from '../../utils/chunkify';
import { sortObjectByKeys } from '../../utils/object-sort';
Expand Down Expand Up @@ -156,9 +159,13 @@ async function getPatternsFromApps(
allWorkspaceFiles: FileData[],
projectGraph: ProjectGraph
): Promise<string[]> {
const graph = await createProjectGraphAsync({ exitOnError: true });
const { projectGraph: graph, sourceMaps } =
await createProjectGraphAndSourceMapsAsync({
exitOnError: true,
});
const affectedGraph = await filterAffected(
graph,
sourceMaps,
calculateFileChanges(affectedFiles, allWorkspaceFiles)
);
return getPatternsFromProjects(
Expand Down
3 changes: 2 additions & 1 deletion packages/nx/src/command-line/graph/graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,8 @@ export async function generateGraph(
{ printWarnings: args.file !== 'stdout' },
readNxJson()
).nxArgs,
rawGraph
rawGraph,
sourceMaps
)
).map((n) => n.name);
}
Expand Down
16 changes: 11 additions & 5 deletions packages/nx/src/command-line/show/projects.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ import {
calculateFileChanges,
} from '../../project-graph/file-utils';
import { filterNodes } from '../../project-graph/operators';
import { createProjectGraphAsync } from '../../project-graph/project-graph';
import {
createProjectGraphAndSourceMapsAsync,
createProjectGraphAsync,
} from '../../project-graph/project-graph';
import { allFileData } from '../../utils/all-file-data';
import {
NxArgs,
Expand All @@ -19,11 +22,13 @@ import {
} from '../../utils/command-line-utils';
import { findMatchingProjects } from '../../utils/find-matching-projects';
import { ShowProjectsOptions } from './command-object';
import { ConfigurationSourceMaps } from '../../project-graph/utils/project-configuration-utils';

export async function showProjectsHandler(
args: ShowProjectsOptions
): Promise<void> {
let graph = await createProjectGraphAsync();
let { projectGraph: graph, sourceMaps } =
await createProjectGraphAndSourceMapsAsync();
const nxJson = readNxJson();
const { nxArgs } = splitArgsIntoNxArgsAndOverrides(
args,
Expand All @@ -37,7 +42,7 @@ export async function showProjectsHandler(
// Affected touches dependencies so it needs to be processed first.
if (args.affected) {
const touchedFiles = await getTouchedFiles(nxArgs);
graph = await getAffectedGraph(touchedFiles, nxJson, graph);
graph = await getAffectedGraph(touchedFiles, nxJson, graph, sourceMaps);
}

const filter = filterNodes((node) => {
Expand Down Expand Up @@ -100,9 +105,10 @@ function getGraphNodesMatchingPatterns(
function getAffectedGraph(
touchedFiles: FileChange[],
nxJson: NxJsonConfiguration<'*' | string[]>,
graph: ProjectGraph
graph: ProjectGraph,
sourceMaps: ConfigurationSourceMaps
) {
return filterAffected(graph, touchedFiles, nxJson);
return filterAffected(graph, sourceMaps, touchedFiles, nxJson);
}

async function getTouchedFiles(nxArgs: NxArgs): Promise<FileChange[]> {
Expand Down
2 changes: 0 additions & 2 deletions packages/nx/src/daemon/server/handle-request-project-graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ import { serializeResult } from '../socket-utils';
import { serverLogger } from './logger';
import { getCachedSerializedProjectGraphPromise } from './project-graph-incremental-recomputation';
import { HandlerResult } from './server';
import { getPlugins } from './plugins';
import { readNxJson } from '../../config/nx-json';

export async function handleRequestProjectGraph(): Promise<HandlerResult> {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ import { serverLogger } from './logger';
import { NxWorkspaceFilesExternals } from '../../native';
import { ConfigurationResult } from '../../project-graph/utils/project-configuration-utils';
import { LoadedNxPlugin } from '../../project-graph/plugins/internal-api';
import { getPlugins } from './plugins';
import {
DaemonProjectGraphError,
ProjectConfigurationsError,
isAggregateProjectGraphError,
} from '../../project-graph/error-types';
import { getPlugins } from '../../project-graph/plugins';

interface SerializedProjectGraph {
error: Error | null;
Expand Down
2 changes: 1 addition & 1 deletion packages/nx/src/daemon/server/shutdown-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import { serverLogger } from './logger';
import { serializeResult } from '../socket-utils';
import { deleteDaemonJsonProcessCache } from '../cache';
import type { Watcher } from '../../native';
import { cleanupPlugins } from './plugins';
import {
DaemonProjectGraphError,
ProjectGraphError,
} from '../../project-graph/error-types';
import { removeDbConnections } from '../../utils/db-connection';
import { cleanupPlugins } from '../../project-graph/plugins/plugins';

export const SERVER_INACTIVITY_TIMEOUT_MS = 10800000 as const; // 10800000 ms = 3 hours

Expand Down
8 changes: 2 additions & 6 deletions packages/nx/src/executors/utils/convert-nx-executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { Executor, ExecutorContext } from '../../config/misc-interfaces';
import { retrieveProjectConfigurations } from '../../project-graph/utils/retrieve-workspace-files';
import { readProjectConfigurationsFromRootMap } from '../../project-graph/utils/project-configuration-utils';
import { ProjectsConfigurations } from '../../config/workspace-json-project-json';
import { loadNxPlugins } from '../../project-graph/plugins/internal-api';
import { getPlugins } from '../../project-graph/plugins';

/**
* Convert an Nx Executor into an Angular Devkit Builder
Expand All @@ -20,10 +20,7 @@ export function convertNxExecutor(executor: Executor) {
const promise = async () => {
const nxJsonConfiguration = readNxJson(builderContext.workspaceRoot);

const [plugins, cleanup] = await loadNxPlugins(
nxJsonConfiguration.plugins,
builderContext.workspaceRoot
);
const plugins = await getPlugins();
const projectsConfigurations: ProjectsConfigurations = {
version: 2,
projects: readProjectConfigurationsFromRootMap(
Expand All @@ -36,7 +33,6 @@ export function convertNxExecutor(executor: Executor) {
).projects
),
};
cleanup();
const context: ExecutorContext = {
root: builderContext.workspaceRoot,
projectName: builderContext.target.project,
Expand Down
25 changes: 13 additions & 12 deletions packages/nx/src/plugins/package-json/create-nodes.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ describe('nx package.json workspaces plugin', () => {
'/root'
);

expect(createNodeFromPackageJson('package.json', '/root'))
expect(createNodeFromPackageJson('package.json', '/root', {}))
.toMatchInlineSnapshot(`
{
"projects": {
Expand Down Expand Up @@ -90,8 +90,9 @@ describe('nx package.json workspaces plugin', () => {
},
}
`);
expect(createNodeFromPackageJson('packages/lib-a/package.json', '/root'))
.toMatchInlineSnapshot(`
expect(
createNodeFromPackageJson('packages/lib-a/package.json', '/root', {})
).toMatchInlineSnapshot(`
{
"projects": {
"packages/lib-a": {
Expand Down Expand Up @@ -132,8 +133,9 @@ describe('nx package.json workspaces plugin', () => {
},
}
`);
expect(createNodeFromPackageJson('packages/lib-b/package.json', '/root'))
.toMatchInlineSnapshot(`
expect(
createNodeFromPackageJson('packages/lib-b/package.json', '/root', {})
).toMatchInlineSnapshot(`
{
"projects": {
"packages/lib-b": {
Expand Down Expand Up @@ -731,13 +733,12 @@ describe('nx package.json workspaces plugin', () => {
);

expect(
createNodeFromPackageJson('apps/myapp/package.json', '/root').projects[
'apps/myapp'
].projectType
createNodeFromPackageJson('apps/myapp/package.json', '/root', {})
.projects['apps/myapp'].projectType
).toEqual('application');

expect(
createNodeFromPackageJson('packages/mylib/package.json', '/root')
createNodeFromPackageJson('packages/mylib/package.json', '/root', {})
.projects['packages/mylib'].projectType
).toEqual('library');
});
Expand All @@ -760,7 +761,7 @@ describe('nx package.json workspaces plugin', () => {
);

expect(
createNodeFromPackageJson('package.json', '/root').projects['.']
createNodeFromPackageJson('package.json', '/root', {}).projects['.']
.projectType
).toEqual('library');
});
Expand All @@ -786,11 +787,11 @@ describe('nx package.json workspaces plugin', () => {
);

expect(
createNodeFromPackageJson('packages/mylib/package.json', '/root')
createNodeFromPackageJson('packages/mylib/package.json', '/root', {})
.projects['packages/mylib'].projectType
).toEqual('library');
expect(
createNodeFromPackageJson('example/package.json', '/root').projects[
createNodeFromPackageJson('example/package.json', '/root', {}).projects[
'example'
].projectType
).toBeUndefined();
Expand Down
Loading

0 comments on commit 41d0324

Please sign in to comment.