2
2
3
3
import * as fs from 'node:fs'
4
4
import * as path from 'node:path'
5
- import prompts from 'prompts'
5
+ import prompts , { type PromptObject } from 'prompts'
6
6
import ejs from 'ejs'
7
7
8
+ async function prompt ( options : Omit < PromptObject , 'name' > ) {
9
+ try {
10
+ const result = await prompts (
11
+ {
12
+ ...options ,
13
+ name : 'name'
14
+ } ,
15
+ {
16
+ onCancel : ( ) => {
17
+ throw new Error ( 'Cancelled' )
18
+ }
19
+ }
20
+ )
21
+
22
+ return result . name
23
+ } catch ( cancelled ) {
24
+ console . log ( cancelled . message )
25
+ process . exit ( 1 )
26
+ }
27
+ }
28
+
29
+ async function textPrompt ( message : string , initial ?: string ) : Promise < string > {
30
+ return prompt ( {
31
+ type : 'text' ,
32
+ message,
33
+ initial
34
+ } )
35
+ }
36
+
37
+ async function togglePrompt ( message : string , initial = false , active = 'Yes' , inactive = 'No' ) : Promise < boolean > {
38
+ return prompt ( {
39
+ type : 'toggle' ,
40
+ message,
41
+ initial,
42
+ active,
43
+ inactive
44
+ } )
45
+ }
46
+
8
47
type Config = {
9
48
scopedPackageName : string
10
49
unscopedPackageName : string
@@ -30,84 +69,26 @@ type Config = {
30
69
async function init ( ) {
31
70
const cwd = process . cwd ( )
32
71
33
- let result : {
34
- packageName: string
35
- githubPath: string
36
- includeDocs: boolean
37
- includeGithubPages ? : boolean
38
- includePlayground: boolean
39
- includeExamples: boolean
40
- } = { }
41
-
42
- try {
43
- result = < any > await prompts(
44
- [
45
- {
46
- name : 'packageName' ,
47
- type : 'text' ,
48
- message : 'Package name' ,
49
- initial : '@skirtle/test-project'
50
- } , {
51
- name : 'githubPath' ,
52
- type : 'text' ,
53
- message : 'GitHub path, e.g. skirtles-code/test-project (optional)' ,
54
- initial : ''
55
- } , {
56
- name : 'includeDocs' ,
57
- type : 'toggle' ,
58
- message : 'Include VitePress for documentation?' ,
59
- initial : true ,
60
- active : 'Yes' ,
61
- inactive : 'No'
62
- } , {
63
- name : 'includeGithubPages' ,
64
- type : ( prev ) => prev ? 'toggle' : null ,
65
- message : 'Include GitHub Pages config for documentation?' ,
66
- initial : false ,
67
- active : 'Yes' ,
68
- inactive : 'No'
69
- } , {
70
- name : 'includePlayground' ,
71
- type : 'toggle' ,
72
- message : 'Include playground application for development?' ,
73
- initial : true ,
74
- active : 'Yes' ,
75
- inactive : 'No'
76
- } , {
77
- name : 'includeExamples' ,
78
- type : 'toggle' ,
79
- message : 'Include example code?' ,
80
- initial : true ,
81
- active : 'Yes' ,
82
- inactive : 'No, just configs'
83
- }
84
- ],
85
- {
86
- onCancel : ( ) = > {
87
- throw new Error ( 'Cancelled' )
88
- } ,
89
- } ,
90
- )
91
- } catch ( cancelled ) {
92
- console . log ( cancelled . message )
93
- process . exit ( 1 )
94
- }
95
-
96
- const scopedPackageName = result.packageName
72
+ const scopedPackageName = await textPrompt ( 'Package name' , '@skirtle/test-project' )
97
73
98
74
// TODO: Tightening this check, e.g. for hyphen positions
99
75
if ( ! / @ [ a - z 0 - 9 - ] + \/ [ a - z 0 - 9 - ] + / . test ( scopedPackageName ) ) {
100
76
console . log ( 'Invalid package name: ' + scopedPackageName )
101
77
process . exit ( 1 )
102
78
}
103
79
104
- const githubPath = result.githubPath
80
+ const githubPath = await textPrompt ( 'GitHub path, e.g. skirtles-code/test-project (optional)' )
105
81
106
82
if ( githubPath && ! / [ \w - ] + \/ [ \w - ] + / . test ( githubPath ) ) {
107
83
console . log ( 'Invalid GitHub path: ' + githubPath )
108
84
process . exit ( 1 )
109
85
}
110
86
87
+ const includeDocs = await togglePrompt ( 'Include VitePress for documentation?' , true )
88
+ const includeGithubPages = includeDocs && await togglePrompt ( 'Include GitHub Pages config for documentation?' )
89
+ const includePlayground = await togglePrompt ( 'Include playground application for development?' , true )
90
+ const includeExamples = await togglePrompt ( 'Include example code?' , true , 'Yes' , 'No, just configs' )
91
+
111
92
const unscopedPackageName = scopedPackageName . replace ( / .* \/ / , '' )
112
93
const shortUnscopedPackageName = unscopedPackageName . replace ( / ^ v u e - / , '' )
113
94
const projectName = unscopedPackageName . replace ( / - + / g, ' ' ) . trim ( ) . split ( ' ' ) . map ( s => s [ 0 ] . toUpperCase ( ) + s . slice ( 1 ) ) . join ( ' ' )
@@ -118,9 +99,9 @@ async function init() {
118
99
const githubUrl = githubPath ? `https://github.com/${ githubPath } ` : ''
119
100
const githubIssues = githubPath ? `${ githubUrl } /issues` : ''
120
101
const githubRepository = githubPath ? `git+${ githubUrl } .git` : ''
121
- const githubPagesOrigin = githubUserName && result . includeGithubPages ? `https://${ githubUserName } .github.io` : ''
122
- const docsBase = githubRepoName && result . includeGithubPages ? `/${ githubRepoName } /` : '/'
123
- const homepageUrl = githubPagesOrigin && result . includeGithubPages ? `${ githubPagesOrigin } ${ docsBase } ` : githubUrl
102
+ const githubPagesOrigin = githubUserName && includeGithubPages ? `https://${ githubUserName } .github.io` : ''
103
+ const docsBase = githubRepoName && includeGithubPages ? `/${ githubRepoName } /` : '/'
104
+ const homepageUrl = githubPagesOrigin && includeGithubPages ? `${ githubPagesOrigin } ${ docsBase } ` : githubUrl
124
105
125
106
const targetDirPath = path . join ( cwd , targetDirName )
126
107
@@ -149,10 +130,10 @@ async function init() {
149
130
githubPagesOrigin,
150
131
docsBase,
151
132
homepageUrl,
152
- includeDocs : result . includeDocs ,
153
- includeGithubPages : ! ! result . includeGithubPages ,
154
- includePlayground : result . includePlayground ,
155
- includeExamples : result . includeExamples
133
+ includeDocs,
134
+ includeGithubPages,
135
+ includePlayground,
136
+ includeExamples
156
137
}
157
138
158
139
copyTemplate ( 'base ', config )
0 commit comments