UNPKG

knip

Version:

Find and fix unused dependencies, exports and files in your TypeScript and JavaScript projects

78 lines (77 loc) 1.88 kB
import parseArgs from 'minimist'; import { toEntry } from "../../util/input.js"; import { isAbsolute, join } from "../../util/path.js"; import { _resolveSync } from "../../util/resolve.js"; import { resolveX } from "./bunx.js"; const commands = new Set([ 'add', 'audit', 'auth', 'build', 'c', 'ci', 'cloud', 'completions', 'config', 'create', 'deploy', 'discord', 'exec', 'feedback', 'fuzzilli', 'getcompletes', 'help', 'i', 'info', 'init', 'install', 'link', 'list', 'login', 'logout', 'outdated', 'patch', 'patch-commit', 'pm', 'prune', 'publish', 'r', 'remove', 'repl', 'rm', 'run', 'test', 'unlink', 'uninstall', 'update', 'upgrade', 'use', 'whoami', 'why', 'x', ]); export const resolve = (_binary, args, options) => { const parsed = parseArgs(args, { string: ['cwd'] }); const [command, script] = parsed._; if (command === 'x') { const argsForX = args.filter(arg => arg !== 'x'); return resolveX(argsForX, options); } const { manifestScriptNames, cwd, fromArgs } = options; if (command === 'run' && manifestScriptNames.has(script)) return []; if (manifestScriptNames.has(command)) return []; if (command !== 'run' && commands.has(command)) return []; const filePath = command === 'run' ? script : command; if (!filePath) return []; const _cwd = parsed.cwd ? join(cwd, parsed.cwd) : cwd; const resolved = _resolveSync(isAbsolute(filePath) ? filePath : join(_cwd, filePath), _cwd); if (resolved) return [toEntry(resolved)]; const dir = parsed.cwd ? join(cwd, parsed.cwd) : undefined; const opts = dir ? { cwd: dir } : {}; return command === 'run' ? [] : fromArgs(args, opts); };