/
config-locations.ts
75 lines (69 loc) · 2.19 KB
/
config-locations.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import {
formatFiles,
getWorkspacePath,
NxJsonConfiguration,
ProjectConfiguration,
readJson,
readProjectConfiguration,
readWorkspaceConfiguration,
Tree,
updateJson,
updateProjectConfiguration,
updateWorkspaceConfiguration,
WorkspaceConfiguration,
writeJson,
} from '@nrwl/devkit';
export default async function update(tree: Tree) {
const nxJson = readJson(tree, 'nx.json') as NxJsonConfiguration & {
projects: Record<
string,
Pick<ProjectConfiguration, 'tags' | 'implicitDependencies'>
> | null;
};
// updateProjectConfiguration automatically saves the project opts into workspace/project.json
if (nxJson.projects) {
Object.entries(nxJson.projects).forEach(([p, nxJsonConfig]) => {
const configuration = readProjectConfiguration(tree, p);
configuration.tags ??= nxJsonConfig.tags;
configuration.implicitDependencies ??= nxJsonConfig.implicitDependencies;
updateProjectConfiguration(tree, p, configuration);
});
delete nxJson.projects;
}
writeJson(tree, 'nx.json', nxJson);
movePropertiesAreInNewLocations(tree); // move config options to new spots.
await formatFiles(tree);
}
/**
* `updateWorkspaceConfiguration` already handles
* placing properties in their new locations, so
* reading + updating it ensures that props are placed
* correctly.
*/
function movePropertiesAreInNewLocations(tree: Tree) {
// If nx.json doesn't exist then there is no where to move these properties to
if (!tree.exists('nx.json')) {
return;
}
const workspacePath = getWorkspacePath(tree);
if (!workspacePath) {
return;
}
const wc = readWorkspaceConfiguration(tree);
updateJson<WorkspaceConfiguration>(tree, workspacePath, (json) => {
wc.generators ??= json.generators ?? (json as any).schematics;
if (wc.cli) {
wc.cli.defaultCollection ??= json.cli?.defaultCollection;
wc.cli.packageManager ??= json.cli?.packageManager;
} else if (json.cli) {
wc.cli ??= json.cli;
}
wc.defaultProject ??= json.defaultProject;
delete json.cli;
delete json.defaultProject;
delete (json as any).schematics;
delete json.generators;
return json;
});
updateWorkspaceConfiguration(tree, wc);
}