Skip to content

Commit cd91e23

Browse files
authored
Generate package.json with 'version' and 'exports' fields using shell script (#133)
* Generate package.json with 'version' and 'exports' fields using shell script * Fix relative export paths; also better structure including prefixing by version
1 parent 8e7bf59 commit cd91e23

4 files changed

Lines changed: 149 additions & 3 deletions

File tree

.github/actions/shared-setup/action.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,5 @@ runs:
3737
if [ "${INPUT_TARGET}" = "python" ]; then
3838
sed -i "s/^version = .*/version = \"$STRIPPED_VERSION\"/" pyproject.toml
3939
elif [ "${INPUT_TARGET}" = "node" ]; then
40-
jq --arg version "$STRIPPED_VERSION" '.version = $version' package.example.json > tmp.$$.json && mv tmp.$$.json package.json
40+
./scripts/generate-package-package.json "$STRIPPED_VERSION"
4141
fi

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ __pycache__/
66
*.pyc
77
/gen-dummy
88
node_modules/
9-
var/
9+
var/
10+
/package.json

package.example.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,5 +47,7 @@
4747
"@connectrpc/connect-web": {
4848
"optional": true
4949
}
50-
}
50+
},
51+
"exports": {},
52+
"sideEffects": false
5153
}

scripts/generate-package-json.sh

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
#!/usr/bin/env sh
2+
3+
set -eu
4+
5+
# first argument is version and we want to check it is a valid semver
6+
if [ -z "${1:-}" ]; then
7+
echo "Usage: $0 <semver-version>"
8+
exit 1
9+
fi
10+
VERSION="$1"
11+
12+
jq --arg version "$VERSION" '.version = $version' package.example.json > tmp.$$.json && mv tmp.$$.json package.json
13+
echo "Updated version to $VERSION in package.json"
14+
15+
# Check if the directory exists before searching
16+
if [ ! -d "gen/typescript/qdrant/cloud" ]; then
17+
echo "Directory gen/typescript/qdrant/cloud does not exist"
18+
exit 1
19+
fi
20+
21+
# Function to add export entry to package.json
22+
add_export() {
23+
local key="$1"
24+
local js_path="$2"
25+
local ts_path="$3"
26+
27+
jq --arg key "$key" \
28+
--arg js_path "$js_path" \
29+
--arg ts_path "$ts_path" \
30+
'.exports[$key] = {
31+
"import": $js_path,
32+
"types": $ts_path
33+
}' package.json > tmp.$$.json && mv tmp.$$.json package.json
34+
}
35+
36+
# Function to convert .d.ts path to relative package paths
37+
get_package_paths() {
38+
local dts_file="$1"
39+
local js_path ts_path
40+
41+
ts_path=$(echo "$dts_file" | sed 's|^gen/typescript/|./|')
42+
js_path=$(echo "$ts_path" | sed 's/\.d\.ts$/.js/')
43+
44+
echo "$js_path|$ts_path"
45+
}
46+
47+
# Create temporary file for processing results
48+
temp_file=$(mktemp)
49+
trap 'rm -f "$temp_file"' EXIT
50+
51+
# Find all .d.ts files and process them
52+
find gen/typescript/qdrant/cloud -name "*.d.ts" -type f | \
53+
while read -r dts_file; do
54+
# Skip if corresponding .js file doesn't exist
55+
js_file=$(echo "$dts_file" | sed 's/\.d\.ts$/.js/')
56+
[ ! -f "$js_file" ] && continue
57+
58+
# Get paths
59+
paths=$(get_package_paths "$dts_file")
60+
js_path=$(echo "$paths" | cut -d'|' -f1)
61+
ts_path=$(echo "$paths" | cut -d'|' -f2)
62+
63+
# Extract service and potential submodule from path
64+
rel_path=$(echo "$dts_file" | sed 's|gen/typescript/qdrant/cloud/||')
65+
service=$(echo "$rel_path" | cut -d'/' -f1)
66+
filename=$(basename "$dts_file" .d.ts)
67+
68+
# Extract path components dynamically
69+
# Structure: service/[submodule1/submodule2/...]/version/file
70+
# Find the version (v1, v2, etc.) to split the path correctly
71+
path_parts=$(echo "$rel_path" | tr '/' '\n')
72+
73+
# Find which part is the version by looking for v[0-9]+
74+
version=""
75+
version_index=0
76+
i=1
77+
for part in $path_parts; do
78+
if echo "$part" | grep -q "^v[0-9]\+$"; then
79+
version="$part"
80+
version_index=$i
81+
break
82+
fi
83+
i=$((i + 1))
84+
done
85+
86+
if [ -z "$version" ]; then
87+
echo "ERROR: No version found in path $rel_path" >&2
88+
echo "ERROR" > "$temp_file"
89+
exit 1
90+
fi
91+
92+
# Build submodule path (everything between service and version)
93+
submodules=""
94+
if [ "$version_index" -gt 2 ]; then
95+
# Extract submodules: everything from position 2 to version_index-1
96+
j=2
97+
while [ $j -lt "$version_index" ]; do
98+
submodule_part=$(echo "$rel_path" | cut -d'/' -f$j)
99+
if [ -z "$submodules" ]; then
100+
submodules="$submodule_part"
101+
else
102+
submodules="$submodules/$submodule_part"
103+
fi
104+
j=$((j + 1))
105+
done
106+
fi
107+
108+
# Generate export key and sort prefix
109+
if [ -z "$submodules" ]; then
110+
# Direct service: service/version/file (e.g., auth/v1/auth_pb.d.ts)
111+
if echo "$filename" | grep -q "_connectquery$"; then
112+
export_key="$service/$version/connect-query"
113+
sort_prefix="$service-$version-1"
114+
else
115+
export_key="$service/$version"
116+
sort_prefix="$service-$version-0"
117+
fi
118+
else
119+
# Has submodules: service/submodule(s)/version/file (e.g., cluster/auth/v1/file.d.ts, serverless/collection/auth/v1/file.d.ts)
120+
submodules_normalized=$(echo "$submodules" | tr '/' '-')
121+
if echo "$filename" | grep -q "_connectquery$"; then
122+
export_key="$service/$submodules/$version/connect-query"
123+
sort_prefix="$service-$version-$submodules_normalized-1"
124+
else
125+
export_key="$service/$submodules/$version"
126+
sort_prefix="$service-$version-$submodules_normalized-0"
127+
fi
128+
fi
129+
130+
echo "$sort_prefix|$export_key|$js_path|$ts_path"
131+
done | \
132+
sort | \
133+
while IFS='|' read -r sort_prefix export_key js_path ts_path; do
134+
add_export "./$export_key" "$js_path" "$ts_path"
135+
done
136+
137+
# Check if there was an error
138+
if [ -f "$temp_file" ] && [ "$(cat "$temp_file")" = "ERROR" ]; then
139+
echo "Script terminated due to error in processing files"
140+
exit 1
141+
fi
142+
143+
echo "Generated exports for package.json"

0 commit comments

Comments
 (0)