@@ -6,7 +6,10 @@ import { readJson } from '../../../generators/utils/json';
6
6
import { NxJsonConfiguration } from '../../../config/nx-json' ;
7
7
import { readNxJson , updateNxJson } from '../../../generators/utils/nx-json' ;
8
8
import { formatChangedFilesWithPrettierIfAvailable } from '../../../generators/internal-utils/format-changed-files-with-prettier-if-available' ;
9
- import { shortenedCloudUrl } from '../../utilities/url-shorten' ;
9
+ import { repoUsesGithub , shortenedCloudUrl } from '../../utilities/url-shorten' ;
10
+ import { commitChanges } from '../../../utils/git-utils' ;
11
+ import * as ora from 'ora' ;
12
+ import * as open from 'open' ;
10
13
11
14
function printCloudConnectionDisabledMessage ( ) {
12
15
output . error ( {
@@ -75,9 +78,10 @@ async function createNxCloudWorkspace(
75
78
76
79
async function printSuccessMessage (
77
80
url : string ,
78
- token : string ,
81
+ token : string | undefined ,
79
82
installationSource : string ,
80
- github : boolean
83
+ usesGithub ?: boolean ,
84
+ directory ?: string
81
85
) {
82
86
if ( process . env . NX_NEW_CLOUD_ONBOARDING !== 'true' ) {
83
87
let origin = 'https://nx.app' ;
@@ -97,20 +101,63 @@ async function printSuccessMessage(
97
101
const connectCloudUrl = await shortenedCloudUrl (
98
102
installationSource ,
99
103
token ,
100
- github
104
+ usesGithub
101
105
) ;
102
106
103
- output . note ( {
104
- title : `Your Nx Cloud workspace is ready.` ,
105
- bodyLines : [
106
- `To claim it, connect it to your Nx Cloud account:` ,
107
- `- Commit and push your changes.` ,
108
- `- Create a pull request for the changes.` ,
109
- `- Go to the following URL to connect your workspace to Nx Cloud:
110
-
111
- ${ connectCloudUrl } ` ,
112
- ] ,
113
- } ) ;
107
+ if ( installationSource === 'nx-connect' && usesGithub ) {
108
+ try {
109
+ const cloudConnectSpinner = ora (
110
+ `Opening Nx Cloud ${ connectCloudUrl } in your browser to connect your workspace.`
111
+ ) . start ( ) ;
112
+ await sleep ( 2000 ) ;
113
+ open ( connectCloudUrl ) ;
114
+ cloudConnectSpinner . succeed ( ) ;
115
+ } catch ( e ) {
116
+ output . note ( {
117
+ title : `Your Nx Cloud workspace is ready.` ,
118
+ bodyLines : [
119
+ `To claim it, connect it to your Nx Cloud account:` ,
120
+ `- Go to the following URL to connect your workspace to Nx Cloud:` ,
121
+ '' ,
122
+ `${ connectCloudUrl } ` ,
123
+ ] ,
124
+ } ) ;
125
+ }
126
+ } else {
127
+ if ( installationSource === 'create-nx-workspace' ) {
128
+ output . note ( {
129
+ title : `Your Nx Cloud workspace is ready.` ,
130
+ bodyLines : [
131
+ `To claim it, connect it to your Nx Cloud account:` ,
132
+ `- Push your repository to your git hosting provider.` ,
133
+ `- Go to the following URL to connect your workspace to Nx Cloud:` ,
134
+ '' ,
135
+ `${ connectCloudUrl } ` ,
136
+ ] ,
137
+ } ) ;
138
+ commitChanges (
139
+ `feat(nx): Added Nx Cloud token to your nx.json
140
+
141
+ To connect your workspace to Nx Cloud, push your repository
142
+ to your git hosting provider and go to the following URL:
143
+
144
+ ${ connectCloudUrl } ` ,
145
+ directory
146
+ ) ;
147
+ } else {
148
+ output . note ( {
149
+ title : `Your Nx Cloud workspace is ready.` ,
150
+ bodyLines : [
151
+ `To claim it, connect it to your Nx Cloud account:` ,
152
+ `- Commit and push your changes.` ,
153
+ `- Create a pull request for the changes.` ,
154
+ `- Go to the following URL to connect your workspace to Nx Cloud:` ,
155
+ '' ,
156
+ `${ connectCloudUrl } ` ,
157
+ ] ,
158
+ } ) ;
159
+ }
160
+ }
114
161
}
115
162
}
116
163
@@ -119,6 +166,7 @@ interface ConnectToNxCloudOptions {
119
166
installationSource ?: string ;
120
167
hideFormatLogs ?: boolean ;
121
168
github ?: boolean ;
169
+ directory ?: string ;
122
170
}
123
171
124
172
function addNxCloudOptionsToNxJson (
@@ -152,27 +200,70 @@ export async function connectToNxCloud(
152
200
printCloudConnectionDisabledMessage ( ) ;
153
201
} ;
154
202
} else {
155
- // TODO: Change to using loading light client when that is enabled by default
156
- const r = await createNxCloudWorkspace (
157
- getRootPackageName ( tree ) ,
158
- schema . installationSource ,
159
- getNxInitDate ( )
160
- ) ;
203
+ if ( process . env . NX_NEW_CLOUD_ONBOARDING !== 'true' ) {
204
+ // TODO: Change to using loading light client when that is enabled by default
205
+ const r = await createNxCloudWorkspace (
206
+ getRootPackageName ( tree ) ,
207
+ schema . installationSource ,
208
+ getNxInitDate ( )
209
+ ) ;
161
210
162
- addNxCloudOptionsToNxJson ( tree , nxJson , r . token ) ;
211
+ addNxCloudOptionsToNxJson ( tree , nxJson , r . token ) ;
163
212
164
- await formatChangedFilesWithPrettierIfAvailable ( tree , {
165
- silent : schema . hideFormatLogs ,
166
- } ) ;
213
+ await formatChangedFilesWithPrettierIfAvailable ( tree , {
214
+ silent : schema . hideFormatLogs ,
215
+ } ) ;
167
216
168
- return async ( ) =>
169
- await printSuccessMessage (
170
- r . url ,
171
- r . token ,
172
- schema . installationSource ,
173
- schema . github
217
+ return async ( ) =>
218
+ await printSuccessMessage ( r . url , r . token , schema . installationSource ) ;
219
+ } else {
220
+ const usesGithub = await repoUsesGithub ( schema . github ) ;
221
+
222
+ let responseFromCreateNxCloudWorkspace :
223
+ | {
224
+ token : string ;
225
+ url : string ;
226
+ }
227
+ | undefined ;
228
+
229
+ // do NOT create Nx Cloud token (createNxCloudWorkspace)
230
+ // if user is using github and is running nx-connect
231
+ if ( ! ( usesGithub && schema . installationSource === 'nx-connect' ) ) {
232
+ responseFromCreateNxCloudWorkspace = await createNxCloudWorkspace (
233
+ getRootPackageName ( tree ) ,
234
+ schema . installationSource ,
235
+ getNxInitDate ( )
236
+ ) ;
237
+
238
+ addNxCloudOptionsToNxJson (
239
+ tree ,
240
+ nxJson ,
241
+ responseFromCreateNxCloudWorkspace ?. token
242
+ ) ;
243
+
244
+ await formatChangedFilesWithPrettierIfAvailable ( tree , {
245
+ silent : schema . hideFormatLogs ,
246
+ } ) ;
247
+ }
248
+ const apiUrl = removeTrailingSlash (
249
+ process . env . NX_CLOUD_API ||
250
+ process . env . NRWL_API ||
251
+ `https://cloud.nx.app`
174
252
) ;
253
+ return async ( ) =>
254
+ await printSuccessMessage (
255
+ responseFromCreateNxCloudWorkspace ?. url ?? apiUrl ,
256
+ responseFromCreateNxCloudWorkspace ?. token ,
257
+ schema . installationSource ,
258
+ usesGithub ,
259
+ schema . directory
260
+ ) ;
261
+ }
175
262
}
176
263
}
177
264
265
+ function sleep ( ms : number ) {
266
+ return new Promise ( ( resolve ) => setTimeout ( resolve , ms ) ) ;
267
+ }
268
+
178
269
export default connectToNxCloud ;
0 commit comments