-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheck.yaml
More file actions
151 lines (133 loc) · 5.58 KB
/
check.yaml
File metadata and controls
151 lines (133 loc) · 5.58 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
version: "1"
nodes:
- id: transform-and-update
name: Transform code and update dependencies
type: automatic
steps:
- id: scan-and-fix
name: "Scan typescript files and apply fixes"
js-ast-grep:
js_file: scripts/codemod.ts
language: tsx
includes:
- "**/*.ts"
- "**/*.tsx"
- id: install-package-if-needed
name: "Install the package and update the relevant package.json and lock file as needed"
run: |
set -euo pipefail
PACKAGE_NAME="arkregex"
PACKAGE_VERSION="0.0.5"
detect_package_manager() {
local dir="$1"
if [ -f "$dir/pnpm-lock.yaml" ]; then
echo "pnpm"
elif [ -f "$dir/yarn.lock" ]; then
echo "yarn"
elif [ -f "$dir/bun.lockb" ]; then
echo "bun"
elif [ -f "$dir/package-lock.json" ]; then
echo "npm"
else
echo "npm"
fi
}
has_package_import() {
local dir="$1"
local package="$2"
if find "$dir" -type f \( -name "*.ts" -o -name "*.tsx" \) \
-not -path "*/node_modules/*" \
-not -path "*/.git/*" \
-not -path "*/dist/*" \
-not -path "*/build/*" \
-exec grep -l "from [\"']${package}[\"']" {} \; 2>/dev/null | head -1 | grep -q .; then
else
fi
}
# Find yarn workspace root (directory containing yarn.lock)
find_yarn_root() {
local dir="$1"
local current="$dir"
while [ "$current" != "/" ]; do
if [ -f "$current/yarn.lock" ]; then
echo "$current"
return 0
fi
current=$(dirname "$current")
done
echo "$dir" # Fallback to original dir if no root found
}
# Get workspace name from package.json
get_workspace_name() {
local package_json="$1"
if [ -f "$package_json" ]; then
# Try to extract name field from package.json
grep -o '"name"[[:space:]]*:[[:space:]]*"[^"]*"' "$package_json" 2>/dev/null | head -1 | sed 's/.*"name"[[:space:]]*:[[:space:]]*"\([^"]*\)".*/\1/' || echo ""
fi
}
install_package() {
local dir="$1"
local pm="$2"
local package="${PACKAGE_NAME}@${PACKAGE_VERSION}"
echo "Installing ${package} in $(basename "$dir") using $pm..."
case "$pm" in
pnpm)
(cd "$dir" && pnpm add "${package}")
;;
yarn)
# For yarn, handle workspaces properly
local yarn_root=$(find_yarn_root "$dir")
local package_json="$dir/package.json"
if [ -f "$yarn_root/package.json" ] && grep -q '"workspaces"' "$yarn_root/package.json" 2>/dev/null; then
# We're in a yarn workspace
local workspace_name=$(get_workspace_name "$package_json")
if [ -n "$workspace_name" ] && [ "$yarn_root" != "$dir" ]; then
# Use yarn workspace command from root
echo "Detected yarn workspace. Installing to workspace: $workspace_name"
(cd "$yarn_root" && yarn workspace "$workspace_name" add "${package}")
else
# Fallback: try adding from the package directory
# This works for yarn v1 (classic) in some cases
(cd "$dir" && yarn add "${package}" || {
echo "Warning: yarn add failed from package directory, trying from workspace root..."
(cd "$yarn_root" && yarn add "${package}" -W)
})
fi
else
# Not a workspace, or we're at the root - use standard yarn add
(cd "$dir" && yarn add "${package}")
fi
;;
bun)
(cd "$dir" && bun add "${package}")
;;
npm)
(cd "$dir" && npm install "${package}")
;;
*)
echo "Unknown package manager: $pm" >&2
return 1
;;
esac
}
target_dir="${PWD}"
root_pm=$(detect_package_manager "$target_dir")
echo "Detected package manager: $root_pm"
while IFS= read -r -d '' package_json; do
package_dir=$(dirname "$package_json")
if grep -q "\"${PACKAGE_NAME}\"" "$package_json" 2>/dev/null; then
echo "Skipping $(basename "$package_dir"): ${PACKAGE_NAME} already in dependencies or devDependencies"
continue
fi
if has_package_import "$package_dir" "$PACKAGE_NAME"; then
local pm=$(detect_package_manager "$package_dir")
install_package "$package_dir" "$pm"
else
echo "Skipping $(basename "$package_dir"): no imports of ${PACKAGE_NAME} found"
fi
done < <(find "$target_dir" -name "package.json" \
-not -path "*/node_modules/*" \
-not -path "*/.git/*" \
-not -path "*/dist/*" \
-not -path "*/build/*" \
-print0)