Skip to content

Commit 9081153

Browse files
committed
Relax some of the naming constraints
1 parent 7290e46 commit 9081153

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

src/index.ts

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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-z0-9-*~][a-z0-9-*_.~]*\/)?[a-z_][a-z0-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+
151163
async 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-z0-9-]*\/)?[a-z][a-z0-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

Comments
 (0)