Skip to content

Conversation

@ProdigyRahul
Copy link

@ProdigyRahul ProdigyRahul commented Feb 8, 2025

image

added:

// NPM package name validation regex
const VALID_PACKAGE_NAME = /^(?:(?:@(?:[a-z0-9-][a-z0-9-.])?/)?[a-z0-9-._~][a-z0-9-]|[a-z0-9-])$/

// Function to sanitize package name
const sanitizePackageName = (name: string): string => {
return name
.toLowerCase()
.replace(/[^a-z0-9-~]/g, '-')
.replace(/^[._]/, '')
.replace(/^-+|-+$/g, '')
|| 'jstack-app'
}

and also added this validation:
// Handle "." input by using current directory name
if (value === ".") {
const currentDir = path.basename(process.cwd())
value = currentDir
}

    // Validate against npm package name rules
    if (!VALID_PACKAGE_NAME.test(value)) {
      const sanitized = sanitizePackageName(value)
      return `Invalid package name. Try: ${sanitized}`
    }

@ProdigyRahul
Copy link
Author

ProdigyRahul commented Feb 8, 2025

I've submitted a PR to a public open-source repo. Hoping they merge it. >_< @joschan21

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant