1
1
import process from 'node:process'
2
2
import { Action , ExitCode } from '@stacksjs/types'
3
- import {
4
- runAction ,
5
- runComponentsDevServer ,
6
- runFunctionsDevServer ,
7
- runPagesDevServer ,
8
- } from '@stacksjs/actions'
3
+ import { runAction , runComponentsDevServer , runFunctionsDevServer , runDocsDevServer } from '@stacksjs/actions'
9
4
import type { CLI , DevOptions } from '@stacksjs/types'
10
- import { intro , log , outro , runCommand } from '@stacksjs/cli'
5
+ import { intro , log , outro , runCommand , prompt } from '@stacksjs/cli'
11
6
import { vitePath } from '@stacksjs/path'
12
7
13
8
export function dev ( buddy : CLI ) {
14
9
const descriptions = {
10
+ dev : 'Starts development server' ,
11
+ views : 'Starts the frontend development server' ,
15
12
components : 'Start the Components development server' ,
16
13
desktop : 'Start the Desktop development server' ,
17
14
api : 'Start the local API development server' ,
18
15
docs : 'Start the Documentation development server' ,
19
- pages : 'Start the Pages development server' ,
16
+ interactive : 'Get asked which development server to start ' ,
20
17
select : 'Which development server are you trying to start?' ,
21
18
verbose : 'Enable verbose output' ,
22
19
}
23
20
24
21
buddy
25
- . command ( 'dev' , 'Start the development server for any of the following' )
22
+ . command ( 'dev [ server]' , descriptions . dev )
26
23
. option ( '-c, --components' , descriptions . components )
27
24
. option ( '-a, --api' , descriptions . api )
28
25
. option ( '-d, --docs' , descriptions . docs )
29
- . option ( '-p , --views ' , descriptions . pages )
26
+ . option ( '-i , --interactive ' , descriptions . interactive , { default : false } )
30
27
. option ( '--verbose' , descriptions . verbose , { default : false } )
31
- . action ( async ( options : DevOptions ) => {
32
- // const perf = await intro('buddy dev')
33
- // const result = await runAction(Action.Dev, options)
34
-
35
- if ( hasNoOptions ( options ) ) {
36
- // const answer = await prompt.require()
37
- // .select(descriptions.select, {
38
- // options: [
39
- // { value: 'all', label: 'All' },
40
- // { value: 'pages', label: 'Frontend' },
41
- // { value: 'api', label: 'Backend' },
42
- // { value: 'desktop', label: 'Desktop' },
43
- // { value: 'components', label: 'Components' },
44
- // { value: 'functions', label: 'Functions' },
45
- // { value: 'docs', label: 'Documentation' },
46
- // ],
47
- // })
48
-
49
- // if (answer === 'components')
50
- // await components(options)
28
+ . action ( async ( server : string | undefined , options : DevOptions ) => {
29
+ const perf = await intro ( 'buddy dev' )
30
+
31
+ console . log ( 'server' , server )
32
+
33
+ switch ( server ) {
34
+ case 'frontend' :
35
+ await runPagesDevServer ( options )
36
+ break
37
+ case 'api' :
38
+ await runFunctionsDevServer ( options )
39
+ break
40
+ case 'components' :
41
+ await runComponentsDevServer ( options )
42
+ break
43
+ case 'docs' :
44
+ await runDocsDevServer ( options )
45
+ break
46
+ default :
47
+ }
48
+
49
+ if ( wantsInteractive ( options ) ) {
50
+ const answer = await prompt . require ( )
51
+ . select ( descriptions . select , {
52
+ options : [
53
+ { value : 'all' , label : 'All' } ,
54
+ { value : 'pages' , label : 'Frontend' } ,
55
+ { value : 'api' , label : 'Backend' } ,
56
+ { value : 'desktop' , label : 'Desktop' } ,
57
+ { value : 'components' , label : 'Components' } ,
58
+ // { value: 'functions', label: 'Functions' },
59
+ { value : 'docs' , label : 'Documentation' } ,
60
+ ] ,
61
+ } )
62
+
63
+ if ( answer === 'components' )
64
+ await runComponentsDevServer ( options )
51
65
// else if (answer === 'functions')
52
- // await functions (options)
53
- // else if (answer === 'pages')
54
- // await pages (options)
55
- // else if (answer === 'docs')
56
- // await docs (options)
66
+ // await runFunctionsDevServer (options)
67
+ else if ( answer === 'pages' )
68
+ await runPagesDevServer ( options )
69
+ else if ( answer === 'docs' )
70
+ await runDocsDevServer ( options )
57
71
58
- // else process.exit(ExitCode.InvalidArgument)
72
+ else {
73
+ log . error ( 'Invalid option during interactive mode' )
74
+ process . exit ( ExitCode . InvalidArgument )
75
+ }
59
76
}
77
+
60
78
else {
61
79
if ( options . components )
62
80
await runComponentsDevServer ( options )
63
- // if (options.docs)
64
- // await runDocsDevServer(options)
81
+ if ( options . docs )
82
+ await runDocsDevServer ( options )
65
83
else if ( options . api )
66
84
await runFunctionsDevServer ( options )
67
85
else if ( options . pages )
68
86
await runPagesDevServer ( options )
69
- // else if (options.docs)
70
87
}
71
- // await startDevelopmentServer(options)
72
88
89
+ await startDevelopmentServer ( options )
90
+
91
+ outro ( 'Exited' , { startTime : perf , useSeconds : true } )
73
92
process . exit ( ExitCode . Success )
74
93
} )
75
94
@@ -139,7 +158,7 @@ export function dev(buddy: CLI) {
139
158
} )
140
159
141
160
buddy
142
- . command ( 'dev:views' , descriptions . pages )
161
+ . command ( 'dev:views' , descriptions . views )
143
162
. option ( '--verbose' , descriptions . verbose , { default : false } )
144
163
. action ( async ( options : DevOptions ) => {
145
164
await runPagesDevServer ( options )
@@ -151,6 +170,15 @@ export function dev(buddy: CLI) {
151
170
} )
152
171
}
153
172
154
- function hasNoOptions ( options : DevOptions ) {
155
- return ! options . components && ! options . all && ! options . docs && ! options . api && ! options . pages
173
+ export async function startDevelopmentServer ( options : DevOptions ) {
174
+ const result = await runAction ( Action . Dev , options )
175
+
176
+ if ( result . isErr ( ) ) {
177
+ log . error ( 'While running the dev command, there was an issue' , result . error )
178
+ process . exit ( ExitCode . InvalidArgument )
179
+ }
180
+ }
181
+
182
+ function wantsInteractive ( options : DevOptions ) {
183
+ return options . interactive
156
184
}
0 commit comments