Skip to content

Commit 15da9f7

Browse files
committed
feat(core): support providing schema in sync generators
1 parent c802c6f commit 15da9f7

File tree

2 files changed

+84
-33
lines changed

2 files changed

+84
-33
lines changed

packages/nx/src/daemon/server/sync-generators.ts

Lines changed: 46 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
1+
import type { NxJsonConfiguration } from '../../config/nx-json';
12
import { readNxJson } from '../../config/nx-json';
23
import type { ProjectGraph } from '../../config/project-graph';
3-
import type { ProjectConfiguration } from '../../config/workspace-json-project-json';
4+
import type { ProjectsConfigurations } from '../../config/workspace-json-project-json';
45
import { FsTree, type Tree } from '../../generators/tree';
56
import { hashArray } from '../../hasher/file-hasher';
67
import { readProjectsConfigurationFromProjectGraph } from '../../project-graph/project-graph';
78
import {
89
collectEnabledTaskSyncGeneratorsFromProjectGraph,
910
collectRegisteredGlobalSyncGenerators,
1011
flushSyncGeneratorChanges,
11-
runSyncGenerator,
1212
type FlushSyncGeneratorChangesResult,
13+
runSyncGenerator,
1314
type SyncGeneratorRunResult,
1415
type SyncGeneratorRunSuccessResult,
1516
} from '../../utils/sync-generators';
@@ -136,13 +137,14 @@ export function collectAndScheduleSyncGenerators(
136137
return;
137138
}
138139

139-
const { projects } =
140+
const projectsConfigurations =
140141
readProjectsConfigurationFromProjectGraph(projectGraph);
142+
const nxJsonConfiguration = readNxJson();
141143

142144
for (const generator of scheduledGenerators) {
143145
syncGeneratorsCacheResultPromises.set(
144146
generator,
145-
runGenerator(generator, projects)
147+
runGenerator(generator, projectsConfigurations, nxJsonConfiguration)
146148
);
147149
}
148150

