@@ -12,6 +12,7 @@ import { link } from '../link/link.js'
12
12
import { sitesCreate } from '../sites/sites-create.js'
13
13
import type { CLIState , SiteInfo } from '../../utils/types.js'
14
14
import { getBuildSettings , saveNetlifyToml } from '../../utils/init/utils.js'
15
+ import { type InitExitCode , LINKED_EXISTING_SITE_EXIT_CODE , LINKED_NEW_SITE_EXIT_CODE } from './constants.js'
15
16
16
17
const persistState = ( { siteInfo, state } : { siteInfo : SiteInfo ; state : CLIState } ) : void => {
17
18
// Save to .netlify/state.json file
@@ -43,10 +44,12 @@ const createNewSiteAndExit = async ({
43
44
command,
44
45
state,
45
46
disableLinking,
47
+ customizeExitMessage,
46
48
} : {
47
49
command : BaseCommand
48
50
state : CLIState
49
51
disableLinking : boolean
52
+ customizeExitMessage : InitExitMessageCustomizer | undefined
50
53
} ) : Promise < never > => {
51
54
const siteInfo = await sitesCreate ( { } , command )
52
55
@@ -78,8 +81,8 @@ const createNewSiteAndExit = async ({
78
81
}
79
82
80
83
log ( )
81
- log ( `To deploy to this project, run ${ chalk . cyanBright . bold ( `${ netlifyCommand ( ) } deploy` ) } ` )
82
-
84
+ const defaultExitMesage = `To deploy to this project, run ${ chalk . cyanBright . bold ( `${ netlifyCommand ( ) } deploy` ) } .`
85
+ log ( customizeExitMessage ?. ( LINKED_NEW_SITE_EXIT_CODE , defaultExitMesage ) ?? defaultExitMesage )
83
86
return exit ( )
84
87
}
85
88
@@ -124,11 +127,13 @@ const handleNoGitRemoteAndExit = async ({
124
127
error,
125
128
state,
126
129
disableLinking,
130
+ customizeExitMessage,
127
131
} : {
128
132
command : BaseCommand
129
133
error ?: unknown
130
134
state : CLIState
131
135
disableLinking : boolean
136
+ customizeExitMessage : InitExitMessageCustomizer | undefined
132
137
} ) : Promise < never > => {
133
138
log ( )
134
139
log ( chalk . yellow ( 'No git remote was found, would you like to set one up?' ) )
@@ -159,7 +164,8 @@ git remote add origin https://github.com/YourUserName/RepoName.git
159
164
] )
160
165
161
166
if ( noGitRemoteChoice === NEW_SITE_NO_GIT ) {
162
- return createNewSiteAndExit ( { state, command, disableLinking } )
167
+ // TODO(ndhoule): Shove a custom error message in here
168
+ return createNewSiteAndExit ( { state, command, disableLinking, customizeExitMessage } )
163
169
}
164
170
return logGitSetupInstructionsAndExit ( )
165
171
}
@@ -194,15 +200,36 @@ const createOrLinkSiteToRepo = async (command: BaseCommand) => {
194
200
return await link ( { } , command )
195
201
}
196
202
197
- const logExistingRepoSetupAndExit = ( { repoUrl, siteName } : { repoUrl : string ; siteName : string } ) : void => {
203
+ const logExistingRepoSetupAndExit = ( {
204
+ repoUrl,
205
+ siteName,
206
+ customizeExitMessage,
207
+ } : {
208
+ repoUrl : string
209
+ siteName : string
210
+ customizeExitMessage : InitExitMessageCustomizer | undefined
211
+ } ) : void => {
198
212
log ( )
199
213
log ( chalk . underline . bold ( `Success` ) )
200
- log ( `This project "${ siteName } " is configured to automatically deploy via ${ repoUrl } ` )
214
+
215
+ const defaultExitMessage = `This project "${ siteName } " is configured to automatically deploy via ${ repoUrl } .`
216
+ log ( customizeExitMessage ?.( LINKED_EXISTING_SITE_EXIT_CODE , defaultExitMessage ) ?? defaultExitMessage )
201
217
// TODO add support for changing GitHub repo in site:config command
202
218
exit ( )
203
219
}
204
220
205
- export const init = async ( options : OptionValues , command : BaseCommand ) : Promise < SiteInfo > => {
221
+ type InitExitMessageCustomizer = ( code : InitExitCode , defaultMessage : string ) => string | undefined
222
+
223
+ type InitExtraOptions = {
224
+ customizeExitMessage ?: InitExitMessageCustomizer | undefined
225
+ exitAfterConfiguringRepo ?: boolean | undefined
226
+ }
227
+
228
+ export const init = async (
229
+ options : OptionValues ,
230
+ command : BaseCommand ,
231
+ { customizeExitMessage, exitAfterConfiguringRepo = false } : InitExtraOptions = { } ,
232
+ ) : Promise < SiteInfo > => {
206
233
command . setAnalyticsPayload ( { manual : options . manual , force : options . force } )
207
234
208
235
const { repositoryRoot, state } = command . netlify
@@ -222,11 +249,13 @@ export const init = async (options: OptionValues, command: BaseCommand): Promise
222
249
// Look for local repo
223
250
const repoData = await getRepoData ( { workingDir : command . workingDir , remoteName : options . gitRemoteName } )
224
251
if ( 'error' in repoData ) {
252
+ // TODO(ndhoule): Custom error messaage here
225
253
return await handleNoGitRemoteAndExit ( {
226
254
command,
227
255
error : repoData . error ,
228
256
state,
229
257
disableLinking : options . disableLinking ,
258
+ customizeExitMessage,
230
259
} )
231
260
}
232
261
@@ -237,12 +266,19 @@ export const init = async (options: OptionValues, command: BaseCommand): Promise
237
266
// Check for existing CI setup
238
267
const remoteBuildRepo = getRepoUrl ( siteInfo )
239
268
if ( remoteBuildRepo && ! options . force ) {
240
- logExistingRepoSetupAndExit ( { siteName : siteInfo . name , repoUrl : remoteBuildRepo } )
269
+ logExistingRepoSetupAndExit ( { siteName : siteInfo . name , repoUrl : remoteBuildRepo , customizeExitMessage } )
241
270
}
242
271
243
272
persistState ( { state, siteInfo } )
244
273
245
274
await configureRepo ( { command, siteId : siteInfo . id , repoData, manual : options . manual } )
275
+ if ( exitAfterConfiguringRepo ) {
276
+ const customErrorMessage = customizeExitMessage ?.( LINKED_EXISTING_SITE_EXIT_CODE , '' )
277
+ if ( customErrorMessage ) {
278
+ log ( customErrorMessage )
279
+ }
280
+ return exit ( )
281
+ }
246
282
247
283
return siteInfo
248
284
}
0 commit comments