@lark-apaas/miaoda-cli 0.1.5 → 0.1.6-alpha.388541c
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -1
- package/dist/api/deploy/index.js +13 -1
- package/dist/api/deploy/modern-types.js +23 -0
- package/dist/api/deploy/modern.js +78 -0
- package/dist/api/deploy/plugin-instances-types.js +6 -0
- package/dist/api/deploy/plugin-instances.js +22 -0
- package/dist/cli/commands/app/index.js +144 -2
- package/dist/cli/commands/deploy/modern.js +50 -0
- package/dist/cli/commands/index.js +68 -5
- package/dist/cli/commands/skills/index.js +79 -0
- package/dist/cli/handlers/app/index.js +9 -1
- package/dist/cli/handlers/app/init.js +132 -0
- package/dist/cli/handlers/app/sync.js +219 -0
- package/dist/cli/handlers/deploy/modern.js +33 -0
- package/dist/cli/handlers/skills/index.js +7 -0
- package/dist/cli/handlers/skills/status.js +31 -0
- package/dist/cli/handlers/skills/sync.js +49 -0
- package/dist/config/fullstack-cli-pin.js +17 -0
- package/dist/config/sync-configs/design-stack.js +98 -0
- package/dist/config/sync-configs/index.js +62 -0
- package/dist/config/sync-configs/nestjs-react-fullstack.js +177 -0
- package/dist/config/sync.js +14 -0
- package/dist/services/app/init/index.js +12 -0
- package/dist/services/app/init/install.js +123 -0
- package/dist/services/app/init/template.js +109 -0
- package/dist/services/deploy/modern/atoms/build.js +59 -0
- package/dist/services/deploy/modern/atoms/context.js +27 -0
- package/dist/services/deploy/modern/atoms/index.js +17 -0
- package/dist/services/deploy/modern/atoms/local-release.js +27 -0
- package/dist/services/deploy/modern/atoms/pre-release.js +13 -0
- package/dist/services/deploy/modern/atoms/save-plugin-instances.js +72 -0
- package/dist/services/deploy/modern/atoms/upload.js +246 -0
- package/dist/services/deploy/modern/check.js +53 -0
- package/dist/services/deploy/modern/constants.js +13 -0
- package/dist/services/deploy/modern/index.js +16 -0
- package/dist/services/deploy/modern/pipelines/index.js +5 -0
- package/dist/services/deploy/modern/pipelines/local.js +75 -0
- package/dist/services/deploy/modern/protocol.js +122 -0
- package/dist/services/deploy/modern/run-types.js +4 -0
- package/dist/services/deploy/modern/run.js +13 -0
- package/dist/services/deploy/modern/template-key-map.js +22 -0
- package/dist/services/skills/index.js +5 -0
- package/dist/services/skills/status.js +37 -0
- package/dist/utils/coding-steering.js +169 -0
- package/dist/utils/file-ops.js +45 -0
- package/dist/utils/githooks.js +55 -0
- package/dist/utils/http.js +21 -11
- package/dist/utils/merge-json.js +63 -0
- package/dist/utils/npm-pack.js +55 -0
- package/dist/utils/platform-sync.js +160 -0
- package/dist/utils/spark-meta.js +42 -0
- package/dist/utils/sync-rule.js +295 -0
- package/package.json +5 -3
- package/upgrade/templates/README.md +34 -0
- package/upgrade/templates/design-stack/templates/.githooks/pre-commit +4 -0
- package/upgrade/templates/design-stack/templates/scripts/dev-local.sh +53 -0
- package/upgrade/templates/design-stack/templates/scripts/dev.sh +25 -0
- package/upgrade/templates/design-stack/templates/scripts/hooks/run-precommit.js +37 -0
- package/upgrade/templates/nestjs-react-fullstack/templates/.githooks/pre-commit +4 -0
- package/upgrade/templates/nestjs-react-fullstack/templates/.gitignore.append +8 -0
- package/upgrade/templates/nestjs-react-fullstack/templates/.spark_project +16 -0
- package/upgrade/templates/nestjs-react-fullstack/templates/drizzle.config.ts +55 -0
- package/upgrade/templates/nestjs-react-fullstack/templates/helper/gen-openapi.ts +34 -0
- package/upgrade/templates/nestjs-react-fullstack/templates/nest-cli.json +25 -0
- package/upgrade/templates/nestjs-react-fullstack/templates/scripts/build.sh +207 -0
- package/upgrade/templates/nestjs-react-fullstack/templates/scripts/dev-local.sh +61 -0
- package/upgrade/templates/nestjs-react-fullstack/templates/scripts/dev.js +295 -0
- package/upgrade/templates/nestjs-react-fullstack/templates/scripts/dev.sh +25 -0
- package/upgrade/templates/nestjs-react-fullstack/templates/scripts/hooks/run-precommit.js +37 -0
- package/upgrade/templates/nestjs-react-fullstack/templates/scripts/lint.js +150 -0
- package/upgrade/templates/nestjs-react-fullstack/templates/scripts/prune-smart.js +330 -0
- package/upgrade/templates/nestjs-react-fullstack/templates/scripts/run.sh +8 -0
- package/upgrade/templates/nestjs-react-fullstack/templates/server/global.d.ts +19 -0
- package/upgrade/templates/nestjs-react-fullstack/templates/tsconfig.node.json +5 -0
|
@@ -0,0 +1,207 @@
|
|
|
1
|
+
#!/usr/bin/env bash
|
|
2
|
+
# This file is auto-generated by @lark-apaas/fullstack-cli
|
|
3
|
+
set -euo pipefail
|
|
4
|
+
|
|
5
|
+
ROOT_DIR="$(pwd)"
|
|
6
|
+
DIST_DIR="$ROOT_DIR/dist"
|
|
7
|
+
|
|
8
|
+
# 记录总开始时间
|
|
9
|
+
TOTAL_START=$(node -e "console.log(Date.now())")
|
|
10
|
+
|
|
11
|
+
# 打印耗时的辅助函数
|
|
12
|
+
print_time() {
|
|
13
|
+
local start=$1
|
|
14
|
+
local end=$(node -e "console.log(Date.now())")
|
|
15
|
+
local elapsed=$((end - start))
|
|
16
|
+
local seconds=$((elapsed / 1000))
|
|
17
|
+
local ms=$((elapsed % 1000))
|
|
18
|
+
echo " ⏱️ 耗时: ${seconds}.$(printf "%03d" $ms)s"
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
# ==================== 步骤 0 ====================
|
|
22
|
+
echo "🗑️ [0/6] 安装插件"
|
|
23
|
+
STEP_START=$(node -e "console.log(Date.now())")
|
|
24
|
+
npx fullstack-cli action-plugin init
|
|
25
|
+
print_time $STEP_START
|
|
26
|
+
echo ""
|
|
27
|
+
|
|
28
|
+
# ==================== 步骤 1 ====================
|
|
29
|
+
echo "📝 [1/6] 更新 openapi 代码"
|
|
30
|
+
STEP_START=$(node -e "console.log(Date.now())")
|
|
31
|
+
npm run gen:openapi
|
|
32
|
+
print_time $STEP_START
|
|
33
|
+
echo ""
|
|
34
|
+
|
|
35
|
+
# ==================== 步骤 2 ====================
|
|
36
|
+
echo "🗑️ [2/6] 清理 dist 目录"
|
|
37
|
+
STEP_START=$(node -e "console.log(Date.now())")
|
|
38
|
+
rm -rf "$ROOT_DIR/dist"
|
|
39
|
+
print_time $STEP_START
|
|
40
|
+
echo ""
|
|
41
|
+
|
|
42
|
+
# ==================== 步骤 3 ====================
|
|
43
|
+
echo "🗺️ [3/6] 生成路由定义"
|
|
44
|
+
STEP_START=$(node -e "console.log(Date.now())")
|
|
45
|
+
|
|
46
|
+
# 在 client/server 构建之前生成到 dist/,供 DefinePlugin 注入前端 bundle
|
|
47
|
+
# 注意:nest-cli.json 中 deleteOutDir 必须为 false(模板默认值),否则 nest build 会清掉 dist/
|
|
48
|
+
echo " ├─ 生成 API 路由定义..."
|
|
49
|
+
npx generate-api-routes --server-dir ./server --out-dir ./dist > /tmp/gen-api-routes.log 2>&1 &
|
|
50
|
+
API_ROUTES_PID=$!
|
|
51
|
+
|
|
52
|
+
echo " ├─ 生成页面路由定义..."
|
|
53
|
+
npx generate-page-routes --app-path ./client/src/app.tsx --out-dir ./dist > /tmp/gen-page-routes.log 2>&1 &
|
|
54
|
+
PAGE_ROUTES_PID=$!
|
|
55
|
+
|
|
56
|
+
API_ROUTES_EXIT=0
|
|
57
|
+
PAGE_ROUTES_EXIT=0
|
|
58
|
+
|
|
59
|
+
wait $API_ROUTES_PID || API_ROUTES_EXIT=$?
|
|
60
|
+
wait $PAGE_ROUTES_PID || PAGE_ROUTES_EXIT=$?
|
|
61
|
+
|
|
62
|
+
if [ $API_ROUTES_EXIT -ne 0 ]; then
|
|
63
|
+
echo " ⚠️ API 路由生成失败(不影响构建)"
|
|
64
|
+
cat /tmp/gen-api-routes.log
|
|
65
|
+
else
|
|
66
|
+
echo " ✅ API 路由生成完成"
|
|
67
|
+
fi
|
|
68
|
+
|
|
69
|
+
if [ $PAGE_ROUTES_EXIT -ne 0 ]; then
|
|
70
|
+
echo " ⚠️ 页面路由生成失败(不影响构建)"
|
|
71
|
+
cat /tmp/gen-page-routes.log
|
|
72
|
+
else
|
|
73
|
+
echo " ✅ 页面路由生成完成"
|
|
74
|
+
fi
|
|
75
|
+
print_time $STEP_START
|
|
76
|
+
echo ""
|
|
77
|
+
|
|
78
|
+
# ==================== 步骤 4 ====================
|
|
79
|
+
echo "🔨 [4/6] 并行构建 server 和 client"
|
|
80
|
+
STEP_START=$(node -e "console.log(Date.now())")
|
|
81
|
+
|
|
82
|
+
# 给 server/client 构建子进程预留 8GB heap,缓解 vite build transform 阶段 OOM
|
|
83
|
+
# (典型错误:Reached heap limit Allocation failed)。
|
|
84
|
+
# 仅在外部未设置 NODE_OPTIONS 时注入,允许 CI / 用户通过外部环境变量完全覆盖
|
|
85
|
+
BUILD_NODE_OPTIONS="${NODE_OPTIONS:---max-old-space-size=8192}"
|
|
86
|
+
|
|
87
|
+
# 根据 only_frontend_change 决定是否构建 server
|
|
88
|
+
if [[ "${only_frontend_change:-false}" == "true" ]]; then
|
|
89
|
+
echo "🔨 [4/6] 仅构建 client (only_frontend_change=true)"
|
|
90
|
+
|
|
91
|
+
echo " ├─ 启动 client 构建..."
|
|
92
|
+
NODE_OPTIONS="$BUILD_NODE_OPTIONS" npm run build:client > /tmp/build-client.log 2>&1
|
|
93
|
+
CLIENT_EXIT=$?
|
|
94
|
+
|
|
95
|
+
if [ $CLIENT_EXIT -ne 0 ]; then
|
|
96
|
+
echo " ❌ Client 构建失败"
|
|
97
|
+
cat /tmp/build-client.log
|
|
98
|
+
exit 1
|
|
99
|
+
fi
|
|
100
|
+
|
|
101
|
+
echo " ✅ Client 构建完成"
|
|
102
|
+
else
|
|
103
|
+
echo "🔨 [4/6] 并行构建 server 和 client"
|
|
104
|
+
|
|
105
|
+
# 并行构建
|
|
106
|
+
echo " ├─ 启动 server 构建..."
|
|
107
|
+
NODE_OPTIONS="$BUILD_NODE_OPTIONS" npm run build:server > /tmp/build-server.log 2>&1 &
|
|
108
|
+
SERVER_PID=$!
|
|
109
|
+
|
|
110
|
+
echo " ├─ 启动 client 构建..."
|
|
111
|
+
NODE_OPTIONS="$BUILD_NODE_OPTIONS" npm run build:client > /tmp/build-client.log 2>&1 &
|
|
112
|
+
CLIENT_PID=$!
|
|
113
|
+
|
|
114
|
+
# 等待两个构建完成
|
|
115
|
+
SERVER_EXIT=0
|
|
116
|
+
CLIENT_EXIT=0
|
|
117
|
+
|
|
118
|
+
wait $SERVER_PID || SERVER_EXIT=$?
|
|
119
|
+
wait $CLIENT_PID || CLIENT_EXIT=$?
|
|
120
|
+
|
|
121
|
+
# 检查构建结果
|
|
122
|
+
if [ $SERVER_EXIT -ne 0 ]; then
|
|
123
|
+
echo " ❌ Server 构建失败"
|
|
124
|
+
cat /tmp/build-server.log
|
|
125
|
+
exit 1
|
|
126
|
+
fi
|
|
127
|
+
|
|
128
|
+
if [ $CLIENT_EXIT -ne 0 ]; then
|
|
129
|
+
echo " ❌ Client 构建失败"
|
|
130
|
+
cat /tmp/build-client.log
|
|
131
|
+
exit 1
|
|
132
|
+
fi
|
|
133
|
+
|
|
134
|
+
echo " ✅ Server 构建完成"
|
|
135
|
+
echo " ✅ Client 构建完成"
|
|
136
|
+
fi
|
|
137
|
+
|
|
138
|
+
print_time $STEP_START
|
|
139
|
+
echo ""
|
|
140
|
+
|
|
141
|
+
# ==================== 步骤 5 ====================
|
|
142
|
+
echo "📦 [5/6] 准备产物"
|
|
143
|
+
STEP_START=$(node -e "console.log(Date.now())")
|
|
144
|
+
|
|
145
|
+
# 移动 client 下的 HTML 文件到 dist/dist/client,保证 views 路径在 dev/prod 下一致
|
|
146
|
+
# 使用 mv 而非 cp:HTML 不能上传到公网 CDN,移走后 dist/client 中不再包含 HTML
|
|
147
|
+
if [ -d "$DIST_DIR/client" ]; then
|
|
148
|
+
mkdir -p "$DIST_DIR/dist/client"
|
|
149
|
+
find "$DIST_DIR/client" -maxdepth 1 -name "*.html" -exec mv {} "$DIST_DIR/dist/client/" \;
|
|
150
|
+
fi
|
|
151
|
+
|
|
152
|
+
# server 相关产物准备(only_frontend_change=true 时跳过)
|
|
153
|
+
if [[ "${only_frontend_change:-false}" == "true" ]]; then
|
|
154
|
+
echo " [skip] 跳过 run.sh/.env 复制 (only_frontend_change=true)"
|
|
155
|
+
else
|
|
156
|
+
# 拷贝 run.sh 到 dist/(prod 从 dist/ 启动,确保 cwd 一致性)
|
|
157
|
+
cp "$ROOT_DIR/scripts/run.sh" "$DIST_DIR/"
|
|
158
|
+
|
|
159
|
+
# 拷贝 .env 文件(如果存在)
|
|
160
|
+
if [ -f "$ROOT_DIR/.env" ]; then
|
|
161
|
+
cp "$ROOT_DIR/.env" "$DIST_DIR/"
|
|
162
|
+
fi
|
|
163
|
+
fi
|
|
164
|
+
|
|
165
|
+
# 清理无用文件
|
|
166
|
+
rm -rf "$DIST_DIR/scripts"
|
|
167
|
+
rm -rf "$DIST_DIR/tsconfig.node.tsbuildinfo"
|
|
168
|
+
|
|
169
|
+
print_time $STEP_START
|
|
170
|
+
echo ""
|
|
171
|
+
|
|
172
|
+
# ==================== 步骤 6 ====================
|
|
173
|
+
echo "✂️ [6/6] 智能依赖裁剪"
|
|
174
|
+
STEP_START=$(node -e "console.log(Date.now())")
|
|
175
|
+
|
|
176
|
+
# 智能依赖裁剪(仅全量构建时执行,纯前端场景不打包 server,无需裁剪)
|
|
177
|
+
if [[ "${only_frontend_change:-false}" == "true" ]]; then
|
|
178
|
+
echo "✂️ [6/6] 跳过智能依赖裁剪 (only_frontend_change=true)"
|
|
179
|
+
else
|
|
180
|
+
echo "✂️ [6/6] 智能依赖裁剪"
|
|
181
|
+
|
|
182
|
+
# 分析实际依赖、复制并裁剪 node_modules、生成精简的 package.json
|
|
183
|
+
node "$ROOT_DIR/scripts/prune-smart.js"
|
|
184
|
+
fi
|
|
185
|
+
|
|
186
|
+
print_time $STEP_START
|
|
187
|
+
echo ""
|
|
188
|
+
|
|
189
|
+
# 总耗时
|
|
190
|
+
echo "构建完成"
|
|
191
|
+
print_time $TOTAL_START
|
|
192
|
+
|
|
193
|
+
# 输出产物信息
|
|
194
|
+
DIST_SIZE=$(du -sh "$DIST_DIR" | cut -f1)
|
|
195
|
+
if [[ "${only_frontend_change:-false}" == "true" ]]; then
|
|
196
|
+
echo ""
|
|
197
|
+
echo "📊 构建产物统计:"
|
|
198
|
+
echo " 产物大小: $DIST_SIZE"
|
|
199
|
+
echo ""
|
|
200
|
+
else
|
|
201
|
+
NODE_MODULES_SIZE=$(du -sh "$DIST_DIR/node_modules" | cut -f1)
|
|
202
|
+
echo ""
|
|
203
|
+
echo "📊 构建产物统计:"
|
|
204
|
+
echo " 产物大小: $DIST_SIZE"
|
|
205
|
+
echo " node_modules: $NODE_MODULES_SIZE"
|
|
206
|
+
echo ""
|
|
207
|
+
fi
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
#!/usr/bin/env bash
|
|
2
|
+
# ============================================================================
|
|
3
|
+
# 本地 dev 启动脚本(平台控制,通过 `miaoda app upgrade` 同步,请勿手改)
|
|
4
|
+
# Stack: nestjs-react-fullstack
|
|
5
|
+
# 流程:env pull → skills sync → 并发起 server + client
|
|
6
|
+
# ============================================================================
|
|
7
|
+
set -euo pipefail
|
|
8
|
+
ROOT_DIR="$(cd "$(dirname "$0")/.." && pwd)"
|
|
9
|
+
cd "$ROOT_DIR"
|
|
10
|
+
|
|
11
|
+
# === Sentinels(必须由本脚本注入,不属于 .env.local 业务范畴)===
|
|
12
|
+
|
|
13
|
+
# Stack 对应 miaoda CLI 的 application scene;沙箱里平台注入,本地脚本兜底,
|
|
14
|
+
# 让后续 miaoda 子命令(skills sync 等)走对的 scene。
|
|
15
|
+
export MIAODA_APP_TYPE="${MIAODA_APP_TYPE:-3}"
|
|
16
|
+
|
|
17
|
+
# 本地开发总开关:SDK(fullstack-vite-preset / fullstack-rspack-preset /
|
|
18
|
+
# fullstack-nestjs-core)据此启用本地兜底逻辑:
|
|
19
|
+
# - vite/rspack-preset:反代 /__runtime__ 到沙箱(带 cookie / webuser header)
|
|
20
|
+
# - nestjs-core:从 cookie 补 csrf header + 注入 webuser header
|
|
21
|
+
# 沙箱 dev / 生产不设此 env,SDK 行为完全不变。
|
|
22
|
+
export MIAODA_LOCAL_DEV=1
|
|
23
|
+
|
|
24
|
+
# 临时:绕 npmmirror/bnpm 内网到 npmjs 的同步延迟,等 coding-steering 正式版去掉。
|
|
25
|
+
export MIAODA_NPM_REGISTRY="${MIAODA_NPM_REGISTRY:-https://registry.npmjs.org/}"
|
|
26
|
+
|
|
27
|
+
# === .env.local / .env 加载由 SDK 侧负责,本脚本不再手工 source ===
|
|
28
|
+
# fullstack-nestjs-core 在 module init 通过 dotenv 读 .env.local + .env;
|
|
29
|
+
# fullstack-{vite,rspack}-preset 的 defineConfig 同理。优先级:shell > .env.local > .env。
|
|
30
|
+
# 历史版本曾在这里用 bash 自写 parser,引号 / 多行 / 行尾注释处理不全且重复造轮子,已下放到 SDK。
|
|
31
|
+
|
|
32
|
+
# 1. 拉本地 dev 所需 ENV(jwt / cookie / sandbox URL 等,写入 .env.local)
|
|
33
|
+
# app_id 从 .spark/meta.json 读 —— `miaoda app init --app-id <id>` 时持久化的权威源,
|
|
34
|
+
# 后续 dev / sync 全部沿用这一个值;不在脚本里 hack 别的推断渠道。
|
|
35
|
+
# lark-cli 子命令真名是 `+env-pull`(带 + 前缀单 token,不是 `env pull` 空格分隔);
|
|
36
|
+
# `--as user` 必须显式:env-pull 只接受 user identity,默认 bot 会校验失败。
|
|
37
|
+
echo "[dev-local] (1/3) env pull..."
|
|
38
|
+
if command -v lark-cli >/dev/null 2>&1; then
|
|
39
|
+
# 用 node 读 .spark/meta.json,不引 python3 依赖(user app 必装 node)。
|
|
40
|
+
APP_ID=$(node -e 'try{process.stdout.write(JSON.parse(require("fs").readFileSync(".spark/meta.json","utf8")).app_id||"")}catch(e){}' 2>/dev/null || echo "")
|
|
41
|
+
if [ -n "$APP_ID" ]; then
|
|
42
|
+
lark-cli apps +env-pull --app-id "$APP_ID" --as user || echo " (env pull 失败,后续按 .env.local 现状继续)"
|
|
43
|
+
else
|
|
44
|
+
echo " (.spark/meta.json 缺 app_id,先跑 \`miaoda app init --app-id <id>\` 写入)"
|
|
45
|
+
fi
|
|
46
|
+
else
|
|
47
|
+
echo " (lark-cli 未装,跳过 env pull;请确保 .env.local 已就绪)"
|
|
48
|
+
fi
|
|
49
|
+
|
|
50
|
+
# 2. 同步 stack 对应 skills(coding-steering)。走 npx 不依赖用户全局装 miaoda;
|
|
51
|
+
# 钉到 @alpha dist-tag 跟随最新发布,miaoda-cli 通道我们自己控制风险小。
|
|
52
|
+
echo "[dev-local] (2/3) miaoda skills sync..."
|
|
53
|
+
npx -y @lark-apaas/miaoda-cli@alpha skills sync || echo " (skills sync 失败,继续启动)"
|
|
54
|
+
|
|
55
|
+
# 3. 并发起 dev:server(NestJS)+ dev:client(vite/rspack)
|
|
56
|
+
# 端口、SANDBOX_* 等业务 env 由 SDK 内 dotenv 加载,不在 banner 里假装显示。
|
|
57
|
+
# 不走 scripts/dev.js(保活 / restart / 日志写文件那套)—— 本地崩就崩,错日志直接 stdout。
|
|
58
|
+
echo "[dev-local] (3/3) 并发起 dev:server + dev:client"
|
|
59
|
+
exec npx --no-install concurrently \
|
|
60
|
+
--names "server,client" --prefix-colors "blue,green" --kill-others-on-fail \
|
|
61
|
+
"npm run dev:server" "npm run dev:client"
|
|
@@ -0,0 +1,295 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
'use strict';
|
|
3
|
+
|
|
4
|
+
const fs = require('fs');
|
|
5
|
+
const path = require('path');
|
|
6
|
+
const { spawn, execSync } = require('child_process');
|
|
7
|
+
const readline = require('readline');
|
|
8
|
+
|
|
9
|
+
// ── Project root ──────────────────────────────────────────────────────────────
|
|
10
|
+
const PROJECT_ROOT = path.resolve(__dirname, '..');
|
|
11
|
+
process.chdir(PROJECT_ROOT);
|
|
12
|
+
|
|
13
|
+
// ── Load .env ─────────────────────────────────────────────────────────────────
|
|
14
|
+
function loadEnv() {
|
|
15
|
+
const envPath = path.join(PROJECT_ROOT, '.env');
|
|
16
|
+
if (!fs.existsSync(envPath)) return;
|
|
17
|
+
const lines = fs.readFileSync(envPath, 'utf8').split('\n');
|
|
18
|
+
for (const line of lines) {
|
|
19
|
+
const trimmed = line.trim();
|
|
20
|
+
if (!trimmed || trimmed.startsWith('#')) continue;
|
|
21
|
+
const eqIdx = trimmed.indexOf('=');
|
|
22
|
+
if (eqIdx === -1) continue;
|
|
23
|
+
const key = trimmed.slice(0, eqIdx).trim();
|
|
24
|
+
const value = trimmed.slice(eqIdx + 1).trim();
|
|
25
|
+
if (!(key in process.env)) {
|
|
26
|
+
process.env[key] = value;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
loadEnv();
|
|
31
|
+
|
|
32
|
+
// ── Configuration ─────────────────────────────────────────────────────────────
|
|
33
|
+
const LOG_DIR = process.env.LOG_DIR || 'logs';
|
|
34
|
+
const MAX_RESTART_COUNT = process.env.MAX_RESTART_COUNT != null && process.env.MAX_RESTART_COUNT !== ''
|
|
35
|
+
? parseInt(process.env.MAX_RESTART_COUNT, 10)
|
|
36
|
+
: Infinity;
|
|
37
|
+
const RESTART_DELAY = parseInt(process.env.RESTART_DELAY, 10) || 2;
|
|
38
|
+
const MAX_DELAY = 8;
|
|
39
|
+
const SERVER_PORT = process.env.SERVER_PORT || '3000';
|
|
40
|
+
const CLIENT_DEV_PORT = process.env.CLIENT_DEV_PORT || '8080';
|
|
41
|
+
|
|
42
|
+
fs.mkdirSync(LOG_DIR, { recursive: true });
|
|
43
|
+
|
|
44
|
+
// ── Logging infrastructure ────────────────────────────────────────────────────
|
|
45
|
+
const devStdLogPath = path.join(LOG_DIR, 'dev.std.log');
|
|
46
|
+
const devLogPath = path.join(LOG_DIR, 'dev.log');
|
|
47
|
+
const devStdLogFd = fs.openSync(devStdLogPath, 'a');
|
|
48
|
+
const devLogFd = fs.openSync(devLogPath, 'a');
|
|
49
|
+
|
|
50
|
+
function timestamp() {
|
|
51
|
+
const now = new Date();
|
|
52
|
+
return (
|
|
53
|
+
now.getFullYear() + '-' +
|
|
54
|
+
String(now.getMonth() + 1).padStart(2, '0') + '-' +
|
|
55
|
+
String(now.getDate()).padStart(2, '0') + ' ' +
|
|
56
|
+
String(now.getHours()).padStart(2, '0') + ':' +
|
|
57
|
+
String(now.getMinutes()).padStart(2, '0') + ':' +
|
|
58
|
+
String(now.getSeconds()).padStart(2, '0')
|
|
59
|
+
);
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
/** Write to terminal + dev.std.log */
|
|
63
|
+
function writeOutput(msg) {
|
|
64
|
+
try { process.stdout.write(msg); } catch {}
|
|
65
|
+
try { fs.writeSync(devStdLogFd, msg); } catch {}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/** Structured event log → terminal + dev.std.log + dev.log */
|
|
69
|
+
function logEvent(level, name, message) {
|
|
70
|
+
const msg = `[${timestamp()}] [${level}] [${name}] ${message}\n`;
|
|
71
|
+
writeOutput(msg);
|
|
72
|
+
try { fs.writeSync(devLogFd, msg); } catch {}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// ── Process group management ──────────────────────────────────────────────────
|
|
76
|
+
function killProcessGroup(pid, signal) {
|
|
77
|
+
try {
|
|
78
|
+
process.kill(-pid, signal);
|
|
79
|
+
} catch {}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function killOrphansByPort(port) {
|
|
83
|
+
try {
|
|
84
|
+
const pids = execSync(`lsof -ti :${port}`, { encoding: 'utf8', timeout: 5000 }).trim();
|
|
85
|
+
if (pids) {
|
|
86
|
+
const pidList = pids.split('\n').filter(Boolean);
|
|
87
|
+
for (const p of pidList) {
|
|
88
|
+
try { process.kill(parseInt(p, 10), 'SIGKILL'); } catch {}
|
|
89
|
+
}
|
|
90
|
+
return pidList;
|
|
91
|
+
}
|
|
92
|
+
} catch {}
|
|
93
|
+
return [];
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
// ── Process supervision ───────────────────────────────────────────────────────
|
|
97
|
+
let stopping = false;
|
|
98
|
+
const managedProcesses = []; // { name, pid, child }
|
|
99
|
+
|
|
100
|
+
function sleep(ms) {
|
|
101
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Start and supervise a process with auto-restart and log piping.
|
|
106
|
+
* Returns a promise that resolves when the process loop ends.
|
|
107
|
+
*/
|
|
108
|
+
function startProcess({ name, command, args, cleanupPort }) {
|
|
109
|
+
const logFilePath = path.join(LOG_DIR, `${name}.std.log`);
|
|
110
|
+
const logFd = fs.openSync(logFilePath, 'a');
|
|
111
|
+
|
|
112
|
+
const entry = { name, pid: null, child: null };
|
|
113
|
+
managedProcesses.push(entry);
|
|
114
|
+
|
|
115
|
+
const run = async () => {
|
|
116
|
+
let restartCount = 0;
|
|
117
|
+
|
|
118
|
+
while (!stopping) {
|
|
119
|
+
const child = spawn(command, args, {
|
|
120
|
+
detached: true,
|
|
121
|
+
stdio: ['ignore', 'pipe', 'pipe'],
|
|
122
|
+
shell: true,
|
|
123
|
+
cwd: PROJECT_ROOT,
|
|
124
|
+
env: { ...process.env },
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
entry.pid = child.pid;
|
|
128
|
+
entry.child = child;
|
|
129
|
+
|
|
130
|
+
const startTime = Date.now();
|
|
131
|
+
logEvent('INFO', name, `Process started (PGID: ${child.pid}): ${command} ${args.join(' ')}`);
|
|
132
|
+
|
|
133
|
+
// Pipe stdout and stderr through readline for timestamped logging
|
|
134
|
+
const pipeLines = (stream) => {
|
|
135
|
+
const rl = readline.createInterface({ input: stream, crlfDelay: Infinity });
|
|
136
|
+
rl.on('line', (line) => {
|
|
137
|
+
const msg = `[${timestamp()}] [${name}] ${line}\n`;
|
|
138
|
+
try { fs.writeSync(logFd, msg); } catch {}
|
|
139
|
+
writeOutput(msg);
|
|
140
|
+
});
|
|
141
|
+
};
|
|
142
|
+
if (child.stdout) pipeLines(child.stdout);
|
|
143
|
+
if (child.stderr) pipeLines(child.stderr);
|
|
144
|
+
|
|
145
|
+
// Wait for the direct child to exit.
|
|
146
|
+
// NOTE: must use 'exit', not 'close'. With shell:true, grandchild processes
|
|
147
|
+
// (e.g. nest's server) inherit stdout pipes. 'close' won't fire until ALL
|
|
148
|
+
// pipe holders exit, causing dev.js to hang when npm/nest dies but server survives.
|
|
149
|
+
const exitCode = await new Promise((resolve) => {
|
|
150
|
+
child.on('exit', (code) => resolve(code ?? 1));
|
|
151
|
+
child.on('error', () => resolve(1));
|
|
152
|
+
});
|
|
153
|
+
|
|
154
|
+
// Kill the entire process group
|
|
155
|
+
if (entry.pid) {
|
|
156
|
+
killProcessGroup(entry.pid, 'SIGTERM');
|
|
157
|
+
await sleep(2000);
|
|
158
|
+
killProcessGroup(entry.pid, 'SIGKILL');
|
|
159
|
+
}
|
|
160
|
+
entry.pid = null;
|
|
161
|
+
entry.child = null;
|
|
162
|
+
|
|
163
|
+
// Port cleanup fallback
|
|
164
|
+
if (cleanupPort) {
|
|
165
|
+
const orphans = killOrphansByPort(cleanupPort);
|
|
166
|
+
if (orphans.length > 0) {
|
|
167
|
+
logEvent('WARN', name, `Killed orphan processes on port ${cleanupPort}: ${orphans.join(' ')}`);
|
|
168
|
+
await sleep(500);
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
if (stopping) break;
|
|
173
|
+
|
|
174
|
+
const runDuration = (Date.now() - startTime) / 1000;
|
|
175
|
+
if (runDuration >= 60) {
|
|
176
|
+
restartCount = 0;
|
|
177
|
+
logEvent('INFO', name, `Ran for ${Math.round(runDuration)}s, resetting restart counter`);
|
|
178
|
+
} else {
|
|
179
|
+
restartCount++;
|
|
180
|
+
}
|
|
181
|
+
if (restartCount >= MAX_RESTART_COUNT) {
|
|
182
|
+
logEvent('ERROR', name, `Max restart count (${MAX_RESTART_COUNT}) reached, giving up`);
|
|
183
|
+
break;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
const delay = Math.min(RESTART_DELAY * (1 << Math.max(0, restartCount - 1)), MAX_DELAY);
|
|
187
|
+
logEvent('WARN', name, `Process exited with code ${exitCode}, restarting (${restartCount}/${MAX_RESTART_COUNT}) in ${delay}s...`);
|
|
188
|
+
await sleep(delay * 1000);
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
try { fs.closeSync(logFd); } catch {}
|
|
192
|
+
};
|
|
193
|
+
|
|
194
|
+
return run();
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
// ── Cleanup ───────────────────────────────────────────────────────────────────
|
|
198
|
+
let cleanupDone = false;
|
|
199
|
+
|
|
200
|
+
async function cleanup() {
|
|
201
|
+
if (cleanupDone) return;
|
|
202
|
+
cleanupDone = true;
|
|
203
|
+
stopping = true;
|
|
204
|
+
|
|
205
|
+
logEvent('INFO', 'main', 'Shutting down all processes...');
|
|
206
|
+
|
|
207
|
+
// Kill all managed process groups
|
|
208
|
+
for (const entry of managedProcesses) {
|
|
209
|
+
if (entry.pid) {
|
|
210
|
+
logEvent('INFO', 'main', `Stopping process group (PGID: ${entry.pid})`);
|
|
211
|
+
killProcessGroup(entry.pid, 'SIGTERM');
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
// Wait for graceful shutdown
|
|
216
|
+
await sleep(2000);
|
|
217
|
+
|
|
218
|
+
// Force kill any remaining
|
|
219
|
+
for (const entry of managedProcesses) {
|
|
220
|
+
if (entry.pid) {
|
|
221
|
+
logEvent('WARN', 'main', `Force killing process group (PGID: ${entry.pid})`);
|
|
222
|
+
killProcessGroup(entry.pid, 'SIGKILL');
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
// Port cleanup fallback
|
|
227
|
+
killOrphansByPort(SERVER_PORT);
|
|
228
|
+
killOrphansByPort(CLIENT_DEV_PORT);
|
|
229
|
+
|
|
230
|
+
logEvent('INFO', 'main', 'All processes stopped');
|
|
231
|
+
|
|
232
|
+
try { fs.closeSync(devStdLogFd); } catch {}
|
|
233
|
+
try { fs.closeSync(devLogFd); } catch {}
|
|
234
|
+
|
|
235
|
+
process.exit(0);
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
process.on('SIGTERM', cleanup);
|
|
239
|
+
process.on('SIGINT', cleanup);
|
|
240
|
+
process.on('SIGHUP', cleanup);
|
|
241
|
+
|
|
242
|
+
// Stale dist makes nest --watch skip missing files; watcher won't self-heal.
|
|
243
|
+
function cleanStaleDist() {
|
|
244
|
+
const distPath = path.join(PROJECT_ROOT, 'dist');
|
|
245
|
+
if (fs.existsSync(distPath)) {
|
|
246
|
+
fs.rmSync(distPath, { recursive: true, force: true });
|
|
247
|
+
logEvent('INFO', 'main', 'Cleaned dist/ to force full rebuild');
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
// ── Main ──────────────────────────────────────────────────────────────────────
|
|
252
|
+
async function main() {
|
|
253
|
+
logEvent('INFO', 'main', '========== Dev session started ==========');
|
|
254
|
+
|
|
255
|
+
cleanStaleDist();
|
|
256
|
+
|
|
257
|
+
// Initialize action plugins
|
|
258
|
+
writeOutput('\n🔌 Initializing action plugins...\n');
|
|
259
|
+
try {
|
|
260
|
+
execSync('fullstack-cli action-plugin init', { cwd: PROJECT_ROOT, stdio: 'inherit' });
|
|
261
|
+
writeOutput('✅ Action plugins initialized\n\n');
|
|
262
|
+
} catch {
|
|
263
|
+
writeOutput('⚠️ Action plugin initialization failed, continuing anyway...\n\n');
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
// Start server and client
|
|
267
|
+
const serverPromise = startProcess({
|
|
268
|
+
name: 'server',
|
|
269
|
+
command: 'npm',
|
|
270
|
+
args: ['run', 'dev:server'],
|
|
271
|
+
cleanupPort: SERVER_PORT,
|
|
272
|
+
});
|
|
273
|
+
|
|
274
|
+
const clientPromise = startProcess({
|
|
275
|
+
name: 'client',
|
|
276
|
+
command: 'npm',
|
|
277
|
+
args: ['run', 'dev:client'],
|
|
278
|
+
cleanupPort: CLIENT_DEV_PORT,
|
|
279
|
+
});
|
|
280
|
+
|
|
281
|
+
writeOutput(`📋 Dev processes running. Press Ctrl+C to stop.\n`);
|
|
282
|
+
writeOutput(`📄 Logs: ${devStdLogPath}\n\n`);
|
|
283
|
+
|
|
284
|
+
// Wait for both (they loop until stopping or max restarts)
|
|
285
|
+
await Promise.all([serverPromise, clientPromise]);
|
|
286
|
+
|
|
287
|
+
if (!cleanupDone) {
|
|
288
|
+
await cleanup();
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
main().catch((err) => {
|
|
293
|
+
console.error('Fatal error:', err);
|
|
294
|
+
process.exit(1);
|
|
295
|
+
});
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
#!/usr/bin/env bash
|
|
2
|
+
# `npm run dev` 入口;按 SANDBOX_ID 是否非空判断运行环境:
|
|
3
|
+
# - SANDBOX_ID 非空(沙箱平台注入应用所属沙箱 ID)→ 直接跑 dev.js
|
|
4
|
+
# (保活 / restart loop / 文件日志 —— 沙箱生产形态)。脚本同步由平台 pod 启动阶段做过,
|
|
5
|
+
# dev 入口不再额外 `npm run upgrade`。
|
|
6
|
+
# - 否则(本地)→ 走 miaoda app sync 兜底 + 跑 dev-local.sh:纯 stdout、崩了就崩、Agent 友好。
|
|
7
|
+
# 显式想跑本地路径可用 `npm run dev:local`(绕过 SANDBOX_ID 判断)。
|
|
8
|
+
set -euo pipefail
|
|
9
|
+
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
|
|
10
|
+
|
|
11
|
+
if [ -n "${SANDBOX_ID:-}" ]; then
|
|
12
|
+
exec node "$SCRIPT_DIR/dev.js" "$@"
|
|
13
|
+
fi
|
|
14
|
+
|
|
15
|
+
if [ ! -x "$SCRIPT_DIR/dev-local.sh" ]; then
|
|
16
|
+
echo "[dev] scripts/dev-local.sh 缺失或不可执行;先跑 \`npx -y @lark-apaas/miaoda-cli@alpha app sync\` 同步平台脚本" >&2
|
|
17
|
+
exit 1
|
|
18
|
+
fi
|
|
19
|
+
|
|
20
|
+
# 本地启动前先跑一次 miaoda app sync:同步 platform-controlled 内容 + 升 @lark-apaas/* 到
|
|
21
|
+
# 钉版 + 迁移老 npm scripts。沙箱不走这里(SANDBOX_ID 分支已经 exec return)。
|
|
22
|
+
# 走 npx 不依赖用户全局装 miaoda;钉到 @alpha dist-tag 跟随最新发布。
|
|
23
|
+
npx -y @lark-apaas/miaoda-cli@alpha app sync || echo "[dev] miaoda app sync 失败,按现状继续" >&2
|
|
24
|
+
|
|
25
|
+
exec "$SCRIPT_DIR/dev-local.sh" "$@"
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
// FULLSTACK_PRECOMMIT_V1
|
|
3
|
+
'use strict';
|
|
4
|
+
|
|
5
|
+
const { spawnSync } = require('node:child_process');
|
|
6
|
+
|
|
7
|
+
const SEP = ' ' + '─'.repeat(36);
|
|
8
|
+
|
|
9
|
+
function failAndExit(step, body) {
|
|
10
|
+
process.stderr.write('\n✗ pre-commit failed: ' + step + '\n');
|
|
11
|
+
process.stderr.write(SEP + '\n');
|
|
12
|
+
if (body && body.length > 0) {
|
|
13
|
+
process.stderr.write(body.replace(/\s+$/, '') + '\n');
|
|
14
|
+
}
|
|
15
|
+
process.stderr.write(SEP + '\n');
|
|
16
|
+
process.stderr.write(' bypass: git commit --no-verify\n');
|
|
17
|
+
process.exit(1);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function runLint() {
|
|
21
|
+
const cwd = process.cwd();
|
|
22
|
+
const res = spawnSync('npm', ['run', 'lint'], {
|
|
23
|
+
cwd,
|
|
24
|
+
stdio: ['ignore', 'pipe', 'pipe'],
|
|
25
|
+
env: process.env,
|
|
26
|
+
});
|
|
27
|
+
if (res.error) {
|
|
28
|
+
failAndExit('lint', String(res.error.message || res.error));
|
|
29
|
+
}
|
|
30
|
+
if (res.status !== 0) {
|
|
31
|
+
const stdout = res.stdout ? res.stdout.toString() : '';
|
|
32
|
+
const stderr = res.stderr ? res.stderr.toString() : '';
|
|
33
|
+
failAndExit('lint', stdout + '\n' + stderr);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
runLint();
|