Skip to content

Commit

Permalink
Throw for duplicate package names (closes #339)
Browse files Browse the repository at this point in the history
  • Loading branch information
webpro committed Nov 28, 2023
1 parent c7234b7 commit 6ad4ced
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions packages/knip/src/ConfigurationChief.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { existsSync } from 'node:fs';
import mapWorkspaces from '@npmcli/map-workspaces';
import { createPkgGraph } from '@pnpm/workspace.pkgs-graph';
import { createPkgGraph, type Manifest } from '@pnpm/workspace.pkgs-graph';

Check failure on line 3 in packages/knip/src/ConfigurationChief.ts

View workflow job for this annotation

GitHub Actions / ubuntu-latest (Node v18)

'Manifest' is defined but never used

Check failure on line 3 in packages/knip/src/ConfigurationChief.ts

View workflow job for this annotation

GitHub Actions / ubuntu-latest (Node v20)

'Manifest' is defined but never used
import micromatch from 'micromatch';
import { ConfigurationValidator } from './ConfigurationValidator.js';
import { ROOT_WORKSPACE_NAME, DEFAULT_EXTENSIONS, KNIP_CONFIG_LOCATIONS } from './constants.js';
Expand Down Expand Up @@ -246,7 +246,7 @@ export class ConfigurationChief {
.map(dir => join(this.cwd, dir));

this.availableWorkspaceManifests = this.getAvailableWorkspaceManifests(this.availableWorkspaceDirs);
this.availableWorkspacePkgNames = new Set(this.availableWorkspaceManifests.map(w => w.manifest.name));
this.availableWorkspacePkgNames = this.getAvailableWorkspacePkgNames(this.availableWorkspaceManifests);
this.workspacesGraph = createPkgGraph(this.availableWorkspaceManifests);
this.enabledWorkspaces = this.getEnabledWorkspaces();
}
Expand Down Expand Up @@ -316,6 +316,16 @@ export class ConfigurationChief {
});
}

private getAvailableWorkspacePkgNames(availableWorkspaceManifests: { dir: string; manifest: PackageJson }[]) {
const pkgNames = new Set<string>();
for (const { dir, manifest } of availableWorkspaceManifests) {
if (!manifest.name) throw new ConfigurationError(`Missing package name in ${join(dir, 'package.json')}`);
if (pkgNames.has(manifest.name)) throw new ConfigurationError(`Duplicate package name: ${manifest.name}`);
pkgNames.add(manifest.name);
}
return pkgNames;
}

private getEnabledWorkspaces() {
if (workspaceArg && !existsSync(workspaceArg)) {
throw new ConfigurationError(`Directory does not exist: ${workspaceArg}`);
Expand Down

0 comments on commit 6ad4ced

Please sign in to comment.