-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): add daemon watcher filters (#13229)
- Loading branch information
Showing
5 changed files
with
195 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
packages/nx/src/daemon/server/file-watching/changed-projects.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { performance } from 'perf_hooks'; | ||
import { projectFileMapWithFiles } from '../project-graph-incremental-recomputation'; | ||
|
||
export type ChangedFile = { | ||
path: string; | ||
type: 'CREATED' | 'UPDATED' | 'DELETED'; | ||
}; | ||
|
||
export function getProjectsAndGlobalChanges( | ||
createdFiles: string[] | null, | ||
updatedFiles: string[] | null, | ||
deletedFiles: string[] | null | ||
) { | ||
const projectAndGlobalChanges: { | ||
projects: { [changedProject: string]: ChangedFile[] }; | ||
globalFiles: ChangedFile[]; | ||
} = { | ||
projects: {}, | ||
globalFiles: [], | ||
}; | ||
|
||
performance.mark('changed-projects:start'); | ||
|
||
const allChangedFiles: ChangedFile[] = [ | ||
...(createdFiles ?? []).map<ChangedFile>((c) => ({ | ||
path: c, | ||
type: 'CREATED', | ||
})), | ||
...(updatedFiles ?? []).map<ChangedFile>((c) => ({ | ||
path: c, | ||
type: 'UPDATED', | ||
})), | ||
...(deletedFiles ?? []).map<ChangedFile>((c) => ({ | ||
path: c, | ||
type: 'DELETED', | ||
})), | ||
]; | ||
|
||
const fileToProjectMap: Record<string, string> = {}; | ||
for (const [projectName, projectFiles] of Object.entries( | ||
projectFileMapWithFiles?.projectFileMap ?? {} | ||
)) { | ||
for (const projectFile of projectFiles) { | ||
fileToProjectMap[projectFile.file] = projectName; | ||
} | ||
} | ||
|
||
for (const changedFile of allChangedFiles) { | ||
const project = fileToProjectMap[changedFile.path]; | ||
if (project) { | ||
(projectAndGlobalChanges.projects[project] ??= []).push(changedFile); | ||
} else { | ||
projectAndGlobalChanges.globalFiles.push(changedFile); | ||
} | ||
} | ||
|
||
performance.mark('changed-projects:end'); | ||
performance.measure( | ||
'changed-projects', | ||
'changed-projects:start', | ||
'changed-projects:end' | ||
); | ||
|
||
return projectAndGlobalChanges; | ||
} |
82 changes: 82 additions & 0 deletions
82
packages/nx/src/daemon/server/file-watching/file-watcher-sockets.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import { Socket } from 'net'; | ||
import { ProjectGraphCache } from '../../../project-graph/nx-deps-cache'; | ||
import { PromisedBasedQueue } from '../../../utils/promised-based-queue'; | ||
import { handleResult } from '../server'; | ||
import { getProjectsAndGlobalChanges } from './changed-projects'; | ||
|
||
const queue = new PromisedBasedQueue(); | ||
|
||
export let registeredFileWatcherSockets: { | ||
socket: Socket; | ||
config: { | ||
watchProjects: string[] | 'all'; | ||
includeGlobalWorkspaceFiles: boolean; | ||
}; | ||
}[] = []; | ||
|
||
export function removeRegisteredFileWatcherSocket(socket: Socket) { | ||
registeredFileWatcherSockets = registeredFileWatcherSockets.filter( | ||
(watcher) => watcher.socket !== socket | ||
); | ||
} | ||
|
||
export function hasRegisteredFileWatcherSockets() { | ||
return registeredFileWatcherSockets.length > 0; | ||
} | ||
|
||
export function notifyFileWatcherSockets( | ||
createdFiles: string[] | null, | ||
updatedFiles: string[], | ||
deletedFiles: string[] | ||
) { | ||
if (!hasRegisteredFileWatcherSockets()) { | ||
return; | ||
} | ||
|
||
queue.sendToQueue(async () => { | ||
const projectAndGlobalChanges = getProjectsAndGlobalChanges( | ||
createdFiles, | ||
updatedFiles, | ||
deletedFiles | ||
); | ||
|
||
await Promise.all( | ||
registeredFileWatcherSockets.map(({ socket, config }) => { | ||
const changedProjects = []; | ||
const changedFiles = []; | ||
if (config.watchProjects === 'all') { | ||
for (const [projectName, projectFiles] of Object.entries( | ||
projectAndGlobalChanges.projects | ||
)) { | ||
changedProjects.push(projectName); | ||
changedFiles.push(...projectFiles); | ||
} | ||
} else { | ||
for (const watchedProject of config.watchProjects) { | ||
if (!!projectAndGlobalChanges.projects[watchedProject]) { | ||
changedProjects.push(watchedProject); | ||
|
||
changedFiles.push( | ||
...projectAndGlobalChanges.projects[watchedProject] | ||
); | ||
} | ||
} | ||
} | ||
|
||
if (config.includeGlobalWorkspaceFiles) { | ||
changedFiles.push(...projectAndGlobalChanges.globalFiles); | ||
} | ||
|
||
if (changedProjects.length > 0 || changedFiles.length > 0) { | ||
return handleResult(socket, { | ||
description: 'File watch changed', | ||
response: JSON.stringify({ | ||
changedProjects, | ||
changedFiles, | ||
}), | ||
}); | ||
} | ||
}) | ||
); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3f2fa6c
There was a problem hiding this comment.
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-dev-git-master-nrwl.vercel.app
nx-five.vercel.app
nx.dev