mirror of
https://github.com/hellodigua/ChatLab.git
synced 2026-10-25 17:54:34 +08:00
An ambient npm_config_runtime=electron (common in Electron dev shells and CI) makes bare prebuild-install "succeed" with an Electron-ABI binding, bypassing the source-build fallbacks and leaving the CLI unable to start. Pin -r node -t to the current system Node instead. Co-authored-by: Cursor <cursoragent@cursor.com>
42 lines
1.5 KiB
Bash
Executable File
42 lines
1.5 KiB
Bash
Executable File
#!/bin/bash
|
||
#
|
||
# 在隔离目录中准备 better-sqlite3 的系统 Node.js 原生模块,
|
||
# 避免与 electron-rebuild 产生冲突。
|
||
#
|
||
# 优先下载官方 Node ABI 预编译包(秒级),无可用预编译时回退源码编译。
|
||
# 产物:apps/cli/native/better_sqlite3.node
|
||
#
|
||
set -e
|
||
|
||
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
|
||
SERVER_DIR="$(dirname "$SCRIPT_DIR")"
|
||
TARGET_DIR="$SERVER_DIR/native"
|
||
|
||
# 获取当前 workspace 中 better-sqlite3 的版本
|
||
BS3_VERSION=$(node -e "const p = require('better-sqlite3/package.json'); console.log(p.version)")
|
||
echo "Building better-sqlite3@${BS3_VERSION} for system Node.js ($(node -v))..."
|
||
|
||
TEMP_DIR=$(mktemp -d)
|
||
trap "rm -rf $TEMP_DIR" EXIT
|
||
|
||
cd "$TEMP_DIR"
|
||
npm init -y > /dev/null 2>&1
|
||
npm install "better-sqlite3@${BS3_VERSION}" --ignore-scripts > /dev/null 2>&1
|
||
|
||
cd "node_modules/better-sqlite3"
|
||
# better-sqlite3 按 Node ABI 发布预编译(无 napi 变体)。显式钉死 runtime/target 到系统 Node,
|
||
# 避免环境残留的 npm_config_runtime=electron 等配置让它"成功"下载 Electron ABI 产物
|
||
npx --yes prebuild-install -r node -t "$(node --version)" || npm run build-release 2>/dev/null || npx --yes node-gyp rebuild --release
|
||
|
||
BUILT="$TEMP_DIR/node_modules/better-sqlite3/build/Release/better_sqlite3.node"
|
||
if [ ! -f "$BUILT" ]; then
|
||
echo "Error: native binary not found at $BUILT"
|
||
exit 1
|
||
fi
|
||
|
||
mkdir -p "$TARGET_DIR"
|
||
cp "$BUILT" "$TARGET_DIR/better_sqlite3.node"
|
||
|
||
echo "Done. Native binary saved to $TARGET_DIR/better_sqlite3.node"
|
||
echo " ABI: $(node -e 'console.log(process.versions.modules)')"
|