Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(cli): handle dot input and validate npm package names #41

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

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