From 42dbfce5605cca92fd9402d96df712ee2bf899c9 Mon Sep 17 00:00:00 2001 From: Lars Kappert Date: Thu, 30 Nov 2023 09:05:49 +0100 Subject: [PATCH] Don't complain about `ignoredWorkspaces` without a `package.json` --- .../knip/fixtures/workspaces-ignored/package.json | 3 ++- .../fixtures/workspaces-ignored/packages/g/main.c | 0 packages/knip/src/ConfigurationChief.ts | 11 +++++++---- packages/knip/src/util/fs.ts | 5 +++++ 4 files changed, 14 insertions(+), 5 deletions(-) create mode 100644 packages/knip/fixtures/workspaces-ignored/packages/g/main.c diff --git a/packages/knip/fixtures/workspaces-ignored/package.json b/packages/knip/fixtures/workspaces-ignored/package.json index f79bd12a3..9cc64b9b5 100644 --- a/packages/knip/fixtures/workspaces-ignored/package.json +++ b/packages/knip/fixtures/workspaces-ignored/package.json @@ -4,7 +4,8 @@ "ignoreWorkspaces": [ "packages/c", "packages/d*", - "packages/f" + "packages/f", + "packages/g" ] } } diff --git a/packages/knip/fixtures/workspaces-ignored/packages/g/main.c b/packages/knip/fixtures/workspaces-ignored/packages/g/main.c new file mode 100644 index 000000000..e69de29bb diff --git a/packages/knip/src/ConfigurationChief.ts b/packages/knip/src/ConfigurationChief.ts index ab57b6be2..8bafa6d51 100644 --- a/packages/knip/src/ConfigurationChief.ts +++ b/packages/knip/src/ConfigurationChief.ts @@ -10,7 +10,7 @@ import { arrayify } from './util/array.js'; import parsedArgValues from './util/cli-arguments.js'; import { partitionCompilers } from './util/compilers.js'; import { ConfigurationError, LoaderError } from './util/errors.js'; -import { findFile, loadJSON } from './util/fs.js'; +import { findFile, isDirectory, isFile, loadJSON } from './util/fs.js'; import { getIncludedIssueTypes } from './util/get-included-issue-types.js'; import { _dirGlob } from './util/glob.js'; import { _load } from './util/loader.js'; @@ -468,8 +468,11 @@ export class ConfigurationChief { public getUnusedIgnoredWorkspaces() { const ignoredWorkspaceNames = this.config.ignoreWorkspaces; const workspaceNames = [...this.manifestWorkspaces.keys(), ...this.additionalWorkspaceNames]; - return ignoredWorkspaceNames.filter( - ignoredWorkspaceName => !workspaceNames.some(name => micromatch.isMatch(name, ignoredWorkspaceName)) - ); + return ignoredWorkspaceNames + .filter(ignoredWorkspaceName => !workspaceNames.some(name => micromatch.isMatch(name, ignoredWorkspaceName))) + .filter(ignoredWorkspaceName => { + const dir = join(this.cwd, ignoredWorkspaceName); + return !isDirectory(dir) || isFile(join(dir, 'package.json')); + }); } } diff --git a/packages/knip/src/util/fs.ts b/packages/knip/src/util/fs.ts index 111c86c05..28081e36d 100644 --- a/packages/knip/src/util/fs.ts +++ b/packages/knip/src/util/fs.ts @@ -5,6 +5,11 @@ import stripJsonComments from 'strip-json-comments'; import { LoaderError } from './errors.js'; import { dirname, join } from './path.js'; +export const isDirectory = (filePath: string) => { + const stat = statSync(filePath, { throwIfNoEntry: false }); + return stat !== undefined && stat.isDirectory(); +}; + export const isFile = (filePath: string) => { const stat = statSync(filePath, { throwIfNoEntry: false }); return stat !== undefined && stat.isFile();