1
1
import { isNullish } from '@junobuild/utils' ;
2
2
import { red } from 'kleur' ;
3
3
import prompts from 'prompts' ;
4
- import type { GeneratorInput } from '../types/generator' ;
4
+ import { GeneratorInput , ProjectKind } from '../types/generator' ;
5
5
import type { Template } from '../types/template' ;
6
6
import { assertAnswerCtrlC } from '../utils/prompts.utils' ;
7
7
8
- const WEBSITE_TEMPLATES : Template [ ] = [ ] ;
8
+ const WEBSITE_TEMPLATES : Template [ ] = [
9
+ {
10
+ framework : `Astro` ,
11
+ key : `astro-starter` ,
12
+ type : 'Starter' ,
13
+ description : 'Opt for a barebones scaffolding to kickstart your website'
14
+ }
15
+ ] ;
16
+
17
+ const APP_TEMPLATES : Template [ ] = [
18
+ {
19
+ framework : `Next.js` ,
20
+ key : `nextjs-starter` ,
21
+ type : 'Starter' ,
22
+ description : 'Opt for a barebones scaffolding to kickstart your app'
23
+ }
24
+ ] ;
9
25
10
- const APP_TEMPLATES : Template [ ] = [ { title : `Next.js` , key : `nextjs` } ] ;
26
+ export const promptTemplate = async ( kind : ProjectKind ) : Promise < Template > => {
27
+ const allTemplates = Object . groupBy (
28
+ kind === 'app' ? APP_TEMPLATES : WEBSITE_TEMPLATES ,
29
+ ( { framework} ) => framework
30
+ ) ;
11
31
12
- export const promptTemplate = async ( type : 'app' | 'website' ) : Promise < Template > => {
13
- const collection = type === 'app' ? APP_TEMPLATES : WEBSITE_TEMPLATES ;
32
+ const frameworks = Object . keys ( allTemplates ) ;
14
33
15
- const { template } : { template : string } = await prompts ( {
34
+ const { framework } : Pick < Template , 'framework' > = await prompts ( {
16
35
type : 'select' ,
17
- name : 'template ' ,
18
- message : 'Which template do you want to use?' ,
19
- choices : collection . map ( ( { title , key } ) => ( { title, value : key } ) )
36
+ name : 'framework ' ,
37
+ message : 'Which framework do you want to use?' ,
38
+ choices : frameworks . map ( ( framework ) => ( { title : framework , value : framework } ) )
20
39
} ) ;
21
40
22
- assertAnswerCtrlC ( template ) ;
41
+ assertAnswerCtrlC ( framework ) ;
23
42
24
- const item = collection . find ( ( { key } ) => key === template ) ;
43
+ const templates = allTemplates [ framework ] ;
25
44
26
- if ( isNullish ( item ) ) {
27
- console . log ( red ( `Invalid ${ type } template: ${ template } ` ) ) ;
45
+ if ( isNullish ( templates ) || templates . length === 0 ) {
46
+ console . log ( `No template(s) found for ${ red ( framework ) } . This is unexpected.` ) ;
28
47
process . exit ( 1 ) ;
29
48
}
30
49
31
- return item ;
32
- } ;
50
+ if ( templates . length === 1 ) {
51
+ return templates [ 0 ] ;
52
+ }
33
53
34
- export const promptStarter = async ( ) => {
35
- const { starter} : { starter : 'blank' | 'tutorial' } = await prompts ( {
54
+ const { template} : { template : Template | undefined } = await prompts ( {
36
55
type : 'select' ,
37
- name : 'starter' ,
38
- message : 'Which starter template would you like to use?' ,
39
- choices : [
40
- {
41
- title : 'Blank (A blank starter with "just" a customized index page)' ,
42
- value : 'blank'
43
- } ,
44
- {
45
- title : 'Tutorial (the "diary" example app)' ,
46
- value : 'tutorial'
47
- }
48
- ]
56
+ name : 'template' ,
57
+ message : 'Choose your starting point?' ,
58
+ choices : templates . map ( ( value ) => ( {
59
+ title : value . type ,
60
+ description : value . description ,
61
+ value
62
+ } ) )
49
63
} ) ;
50
64
51
- assertAnswerCtrlC ( starter ) ;
65
+ if ( isNullish ( template ) ) {
66
+ process . exit ( 1 ) ;
67
+ }
52
68
53
- return starter ;
69
+ return template ;
54
70
} ;
55
71
56
72
export const promptDestination = async ( ) : Promise < Pick < GeneratorInput , 'destination' > > => {
@@ -68,8 +84,8 @@ export const promptDestination = async (): Promise<Pick<GeneratorInput, 'destina
68
84
return { destination} ;
69
85
} ;
70
86
71
- export const promptKind = async ( ) : Promise < Pick < GeneratorInput , 'kind' > > => {
72
- const { kind} : Pick < GeneratorInput , ' kind' > = await prompts ( {
87
+ export const promptProjectKind = async ( ) : Promise < ProjectKind > => {
88
+ const { kind} : { kind : ProjectKind | undefined } = await prompts ( {
73
89
type : 'select' ,
74
90
name : 'kind' ,
75
91
message : 'What kind of project are you starting?' ,
@@ -81,5 +97,5 @@ export const promptKind = async (): Promise<Pick<GeneratorInput, 'kind'>> => {
81
97
82
98
assertAnswerCtrlC ( kind ) ;
83
99
84
- return { kind} ;
100
+ return kind ;
85
101
} ;
0 commit comments