Skip to content

fix(install), validate package names #9582

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

Merged
merged 3 commits into from
Feb 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions scopes/workspace/install/install.cmd.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Command, CommandOptions } from '@teambit/cli';
import packageNameValidate from 'validate-npm-package-name';
import { WorkspaceDependencyLifecycleType } from '@teambit/dependency-resolver';
import { Logger } from '@teambit/logger';
import chalk from 'chalk';
Expand Down Expand Up @@ -85,6 +86,13 @@ export default class InstallCmd implements Command {
`--update-existing is deprecated, please omit it. "bit install" will update existing dependencies by default`
);
}

packages.forEach((pkg) => {
const pkgName = extractPackageName(pkg);
if (!packageNameValidate(pkgName).validForNewPackages) {
throw new Error(`the package name "${pkgName}" is invalid. please provide a valid package name.`);
}
});
this.logger.console(`Resolving component dependencies for workspace: '${chalk.cyan(this.workspace.name)}'`);
const installOpts: WorkspaceInstallOptions = {
lifecycleType: options.addMissingPeers ? 'peer' : options.type,
Expand Down Expand Up @@ -150,3 +158,26 @@ export function getAnotherInstallRequiredOutput(recurringInstall = false, oldNon
const msg = `${firstPart}${suggestRecurringInstall}\n${envsStr}\n${docsLink}`;
return chalk.yellow(msg);
}

function extractPackageName(packageString: string): string {
const lastAtIndex = packageString.lastIndexOf('@');

// If no '@' is present, or it's at the start (meaning it's just a scope),
// we can assume there's no separate version part to remove.
if (lastAtIndex <= 0) {
return packageString;
}

// Get everything after the last '@'
const afterLastAt = packageString.slice(lastAtIndex + 1);

// If the substring after the last '@' contains a slash, it's part of the scoped package name,
// not a version. In that case, do not remove anything.
if (afterLastAt.includes('/')) {
return packageString;
}

// Otherwise, we assume that last '@' starts the version,
// so we remove that part.
return packageString.slice(0, lastAtIndex);
}
2 changes: 1 addition & 1 deletion workspace.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -573,7 +573,7 @@
"user-home": "^3.0.0",
"util": "^0.12.5",
"uuid": "8.3.2",
"validate-npm-package-name": "3.0.0",
"validate-npm-package-name": "6.0.0",
"version-selector-type": "3.0.0",
"vinyl": "2.2.1",
"vinyl-file": "3.0.0",
Expand Down