UNPKG

knip

Version:

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

87 lines (86 loc) 2.04 kB
import parseArgs from 'minimist'; import { toBinary } from "../../util/input.js"; import { isValidBinary } from "../../util/modules.js"; import { resolveDlx } from "./pnpx.js"; const commands = [ 'add', 'approve-builds', 'audit', 'bin', 'cache', 'cat-file', 'cat-index', 'config', 'dedupe', 'deploy', 'dlx', 'doctor', 'env', 'fetch', 'find-hash', 'i', 'ignored-builds', 'import', 'init', 'install-test', 'install', 'it', 'licenses', 'link', 'list', 'ln', 'ls', 'outdated', 'pack', 'patch-commit', 'patch-remove', 'patch', 'prepare', 'prune', 'publish', 'rb', 'rebuild', 'remove', 'rm', 'root', 'run', 'self-update', 'server', 'setup', 'start', 'store', 't', 'test', 'tst', 'un', 'uninstall', 'unlink', 'up', 'update', 'upgrade', 'version', 'why', ]; export const resolve = (_binary, args, options) => { const parsed = parseArgs(args, { boolean: ['aggregate-output', 'if-present', 'parallel', 'recursive', 'reverse', 'shell-mode', 'silent', 'stream'], alias: { recursive: 'r', silent: 's', 'shell-mode': 'c', filter: 'F' }, '--': true, }); const [command] = parsed._; if (command === 'dlx') { const argsForDlx = args.filter(arg => arg !== 'dlx'); return resolveDlx(argsForDlx, options); } const { manifestScriptNames, fromArgs } = options; if (parsed.filter && !parsed.recursive) return []; const childInputs = parsed['--'] && parsed['--'].length > 0 ? fromArgs(parsed['--'], { knownBinsOnly: true }) : []; if (command === 'exec') { return childInputs.length > 0 ? childInputs : fromArgs(parsed._.slice(1)); } const isScript = manifestScriptNames.has(command); if (isScript || commands.includes(command)) return childInputs; return command && isValidBinary(command) ? [toBinary(command)] : []; };