-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate_openai_codex.sh
More file actions
77 lines (59 loc) · 2.22 KB
/
update_openai_codex.sh
File metadata and controls
77 lines (59 loc) · 2.22 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
#!/bin/bash
set -euo pipefail
PACKAGE_NAME="@openai/codex"
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
NC='\033[0m'
FINAL_VERSION_FILE="$(mktemp)"
print_version_summary() {
local old_version="${1:-not installed}"
local new_version="${2:-unknown}"
echo "VERSION_SUMMARY|old=${old_version}|new=${new_version}"
}
print_final_version() {
echo
echo -e "${YELLOW}Checking final codex version...${NC}"
if ! command -v codex >/dev/null 2>&1; then
echo -e "${RED}Error: 'codex' command not found after update check.${NC}"
exit 1
fi
codex --version | tee "${FINAL_VERSION_FILE}"
}
version_gt() {
test "$(printf '%s\n' "$@" | sort -V | tail -n 1)" = "$1" && [ "$1" != "$2" ]
}
echo -e "${YELLOW}Checking global ${PACKAGE_NAME} version...${NC}"
if ! command -v npm >/dev/null 2>&1; then
echo -e "${RED}Error: 'npm' command not found.${NC}"
exit 1
fi
latest_version=$(npm view "${PACKAGE_NAME}" version 2>/dev/null)
if [ -z "${latest_version}" ]; then
echo -e "${RED}Error: failed to fetch the latest version from npm.${NC}"
exit 1
fi
local_version=$(
{
npm list -g "${PACKAGE_NAME}" --depth=0 --json 2>/dev/null || true
} | node -e 'let input=""; process.stdin.on("data", chunk => input += chunk); process.stdin.on("end", () => { try { const data = JSON.parse(input); process.stdout.write(data.dependencies?.["@openai/codex"]?.version || ""); } catch { process.stdout.write(""); } });'
)
if [ -n "${local_version}" ]; then
echo "Local version: ${local_version}"
else
echo "Local version: not installed"
fi
echo "Latest version: ${latest_version}"
if [ -z "${local_version}" ]; then
echo -e "${YELLOW}${PACKAGE_NAME} is not installed globally. Installing latest...${NC}"
npm i -g "${PACKAGE_NAME}@latest"
elif version_gt "${latest_version}" "${local_version}"; then
echo -e "${YELLOW}New version detected. Updating ${PACKAGE_NAME}...${NC}"
npm i -g "${PACKAGE_NAME}@latest"
else
echo -e "${GREEN}${PACKAGE_NAME} is already up to date.${NC}"
fi
print_final_version
final_version="$(awk 'NR==1 { print $NF }' "${FINAL_VERSION_FILE}")"
rm -f "${FINAL_VERSION_FILE}"
print_version_summary "${local_version:-not installed}" "${final_version:-unknown}"