Skip to content

Commit 3fd1841

Browse files
authored
fix(devkit): do not move properties when nx.json doesn't exist (#13825)
1 parent 03be42e commit 3fd1841

File tree

3 files changed

+55
-45
lines changed

3 files changed

+55
-45
lines changed

packages/devkit/src/generators/format-files.ts

Lines changed: 1 addition & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,7 @@ import type { Tree } from 'nx/src/generators/tree';
22
import * as path from 'path';
33
import type * as Prettier from 'prettier';
44
import { readJson, updateJson, writeJson } from 'nx/src/generators/utils/json';
5-
import {
6-
getWorkspacePath,
7-
readWorkspaceConfiguration,
8-
updateWorkspaceConfiguration,
9-
WorkspaceConfiguration,
10-
} from 'nx/src/generators/utils/project-configuration';
5+
import { getWorkspacePath } from 'nx/src/generators/utils/project-configuration';
116
import { sortObjectByKeys } from 'nx/src/utils/object-sort';
127

138
/**
@@ -20,7 +15,6 @@ export async function formatFiles(tree: Tree): Promise<void> {
2015
prettier = await import('prettier');
2116
} catch {}
2217

23-
ensurePropertiesAreInNewLocations(tree);
2418
sortWorkspaceJson(tree);
2519
sortTsConfig(tree);
2620

@@ -88,36 +82,6 @@ function sortWorkspaceJson(tree: Tree) {
8882
}
8983
}
9084

91-
/**
92-
* `updateWorkspaceConfiguration` already handles
93-
* placing properties in their new locations, so
94-
* reading + updating it ensures that props are placed
95-
* correctly.
96-
*/
97-
function ensurePropertiesAreInNewLocations(tree: Tree) {
98-
const workspacePath = getWorkspacePath(tree);
99-
if (!workspacePath) {
100-
return;
101-
}
102-
const wc = readWorkspaceConfiguration(tree);
103-
updateJson<WorkspaceConfiguration>(tree, workspacePath, (json) => {
104-
wc.generators ??= json.generators ?? (json as any).schematics;
105-
if (wc.cli) {
106-
wc.cli.defaultCollection ??= json.cli?.defaultCollection;
107-
wc.cli.packageManager ??= json.cli?.packageManager;
108-
} else if (json.cli) {
109-
wc.cli ??= json.cli;
110-
}
111-
wc.defaultProject ??= json.defaultProject;
112-
delete json.cli;
113-
delete json.defaultProject;
114-
delete (json as any).schematics;
115-
delete json.generators;
116-
return json;
117-
});
118-
updateWorkspaceConfiguration(tree, wc);
119-
}
120-
12185
function sortTsConfig(tree: Tree) {
12286
try {
12387
const tsConfigPath = getRootTsConfigPath(tree);

packages/workspace/src/generators/new/generate-preset.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ export function addPresetDependencies(host: Tree, options: NormalizedSchema) {
3535
export function generatePreset(host: Tree, opts: NormalizedSchema) {
3636
const parsedArgs = yargsParser(process.argv, {
3737
boolean: ['interactive'],
38+
default: {
39+
interactive: true,
40+
},
3841
});
3942
const spawnOptions = {
4043
stdio: [process.stdin, process.stdout, process.stderr],
Lines changed: 51 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
11
import {
2+
formatFiles,
3+
getWorkspacePath,
24
NxJsonConfiguration,
35
ProjectConfiguration,
46
readJson,
5-
writeJson,
67
readProjectConfiguration,
8+
readWorkspaceConfiguration,
79
Tree,
10+
updateJson,
811
updateProjectConfiguration,
9-
formatFiles,
12+
updateWorkspaceConfiguration,
13+
WorkspaceConfiguration,
14+
writeJson,
1015
} from '@nrwl/devkit';
1116

12-
export default async function update(host: Tree) {
13-
const nxJson = readJson(host, 'nx.json') as NxJsonConfiguration & {
17+
export default async function update(tree: Tree) {
18+
const nxJson = readJson(tree, 'nx.json') as NxJsonConfiguration & {
1419
projects: Record<
1520
string,
1621
Pick<ProjectConfiguration, 'tags' | 'implicitDependencies'>
@@ -19,14 +24,52 @@ export default async function update(host: Tree) {
1924
// updateProjectConfiguration automatically saves the project opts into workspace/project.json
2025
if (nxJson.projects) {
2126
Object.entries(nxJson.projects).forEach(([p, nxJsonConfig]) => {
22-
const configuration = readProjectConfiguration(host, p);
27+
const configuration = readProjectConfiguration(tree, p);
2328
configuration.tags ??= nxJsonConfig.tags;
2429
configuration.implicitDependencies ??= nxJsonConfig.implicitDependencies;
25-
updateProjectConfiguration(host, p, configuration);
30+
updateProjectConfiguration(tree, p, configuration);
2631
});
2732
delete nxJson.projects;
2833
}
2934

30-
writeJson(host, 'nx.json', nxJson);
31-
await formatFiles(host); // format files handles moving config options to new spots.
35+
writeJson(tree, 'nx.json', nxJson);
36+
37+
movePropertiesAreInNewLocations(tree); // move config options to new spots.
38+
39+
await formatFiles(tree);
40+
}
41+
42+
/**
43+
* `updateWorkspaceConfiguration` already handles
44+
* placing properties in their new locations, so
45+
* reading + updating it ensures that props are placed
46+
* correctly.
47+
*/
48+
function movePropertiesAreInNewLocations(tree: Tree) {
49+
// If nx.json doesn't exist then there is no where to move these properties to
50+
if (!tree.exists('nx.json')) {
51+
return;
52+
}
53+
54+
const workspacePath = getWorkspacePath(tree);
55+
if (!workspacePath) {
56+
return;
57+
}
58+
const wc = readWorkspaceConfiguration(tree);
59+
updateJson<WorkspaceConfiguration>(tree, workspacePath, (json) => {
60+
wc.generators ??= json.generators ?? (json as any).schematics;
61+
if (wc.cli) {
62+
wc.cli.defaultCollection ??= json.cli?.defaultCollection;
63+
wc.cli.packageManager ??= json.cli?.packageManager;
64+
} else if (json.cli) {
65+
wc.cli ??= json.cli;
66+
}
67+
wc.defaultProject ??= json.defaultProject;
68+
delete json.cli;
69+
delete json.defaultProject;
70+
delete (json as any).schematics;
71+
delete json.generators;
72+
return json;
73+
});
74+
updateWorkspaceConfiguration(tree, wc);
3275
}

0 commit comments

Comments
 (0)