knip
Version:
Find and fix unused dependencies, exports and files in your TypeScript and JavaScript projects
79 lines (78 loc) • 1.99 kB
JavaScript
import parseArgs from 'minimist';
import { toBinary, 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 binary = toBinary(_binary);
const parsed = parseArgs(args, { string: ['cwd'] });
const [command, script] = parsed._;
if (command === 'x') {
const argsForX = args.filter(arg => arg !== 'x');
return [binary, ...resolveX(argsForX, options)];
}
const { manifest, cwd, fromArgs } = options;
if (command === 'run' && manifest.scriptNames.has(script))
return [binary];
if (manifest.scriptNames.has(command))
return [binary];
if (command !== 'run' && commands.has(command))
return [binary];
const filePath = command === 'run' ? script : command;
if (!filePath)
return [binary];
const _cwd = parsed.cwd ? join(cwd, parsed.cwd) : cwd;
const resolved = _resolveSync(isAbsolute(filePath) ? filePath : join(_cwd, filePath), _cwd);
if (resolved)
return [binary, toEntry(resolved)];
const dir = parsed.cwd ? join(cwd, parsed.cwd) : undefined;
const opts = dir ? { cwd: dir } : {};
return command === 'run' ? [binary] : [binary, ...fromArgs(args, opts)];
};