Skip to content

Commit c881d78

Browse files
committedOct 28, 2024
Fix binary extraction from specifier
1 parent 62b10ba commit c881d78

File tree

5 files changed

+18
-21
lines changed

5 files changed

+18
-21
lines changed
 

‎packages/knip/src/binaries/bash-parser.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@ import { pluginArgsMap } from '../plugins.js';
33
import type { GetInputsFromScriptsOptions } from '../types/config.js';
44
import { debugLogObject } from '../util/debug.js';
55
import { type Input, toBinary, toDeferResolve } from '../util/input.js';
6+
import { extractBinary } from '../util/modules.js';
67
import { resolve as fallbackResolve } from './fallback.js';
78
import PackageManagerResolvers from './package-manager/index.js';
89
import { resolve as resolverFromPlugins } from './plugins.js';
9-
import { parseNodeArgs, trimBinary } from './util.js';
10+
import { parseNodeArgs } from './util.js';
1011

1112
// https://vorpaljs.github.io/bash-parser-playground/
1213

@@ -35,7 +36,7 @@ export const getDependenciesFromScript = (script: string, options: GetInputsFrom
3536
switch (node.type) {
3637
case 'Command': {
3738
const text = node.name?.text;
38-
const binary = text ? trimBinary(text) : text;
39+
const binary = text ? extractBinary(text) : text;
3940

4041
const commandExpansions =
4142
node.prefix

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import parseArgs from 'minimist';
22
import type { BinaryResolver } from '../../types/config.js';
33
import { toBinary, toDependency } from '../../util/input.js';
4+
import { stripVersionFromSpecifier } from '../../util/modules.js';
45
import { isInternal } from '../../util/path.js';
5-
import { argsFrom, stripVersionFromSpecifier } from '../util.js';
6+
import { argsFrom } from '../util.js';
67

78
export const resolve: BinaryResolver = (_binary, args, options) => {
89
const { fromArgs } = options;

‎packages/knip/src/binaries/util.ts

-11
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,5 @@
11
import parseArgs from 'minimist';
22

3-
export const stripVersionFromSpecifier = (specifier: string) => specifier.replace(/(\S+)@.*/, '$1');
4-
5-
const stripNodeModulesFromPath = (command: string) => command.replace(/^(\.\/)?node_modules\//, '');
6-
7-
export const trimBinary = (command: string) =>
8-
stripVersionFromSpecifier(
9-
stripNodeModulesFromPath(command)
10-
.replace(/^(\.bin\/)/, '')
11-
.replace(/\$\(npm bin\)\/(\w+)/, '$1') // Removed in npm v9
12-
);
13-
143
export const argsFrom = (args: string[], from: string) => args.slice(args.indexOf(from));
154

165
export const parseNodeArgs = (args: string[]) =>

‎packages/knip/src/util/get-referenced-inputs.ts

+1-7
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
import type { ConfigurationChief, Workspace } from '../ConfigurationChief.js';
22
import type { DependencyDeputy } from '../DependencyDeputy.js';
33
import type { IssueCollector } from '../IssueCollector.js';
4-
import { trimBinary } from '../binaries/util.js';
54
import { IGNORED_RUNTIME_DEPENDENCIES } from '../constants.js';
65
import { debugLog } from './debug.js';
7-
import { toBinary, toDebugString } from './input.js';
6+
import { toDebugString } from './input.js';
87
import { type Input, fromBinary, isBinary, isConfigPattern, isDeferResolveEntry, isDependency } from './input.js';
98
import { getPackageNameFromSpecifier } from './modules.js';
109
import { dirname, isAbsolute, isInternal, join } from './path.js';
@@ -28,11 +27,6 @@ export const getReferencedInputsHandler =
2827

2928
if (!containingFilePath || IGNORED_RUNTIME_DEPENDENCIES.has(specifier)) return;
3029

31-
if (isDeferResolveEntry(input) && specifier.includes('node_modules/.bin')) {
32-
// TODO? this quick-fixes edge case for entry like `node node_modules/.bin/jest`, maybe do it in binaries/node.ts alone or call this function again properly to avoid this mutation
33-
Object.assign(input, toBinary(trimBinary(input.specifier)));
34-
}
35-
3630
if (isBinary(input)) {
3731
const binaryName = fromBinary(input);
3832
const ws = (input.dir && chief.findWorkspaceByFilePath(`${input.dir}/`)) || workspace;

‎packages/knip/src/util/modules.ts

+12
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export const getPackageNameFromModuleSpecifier = (moduleSpecifier: string) => {
1010

1111
const lastPackageNameMatch = /(?<=node_modules\/)(@[^/]+\/[^/]+|[^/]+)/g;
1212
export const getPackageNameFromFilePath = (value: string) => {
13+
if (value.includes('node_modules/.bin/')) return extractBinary(value);
1314
const match = toPosix(value).match(lastPackageNameMatch);
1415
if (match) return match[match.length - 1];
1516
return value;
@@ -20,6 +21,17 @@ export const getPackageNameFromSpecifier = (specifier: string) =>
2021

2122
export const isStartsLikePackageName = (specifier: string) => /^(@[a-z0-9._]|[a-z0-9])/.test(specifier);
2223

24+
export const stripVersionFromSpecifier = (specifier: string) => specifier.replace(/(\S+)@.*/, '$1');
25+
26+
const stripNodeModulesFromPath = (command: string) => command.replace(/^(\.\/)?node_modules\//, '');
27+
28+
export const extractBinary = (command: string) =>
29+
stripVersionFromSpecifier(
30+
stripNodeModulesFromPath(command)
31+
.replace(/^(\.bin\/)/, '')
32+
.replace(/\$\(npm bin\)\/(\w+)/, '$1') // Removed in npm v9
33+
);
34+
2335
export const isDefinitelyTyped = (packageName: string) => packageName.startsWith(`${DT_SCOPE}/`);
2436

2537
export const getDefinitelyTypedFor = (packageName: string) => {

0 commit comments

Comments
 (0)
Please sign in to comment.