@@ -172,21 +174,26 @@ export async function getCachedRegisteredSyncGenerators(): Promise<{
172174
async function getFromCacheOrRunGenerators(
173175
generators: string[]
174176
): Promise<SyncGeneratorRunResult[]> {
175-
let projects: Record<string, ProjectConfiguration> | null;
177+
let projectsConfigurations: ProjectsConfigurations | null;
178+
let nxJsonConfiguration: NxJsonConfiguration | null;
176179
let errored = false;
177-
const getProjectsConfigurations = async () => {
178-
if (projects || errored) {
179-
return projects;
180+
181+
const getConfigurations = async () => {
182+
if (projectsConfigurations || errored) {
183+
return { projectsConfigurations, nxJsonConfiguration };
180184
}
181185

182186
const { projectGraph, error } =
183187
await getCachedSerializedProjectGraphPromise();
184-
projects = projectGraph
185-
? readProjectsConfigurationFromProjectGraph(projectGraph).projects
188+
projectsConfigurations = projectGraph
189+
? readProjectsConfigurationFromProjectGraph(projectGraph)
186190
: null;
191+
192+
nxJsonConfiguration = readNxJson();
193+
187194
errored = error !== undefined;
188195

189-
return projects;
196+
return { projectsConfigurations, nxJsonConfiguration };
190197
};
191198

192199
return (
@@ -198,12 +205,17 @@ async function getFromCacheOrRunGenerators(
198205
) {
199206
// it's scheduled to run (there are pending changes to process) or
200207
// it's not scheduled and there's no cached result, so run it
201-
const projects = await getProjectsConfigurations();
202-
if (projects) {
208+
const { projectsConfigurations, nxJsonConfiguration } =
209+
await getConfigurations();
210+
if (projectsConfigurations) {
203211
log(generator, 'already scheduled or not cached, running it now');
204212
syncGeneratorsCacheResultPromises.set(
205213
generator,
206-
runGenerator(generator, projects)
214+
runGenerator(
215+
generator,
216+
projectsConfigurations,
217+
nxJsonConfiguration
218+
)
207219
);
208220
} else {
209221
log(
@@ -242,11 +254,12 @@ async function runConflictingGenerators(
242254
generators: string[]
243255
): Promise<SyncGeneratorRunResult[]> {
244256
const { projectGraph } = await getCachedSerializedProjectGraphPromise();
245-
const projects = projectGraph
246-
? readProjectsConfigurationFromProjectGraph(projectGraph).projects
257+
const projectsConfigurations = projectGraph
258+
? readProjectsConfigurationFromProjectGraph(projectGraph)
247259
: null;
260+
const nxJsonConfiguration = readNxJson();
248261

249-
if (!projects) {
262+
if (!projectsConfigurations) {
250263
/**
251264
* This should never happen. This is invoked imperatively, and by
252265
* the time it is invoked, the project graph would have already
@@ -266,7 +279,14 @@ async function runConflictingGenerators(
266279
const results: SyncGeneratorRunResult[] = [];
267280
for (const generator of generators) {
268281
log(generator, 'running it now');
269-
results.push(await runGenerator(generator, projects, tree));
282+
results.push(
283+
await runGenerator(
284+
generator,
285+
projectsConfigurations,
286+
nxJsonConfiguration,
287+
tree
288+
)
289+
);
270290
}
271291

272292
return results;
@@ -449,7 +469,8 @@ function collectAllRegisteredSyncGenerators(projectGraph: ProjectGraph): void {
449469

450470
function runGenerator(
451471
generator: string,
452-
projects: Record<string, ProjectConfiguration>,
472+
projectsConfigurations: ProjectsConfigurations,
473+
nxJsonConfiguration: NxJsonConfiguration,
453474
tree?: Tree
454475
): Promise<SyncGeneratorRunResult> {
455476
log('running scheduled generator', generator);
@@ -461,7 +482,12 @@ function runGenerator(
461482
`running sync generator ${generator}`
462483
);
463484

464-
return runSyncGenerator(tree, generator, projects).then((result) => {
485+
return runSyncGenerator(
486+
tree,
487+
generator,
488+
projectsConfigurations,
489+
nxJsonConfiguration
490+
).then((result) => {
465491
if ('error' in result) {
466492
log(generator, 'error:', result.error.message);
467493
} else {

packages/nx/src/utils/sync-generators.ts

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,16 @@ import { performance } from 'perf_hooks';
22
import { parseGeneratorString } from '../command-line/generate/generate';
33
import { getGeneratorInformation } from '../command-line/generate/generator-utils';
44
import type { GeneratorCallback } from '../config/misc-interfaces';
5-
import { readNxJson, type NxJsonConfiguration } from '../config/nx-json';
5+
import { type NxJsonConfiguration, readNxJson } from '../config/nx-json';
66
import type { ProjectGraph } from '../config/project-graph';
77
import type { TaskGraph } from '../config/task-graph';
8-
import type { ProjectConfiguration } from '../config/workspace-json-project-json';
8+
import type { ProjectsConfigurations } from '../config/workspace-json-project-json';
99
import { daemonClient } from '../daemon/client/client';
1010
import { isOnDaemon } from '../daemon/is-on-daemon';
1111
import {
12+
type FileChange,
1213
flushChanges,
1314
FsTree,
14-
type FileChange,
1515
type Tree,
1616
} from '../generators/tree';
1717
import {
@@ -20,6 +20,7 @@ import {
2020
} from '../project-graph/project-graph';
2121
import { updateContextWithChangedFiles } from './workspace-context';
2222
import { workspaceRoot } from './workspace-root';
23+
import { combineOptionsForGenerator } from './params';
2324
import chalk = require('chalk');
2425

2526
export type SyncGeneratorResult = void | {
@@ -28,8 +29,9 @@ export type SyncGeneratorResult = void | {
2829
outOfSyncDetails?: string[];
2930
};
3031

31-
export type SyncGenerator = (
32-
tree: Tree
32+
export type SyncGenerator<T = unknown> = (
33+
tree: Tree,
34+
schema: T
3335
) => SyncGeneratorResult | Promise<SyncGeneratorResult>;
3436

3537
export type SyncGeneratorRunSuccessResult = {
@@ -136,19 +138,35 @@ export async function collectAllRegisteredSyncGenerators(
136138
export async function runSyncGenerator(
137139
tree: Tree,
138140
generatorSpecifier: string,
139-
projects: Record<string, ProjectConfiguration>
141+
projectsConfigurations: ProjectsConfigurations,
142+
nxJsonConfiguration: NxJsonConfiguration
140143
): Promise<SyncGeneratorRunResult> {
141144
try {
142145
performance.mark(`run-sync-generator:${generatorSpecifier}:start`);
143146
const { collection, generator } = parseGeneratorString(generatorSpecifier);
144-
const { implementationFactory } = getGeneratorInformation(
147+
const { implementationFactory, schema, normalizedGeneratorName } =
148+
getGeneratorInformation(
149+
collection,
150+
generator,
151+
workspaceRoot,
152+
projectsConfigurations.projects
153+
);
154+
155+
// Combine options from nx.json and project configuration (no command line options for sync generators)
156+
const combinedOpts = await combineOptionsForGenerator(
157+
{}, // No command line options for sync generators
145158
collection,
146-
generator,
147-
workspaceRoot,
148-
projects
159+
normalizedGeneratorName,
160+
projectsConfigurations,
161+
nxJsonConfiguration,
162+
schema,
163+
false,
164+
null,
165+
null
149166
);
167+
150168
const implementation = implementationFactory() as SyncGenerator;
151-
const result = await implementation(tree);
169+
const result = await implementation(tree, combinedOpts);
152170

153171
let callback: GeneratorCallback | undefined;
154172
let outOfSyncMessage: string | undefined;
@@ -412,11 +430,18 @@ async function runSyncGenerators(
412430
): Promise<SyncGeneratorRunResult[]> {
413431
const tree = new FsTree(workspaceRoot, false, 'running sync generators');
414432
const projectGraph = await createProjectGraphAsync();
415-
const { projects } = readProjectsConfigurationFromProjectGraph(projectGraph);
433+
const projectsConfigurations =
434+
readProjectsConfigurationFromProjectGraph(projectGraph);
435+
const nxJsonConfiguration = readNxJson();
416436

417437
const results: SyncGeneratorRunResult[] = [];
418438
for (const generator of generators) {
419-
const result = await runSyncGenerator(tree, generator, projects);
439+
const result = await runSyncGenerator(
440+
tree,
441+
generator,
442+
projectsConfigurations,
443+
nxJsonConfiguration
444+
);
420445
results.push(result);
421446
}
422447

0 commit comments

Comments
 (0)