Skip to content

Commit

Permalink
Improve pm binary/package handling
Browse files Browse the repository at this point in the history
  • Loading branch information
webpro committed Jan 17, 2025
1 parent 96b6783 commit 4b78b61
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 17 deletions.
3 changes: 3 additions & 0 deletions packages/knip/src/binaries/package-manager/pnpm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,13 @@ export const resolve: BinaryResolver = (_binary, args, options) => {
}

const { manifestScriptNames, fromArgs } = options;

if (manifestScriptNames.has(command) || commands.includes(command)) return [];

if (command === 'exec') {
if (parsed._.length > 2) return [toBinary(binary), ...fromArgs(parsed._.slice(1))];
return [toBinary(binary)];
}

return command ? [toBinary(command)] : [];
};
44 changes: 37 additions & 7 deletions packages/knip/src/binaries/package-manager/yarn.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import parseArgs from 'minimist';
import type { BinaryResolver } from '../../types/config.js';
import { toBinary } from '../../util/input.js';
import type { BinaryResolver, BinaryResolverOptions } from '../../types/config.js';
import { isBinary, isDependency, toBinary, toDependency } from '../../util/input.js';
import { stripVersionFromSpecifier } from '../../util/modules.js';
import { join } from '../../util/path.js';

// https://yarnpkg.com/cli
Expand Down Expand Up @@ -39,14 +40,43 @@ const commands = [
'workspaces',
];

export const resolve: BinaryResolver = (_binary, args, { manifestScriptNames, fromArgs, cwd, rootCwd }) => {
const resolveDlx = (args: string[], options: BinaryResolverOptions) => {
const parsed = parseArgs(args, {
boolean: ['quiet'],
alias: { package: 'p', quiet: 'q' },
});
const packageSpecifier = parsed._[0];
const specifier = packageSpecifier ? stripVersionFromSpecifier(packageSpecifier) : '';
const packages = parsed.package && !parsed.yes ? [parsed.package].flat().map(stripVersionFromSpecifier) : [];
const command = specifier ? options.fromArgs(parsed._) : [];
return [...packages.map(id => toDependency(id)), ...command].map(id =>
isDependency(id) || isBinary(id) ? Object.assign(id, { optional: true }) : id
);
};

export const resolve: BinaryResolver = (_binary, args, options) => {
const { manifestScriptNames, fromArgs, cwd, rootCwd } = options;
const parsed = parseArgs(args, { boolean: ['top-level'], string: ['cwd'] });
const [command, binary] = parsed._;
const dir = parsed['top-level'] ? rootCwd : parsed.cwd ? join(cwd, parsed.cwd) : undefined;
if ((!dir && manifestScriptNames.has(command)) || commands.includes(command)) return [];
if (!dir && command === 'run' && manifestScriptNames.has(binary)) return [];
const [command, binary] = parsed._;

if (command === 'run') {
if (manifestScriptNames.has(binary)) return [];
const bin = toBinary(binary, { optional: true });
if (dir) Object.assign(bin, { dir });
return [bin];
}

if (command === 'node') return fromArgs(parsed._);
const bin = command === 'run' || command === 'exec' ? toBinary(binary) : toBinary(command);

if (command === 'dlx') {
const argsForDlx = args.filter(arg => arg !== 'dlx');
return resolveDlx(argsForDlx, options);
}

if ((!dir && manifestScriptNames.has(command)) || commands.includes(command)) return [];

const bin = command === 'exec' ? toBinary(binary) : toBinary(command);
if (dir) Object.assign(bin, { dir });
return [bin];
};
25 changes: 15 additions & 10 deletions packages/knip/test/util/get-inputs-from-scripts.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,13 +156,6 @@ test('getInputsFromScripts (npx)', () => {
t('npx tsx ./main.ts -- build', [toBinary('tsx'), ts]);
});

test('getInputsFromScripts (pnpx/pnpm dlx)', () => {
t('pnpx pkg', [toDependency('pkg', optional)]);
const inputs = [toDependency('cowsay', optional), toDependency('lolcatjs', optional), toBinary('echo'), toBinary('cowsay'), toBinary('lolcatjs')];
t('pnpx --package cowsay --package lolcatjs -c \'echo "hi pnpm" | cowsay | lolcatjs\'', inputs);
t('pnpm --package cowsay --package lolcatjs -c dlx \'echo "hi pnpm" | cowsay | lolcatjs\'', inputs);
});

test('getInputsFromScripts (bunx/bun x)', () => {
t('bunx pkg', [toDependency('pkg', optional)]);
t('bunx cowsay "Hello world!"', [toDependency('cowsay', optional)]);
Expand All @@ -184,17 +177,29 @@ test('getInputsFromScripts (pnpm)', () => {
t('pnpm --silent run program script.js', [], pkgScripts);
});

test('getInputsFromScripts (pnpx/pnpm dlx)', () => {
t('pnpx pkg', [toDependency('pkg', optional)]);
const inputs = [toDependency('cowsay', optional), toDependency('lolcatjs', optional), toBinary('echo'), toBinary('cowsay'), toBinary('lolcatjs')];
t('pnpx --package cowsay --package lolcatjs -c \'echo "hi pnpm" | cowsay | lolcatjs\'', inputs);
t('pnpm --package cowsay --package lolcatjs -c dlx \'echo "hi pnpm" | cowsay | lolcatjs\'', inputs);
});

test('getInputsFromScripts (yarn)', () => {
t('yarn exec program', [toBinary('program')]);
t('yarn run program', [toBinary('program')]);
t('yarn run program', [toBinary('program', optional)]);
t('yarn program', [toBinary('program')]);
t('yarn run program', [], pkgScripts);
t('yarn program', [], pkgScripts);
t('yarn dlx pkg', []);
t('yarn --package=pkg-a -p pkg-b dlx pkg', []);
t('yarn node script.js', [toBinary('node'), toDeferResolveEntry('script.js')]);
});

test('getInputsFromScripts (yarn dlx)', () => {
t('yarn dlx pkg', [toBinary('pkg', optional)]);
t('yarn dlx -p typescript -p ts-node ts-node -T -e "console.log(\'hello!\')"', [toDependency('typescript', optional), toDependency('ts-node', optional), toBinary('ts-node', optional)]);
t('yarn dlx -p ts-node ts-node ./main.ts', [toDependency('ts-node', optional), toBinary('ts-node', optional), ts]);
t('yarn --package=pkg-a -p pkg-b dlx pkg', [toDependency('pkg-a', optional), toDependency('pkg-b', optional), toBinary('pkg', optional)]);
});

test('getInputsFromScripts (rollup)', () => {
t('rollup --watch --watch.onEnd="node ./script.js"', [toBinary('rollup'), toBinary('node'), js]);
t('rollup -p ./require.js', [toBinary('rollup'), req]);
Expand Down

0 comments on commit 4b78b61

Please sign in to comment.