fork of hey-api/openapi-ts because I need some additional things
1#!/usr/bin/env bash
2
3# Generate client code for all examples that have openapi-ts script
4# This script is used to ensure examples are up-to-date with the latest code
5
6set -e
7
8# Get the directory of this script
9SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
10ROOT_DIR="$(cd "$SCRIPT_DIR/.." && pwd)"
11
12echo "⏳ Generating client code for all examples..."
13
14# Find all examples with openapi-ts script and generate code in parallel
15# Concurrency control: adjust this number depending on CI machine resources
16CONCURRENCY=${CONCURRENCY:-4}
17tmpdir=$(mktemp -d)
18# Use a simple space-separated list of pids and per-pid files for metadata
19PIDS=""
20
21wait_for_slot() {
22 # Wait until number of background jobs is less than CONCURRENCY
23 while [ "$(jobs -rp | wc -l)" -ge "$CONCURRENCY" ]; do
24 sleep 0.2
25 done
26}
27
28for dir in "$ROOT_DIR"/examples/*/; do
29 package_json="$dir/package.json"
30 if [ ! -f "$package_json" ]; then
31 continue
32 fi
33
34 if ! grep -q "\"openapi-ts\":" "$package_json"; then
35 continue
36 fi
37
38 example_name=$(basename "$dir")
39 echo "📦 Scheduling: $example_name"
40
41 wait_for_slot
42
43 log="$tmpdir/${example_name}.log"
44 (
45 echo "Generating: $example_name"
46 set -e
47 cd "$dir"
48 echo "-> Running openapi-ts"
49 pnpm run openapi-ts
50
51 # Format generated files in this example only to keep the step fast
52 if command -v pnpm >/dev/null 2>&1 && pnpm -w -s --version >/dev/null 2>&1; then
53 pnpm -s exec prettier --write "src/**/*.{ts,tsx,js,jsx,json,md}" || true
54 pnpm -s exec eslint --fix "src/**/*.{ts,tsx,js,jsx,json,md}" || true
55 else
56 if [ -x "node_modules/.bin/prettier" ]; then
57 ./node_modules/.bin/prettier --write "src/**/*.{ts,tsx,js,jsx,json,md}" || true
58 fi
59 if [ -x "node_modules/.bin/eslint" ]; then
60 ./node_modules/.bin/eslint --fix "src/**/*.{ts,tsx,js,jsx,json,md}" || true
61 fi
62 fi
63
64 echo "Completed: $example_name"
65 ) >"$log" 2>&1 &
66
67 pid=$!
68 PIDS="$PIDS $pid"
69 printf '%s' "$example_name" >"$tmpdir/$pid.name"
70 printf '%s' "$log" >"$tmpdir/$pid.log"
71done
72
73failed=0
74for pid in $PIDS; do
75 if wait "$pid"; then
76 name=$(cat "$tmpdir/$pid.name" 2>/dev/null || echo "$pid")
77 echo "✅ $name succeeded"
78 else
79 name=$(cat "$tmpdir/$pid.name" 2>/dev/null || echo "$pid")
80 # Read the metadata file which contains the path to the real log
81 logpath=$(cat "$tmpdir/$pid.log" 2>/dev/null || echo "")
82 if [ -n "$logpath" ] && [ -f "$logpath" ]; then
83 echo "❌ $name failed — showing full log ($logpath):"
84 echo "---- full log start ----"
85 cat "$logpath" || true
86 echo "---- full log end ----"
87 else
88 echo "❌ $name failed — no log found (metadata: $tmpdir/$pid.log)"
89 fi
90 failed=1
91 fi
92done
93
94if [ "$failed" -ne 0 ]; then
95 echo "One or more examples failed to generate. Logs are in: $tmpdir"
96 exit 1
97fi
98
99echo "✨ All examples generated successfully!"