Skip to content

Commit 5a3177f

Browse files
committedJan 15, 2025·
Reduce noise
1 parent 03abffd commit 5a3177f

File tree

8 files changed

+76
-79
lines changed

8 files changed

+76
-79
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,27 @@
11
import parseArgs from 'minimist';
22
import type { BinaryResolver } from '../../types/config.js';
33
import { isFile } from '../../util/fs.js';
4-
import { toBinary, toEntry } from '../../util/input.js';
4+
import { toEntry } from '../../util/input.js';
55
import { isAbsolute, join } from '../../util/path.js';
66
import { resolveX } from './bunx.js';
77

88
const commands = ['add', 'create', 'init', 'install', 'link', 'pm', 'remove', 'run', 'test', 'update', 'upgrade', 'x'];
99

10-
export const resolve: BinaryResolver = (binary, args, options) => {
11-
const bin = toBinary(binary);
10+
export const resolve: BinaryResolver = (_binary, args, options) => {
1211
const parsed = parseArgs(args);
1312
const [command, script] = parsed._;
1413

1514
if (command === 'x') {
1615
const argsForX = args.filter(arg => arg !== 'x');
17-
return [bin, ...resolveX(argsForX, options)];
16+
return resolveX(argsForX, options);
1817
}
1918

2019
const { manifestScriptNames, cwd, fromArgs } = options;
2120

22-
if (command === 'run' && manifestScriptNames.has(script)) return [bin];
23-
if (manifestScriptNames.has(command) || commands.includes(command)) return [bin];
21+
if (command === 'run' && manifestScriptNames.has(script)) return [];
22+
if (manifestScriptNames.has(command) || commands.includes(command)) return [];
2423
const filePath = command === 'run' ? script : command;
2524
const absFilePath = isAbsolute(filePath) ? filePath : join(cwd, filePath);
26-
if (isFile(absFilePath)) return [bin, toEntry(absFilePath)];
27-
return [bin, ...fromArgs(args)];
25+
if (isFile(absFilePath)) return [toEntry(absFilePath)];
26+
return fromArgs(args);
2827
};

‎packages/knip/src/binaries/package-manager/bunx.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import parseArgs from 'minimist';
22
import type { BinaryResolver, BinaryResolverOptions } from '../../types/config.js';
3-
import { toBinary, toDependency } from '../../util/input.js';
3+
import { toDependency } from '../../util/input.js';
44
import { stripVersionFromSpecifier } from '../../util/modules.js';
55
import { argsFrom } from '../util.js';
66

@@ -17,5 +17,5 @@ export const resolveX = (args: string[], options: BinaryResolverOptions) => {
1717
};
1818

1919
export const resolve: BinaryResolver = (_binary, args, options) => {
20-
return [toBinary(_binary), ...resolveX(args, options)];
20+
return resolveX(args, options);
2121
};
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
import parseArgs from 'minimist';
22
import type { BinaryResolver } from '../../types/config.js';
3-
import { toBinary } from '../../util/input.js';
43

5-
export const resolve: BinaryResolver = (binary, args, options) => {
4+
export const resolve: BinaryResolver = (_binary, args, options) => {
65
const { fromArgs } = options;
76
const parsed = parseArgs(args);
87
const [command] = parsed._;
9-
return [toBinary(binary), ...(command !== 'exec' ? [] : fromArgs(parsed._.slice(1)))];
8+
return command !== 'exec' ? [] : fromArgs(parsed._.slice(1));
109
};

‎packages/knip/src/binaries/package-manager/npx.ts

-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ export const resolve: BinaryResolver = (_binary, args, options) => {
2424
const specifiers = dependency && !parsed.yes ? [dependency] : [];
2525

2626
return [
27-
toBinary(_binary),
2827
...specifiers,
2928
...packages.map(id => toDependency(id, { optional: true })),
3029
...command,

‎packages/knip/src/binaries/package-manager/pnpm.ts

+7-8
Original file line numberDiff line numberDiff line change
@@ -57,24 +57,23 @@ const commands = [
5757
'why',
5858
];
5959

60-
export const resolve: BinaryResolver = (binary, args, options) => {
61-
const bin = toBinary(binary);
60+
export const resolve: BinaryResolver = (_binary, args, options) => {
6261
const parsed = parseArgs(args, {
6362
boolean: ['recursive', 'silent', 'shell-mode'],
6463
alias: { recursive: 'r', silent: 's', 'shell-mode': 'c' },
6564
});
66-
const [command, __binary] = parsed._;
65+
const [command, binary] = parsed._;
6766

6867
if (command === 'dlx') {
6968
const argsForDlx = args.filter(arg => arg !== 'dlx');
70-
return [toBinary(binary), ...resolveDlx(argsForDlx, options)];
69+
return resolveDlx(argsForDlx, options);
7170
}
7271

7372
const { manifestScriptNames, fromArgs } = options;
74-
if (manifestScriptNames.has(command) || commands.includes(command)) return [bin];
73+
if (manifestScriptNames.has(command) || commands.includes(command)) return [];
7574
if (command === 'exec') {
76-
if (parsed._.length > 2) return [bin, toBinary(__binary), ...fromArgs(parsed._.slice(1))];
77-
return [bin, toBinary(__binary)];
75+
if (parsed._.length > 2) return [toBinary(binary), ...fromArgs(parsed._.slice(1))];
76+
return [toBinary(binary)];
7877
}
79-
return command ? [bin, toBinary(command)] : [bin];
78+
return command ? [toBinary(command)] : [];
8079
};

‎packages/knip/src/binaries/package-manager/pnpx.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import parseArgs from 'minimist';
22
import type { BinaryResolver, BinaryResolverOptions } from '../../types/config.js';
3-
import { toBinary, toDependency } from '../../util/input.js';
3+
import { toDependency } from '../../util/input.js';
44
import { stripVersionFromSpecifier } from '../../util/modules.js';
55

66
export const resolveDlx = (args: string[], options: BinaryResolverOptions) => {
@@ -17,5 +17,5 @@ export const resolveDlx = (args: string[], options: BinaryResolverOptions) => {
1717
};
1818

1919
export const resolve: BinaryResolver = (_binary, args, options) => {
20-
return [toBinary(_binary), ...resolveDlx(args, options)];
20+
return resolveDlx(args, options);
2121
};

‎packages/knip/src/binaries/package-manager/yarn.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ export const resolve: BinaryResolver = (_binary, args, { manifestScriptNames, fr
4545
const dir = parsed['top-level'] ? rootCwd : parsed.cwd ? join(cwd, parsed.cwd) : undefined;
4646
if ((!dir && manifestScriptNames.has(command)) || commands.includes(command)) return [];
4747
if (!dir && command === 'run' && manifestScriptNames.has(binary)) return [];
48-
if (command === 'run' || command === 'exec') return dir ? [{ ...toBinary(binary), dir }] : [toBinary(binary)];
4948
if (command === 'node') return fromArgs(parsed._);
50-
return command ? (dir ? [{ ...toBinary(command), dir }] : [toBinary(command)]) : [];
49+
const bin = command === 'run' || command === 'exec' ? toBinary(binary) : toBinary(command);
50+
if (dir) Object.assign(bin, { dir });
51+
return [bin];
5152
};

‎packages/knip/test/util/get-inputs-from-scripts.test.ts

+53-53
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { join, resolve } from '../../src/util/path.js';
66

77
const cwd = resolve('fixtures/binaries');
88
const containingFilePath = join(cwd, 'package.json');
9-
const pkgScripts = { cwd, manifestScriptNames: new Set(['program']) };
9+
const pkgScripts = { cwd, manifestScriptNames: new Set(['program', 'spl:t']) };
1010
const knownOnly = { cwd, knownBinsOnly: true };
1111
const optional = { optional: true };
1212

@@ -125,63 +125,63 @@ test('getInputsFromScripts (nx)', () => {
125125
});
126126

127127
test('getInputsFromScripts (npm)', () => {
128-
t('npm run script', [toBinary('npm')]);
129-
t('npm run publish:latest -- --npm-tag=debug --no-push', [toBinary('npm')]);
130-
t('npm exec -- vitest -c vitest.e2e.config.mts', [toBinary('npm'), toBinary('vitest'), toConfig('vitest', 'vitest.e2e.config.mts', containingFilePath)]);
128+
t('npm run script', []);
129+
t('npm run publish:latest -- --npm-tag=debug --no-push', []);
130+
t('npm exec -- vitest -c vitest.e2e.config.mts', [toBinary('vitest'), toConfig('vitest', 'vitest.e2e.config.mts', containingFilePath)]);
131131
});
132132

133133
test('getInputsFromScripts (npx)', () => {
134-
t('npx pkg', [toBinary('npx'), toBinary('pkg')]);
135-
t('npx prisma migrate reset --force', [toBinary('npx'), toBinary('prisma')]);
136-
t('npx @scope/pkg', [toBinary('npx'), toDependency('@scope/pkg', optional)]);
137-
t('npx tsx watch main', [toBinary('npx'), toBinary('tsx'), toDeferResolveEntry('main')]);
138-
t('npx -y pkg', [toBinary('npx')]);
139-
t('npx --yes pkg', [toBinary('npx')]);
140-
t('npx --no pkg --edit ${1}', [toBinary('npx'), toBinary('pkg')]);
141-
t('npx --no -- pkg --edit ${1}', [toBinary('npx'), toBinary('pkg')]);
142-
t('npx pkg install --with-deps', [toBinary('npx'), toBinary('pkg')]);
143-
t('npx pkg migrate reset --force', [toBinary('npx'), toBinary('pkg')]);
144-
t('npx pkg@1.0.0 migrate reset --force', [toBinary('npx'), toDependency('pkg', optional)]);
145-
t('npx @scope/cli migrate reset --force', [toBinary('npx'), toDependency('@scope/cli', optional)]);
146-
t('npx -- pkg', [toBinary('npx'), toBinary('pkg')]);
147-
t('npx -- @scope/cli@1.0.0 migrate reset --force', [toBinary('npx'), toDependency('@scope/cli', optional)]);
148-
t('npx retry-cli@0.6.0 -- curl --output /dev/null ', [toBinary('npx'), toDependency('retry-cli', optional), toBinary('curl')]);
149-
t('npx --package pkg@0.6.0 -- curl --output /dev/null', [toBinary('npx'), toBinary('curl'), toDependency('pkg', optional)]);
150-
t('npx --package @scope/pkg@0.6.0 --package pkg -- curl', [toBinary('npx'), toBinary('curl'), toDependency('@scope/pkg', optional), toDependency('pkg', optional)]);
151-
t("npx --package=foo -c 'curl --output /dev/null'", [toBinary('npx'), toDependency('foo', optional), toBinary('curl')]);
152-
t('npx swagger-typescript-api -p http://localhost:3030/swagger.v1.json', [toBinary('npx'), toBinary('swagger-typescript-api')]);
153-
t('npx swagger-typescript-api -- -p http://localhost:3030/swagger.v1.json', [toBinary('npx'), toBinary('swagger-typescript-api')]);
154-
t('npx tsx main', [toBinary('npx'), toBinary('tsx'), toDeferResolveEntry('main')]);
155-
t('npx tsx ./main.ts build', [toBinary('npx'), toBinary('tsx'), ts]);
156-
t('npx tsx ./main.ts -- build', [toBinary('npx'), toBinary('tsx'), ts]);
134+
t('npx pkg', [toBinary('pkg')]);
135+
t('npx prisma migrate reset --force', [toBinary('prisma')]);
136+
t('npx @scope/pkg', [toDependency('@scope/pkg', optional)]);
137+
t('npx tsx watch main', [toBinary('tsx'), toDeferResolveEntry('main')]);
138+
t('npx -y pkg', []);
139+
t('npx --yes pkg', []);
140+
t('npx --no pkg --edit ${1}', [toBinary('pkg')]);
141+
t('npx --no -- pkg --edit ${1}', [toBinary('pkg')]);
142+
t('npx pkg install --with-deps', [toBinary('pkg')]);
143+
t('npx pkg migrate reset --force', [toBinary('pkg')]);
144+
t('npx pkg@1.0.0 migrate reset --force', [toDependency('pkg', optional)]);
145+
t('npx @scope/cli migrate reset --force', [toDependency('@scope/cli', optional)]);
146+
t('npx -- pkg', [toBinary('pkg')]);
147+
t('npx -- @scope/cli@1.0.0 migrate reset --force', [toDependency('@scope/cli', optional)]);
148+
t('npx retry-cli@0.6.0 -- curl --output /dev/null ', [toDependency('retry-cli', optional), toBinary('curl')]);
149+
t('npx --package pkg@0.6.0 -- curl --output /dev/null', [toBinary('curl'), toDependency('pkg', optional)]);
150+
t('npx --package @scope/pkg@0.6.0 --package pkg -- curl', [toBinary('curl'), toDependency('@scope/pkg', optional), toDependency('pkg', optional)]);
151+
t("npx --package=foo -c 'curl --output /dev/null'", [toDependency('foo', optional), toBinary('curl')]);
152+
t('npx swagger-typescript-api -p http://localhost:3030/swagger.v1.json', [toBinary('swagger-typescript-api')]);
153+
t('npx swagger-typescript-api -- -p http://localhost:3030/swagger.v1.json', [toBinary('swagger-typescript-api')]);
154+
t('npx tsx main', [toBinary('tsx'), toDeferResolveEntry('main')]);
155+
t('npx tsx ./main.ts build', [toBinary('tsx'), ts]);
156+
t('npx tsx ./main.ts -- build', [toBinary('tsx'), ts]);
157157
});
158158

159159
test('getInputsFromScripts (pnpx/pnpm dlx)', () => {
160-
t('pnpx pkg', [toBinary('pnpx'), toDependency('pkg', optional)]);
161-
const s = [toDependency('cowsay', optional), toDependency('lolcatjs', optional), toBinary('echo'), toBinary('cowsay'), toBinary('lolcatjs')];
162-
t('pnpx --package cowsay --package lolcatjs -c \'echo "hi pnpm" | cowsay | lolcatjs\'', [toBinary('pnpx'), ...s]);
163-
t('pnpm --package cowsay --package lolcatjs -c dlx \'echo "hi pnpm" | cowsay | lolcatjs\'', [toBinary('pnpm'), ...s]);
160+
t('pnpx pkg', [toDependency('pkg', optional)]);
161+
const inputs = [toDependency('cowsay', optional), toDependency('lolcatjs', optional), toBinary('echo'), toBinary('cowsay'), toBinary('lolcatjs')];
162+
t('pnpx --package cowsay --package lolcatjs -c \'echo "hi pnpm" | cowsay | lolcatjs\'', inputs);
163+
t('pnpm --package cowsay --package lolcatjs -c dlx \'echo "hi pnpm" | cowsay | lolcatjs\'', inputs);
164164
});
165165

166166
test('getInputsFromScripts (bunx/bun x)', () => {
167-
t('bunx pkg', [toBinary('bunx'), toDependency('pkg', optional)]);
168-
t('bunx cowsay "Hello world!"', [toBinary('bunx'), toDependency('cowsay', optional)]);
169-
t('bunx my-cli --foo bar', [toBinary('bunx'), toDependency('my-cli', optional)]);
170-
t('bun x pkg', [toBinary('bun'), toDependency('pkg', optional)]);
167+
t('bunx pkg', [toDependency('pkg', optional)]);
168+
t('bunx cowsay "Hello world!"', [toDependency('cowsay', optional)]);
169+
t('bunx my-cli --foo bar', [toDependency('my-cli', optional)]);
170+
t('bun x pkg', [toDependency('pkg', optional)]);
171171
});
172172

173173
test('getInputsFromScripts (pnpm)', () => {
174-
t('pnpm exec program', [toBinary('pnpm'), toBinary('program')]);
175-
t('pnpm run program', [toBinary('pnpm')]);
176-
t('pnpm program', [toBinary('pnpm'), toBinary('program')]);
177-
t('pnpm run program', [toBinary('pnpm')], pkgScripts);
178-
t('pnpm program', [toBinary('pnpm')], pkgScripts);
179-
t('pnpm dlx pkg', [toBinary('pnpm'), toDependency('pkg', optional)]);
180-
t('pnpm --package=pkg-a dlx pkg', [toBinary('pnpm'), toDependency('pkg', optional), toDependency('pkg-a', optional)]);
181-
t('pnpm --recursive --parallel test -- --sequence.seed=1700316221712', [toBinary('pnpm')]);
182-
t('pnpm program script.js', [toBinary('pnpm')], pkgScripts);
183-
t('pnpm --silent program script.js', [toBinary('pnpm')], pkgScripts);
184-
t('pnpm --silent run program script.js', [toBinary('pnpm')], pkgScripts);
174+
t('pnpm exec program', [toBinary('program')]);
175+
t('pnpm run program', []);
176+
t('pnpm program', [toBinary('program')]);
177+
t('pnpm run program', [], pkgScripts);
178+
t('pnpm program', [], pkgScripts);
179+
t('pnpm dlx pkg', [toDependency('pkg', optional)]);
180+
t('pnpm --package=pkg-a dlx pkg', [toDependency('pkg', optional), toDependency('pkg-a', optional)]);
181+
t('pnpm --recursive --parallel test -- --sequence.seed=1700316221712', []);
182+
t('pnpm program script.js', [], pkgScripts);
183+
t('pnpm --silent program script.js', [], pkgScripts);
184+
t('pnpm --silent run program script.js', [], pkgScripts);
185185
});
186186

187187
test('getInputsFromScripts (yarn)', () => {
@@ -204,18 +204,18 @@ test('getInputsFromScripts (rollup)', () => {
204204

205205
test('getInputsFromScripts (execa)', () => {
206206
t('execa --quiet ./script.js', [toBinary('execa'), toDeferResolve('./script.js')]);
207-
t('npx --yes execa --quiet ./script.js', [toBinary('npx'), toDeferResolve('./script.js')]);
207+
t('npx --yes execa --quiet ./script.js', [toDeferResolve('./script.js')]);
208208
});
209209

210210
test('getInputsFromScripts (zx)', () => {
211211
t('zx --quiet script.js', [toBinary('zx'), toDeferResolve('script.js')]);
212-
t('npx --yes zx --quiet script.js', [toBinary('npx'), toDeferResolve('script.js')]);
212+
t('npx --yes zx --quiet script.js', [toDeferResolve('script.js')]);
213213
});
214214

215215
test('getInputsFromScripts (c8)', () => {
216216
t('c8 node script.js', [toBinary('c8'), toBinary('node'), toDeferResolveEntry('script.js')]);
217-
t('c8 npm test', [toBinary('c8'), toBinary('npm')]);
218-
t('c8 check-coverage --lines 95 --per-file npm test', [toBinary('c8'), toBinary('npm')]);
217+
t('c8 npm test', [toBinary('c8')]);
218+
t('c8 check-coverage --lines 95 --per-file npm test', [toBinary('c8')]);
219219
t("c8 --reporter=lcov --reporter text mocha 'test/**/*.spec.js'", [toBinary('c8'), toBinary('mocha')]);
220220
t('c8 --reporter=lcov --reporter text node --test --test-reporter=@org/rep', [toBinary('c8'), toBinary('node'), toDeferResolve('@org/rep')]);
221221
});
@@ -233,7 +233,7 @@ test('getInputsFromScripts (double-dash)', () => {
233233
test('getInputsFromScripts (bash expressions)', () => {
234234
t('if test "$NODE_ENV" = "production" ; then make install ; fi ', [toBinary('make')]);
235235
t('node -e "if (NODE_ENV === \'production\'){process.exit(1)} " || make install', [toBinary('node'), toBinary('make')]);
236-
t('if ! npx pkg --verbose ; then exit 1 ; fi', [toBinary('npx'), toBinary('pkg'), toBinary('exit')]);
236+
t('if ! npx pkg --verbose ; then exit 1 ; fi', [toBinary('pkg'), toBinary('exit')]);
237237
t('exec < /dev/tty && node_modules/.bin/cz --hook || true', [toBinary('exec'), toBinary('cz'), toBinary('true')]);
238238
});
239239

@@ -243,8 +243,8 @@ test('getInputsFromScripts (bash expansion)', () => {
243243
});
244244

245245
test('getInputsFromScripts (multiline)', () => {
246-
t('#!/bin/sh\n. "$(dirname "$0")/_/husky.sh"\nnpx lint-staged', [toBinary('npx'), toBinary('lint-staged')]);
247-
t(`for S in "s"; do\n\tnpx rc@0.6.0\n\tnpx @scope/rc@0.6.0\ndone`, [toBinary('npx'), toDependency('rc', optional), toBinary('npx'), toDependency('@scope/rc', optional)]);
246+
t('#!/bin/sh\n. "$(dirname "$0")/_/husky.sh"\nnpx lint-staged', [toBinary('lint-staged')]);
247+
t(`for S in "s"; do\n\tnpx rc@0.6.0\n\tnpx @scope/rc@0.6.0\ndone`, [toDependency('rc', optional), toDependency('@scope/rc', optional)]);
248248
});
249249

250250
test('getInputsFromScripts (bail outs)', () => {

0 commit comments

Comments
 (0)
Please sign in to comment.