@@ -148,6 +148,18 @@ function processArgs(): Args {
148
148
}
149
149
}
150
150
151
+ function isValidPackageName ( packageName ) {
152
+ // This is a bit stricter than npm requires, but we use the package name
153
+ // to generate various other things, like directory names and the global
154
+ // variable name, so we insist on having a 'safe' first character.
155
+ return / ^ ( @ [ a - z 0 - 9 - * ~ ] [ a -z 0 -9 -* _ .~ ] * \/ ) ? [ a - z _ ] [ a - z 0 - 9 - _ .~ ] * $ / . test ( packageName )
156
+ }
157
+
158
+ function isValidDirName ( dirName : string ) {
159
+ // This might be stricter than necessary, but it should be fine for real use cases
160
+ return / ^ [ a - zA - Z_ ] [ \w- . ~ ] * $ / . test ( dirName )
161
+ }
162
+
151
163
async function init ( ) {
152
164
const cwd = process . cwd ( )
153
165
@@ -165,7 +177,7 @@ async function init() {
165
177
166
178
const scopedPackageName = await textPrompt ( 'Package name' , '' )
167
179
168
- if ( ! / ^ ( @ [ a - z ] [ a - z 0 - 9 - ] * \/ ) ? [ a - z ] [ a - z 0 - 9 - _ . ] * $ / . test ( scopedPackageName ) ) {
180
+ if ( ! isValidPackageName ( scopedPackageName ) ) {
169
181
console . log ( 'Invalid package name: ' + scopedPackageName )
170
182
process . exit ( 1 )
171
183
}
@@ -174,7 +186,7 @@ async function init() {
174
186
175
187
const targetDirName = await textPrompt ( 'Target directory' , unscopedPackageName )
176
188
177
- if ( targetDirName !== '.' && ! / ^ [ \w - ] + $ / . test ( targetDirName ) ) {
189
+ if ( targetDirName !== '.' && ! isValidDirName ( targetDirName ) ) {
178
190
console . log ( 'Invalid directory name: ' + targetDirName )
179
191
process . exit ( 1 )
180
192
}
@@ -194,7 +206,7 @@ async function init() {
194
206
195
207
const mainPackageDirName = await textPromptIf ( extended , 'Main package directory' , unscopedPackageName )
196
208
197
- if ( ! / ^ [ \w - ] + $ / . test ( mainPackageDirName ) ) {
209
+ if ( ! isValidDirName ( mainPackageDirName ) ) {
198
210
console . log ( 'Invalid directory name: ' + mainPackageDirName )
199
211
process . exit ( 1 )
200
212
}
0 commit comments