Skip to content

Commit

Permalink
Add --directory [dir] argument to run the process from a different dir
Browse files Browse the repository at this point in the history
  • Loading branch information
webpro committed Oct 17, 2023
1 parent 7f63c75 commit b331033
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions src/util/cli-arguments.ts
Expand Up @@ -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)
Expand Down Expand Up @@ -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' },
Expand Down
5 changes: 4 additions & 1 deletion 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;

Expand All @@ -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);
Expand Down

0 comments on commit b331033

Please sign in to comment.