@@ -27,11 +27,13 @@ async function prompt(options: Omit<PromptObject, 'name'>) {
27
27
}
28
28
29
29
async function textPrompt ( message : string , initial ?: string ) : Promise < string > {
30
- return prompt ( {
30
+ const resp = await prompt ( {
31
31
type : 'text' ,
32
32
message,
33
33
initial
34
34
} )
35
+
36
+ return resp . trim ( )
35
37
}
36
38
37
39
async function togglePrompt ( message : string , initial = false , active = 'Yes' , inactive = 'No' ) : Promise < boolean > {
@@ -72,14 +74,39 @@ async function init() {
72
74
const scopedPackageName = await textPrompt ( 'Package name' , '@skirtle/test-project' )
73
75
74
76
// TODO: Tightening this check, e.g. for hyphen positions
75
- if ( ! / @ [ a - z 0 - 9 - ] + \/ [ a - z 0 - 9 - ] + / . test ( scopedPackageName ) ) {
77
+ if ( ! / ^ @ [ a - z 0 - 9 - ] + \/ [ a - z 0 - 9 - ] + $ / . test ( scopedPackageName ) ) {
76
78
console . log ( 'Invalid package name: ' + scopedPackageName )
77
79
process . exit ( 1 )
78
80
}
79
81
82
+ const unscopedPackageName = scopedPackageName . replace ( / .* \/ / , '' )
83
+ const shortUnscopedPackageName = unscopedPackageName . replace ( / ^ v u e - / , '' )
84
+ const projectName = unscopedPackageName . replace ( / - + / g, ' ' ) . trim ( ) . split ( ' ' ) . map ( s => s [ 0 ] . toUpperCase ( ) + s . slice ( 1 ) ) . join ( ' ' )
85
+ const globalVariableName = projectName . replace ( / / g, '' )
86
+
87
+ const targetDirName = await textPrompt ( 'Target directory' , unscopedPackageName )
88
+
89
+ if ( targetDirName !== '.' && ! / ^ [ \w - ] + $ / . test ( targetDirName ) ) {
90
+ console . log ( 'Invalid directory name: ' + targetDirName )
91
+ process . exit ( 1 )
92
+ }
93
+
94
+ const targetDirPath = path . join ( cwd , targetDirName )
95
+
96
+ if ( targetDirName === '.' ) {
97
+ // TODO: Check files properly and prompt accordingly
98
+ if ( fs . existsSync ( path . join ( targetDirPath , 'package.json' ) ) ) {
99
+ console . log ( 'Target directory already contains package.json' )
100
+ }
101
+ } else {
102
+ if ( fs . existsSync ( targetDirPath ) ) {
103
+ console . log ( 'Target directory already exists' )
104
+ }
105
+ }
106
+
80
107
const githubPath = await textPrompt ( 'GitHub path, e.g. skirtles-code/test-project (optional)' )
81
108
82
- if ( githubPath && ! / [ \w - ] + \/ [ \w - ] + / . test ( githubPath ) ) {
109
+ if ( githubPath && ! / ^ [ \w - ] + \/ [ \w - ] + $ / . test ( githubPath ) ) {
83
110
console . log ( 'Invalid GitHub path: ' + githubPath )
84
111
process . exit ( 1 )
85
112
}
@@ -89,12 +116,6 @@ async function init() {
89
116
const includePlayground = await togglePrompt ( 'Include playground application for development?' , true )
90
117
const includeExamples = await togglePrompt ( 'Include example code?' , true , 'Yes' , 'No, just configs' )
91
118
92
- const unscopedPackageName = scopedPackageName . replace ( / .* \/ / , '' )
93
- const shortUnscopedPackageName = unscopedPackageName . replace ( / ^ v u e - / , '' )
94
- const projectName = unscopedPackageName . replace ( / - + / g, ' ' ) . trim ( ) . split ( ' ' ) . map ( s => s [ 0 ] . toUpperCase ( ) + s . slice ( 1 ) ) . join ( ' ' )
95
- const globalVariableName = projectName . replace ( / / g, '' )
96
- const targetDirName = unscopedPackageName
97
-
98
119
const [ githubUserName , githubRepoName ] = ( githubPath || '/' ) . split ( '/' )
99
120
const githubUrl = githubPath ? `https://github.com/${ githubPath } ` : ''
100
121
const githubIssues = githubPath ? `${ githubUrl } /issues` : ''
@@ -103,15 +124,6 @@ async function init() {
103
124
const docsBase = githubRepoName && includeGithubPages ? `/${ githubRepoName } /` : '/'
104
125
const homepageUrl = githubPagesOrigin && includeGithubPages ? `${ githubPagesOrigin } ${ docsBase } ` : githubUrl
105
126
106
- const targetDirPath = path . join ( cwd , targetDirName )
107
-
108
- if ( fs . existsSync ( targetDirPath ) ) {
109
- console . log ( 'Target directory already exists' )
110
- } else {
111
- // TODO: Shouldn't need recursive once we're done
112
- fs . mkdirSync ( targetDirPath , { recursive : true } )
113
- }
114
-
115
127
const templateDirPath = path . resolve ( __dirname , 'template' )
116
128
117
129
const config : Config = {
@@ -153,7 +165,11 @@ async function init() {
153
165
console . log ( 'Project created ')
154
166
console . log ( 'Note: pnpm must be used as the package manager' )
155
167
console . log ( )
156
- console . log ( 'cd ' + targetDirName )
168
+
169
+ if ( targetDirName !== '.' ) {
170
+ console . log ( 'cd ' + targetDirName )
171
+ }
172
+
157
173
console . log ( 'pnpm install' )
158
174
console . log ( )
159
175
console . log ( `You should add a suitable license at ${ targetDirName } /packages/${ config . shortUnscopedPackageName } /LICENSE` )
0 commit comments