1
1
import process from 'node:process'
2
2
import type { CLI , CloudOptions } from '@stacksjs/types'
3
- import { intro , italic , log , outro , runCommand } from '@stacksjs/cli'
3
+ import { intro , italic , log , outro , prompts , runCommand } from '@stacksjs/cli'
4
4
import { path as p } from '@stacksjs/path'
5
5
import { ExitCode } from '@stacksjs/types'
6
+ import { addJumpBox , deleteJumpBox , getJumpBoxInstanceId } from '@stacksjs/cloud'
6
7
7
8
export function cloud ( buddy : CLI ) {
8
9
const descriptions = {
9
10
cloud : 'Interact with the Stacks Cloud' ,
10
11
ssh : 'SSH into the Stacks Cloud' ,
12
+ add : 'Add a resource to the Stacks Cloud.' ,
11
13
remove : 'Remove the Stacks Cloud. In case it fails, try again.' ,
12
14
verbose : 'Enable verbose output' ,
13
15
}
@@ -20,7 +22,8 @@ export function cloud(buddy: CLI) {
20
22
. action ( async ( options : CloudOptions ) => {
21
23
if ( options . ssh || options . connect ) {
22
24
const startTime = performance . now ( )
23
- const result = await runCommand ( 'aws ssm start-session --target i-0a4a48be7544a72e3' , {
25
+ const jumpBoxId = await getJumpBoxInstanceId ( )
26
+ const result = await runCommand ( `aws ssm start-session --target ${ jumpBoxId } ` , {
24
27
...options ,
25
28
cwd : p . projectPath ( ) ,
26
29
stdin : 'pipe' ,
@@ -39,18 +42,76 @@ export function cloud(buddy: CLI) {
39
42
process . exit ( ExitCode . Success )
40
43
} )
41
44
45
+ buddy
46
+ . command ( 'cloud:add' , descriptions . add )
47
+ . option ( '--jump-box' , 'Remove the jump-box' , { default : true } )
48
+ . option ( '--verbose' , descriptions . verbose , { default : false } )
49
+ . action ( async ( options : CloudOptions ) => {
50
+ const startTime = await intro ( 'buddy cloud:add' )
51
+
52
+ if ( options . jumpBox ) {
53
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
54
+ const { confirm } = await prompts ( {
55
+ name : 'confirm' ,
56
+ type : 'confirm' ,
57
+ message : 'Would you like to add a jump-box?' ,
58
+ } )
59
+
60
+ if ( ! confirm ) {
61
+ await outro ( 'Exited' , { startTime, useSeconds : true } )
62
+ process . exit ( ExitCode . Success )
63
+ }
64
+
65
+ log . info ( '' )
66
+ log . info ( 'The jump-box is getting added to your cloud resources...' )
67
+ log . info ( 'This takes a few moments, please be patient' )
68
+ log . info ( '' )
69
+ // sleep for 2 seconds to get the user to read the message
70
+ await new Promise ( resolve => setTimeout ( resolve , 2000 ) )
71
+
72
+ await addJumpBox ( )
73
+
74
+ await outro ( 'Your jump-box was added.' , { startTime, useSeconds : true } )
75
+ process . exit ( ExitCode . Success )
76
+ }
77
+
78
+ log . info ( '' )
79
+ log . info ( 'This functionality is not yet implemented.' )
80
+ process . exit ( ExitCode . Success )
81
+ } )
82
+
42
83
buddy
43
84
. command ( 'cloud:remove' , descriptions . remove )
44
85
. alias ( 'cloud:destroy' )
45
- // .option('--jump-box', 'Remove the jump box', { default: true })
86
+ . option ( '--jump-box' , 'Remove the jump- box' , { default : false } )
46
87
. option ( '--verbose' , descriptions . verbose , { default : false } )
47
88
. action ( async ( options : CloudOptions ) => {
48
89
const startTime = await intro ( 'buddy cloud:remove' )
49
90
91
+ if ( options . jumpBox ) {
92
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
93
+ const { confirm } = await prompts ( {
94
+ name : 'confirm' ,
95
+ type : 'confirm' ,
96
+ message : 'Would you like to remove your jump-box for now?' ,
97
+ } )
98
+
99
+ if ( ! confirm ) {
100
+ await outro ( 'Exited' , { startTime, useSeconds : true } )
101
+ process . exit ( ExitCode . Success )
102
+ }
103
+
104
+ await deleteJumpBox ( )
105
+
106
+ await outro ( 'Your jump-box was removed.' , { startTime, useSeconds : true } )
107
+ process . exit ( ExitCode . Success )
108
+ }
109
+
50
110
log . info ( '' )
51
111
log . info ( 'Please note, removing your cloud resources will take a while to complete. Please be patient.' )
52
112
log . info ( '' )
53
- log . info ( italic ( 'If you see an error, please try again. If the error persists, please contact support.' ) )
113
+ log . info ( italic ( 'Due to the nature of Lambda@edge functions, this command may fail on first execution.' ) )
114
+ log . info ( italic ( 'If it does fail, don\'t worry—please try again, or contact us trough Discord.' ) )
54
115
// sleep for 2 seconds to get the user to read the message
55
116
await new Promise ( resolve => setTimeout ( resolve , 2000 ) )
56
117
0 commit comments