@@ -12,6 +12,7 @@ import { link } from '../link/link.js'
1212import { sitesCreate } from '../sites/sites-create.js'
1313import type { CLIState , SiteInfo } from '../../utils/types.js'
1414import { getBuildSettings , saveNetlifyToml } from '../../utils/init/utils.js'
15+ import { type InitExitCode , LINKED_EXISTING_SITE_EXIT_CODE , LINKED_NEW_SITE_EXIT_CODE } from './constants.js'
1516
1617const persistState = ( { siteInfo, state } : { siteInfo : SiteInfo ; state : CLIState } ) : void => {
1718 // Save to .netlify/state.json file
@@ -43,10 +44,12 @@ const createNewSiteAndExit = async ({
4344 command,
4445 state,
4546 disableLinking,
47+ customizeExitMessage,
4648} : {
4749 command : BaseCommand
4850 state : CLIState
4951 disableLinking : boolean
52+ customizeExitMessage : InitExitMessageCustomizer | undefined
5053} ) : Promise < never > => {
5154 const siteInfo = await sitesCreate ( { } , command )
5255
@@ -78,8 +81,8 @@ const createNewSiteAndExit = async ({
7881 }
7982
8083 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 )
8386 return exit ( )
8487}
8588
@@ -124,11 +127,13 @@ const handleNoGitRemoteAndExit = async ({
124127 error,
125128 state,
126129 disableLinking,
130+ customizeExitMessage,
127131} : {
128132 command : BaseCommand
129133 error ?: unknown
130134 state : CLIState
131135 disableLinking : boolean
136+ customizeExitMessage : InitExitMessageCustomizer | undefined
132137} ) : Promise < never > => {
133138 log ( )
134139 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
159164 ] )
160165
161166 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 } )
163169 }
164170 return logGitSetupInstructionsAndExit ( )
165171}
@@ -194,15 +200,36 @@ const createOrLinkSiteToRepo = async (command: BaseCommand) => {
194200 return await link ( { } , command )
195201}
196202
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 => {
198212 log ( )
199213 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 )
201217 // TODO add support for changing GitHub repo in site:config command
202218 exit ( )
203219}
204220
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 > => {
206233 command . setAnalyticsPayload ( { manual : options . manual , force : options . force } )
207234
208235 const { repositoryRoot, state } = command . netlify
@@ -222,11 +249,13 @@ export const init = async (options: OptionValues, command: BaseCommand): Promise
222249 // Look for local repo
223250 const repoData = await getRepoData ( { workingDir : command . workingDir , remoteName : options . gitRemoteName } )
224251 if ( 'error' in repoData ) {
252+ // TODO(ndhoule): Custom error messaage here
225253 return await handleNoGitRemoteAndExit ( {
226254 command,
227255 error : repoData . error ,
228256 state,
229257 disableLinking : options . disableLinking ,
258+ customizeExitMessage,
230259 } )
231260 }
232261
@@ -237,12 +266,19 @@ export const init = async (options: OptionValues, command: BaseCommand): Promise
237266 // Check for existing CI setup
238267 const remoteBuildRepo = getRepoUrl ( siteInfo )
239268 if ( remoteBuildRepo && ! options . force ) {
240- logExistingRepoSetupAndExit ( { siteName : siteInfo . name , repoUrl : remoteBuildRepo } )
269+ logExistingRepoSetupAndExit ( { siteName : siteInfo . name , repoUrl : remoteBuildRepo , customizeExitMessage } )
241270 }
242271
243272 persistState ( { state, siteInfo } )
244273
245274 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+ }
246282
247283 return siteInfo
248284}
0 commit comments