@@ -3,7 +3,7 @@ import process from 'node:process'
3
3
import { ExitCode } from '@stacksjs/types'
4
4
import type { CLI , DeployOptions } from '@stacksjs/types'
5
5
import { runAction } from '@stacksjs/actions'
6
- import { intro , italic , log , outro } from '@stacksjs/cli'
6
+ import { intro , italic , log , outro , runCommand } from '@stacksjs/cli'
7
7
import { Action } from '@stacksjs/enums'
8
8
import { app } from '@stacksjs/config'
9
9
import { addDomain , hasUserDomainBeenAddedToCloud } from '@stacksjs/dns'
@@ -24,77 +24,95 @@ export function deploy(buddy: CLI) {
24
24
const startTime = await intro ( 'buddy deploy' )
25
25
const domain = options . domain || app . url
26
26
27
- if ( ! domain ) {
28
- log . info ( 'We could not identify a domain to deploy to.' )
29
- log . info ( 'Please set your .env or ./config/app.ts properly.' )
30
- console . log ( '' )
31
- log . info ( 'Alternatively, specify a domain to deploy via the `--domain` flag.' )
32
- console . log ( '' )
33
- log . info ( ' ➡️ Example: `buddy deploy --domain example.com`' )
34
- console . log ( '' )
35
- process . exit ( ExitCode . FatalError )
36
- }
37
-
38
- // TODO: we can improve this check at some point, otherwise domains that legitimately include the word localhost will fail
39
- // TODO: add check for whether the local APP_ENV is getting deployed, if so, ask if the user meant to deploy `dev`
40
- if ( domain . includes ( 'localhost' ) ) {
41
- log . info ( 'You are deploying to a local environment.' )
42
- log . info ( 'Please set your .env or ./config/app.ts properly.' )
43
- console . log ( '' )
44
- log . info ( 'Alternatively, specify a domain to deploy via the `--domain` flag.' )
45
- console . log ( '' )
46
- log . info ( ' ➡️ Example: `buddy deploy --domain example.com`' )
47
- console . log ( '' )
48
- process . exit ( ExitCode . FatalError )
49
- }
50
-
51
- if ( await hasUserDomainBeenAddedToCloud ( domain ) ) {
52
- log . info ( 'Domain is properly configured' )
53
- log . info ( 'Your cloud is deploying...' )
54
-
55
- log . info ( `${ italic ( 'This may take a while...' ) } ` )
56
- options . domain = domain
57
-
58
- // now that we know the domain has been added to the users (AWS) cloud, we can deploy
59
- const result = await runAction ( Action . Deploy , options )
27
+ await checkIfAwsIsConfigured ( )
60
28
61
- if ( result . isErr ( ) ) {
62
- await outro ( 'While running the `buddy deploy`, there was an issue' , { startTime, useSeconds : true } , result . error )
63
- process . exit ( ExitCode . FatalError )
64
- }
29
+ options . domain = await configureDomain ( domain )
65
30
66
- await outro ( 'Deployment succeeded.' , { startTime, useSeconds : true } )
67
-
68
- process . exit ( ExitCode . Success )
69
- }
70
-
71
- // if the domain hasn't been added to the user's (AWS) cloud, we will add it for them
72
- // and then exit the process with prompts for the user to update their nameservers
73
- console . log ( '' )
74
- log . info ( ` 👋 It appears to be your first ${ italic ( domain ) } deployment.` )
75
- console . log ( '' )
76
- log . info ( italic ( 'Let’s ensure it is all connected properly.' ) )
77
- log . info ( italic ( 'One moment...' ) )
78
- console . log ( '' )
79
-
80
- options . domain = domain
81
- const result = await addDomain ( {
82
- ...options ,
83
- deploy : true ,
84
- startTime,
85
- } )
31
+ const result = await runAction ( Action . Deploy , options )
86
32
87
33
if ( result . isErr ( ) ) {
88
34
await outro ( 'While running the `buddy deploy`, there was an issue' , { startTime, useSeconds : true } , result . error )
89
35
process . exit ( ExitCode . FatalError )
90
36
}
91
37
92
- await outro ( 'Added your domain.' , { startTime, useSeconds : true } )
93
- process . exit ( ExitCode . Success )
38
+ await outro ( 'Project deployed.' , { startTime, useSeconds : true } )
94
39
} )
95
40
96
41
buddy . on ( 'deploy:*' , ( ) => {
97
42
log . error ( 'Invalid command: %s\nSee --help for a list of available commands.' , buddy . args . join ( ' ' ) )
98
43
process . exit ( 1 )
99
44
} )
100
45
}
46
+
47
+ async function configureDomain ( domain : string ) {
48
+ if ( ! domain ) {
49
+ log . info ( 'We could not identify a domain to deploy to.' )
50
+ log . info ( 'Please set your .env or ./config/app.ts properly.' )
51
+ console . log ( '' )
52
+ log . info ( 'Alternatively, specify a domain to deploy via the `--domain` flag.' )
53
+ console . log ( '' )
54
+ log . info ( ' ➡️ Example: `buddy deploy --domain example.com`' )
55
+ console . log ( '' )
56
+ process . exit ( ExitCode . FatalError )
57
+ }
58
+
59
+ // TODO: we can improve this check at some point, otherwise domains that legitimately include the word localhost will fail
60
+ // TODO: add check for whether the local APP_ENV is getting deployed, if so, ask if the user meant to deploy `dev`
61
+ if ( domain . includes ( 'localhost' ) ) {
62
+ log . info ( 'You are deploying to a local environment.' )
63
+ log . info ( 'Please set your .env or ./config/app.ts properly.' )
64
+ console . log ( '' )
65
+ log . info ( 'Alternatively, specify a domain to deploy via the `--domain` flag.' )
66
+ console . log ( '' )
67
+ log . info ( ' ➡️ Example: `buddy deploy --domain example.com`' )
68
+ console . log ( '' )
69
+ process . exit ( ExitCode . FatalError )
70
+ }
71
+
72
+ if ( await hasUserDomainBeenAddedToCloud ( domain ) ) {
73
+ log . info ( 'Domain is properly configured' )
74
+ log . info ( 'Your cloud is deploying...' )
75
+
76
+ log . info ( `${ italic ( 'This may take a while...' ) } ` )
77
+
78
+ return domain
79
+ }
80
+
81
+ // if the domain hasn't been added to the user's (AWS) cloud, we will add it for them
82
+ // and then exit the process with prompts for the user to update their nameservers
83
+ console . log ( '' )
84
+ log . info ( ` 👋 It appears to be your first ${ italic ( domain ) } deployment.` )
85
+ console . log ( '' )
86
+ log . info ( italic ( 'Let’s ensure it is all connected properly.' ) )
87
+ log . info ( italic ( 'One moment...' ) )
88
+ console . log ( '' )
89
+
90
+ const result = await addDomain ( {
91
+ ...options ,
92
+ deploy : true ,
93
+ startTime,
94
+ } )
95
+
96
+ if ( result . isErr ( ) ) {
97
+ await outro ( 'While running the `buddy deploy`, there was an issue' , { startTime, useSeconds : true } , result . error )
98
+ process . exit ( ExitCode . FatalError )
99
+ }
100
+
101
+ await outro ( 'Added your domain.' , { startTime, useSeconds : true } )
102
+ process . exit ( ExitCode . Success )
103
+ }
104
+
105
+ async function checkIfAwsIsConfigured ( ) {
106
+ log . info ( 'Checking if AWS is configured...' )
107
+ const result = await runCommand ( 'buddy configure:aws --quiet' , {
108
+ silent : true ,
109
+ } )
110
+
111
+ if ( result . isErr ( ) ) {
112
+ log . error ( 'AWS is not configured properly.' )
113
+ log . error ( 'Please run `buddy configure:aws` to set up your AWS credentials.' )
114
+ process . exit ( ExitCode . FatalError )
115
+ }
116
+
117
+ log . info ( 'AWS is configured' )
118
+ }
0 commit comments