@@ -21,12 +21,19 @@ export function addDevServerTargetToConfig(
21
21
22
22
let ast = tsquery . ast ( configFileContents ) ;
23
23
24
- const NX_E2E_PRESET_OPTIONS_SELECTOR =
25
- 'PropertyAssignment:has(Identifier[name=e2e]) CallExpression:has(Identifier[name=nxE2EPreset]) > ObjectLiteralExpression ' ;
26
- const nxE2ePresetOptionsNodes = tsquery ( ast , NX_E2E_PRESET_OPTIONS_SELECTOR , {
24
+ const NX_E2E_PRESET_SELECTOR =
25
+ 'PropertyAssignment:has(Identifier[name=e2e]) CallExpression:has(Identifier[name=nxE2EPreset])' ;
26
+ const nxE2ePresetOptionsNodes = tsquery ( ast , NX_E2E_PRESET_SELECTOR , {
27
27
visitAllChildren : true ,
28
28
} ) ;
29
29
if ( nxE2ePresetOptionsNodes . length !== 0 ) {
30
+ const NX_E2E_PRESET_OPTIONS_SELECTOR =
31
+ 'PropertyAssignment:has(Identifier[name=e2e]) CallExpression:has(Identifier[name=nxE2EPreset]) > ObjectLiteralExpression' ;
32
+ const optionsObjectNodes = tsquery ( ast , NX_E2E_PRESET_OPTIONS_SELECTOR , {
33
+ visitAllChildren : true ,
34
+ } ) ;
35
+ const hasObjectDefinition = optionsObjectNodes ?. length > 0 ;
36
+
30
37
let nxE2ePresetOptionsNode = nxE2ePresetOptionsNodes [ 0 ] ;
31
38
const WEB_SERVER_COMMANDS_SELECTOR =
32
39
'PropertyAssignment:has(Identifier[name=webServerCommands])' ;
@@ -47,24 +54,43 @@ export function addDevServerTargetToConfig(
47
54
) } ${ configFileContents . slice ( webServerCommandsNodes [ 0 ] . getEnd ( ) ) } `
48
55
) ;
49
56
} else {
50
- tree . write (
51
- configFilePath ,
52
- `${ configFileContents . slice (
53
- 0 ,
54
- nxE2ePresetOptionsNode . getStart ( ) + 1
55
- ) } webServerCommands: ${ JSON . stringify (
56
- webServerCommands
57
- ) } ,${ configFileContents . slice ( nxE2ePresetOptionsNode . getStart ( ) + 1 ) } `
58
- ) ;
57
+ if ( hasObjectDefinition ) {
58
+ tree . write (
59
+ configFilePath ,
60
+ `${ configFileContents . slice (
61
+ 0 ,
62
+ optionsObjectNodes [ 0 ] . getStart ( ) + 1
63
+ ) } webServerCommands: ${ JSON . stringify (
64
+ webServerCommands
65
+ ) } ,${ configFileContents . slice ( optionsObjectNodes [ 0 ] . getStart ( ) + 1 ) } `
66
+ ) ;
67
+ } else {
68
+ tree . write (
69
+ configFilePath ,
70
+ `${ configFileContents . slice (
71
+ 0 ,
72
+ nxE2ePresetOptionsNode . getEnd ( ) - 1
73
+ ) } ,{ webServerCommands: ${ JSON . stringify (
74
+ webServerCommands
75
+ ) } ,}${ configFileContents . slice ( nxE2ePresetOptionsNode . getEnd ( ) - 1 ) } `
76
+ ) ;
77
+ }
59
78
}
60
79
61
80
if ( ciDevServerTarget ) {
62
81
configFileContents = tree . read ( configFilePath , 'utf-8' ) ;
63
82
ast = tsquery . ast ( configFileContents ) ;
64
- nxE2ePresetOptionsNode = tsquery ( ast , NX_E2E_PRESET_OPTIONS_SELECTOR , {
83
+ nxE2ePresetOptionsNode = tsquery ( ast , NX_E2E_PRESET_SELECTOR , {
65
84
visitAllChildren : true ,
66
85
} ) [ 0 ] ;
67
86
87
+ const NX_E2E_PRESET_OPTIONS_SELECTOR =
88
+ 'PropertyAssignment:has(Identifier[name=e2e]) CallExpression:has(Identifier[name=nxE2EPreset]) > ObjectLiteralExpression' ;
89
+ const optionsObjectNodes = tsquery ( ast , NX_E2E_PRESET_OPTIONS_SELECTOR , {
90
+ visitAllChildren : true ,
91
+ } ) ;
92
+ const hasObjectDefinition = optionsObjectNodes ?. length > 0 ;
93
+
68
94
const CI_WEB_SERVER_COMMANDS_SELECTOR =
69
95
'PropertyAssignment:has(Identifier[name=ciWebServerCommand])' ;
70
96
const ciWebServerCommandsNodes = tsquery (
@@ -91,15 +117,27 @@ export function addDevServerTargetToConfig(
91
117
) ;
92
118
}
93
119
} else {
94
- tree . write (
95
- configFilePath ,
96
- `${ configFileContents . slice (
97
- 0 ,
98
- nxE2ePresetOptionsNode . getStart ( ) + 1
99
- ) } ciWebServerCommand: "${ ciDevServerTarget } ",${ configFileContents . slice (
100
- nxE2ePresetOptionsNode . getStart ( ) + 1
101
- ) } `
102
- ) ;
120
+ if ( hasObjectDefinition ) {
121
+ tree . write (
122
+ configFilePath ,
123
+ `${ configFileContents . slice (
124
+ 0 ,
125
+ optionsObjectNodes [ 0 ] . getStart ( ) + 1
126
+ ) } ciWebServerCommand: "${ ciDevServerTarget } ",${ configFileContents . slice (
127
+ optionsObjectNodes [ 0 ] . getStart ( ) + 1
128
+ ) } `
129
+ ) ;
130
+ } else {
131
+ tree . write (
132
+ configFilePath ,
133
+ `${ configFileContents . slice (
134
+ 0 ,
135
+ nxE2ePresetOptionsNode . getEnd ( ) - 1
136
+ ) } ,{ ciWebServerCommand: "${ ciDevServerTarget } ",}${ configFileContents . slice (
137
+ nxE2ePresetOptionsNode . getEnd ( ) - 1
138
+ ) } `
139
+ ) ;
140
+ }
103
141
}
104
142
}
105
143
}
0 commit comments