From 6c71399417991fb35177c8bcc4ae334408001edc Mon Sep 17 00:00:00 2001 From: Lars Kappert Date: Fri, 3 May 2024 18:53:20 +0200 Subject: [PATCH] Allow workspace patterns to be relative (fixes #617) --- packages/knip/fixtures/workspaces-tooling/package.json | 4 +--- packages/knip/src/ConfigurationChief.ts | 3 ++- packages/knip/src/util/map-workspaces.ts | 2 +- 3 files changed, 4 insertions(+), 5 deletions(-) diff --git a/packages/knip/fixtures/workspaces-tooling/package.json b/packages/knip/fixtures/workspaces-tooling/package.json index f29659320..d2f91b9ed 100644 --- a/packages/knip/fixtures/workspaces-tooling/package.json +++ b/packages/knip/fixtures/workspaces-tooling/package.json @@ -1,6 +1,4 @@ { "name": "@workspaces-tooling/root", - "workspaces": [ - "packages/*" - ] + "workspaces": ["./packages/*"] } diff --git a/packages/knip/src/ConfigurationChief.ts b/packages/knip/src/ConfigurationChief.ts index bfedff9b3..b49389e22 100644 --- a/packages/knip/src/ConfigurationChief.ts +++ b/packages/knip/src/ConfigurationChief.ts @@ -267,11 +267,12 @@ export class ConfigurationChief { } private getListedWorkspaces() { - return this.manifest?.workspaces + const workspaces = this.manifest?.workspaces ? Array.isArray(this.manifest.workspaces) ? this.manifest.workspaces : this.manifest.workspaces.packages ?? [] : []; + return workspaces.map(pattern => pattern.replace(/(?<=!?)\.\//, '')); } private getIgnoredWorkspacePatterns() { diff --git a/packages/knip/src/util/map-workspaces.ts b/packages/knip/src/util/map-workspaces.ts index 76bb2ba82..58141c55c 100644 --- a/packages/knip/src/util/map-workspaces.ts +++ b/packages/knip/src/util/map-workspaces.ts @@ -9,7 +9,7 @@ import { join } from './path.js'; import { _require } from './require.js'; export default async function mapWorkspaces(cwd: string, workspaces: string[]) { - const [negatedPatterns, patterns] = partition(workspaces, p => p.match(/^!+/)); + const [negatedPatterns, patterns] = partition(workspaces, p => p.match(/^!/)); const byPkgDir = new Map(); const byPkgName = new Map();