mirror of
https://github.com/moltbot/moltbot.git
synced 2026-03-07 22:44:16 +00:00
refactor(runtime): unify node version guard parsing
This commit is contained in:
21
openclaw.mjs
21
openclaw.mjs
@@ -4,18 +4,27 @@ import module from "node:module";
|
||||
|
||||
const MIN_NODE_MAJOR = 22;
|
||||
const MIN_NODE_MINOR = 12;
|
||||
const MIN_NODE_VERSION = `${MIN_NODE_MAJOR}.${MIN_NODE_MINOR}`;
|
||||
|
||||
const parseNodeVersion = (rawVersion) => {
|
||||
const [majorRaw = "0", minorRaw = "0"] = rawVersion.split(".");
|
||||
return {
|
||||
major: Number(majorRaw),
|
||||
minor: Number(minorRaw),
|
||||
};
|
||||
};
|
||||
|
||||
const isSupportedNodeVersion = (version) =>
|
||||
version.major > MIN_NODE_MAJOR ||
|
||||
(version.major === MIN_NODE_MAJOR && version.minor >= MIN_NODE_MINOR);
|
||||
|
||||
const ensureSupportedNodeVersion = () => {
|
||||
const [majorRaw = "0", minorRaw = "0"] = process.versions.node.split(".");
|
||||
const major = Number(majorRaw);
|
||||
const minor = Number(minorRaw);
|
||||
const supported = major > MIN_NODE_MAJOR || (major === MIN_NODE_MAJOR && minor >= MIN_NODE_MINOR);
|
||||
if (supported) {
|
||||
if (isSupportedNodeVersion(parseNodeVersion(process.versions.node))) {
|
||||
return;
|
||||
}
|
||||
|
||||
process.stderr.write(
|
||||
`openclaw: Node.js v${MIN_NODE_MAJOR}.${MIN_NODE_MINOR}+ is required (current: v${process.versions.node}).\n` +
|
||||
`openclaw: Node.js v${MIN_NODE_VERSION}+ is required (current: v${process.versions.node}).\n` +
|
||||
"If you use nvm, run:\n" +
|
||||
" nvm install 22\n" +
|
||||
" nvm use 22\n" +
|
||||
|
||||
@@ -16,6 +16,9 @@ MUTED='\033[38;2;90;100;128m' # text-muted #5a6480
|
||||
NC='\033[0m' # No Color
|
||||
|
||||
DEFAULT_TAGLINE="All your chats, one OpenClaw."
|
||||
NODE_MIN_MAJOR=22
|
||||
NODE_MIN_MINOR=12
|
||||
NODE_MIN_VERSION="${NODE_MIN_MAJOR}.${NODE_MIN_MINOR}"
|
||||
|
||||
ORIGINAL_PATH="${PATH:-}"
|
||||
|
||||
@@ -1247,26 +1250,10 @@ install_homebrew() {
|
||||
}
|
||||
|
||||
# Check Node.js version
|
||||
node_major_version() {
|
||||
parse_node_version_components() {
|
||||
if ! command -v node &> /dev/null; then
|
||||
return 1
|
||||
fi
|
||||
local version major
|
||||
version="$(node -v 2>/dev/null || true)"
|
||||
major="${version#v}"
|
||||
major="${major%%.*}"
|
||||
if [[ "$major" =~ ^[0-9]+$ ]]; then
|
||||
echo "$major"
|
||||
return 0
|
||||
fi
|
||||
return 1
|
||||
}
|
||||
|
||||
node_is_at_least_22_12() {
|
||||
if ! command -v node &> /dev/null; then
|
||||
return 1
|
||||
fi
|
||||
|
||||
local version major minor
|
||||
version="$(node -v 2>/dev/null || true)"
|
||||
major="${version#v}"
|
||||
@@ -1281,11 +1268,32 @@ node_is_at_least_22_12() {
|
||||
if [[ ! "$minor" =~ ^[0-9]+$ ]]; then
|
||||
return 1
|
||||
fi
|
||||
echo "${major} ${minor}"
|
||||
return 0
|
||||
}
|
||||
|
||||
if [[ "$major" -gt 22 ]]; then
|
||||
node_major_version() {
|
||||
local version_components major minor
|
||||
version_components="$(parse_node_version_components || true)"
|
||||
read -r major minor <<< "$version_components"
|
||||
if [[ "$major" =~ ^[0-9]+$ && "$minor" =~ ^[0-9]+$ ]]; then
|
||||
echo "$major"
|
||||
return 0
|
||||
fi
|
||||
if [[ "$major" -eq 22 && "$minor" -ge 12 ]]; then
|
||||
return 1
|
||||
}
|
||||
|
||||
node_is_at_least_required() {
|
||||
local version_components major minor
|
||||
version_components="$(parse_node_version_components || true)"
|
||||
read -r major minor <<< "$version_components"
|
||||
if [[ ! "$major" =~ ^[0-9]+$ || ! "$minor" =~ ^[0-9]+$ ]]; then
|
||||
return 1
|
||||
fi
|
||||
if [[ "$major" -gt "$NODE_MIN_MAJOR" ]]; then
|
||||
return 0
|
||||
fi
|
||||
if [[ "$major" -eq "$NODE_MIN_MAJOR" && "$minor" -ge "$NODE_MIN_MINOR" ]]; then
|
||||
return 0
|
||||
fi
|
||||
return 1
|
||||
@@ -1343,7 +1351,7 @@ ensure_macos_node22_active() {
|
||||
}
|
||||
|
||||
ensure_node22_active_shell() {
|
||||
if node_is_at_least_22_12; then
|
||||
if node_is_at_least_required; then
|
||||
return 0
|
||||
fi
|
||||
|
||||
@@ -1351,7 +1359,7 @@ ensure_node22_active_shell() {
|
||||
active_path="$(command -v node 2>/dev/null || echo "not found")"
|
||||
active_version="$(node -v 2>/dev/null || echo "missing")"
|
||||
|
||||
ui_error "Active Node.js must be v22.12+ but this shell is using ${active_version} (${active_path})"
|
||||
ui_error "Active Node.js must be v${NODE_MIN_VERSION}+ but this shell is using ${active_version} (${active_path})"
|
||||
print_active_node_paths || true
|
||||
|
||||
local nvm_detected=0
|
||||
@@ -1380,15 +1388,15 @@ ensure_node22_active_shell() {
|
||||
check_node() {
|
||||
if command -v node &> /dev/null; then
|
||||
NODE_VERSION="$(node_major_version || true)"
|
||||
if node_is_at_least_22_12; then
|
||||
if node_is_at_least_required; then
|
||||
ui_success "Node.js v$(node -v | cut -d'v' -f2) found"
|
||||
print_active_node_paths || true
|
||||
return 0
|
||||
else
|
||||
if [[ -n "$NODE_VERSION" ]]; then
|
||||
ui_info "Node.js $(node -v) found, upgrading to v22.12+"
|
||||
ui_info "Node.js $(node -v) found, upgrading to v${NODE_MIN_VERSION}+"
|
||||
else
|
||||
ui_info "Node.js found but version could not be parsed; reinstalling v22.12+"
|
||||
ui_info "Node.js found but version could not be parsed; reinstalling v${NODE_MIN_VERSION}+"
|
||||
fi
|
||||
return 1
|
||||
fi
|
||||
|
||||
Reference in New Issue
Block a user