2
2
3
3
import process from 'node:process'
4
4
import chalk from 'chalk'
5
- import minimist from 'minimist'
6
- import { createHost , parseHost } from './host.js'
5
+ import minimist , { ParsedArgs } from 'minimist'
6
+ import { Context , createHost , parseHost } from './host.js'
7
7
import { runTask } from './task.js'
8
8
import './recipe/common.js'
9
- import { exec , humanPath } from './utils.js'
10
- import { startSpinner , stopSpinner } from './spinner.js'
11
- import { ask , confirm , skipPrompts } from './prompt.js'
9
+ import { exec } from './utils.js'
10
+ import { disableSpinner , startSpinner , stopSpinner } from './spinner.js'
11
+ import { ask , skipPrompts } from './prompt.js'
12
12
import fs from 'node:fs'
13
13
import { handleError , StopError } from './error.js'
14
- import { detectFramework , setupFramework } from './framework.js'
15
14
import { login } from './api.js'
16
15
import { loadUserConfig } from './env.js'
17
16
@@ -41,6 +40,7 @@ await async function main() {
41
40
} ,
42
41
} )
43
42
skipPrompts ( argv . yes )
43
+ normiliazeArgs ( argv )
44
44
45
45
if ( argv . version ) {
46
46
console . log ( JSON . parse ( fs . readFileSync ( new URL ( '../../package.json' , import . meta. url ) , 'utf8' ) ) . version )
@@ -59,21 +59,7 @@ await async function main() {
59
59
await loadUserConfig ( )
60
60
await login ( )
61
61
62
- let task , remoteUser , hostname , become
63
- if ( argv . _ . length == 2 ) {
64
- task = argv . _ [ 0 ] ;
65
- ( { remoteUser, hostname, become} = parseHost ( argv . _ [ 1 ] ) )
66
- } else if ( argv . _ . length == 1 ) {
67
- ( { remoteUser, hostname, become} = parseHost ( argv . _ [ 0 ] ) )
68
- } else if ( argv . _ . length == 0 ) {
69
- ( { remoteUser, hostname, become} = parseHost ( await ask ( 'Enter hostname: ' ) ) )
70
- }
71
- if ( ! task ) {
72
- task = 'provision-and-deploy'
73
- }
74
- if ( argv . scripts && ! Array . isArray ( argv . scripts ) ) {
75
- argv . scripts = [ argv . scripts ]
76
- }
62
+ const { task, remoteUser, hostname, become} = await parseTaskAndHost ( argv )
77
63
78
64
const context = createHost ( {
79
65
remoteUser,
@@ -83,60 +69,57 @@ await async function main() {
83
69
} )
84
70
85
71
// Check SSH connection
86
- const shellName = await context . $ . with ( { nothrow : true } ) `echo $0`
87
- if ( shellName . toString ( ) != 'bash' ) {
88
- throw new StopError (
89
- `Webpod cannot connect to ${ bold ( `${ remoteUser } @${ hostname } ` ) } ` ,
90
- `Please, verify that you can connect to the host using ${ bold ( 'ssh' ) } command.\n\n` +
91
- ` ssh ${ remoteUser } @${ hostname } ${ context . config . port ? `-p ${ context . config . port } ` : '' } \n`
92
- )
93
- }
72
+ await checkSSHConnection ( context )
94
73
95
- do {
96
- const framework = detectFramework ( )
97
- await setupFramework ( framework , context )
98
-
99
- await context . host . domain
100
- await context . host . uploadDir
101
- await context . host . publicDir
102
-
103
- console . log ( `Webpod now will configure your server with:` )
104
- console . log ( ` ${ bold ( '✔' ) } Updates` )
105
- console . log ( ` ${ bold ( '✔' ) } Firewall` )
106
- console . log ( ` ${ bold ( '✔' ) } Webserver` )
107
- console . log ( ` ${ bold ( '✔' ) } SSL/TLS certificates` )
108
- console . log ( ` ${ bold ( '✔' ) } Node.js` )
109
- console . log ( ` ${ bold ( '✔' ) } Supervisor` )
110
- console . log ( `and deploy your app:` )
111
- if ( framework == 'next' ) {
112
- console . log ( `${ cyan ( 'Next.js' ) } detected` )
113
- } else {
114
- console . log ( ` ${ bold ( '✔' ) } Uploading ${ cyan ( humanPath ( await context . host . uploadDir ) ) } to ${ cyan ( await context . host . remoteUser + '@' + await context . host . hostname ) } ` )
115
- if ( await context . host . static ) {
116
- console . log ( ` ${ bold ( '✔' ) } Serving ${ cyan ( humanPath ( await context . host . uploadDir , await context . host . publicDir ) ) } at ${ cyan ( 'https://' + await context . host . domain ) } ` )
117
- }
118
- for ( const script of await context . host . scripts ) {
119
- console . log ( ` ${ bold ( '✔' ) } Running ${ cyan ( humanPath ( await context . host . uploadDir , script ) ) } ` )
120
- }
121
- }
122
-
123
- if ( await confirm ( `Correct?` ) ) {
124
- break
125
- } else {
126
- delete context . config . domain
127
- delete context . config . uploadDir
128
- delete context . config . publicDir
129
- delete context . config . scripts
130
- }
131
- } while ( true )
132
-
133
- if ( ! context . config . verbose ) startSpinner ( )
74
+ if ( context . config . verbose ) {
75
+ disableSpinner ( )
76
+ }
77
+ startSpinner ( )
134
78
try {
135
79
await runTask ( task , context )
136
80
} finally {
137
81
stopSpinner ( )
138
82
}
139
83
140
84
console . log ( `${ green ( 'Done!' ) } ${ cyan ( 'https://' + await context . host . domain ) } 🎉` )
141
-
142
85
} ( ) . catch ( handleError )
86
+
87
+ function normiliazeArgs ( argv : ParsedArgs ) {
88
+ if ( argv . scripts && ! Array . isArray ( argv . scripts ) ) {
89
+ argv . scripts = [ argv . scripts ]
90
+ }
91
+ }
92
+
93
+ type TaskAndHost = {
94
+ task : string
95
+ remoteUser : string
96
+ hostname : string
97
+ become ? : string
98
+ }
99
+
100
+ async function parseTaskAndHost ( argv : ParsedArgs ) : Promise < TaskAndHost > {
101
+ let task = 'default'
102
+ let remoteUser : string
103
+ let hostname : string
104
+ let become : string | undefined
105
+ if ( argv . _ . length == 2 ) {
106
+ task = argv . _ [ 0 ] ;
107
+ ( { remoteUser, hostname, become} = parseHost ( argv . _ [ 1 ] ) )
108
+ } else if ( argv . _ . length == 1 ) {
109
+ ( { remoteUser, hostname, become} = parseHost ( argv . _ [ 0 ] ) )
110
+ } else {
111
+ ( { remoteUser, hostname, become} = parseHost ( await ask ( 'Enter hostname: ' ) ) )
112
+ }
113
+ return { task, remoteUser, hostname, become}
114
+ }
115
+
116
+ async function checkSSHConnection ( context : Context ) {
117
+ const shellName = await context . $ . with ( { nothrow : true } ) `echo $0`
118
+ if ( shellName . toString ( ) != 'bash' ) {
119
+ throw new StopError (
120
+ `Webpod cannot connect to ${ bold ( `${ context . config . remoteUser } @${ context . config . hostname } ` ) } ` ,
121
+ `Please, verify that you can connect to the host using ${ bold ( 'ssh' ) } command.\n\n` +
122
+ ` ssh ${ context . config . remoteUser } @${ context . config . hostname } ${ context . config . port ? `-p ${ context . config . port } ` : '' } \n` ,
123
+ )
124
+ }
125
+ }
0 commit comments