@@ -148,6 +148,18 @@ function processArgs(): Args {
148148 }
149149}
150150
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+
151163async function init ( ) {
152164 const cwd = process . cwd ( )
153165
@@ -165,7 +177,7 @@ async function init() {
165177
166178 const scopedPackageName = await textPrompt ( 'Package name' , '' )
167179
168- if ( ! / ^ ( @ [ a - z ] [ a - z 0 - 9 - ] * \/ ) ? [ a - z ] [ a - z 0 - 9 - _ . ] * $ / . test ( scopedPackageName ) ) {
180+ if ( ! isValidPackageName ( scopedPackageName ) ) {
169181 console . log ( 'Invalid package name: ' + scopedPackageName )
170182 process . exit ( 1 )
171183 }
@@ -174,7 +186,7 @@ async function init() {
174186
175187 const targetDirName = await textPrompt ( 'Target directory' , unscopedPackageName )
176188
177- if ( targetDirName !== '.' && ! / ^ [ \w - ] + $ / . test ( targetDirName ) ) {
189+ if ( targetDirName !== '.' && ! isValidDirName ( targetDirName ) ) {
178190 console . log ( 'Invalid directory name: ' + targetDirName )
179191 process . exit ( 1 )
180192 }
@@ -194,7 +206,7 @@ async function init() {
194206
195207 const mainPackageDirName = await textPromptIf ( extended , 'Main package directory' , unscopedPackageName )
196208
197- if ( ! / ^ [ \w - ] + $ / . test ( mainPackageDirName ) ) {
209+ if ( ! isValidDirName ( mainPackageDirName ) ) {
198210 console . log ( 'Invalid directory name: ' + mainPackageDirName )
199211 process . exit ( 1 )
200212 }
0 commit comments