Skip to content

Commit 8e1dca6

Browse files
committedNov 28, 2023
Throw for duplicate package names (closes #339)
1 parent c7234b7 commit 8e1dca6

File tree

7 files changed

+17
-2
lines changed

7 files changed

+17
-2
lines changed
 

‎packages/knip/fixtures/dependencies/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{
2+
"name": "@fixtures/dependencies",
23
"scripts": {
34
"start": "start-server",
45
"test": "jest"

‎packages/knip/fixtures/plugins/ava/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{
2+
"name": "@fixtures/ava",
23
"ava": {
34
"files": [
45
"**/*.test.*"

‎packages/knip/fixtures/plugins/ava2/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{
2+
"name": "@fixtures/ava2",
23
"knip": {
34
"ava": true
45
}

‎packages/knip/fixtures/plugins/ava3/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{
2+
"name": "@fixtures/ava3",
23
"knip": {
34
"ava": true
45
}

‎packages/knip/fixtures/tsconfig-extends/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"name": "@fixtures/typescript-extends",
2+
"name": "@fixtures/tsconfig-extends",
33
"workspaces": [
44
"packages/*"
55
],

‎packages/knip/fixtures/tsconfig-extends/packages/frontend/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{
2+
"name": "@tsconfig-extends/frontend",
23
"dependencies": {
34
"hastscript": "*"
45
},

‎packages/knip/src/ConfigurationChief.ts

+11-1
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ export class ConfigurationChief {
246246
.map(dir => join(this.cwd, dir));
247247

248248
this.availableWorkspaceManifests = this.getAvailableWorkspaceManifests(this.availableWorkspaceDirs);
249-
this.availableWorkspacePkgNames = new Set(this.availableWorkspaceManifests.map(w => w.manifest.name));
249+
this.availableWorkspacePkgNames = this.getAvailableWorkspacePkgNames(this.availableWorkspaceManifests);
250250
this.workspacesGraph = createPkgGraph(this.availableWorkspaceManifests);
251251
this.enabledWorkspaces = this.getEnabledWorkspaces();
252252
}
@@ -316,6 +316,16 @@ export class ConfigurationChief {
316316
});
317317
}
318318

319+
private getAvailableWorkspacePkgNames(availableWorkspaceManifests: { dir: string; manifest: PackageJson }[]) {
320+
const pkgNames = new Set<string>();
321+
for (const { dir, manifest } of availableWorkspaceManifests) {
322+
if (!manifest.name) throw new ConfigurationError(`Missing package name in ${join(dir, 'package.json')}`);
323+
if (pkgNames.has(manifest.name)) throw new ConfigurationError(`Duplicate package name: ${manifest.name}`);
324+
pkgNames.add(manifest.name);
325+
}
326+
return pkgNames;
327+
}
328+
319329
private getEnabledWorkspaces() {
320330
if (workspaceArg && !existsSync(workspaceArg)) {
321331
throw new ConfigurationError(`Directory does not exist: ${workspaceArg}`);

0 commit comments

Comments
 (0)
Please sign in to comment.