forked from jordanlambrecht/tracker-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathknip-filter.sh
More file actions
executable file
·97 lines (86 loc) · 4.05 KB
/
knip-filter.sh
File metadata and controls
executable file
·97 lines (86 loc) · 4.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#!/usr/bin/env bash
# scripts/knip-filter.sh
#
# Filter knip results by file paths or export/dependency names
# Usage: pnpm knip --reporter json --no-exit-code | bash scripts/knip-filter.sh pattern1 pattern2 ...
set -euo pipefail
# Read piped JSON input
input=$(cat)
# Get filter patterns from arguments
patterns=("$@")
if [ ${#patterns[@]} -eq 0 ]; then
echo "Usage: knip --reporter json | bash scripts/knip-filter.sh pattern1 pattern2 ..."
echo "Example: knip --reporter json | bash scripts/knip-filter.sh adapter crypto proxy"
exit 1
fi
# Build pattern matching for jq
pattern_json=$(printf '%s\n' "${patterns[@]}" | jq -R . | jq -s .)
# Process knip JSON output
echo "$input" | jq -r --argjson patterns "$pattern_json" '
# Helper function to check if text matches any pattern
def matches_pattern:
. as $text |
($patterns | map(ascii_downcase) | any(. as $pattern | $text | ascii_downcase | contains($pattern)));
# Filter unused files
def filter_files:
.files // [] | map(select(. | matches_pattern));
# Filter issues
def filter_issues:
.issues // [] | map(
select(.file | matches_pattern)
);
# Build result
{
unused_files: filter_files,
filtered_issues: filter_issues
} |
# ANSI color codes
def yellow: "\u001b[1;33m";
def reset: "\u001b[0m";
# Format output to match knip native format
if (.unused_files | length) > 0 or (.filtered_issues | length) > 0 then
[
(if (.unused_files | length) > 0 then
"\n" + yellow + "Unused files (" + (.unused_files | length | tostring) + ")" + reset,
(.unused_files | map(" " + .) | .[])
else empty end),
(
(.filtered_issues | map(.file as $file | .dependencies // [] | map(. + {file: $file})) | flatten) as $all_deps |
if ($all_deps | length) > 0 then
"\n" + yellow + "Unused dependencies (" + ($all_deps | length | tostring) + ")" + reset,
($all_deps | map(" " + ((.name + (" " * ((40 - (.name | length)) | max(0; .))))[0:40]) + " " + .file + ":" + (.line | tostring) + ":" + (.col | tostring)) | .[])
else empty end
),
(
(.filtered_issues | map(.file as $file | .devDependencies // [] | map(. + {file: $file})) | flatten) as $all_devdeps |
if ($all_devdeps | length) > 0 then
"\n" + yellow + "Unused devDependencies (" + ($all_devdeps | length | tostring) + ")" + reset,
($all_devdeps | map(" " + ((.name + (" " * ((40 - (.name | length)) | max(0; .))))[0:40]) + " " + .file + ":" + (.line | tostring) + ":" + (.col | tostring)) | .[])
else empty end
),
(
(.filtered_issues | map(.file as $file | .exports // [] | map(. + {file: $file})) | flatten) as $all_exports |
if ($all_exports | length) > 0 then
"\n" + yellow + "Unused exports (" + ($all_exports | length | tostring) + ")" + reset,
($all_exports | map(" " + ((.name + (" " * ((40 - (.name | length)) | max(0; .))))[0:40]) + " " + .file + ":" + (.line | tostring) + ":" + (.col | tostring)) | .[])
else empty end
),
(
(.filtered_issues | map(.file as $file | .types // [] | map(. + {file: $file})) | flatten) as $all_types |
if ($all_types | length) > 0 then
"\n" + yellow + "Unused types (" + ($all_types | length | tostring) + ")" + reset,
($all_types | map(" " + ((.name + (" " * ((40 - (.name | length)) | max(0; .))))[0:40]) + " " + .file + ":" + (.line | tostring) + ":" + (.col | tostring)) | .[])
else empty end
),
(
(.filtered_issues | map(.file as $file | .unlisted // [] | map(. + {file: $file})) | flatten) as $all_unlisted |
if ($all_unlisted | length) > 0 then
"\n" + yellow + "Unlisted dependencies (" + ($all_unlisted | length | tostring) + ")" + reset,
($all_unlisted | map(" " + ((.name + (" " * ((40 - (.name | length)) | max(0; .))))[0:40]) + " " + .file + ":" + (.line | tostring) + ":" + (.col | tostring)) | .[])
else empty end
)
] | join("\n")
else
"No matches found for patterns: " + ($patterns | join(", "))
end
'