1
1
import {
2
+ formatFiles ,
3
+ getWorkspacePath ,
2
4
NxJsonConfiguration ,
3
5
ProjectConfiguration ,
4
6
readJson ,
5
- writeJson ,
6
7
readProjectConfiguration ,
8
+ readWorkspaceConfiguration ,
7
9
Tree ,
10
+ updateJson ,
8
11
updateProjectConfiguration ,
9
- formatFiles ,
12
+ updateWorkspaceConfiguration ,
13
+ WorkspaceConfiguration ,
14
+ writeJson ,
10
15
} from '@nrwl/devkit' ;
11
16
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 & {
14
19
projects : Record <
15
20
string ,
16
21
Pick < ProjectConfiguration , 'tags' | 'implicitDependencies' >
@@ -19,14 +24,52 @@ export default async function update(host: Tree) {
19
24
// updateProjectConfiguration automatically saves the project opts into workspace/project.json
20
25
if ( nxJson . projects ) {
21
26
Object . entries ( nxJson . projects ) . forEach ( ( [ p , nxJsonConfig ] ) => {
22
- const configuration = readProjectConfiguration ( host , p ) ;
27
+ const configuration = readProjectConfiguration ( tree , p ) ;
23
28
configuration . tags ??= nxJsonConfig . tags ;
24
29
configuration . implicitDependencies ??= nxJsonConfig . implicitDependencies ;
25
- updateProjectConfiguration ( host , p , configuration ) ;
30
+ updateProjectConfiguration ( tree , p , configuration ) ;
26
31
} ) ;
27
32
delete nxJson . projects ;
28
33
}
29
34
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 ) ;
32
75
}
0 commit comments