diff --git a/README.md b/README.md index 66ac60948..66fc6a174 100644 --- a/README.md +++ b/README.md @@ -694,6 +694,7 @@ $ npx knip --help --strict Consider only direct dependencies of workspace (not devDependencies, not other workspaces) --ignore-internal Ignore exports with tag @internal (JSDoc/TSDoc) -W, --workspace [dir] Analyze a single workspace (default: analyze all configured workspaces) + --directory [dir] Run process from a different directory (default: cwd) --no-gitignore Don't use .gitignore --include Report only provided issue type(s), can be comma-separated or repeated (1) --exclude Exclude provided issue type(s) from report, can be comma-separated or repeated (1) diff --git a/src/util/cli-arguments.ts b/src/util/cli-arguments.ts index 300f10a83..a9ae2b2ae 100644 --- a/src/util/cli-arguments.ts +++ b/src/util/cli-arguments.ts @@ -11,6 +11,7 @@ Options: --strict Consider only direct dependencies of workspace (not devDependencies, not other workspaces) --ignore-internal Ignore exports with tag @internal (JSDoc/TSDoc) -W, --workspace [dir] Analyze a single workspace (default: analyze all configured workspaces) + --directory [dir] Run process from a different directory (default: cwd) --no-gitignore Don't use .gitignore --include Report only provided issue type(s), can be comma-separated or repeated (1) --exclude Exclude provided issue type(s) from report, can be comma-separated or repeated (1) @@ -52,6 +53,7 @@ try { debug: { type: 'boolean', short: 'd' }, 'debug-file-filter': { type: 'string' }, dependencies: { type: 'boolean' }, + directory: { type: 'string' }, exclude: { type: 'string', multiple: true }, exports: { type: 'boolean' }, help: { type: 'boolean', short: 'h' }, diff --git a/src/util/path.ts b/src/util/path.ts index 8473efa37..2b68dfab1 100644 --- a/src/util/path.ts +++ b/src/util/path.ts @@ -1,5 +1,8 @@ // eslint-disable-next-line n/no-restricted-import import path from 'node:path'; +import parsedArgValues from './cli-arguments.js'; + +const { directory } = parsedArgValues; const isAbsolute = path.isAbsolute; @@ -11,7 +14,7 @@ export const join = path.posix.join; export const toPosix = (value: string) => value.split(path.sep).join(path.posix.sep); -export const cwd = toPosix(process.cwd()); +export const cwd = directory ? path.posix.resolve(directory) : toPosix(process.cwd()); export const resolve = (...paths: string[]) => paths.length === 1 ? path.posix.join(cwd, paths[0]) : path.posix.resolve(...paths);