Skip to content

Commit

Permalink
feat(core): retrieve file data from the daemon
Browse files Browse the repository at this point in the history
  • Loading branch information
vsavkin committed May 24, 2023
1 parent c8d2451 commit f3d269e
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 18 deletions.
5 changes: 2 additions & 3 deletions packages/nx/src/command-line/affected/affected.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ import { TargetDependencyConfig } from '../../config/workspace-json-project-json
import { readNxJson } from '../../config/configuration';
import { workspaceConfigurationCheck } from '../../utils/workspace-configuration-check';
import { findMatchingProjects } from '../../utils/find-matching-projects';
import { fileHasher } from '../../hasher/impl';
import { generateGraph } from '../graph/graph';
import { allFileData } from '../../utils/all-file-data';

export async function affected(
command: 'graph' | 'print-affected' | 'affected',
Expand Down Expand Up @@ -125,14 +125,13 @@ async function projectsToRun(
nxArgs: NxArgs,
projectGraph: ProjectGraph
): Promise<ProjectGraphProjectNode[]> {
fileHasher.ensureInitialized();
let affectedGraph = nxArgs.all
? projectGraph
: await filterAffected(
projectGraph,
calculateFileChanges(
parseFiles(nxArgs).files,
fileHasher.allFileData(),
await allFileData(),
nxArgs
)
);
Expand Down
4 changes: 2 additions & 2 deletions packages/nx/src/command-line/format/format.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import { filterAffected } from '../../project-graph/affected/affected-project-gr
import { readNxJson } from '../../config/configuration';
import { ProjectGraph } from '../../config/project-graph';
import { chunkify } from '../../utils/chunkify';
import { fileHasher } from '../../hasher/impl';
import { allFileData } from '../../utils/all-file-data';

const PRETTIER_PATH = require.resolve('prettier/bin-prettier');

Expand Down Expand Up @@ -93,7 +93,7 @@ async function getPatterns(
);

return args.libsAndApps
? await getPatternsFromApps(patterns, fileHasher.allFileData(), graph)
? await getPatternsFromApps(patterns, await allFileData(), graph)
: patterns;
} catch {
return allFilesPattern;
Expand Down
16 changes: 4 additions & 12 deletions packages/nx/src/command-line/show/show.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,8 @@ import {
ProjectGraphProjectNode,
} from '../../config/project-graph';
import { findMatchingProjects } from '../../utils/find-matching-projects';
import { fileHasher } from '../../hasher/impl';
import {
NxShowArgs,
ShowProjectsOptions,
ShowProjectOptions,
} from './command-object';
import { ShowProjectOptions, ShowProjectsOptions } from './command-object';
import { allFileData } from '../../utils/all-file-data';

export async function showProjectsHandler(
args: ShowProjectsOptions
Expand Down Expand Up @@ -137,18 +133,14 @@ function getGraphNodesMatchingPatterns(
return nodes;
}

function getAffectedGraph(
async function getAffectedGraph(
nxArgs: NxArgs,
nxJson: NxJsonConfiguration<'*' | string[]>,
graph: ProjectGraph
) {
return filterAffected(
graph,
calculateFileChanges(
parseFiles(nxArgs).files,
fileHasher.allFileData(),
nxArgs
),
calculateFileChanges(parseFiles(nxArgs).files, await allFileData(), nxArgs),
nxJson
);
}
6 changes: 5 additions & 1 deletion packages/nx/src/daemon/client/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
isDaemonDisabled,
removeSocketDir,
} from '../tmp-dir';
import { ProjectGraph } from '../../config/project-graph';
import { FileData, ProjectGraph } from '../../config/project-graph';
import { isCI } from '../../utils/is-ci';
import { NxJsonConfiguration } from '../../config/nx-json';
import { readNxJson } from '../../config/configuration';
Expand Down Expand Up @@ -116,6 +116,10 @@ export class DaemonClient {
.projectGraph;
}

async getAllFileData(): Promise<FileData[]> {
return await this.sendToDaemonViaQueue({ type: 'REQUEST_FILE_DATA' });
}

hashTasks(runnerOptions: any, tasks: Task[]): Promise<Hash[]> {
return this.sendToDaemonViaQueue({
type: 'HASH_TASKS',
Expand Down
9 changes: 9 additions & 0 deletions packages/nx/src/daemon/server/handle-request-file-data.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { fileHasher } from '../../hasher/impl';

export async function handleRequestFileData() {
const response = JSON.stringify(fileHasher.allFileData());
return {
response,
description: 'handleRequestFileData',
};
}
3 changes: 3 additions & 0 deletions packages/nx/src/daemon/server/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ import { PackageJson } from '../../utils/package-json';
import { getDaemonProcessIdSync, writeDaemonJsonProcessCache } from '../cache';
import { handleHashTasks } from './handle-hash-tasks';
import { fileHasher, hashArray } from '../../hasher/impl';
import { handleRequestFileData } from './handle-request-file-data';

let performanceObserver: PerformanceObserver | undefined;
let workspaceWatcherError: Error | undefined;
Expand Down Expand Up @@ -141,6 +142,8 @@ async function handleMessage(socket, data: string) {
await handleResult(socket, await handleRequestProjectGraph());
} else if (payload.type === 'HASH_TASKS') {
await handleResult(socket, await handleHashTasks(payload));
} else if (payload.type === 'REQUEST_FILE_DATA') {
await handleResult(socket, await handleRequestFileData());
} else if (payload.type === 'PROCESS_IN_BACKGROUND') {
await handleResult(socket, await handleProcessInBackground(payload));
} else if (payload.type === 'RECORD_OUTPUTS_HASH') {
Expand Down
12 changes: 12 additions & 0 deletions packages/nx/src/utils/all-file-data.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { FileData } from '../config/project-graph';
import { daemonClient } from '../daemon/client/client';
import { fileHasher } from '../hasher/impl';

export function allFileData(): Promise<FileData[]> {
if (daemonClient.enabled()) {
return daemonClient.getAllFileData();
} else {
fileHasher.ensureInitialized();
return Promise.resolve(fileHasher.allFileData());
}
}

1 comment on commit f3d269e

@vercel
Copy link

@vercel vercel bot commented on f3d269e May 24, 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-nrwl.vercel.app
nx-five.vercel.app
nx-dev-git-master-nrwl.vercel.app
nx.dev

Please sign in to comment.