UNPKG

knip

Version:

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

106 lines (105 loc) 2.27 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', 'ci', 'clean-install', 'clean', 'config', 'dedupe', 'deploy', 'dlx', 'docs', 'doctor', 'env', 'fetch', 'find-hash', 'home', 'i', 'ic', 'ignored-builds', 'import', 'init', 'install-clean', 'install-test', 'install', 'it', 'la', 'licenses', 'link', 'list', 'll', 'ln', 'ls', 'outdated', 'pack-app', 'pack', 'patch-commit', 'patch-remove', 'patch', 'peers', 'ping', 'pkg', 'pm', 'prepare', 'prune', 'publish', 'purge', 'rb', 'rebuild', 'remove', 'rm', 'root', 'rt', 'run', 'runtime', 'sbom', 'self-update', 'server', 'setup', 'start', 'store', 't', 'test', 'tst', 'un', 'uninstall', 'unlink', 'up', 'update', 'upgrade', 'version', 'why', 'with', ]; 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 { manifest, 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 = manifest.scriptNames.has(command); if (isScript || commands.includes(command)) return childInputs; return command && isValidBinary(command) ? [toBinary(command)] : []; };