-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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 initial implementation for running sync generators
- Loading branch information
1 parent
995898e
commit 225865f
Showing
6 changed files
with
206 additions
and
9 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
17 changes: 17 additions & 0 deletions
17
packages/nx/src/daemon/message-types/get-sync-generator-changes.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,17 @@ | ||
export const GET_SYNC_GENERATOR_CHANGES = 'GET_SYNC_GENERATOR_CHANGES' as const; | ||
|
||
export type HandleGetSyncGeneratorChangesMessage = { | ||
type: typeof GET_SYNC_GENERATOR_CHANGES; | ||
generators: string[]; | ||
}; | ||
|
||
export function isHandleGetSyncGeneratorChangesMessage( | ||
message: unknown | ||
): message is HandleGetSyncGeneratorChangesMessage { | ||
return ( | ||
typeof message === 'object' && | ||
message !== null && | ||
'type' in message && | ||
message['type'] === GET_SYNC_GENERATOR_CHANGES | ||
); | ||
} |
13 changes: 13 additions & 0 deletions
13
packages/nx/src/daemon/server/handle-get-sync-generator-changes.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,13 @@ | ||
import { getSyncGeneratorChanges } from '../../utils/sync-generators'; | ||
import type { HandlerResult } from './server'; | ||
|
||
export async function handleGetSyncGeneratorChanges( | ||
generators: string[] | ||
): Promise<HandlerResult> { | ||
const changes = await getSyncGeneratorChanges(generators); | ||
|
||
return { | ||
response: JSON.stringify(changes), | ||
description: 'handleGetSyncGeneratorChanges', | ||
}; | ||
} |
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
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,52 @@ | ||
import { parseGeneratorString } from '../command-line/generate/generate'; | ||
import { getGeneratorInformation } from '../command-line/generate/generator-utils'; | ||
import type { ProjectConfiguration } from '../config/workspace-json-project-json'; | ||
import { daemonClient } from '../daemon/client/client'; | ||
import { isOnDaemon } from '../daemon/is-on-daemon'; | ||
import { FsTree, type FileChange } from '../generators/tree'; | ||
import { | ||
createProjectGraphAsync, | ||
readProjectsConfigurationFromProjectGraph, | ||
} from '../project-graph/project-graph'; | ||
import { workspaceRoot } from './workspace-root'; | ||
|
||
export async function getSyncGeneratorChanges( | ||
generators: string[] | ||
): Promise<FileChange[]> { | ||
if (isOnDaemon() || !daemonClient.enabled()) { | ||
return await runSyncGenerators(generators); | ||
} | ||
|
||
// TODO(leo): look into registering the generators with the daemon and let it run them in the background when changes are detected | ||
return await daemonClient.getSyncGeneratorChanges(generators); | ||
} | ||
|
||
async function runSyncGenerators(generators: string[]): Promise<FileChange[]> { | ||
const tree = new FsTree(workspaceRoot, false, 'running sync generators'); | ||
const projectGraph = isOnDaemon() | ||
? (await daemonClient.getProjectGraphAndSourceMaps()).projectGraph | ||
: await createProjectGraphAsync(); | ||
const { projects } = readProjectsConfigurationFromProjectGraph(projectGraph); | ||
|
||
for (const generator of generators) { | ||
await runGenerator(tree, generator, projects); | ||
} | ||
|
||
return tree.listChanges(); | ||
} | ||
|
||
async function runGenerator( | ||
tree: FsTree, | ||
generatorSpecifier: string, | ||
projects: Record<string, ProjectConfiguration> | ||
): Promise<void> { | ||
const { collection, generator } = parseGeneratorString(generatorSpecifier); | ||
const { implementationFactory } = getGeneratorInformation( | ||
collection, | ||
generator, | ||
workspaceRoot, | ||
projects | ||
); | ||
const implementation = implementationFactory(); | ||
await implementation(tree, {}); | ||
} |