@supersdk/supercli 0.3.2 → 0.3.3-staging.47

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.
Files changed (107) hide show
  1. package/dist/build/artifactIntegrity.js +275 -0
  2. package/dist/build/artifactIntegrity.js.map +1 -0
  3. package/dist/build/projectBuild.js +335 -0
  4. package/dist/build/projectBuild.js.map +1 -0
  5. package/dist/cli.js +20 -11
  6. package/dist/cli.js.map +1 -1
  7. package/dist/commands/add.js +111 -57
  8. package/dist/commands/add.js.map +1 -1
  9. package/dist/commands/analytics.js +15 -9
  10. package/dist/commands/analytics.js.map +1 -1
  11. package/dist/commands/build.js +533 -0
  12. package/dist/commands/build.js.map +1 -0
  13. package/dist/commands/db.js +24 -19
  14. package/dist/commands/db.js.map +1 -1
  15. package/dist/commands/deploy.js +434 -751
  16. package/dist/commands/deploy.js.map +1 -1
  17. package/dist/commands/env.js +8 -8
  18. package/dist/commands/env.js.map +1 -1
  19. package/dist/commands/image.js +31 -20
  20. package/dist/commands/image.js.map +1 -1
  21. package/dist/commands/init.js +81 -57
  22. package/dist/commands/init.js.map +1 -1
  23. package/dist/commands/logs.js +16 -7
  24. package/dist/commands/logs.js.map +1 -1
  25. package/dist/commands/publishSite.js +28 -943
  26. package/dist/commands/publishSite.js.map +1 -1
  27. package/dist/commands/register.js +84 -218
  28. package/dist/commands/register.js.map +1 -1
  29. package/dist/commands/settings.js +10 -6
  30. package/dist/commands/settings.js.map +1 -1
  31. package/dist/config/appConfig.js +48 -171
  32. package/dist/config/appConfig.js.map +1 -1
  33. package/dist/config/superConfig.js +457 -0
  34. package/dist/config/superConfig.js.map +1 -0
  35. package/dist/deploy/adapterTypes.js +3 -0
  36. package/dist/deploy/adapterTypes.js.map +1 -0
  37. package/dist/deploy/manifest.js +403 -0
  38. package/dist/deploy/manifest.js.map +1 -0
  39. package/dist/deploy/preflight.js +201 -0
  40. package/dist/deploy/preflight.js.map +1 -0
  41. package/dist/deploy/resultWriter.js +178 -0
  42. package/dist/deploy/resultWriter.js.map +1 -0
  43. package/dist/deploy/runAdapter.js +302 -0
  44. package/dist/deploy/runAdapter.js.map +1 -0
  45. package/dist/deploy/siteAdapter.js +182 -0
  46. package/dist/deploy/siteAdapter.js.map +1 -0
  47. package/dist/docker/dockerCli.js +110 -53
  48. package/dist/docker/dockerCli.js.map +1 -1
  49. package/dist/output/envelope.js +5 -1
  50. package/dist/output/envelope.js.map +1 -1
  51. package/dist/output/resultPrinter.js +3 -0
  52. package/dist/output/resultPrinter.js.map +1 -1
  53. package/dist/publish/responseClassifier.js +4 -4
  54. package/dist/publish/responseClassifier.js.map +1 -1
  55. package/dist/publish/runClient.js +11 -11
  56. package/dist/publish/runClient.js.map +1 -1
  57. package/dist/publish/siteZip.js +4 -5
  58. package/dist/publish/siteZip.js.map +1 -1
  59. package/dist/template/README.md +26 -257
  60. package/dist/template/android/app/src/main/java/com/supersdk/simple/EnvironmentStore.java +1 -1
  61. package/dist/template/android/app/src/main/java/com/supersdk/simple/SimpleApplication.java +2 -2
  62. package/dist/template/android/gradle/supersdk-config-reader.gradle +13 -61
  63. package/dist/template/android/scripts/build.sh +11 -0
  64. package/dist/template/android/supersdk-default.config.json +3 -2
  65. package/dist/template/ios/Sources/App/AppDelegate.swift +1 -1
  66. package/dist/template/ios/Sources/App/SimpleSDKBootstrap.swift +1 -1
  67. package/dist/template/ios/project.yml +1 -1
  68. package/dist/template/ios/scripts/build.sh +19 -0
  69. package/dist/template/ios/scripts/render_project.py +60 -0
  70. package/dist/template/ios/supersdk-default.config.json +6 -0
  71. package/dist/template/service/Dockerfile +2 -0
  72. package/dist/template/service/scripts/build.sh +11 -0
  73. package/dist/template/super.json +62 -0
  74. package/dist/template/template_manifest.json +6 -9
  75. package/dist/template/web/package.json +8 -0
  76. package/dist/template/web/scripts/build.sh +8 -0
  77. package/dist/template/web/scripts/web_package.py +490 -0
  78. package/dist/template/web/src/js/app.js +2 -2
  79. package/dist/util/shell.js +3 -3
  80. package/dist/workspace/config.js +69 -183
  81. package/dist/workspace/config.js.map +1 -1
  82. package/dist/workspace/identity.js +10 -18
  83. package/dist/workspace/identity.js.map +1 -1
  84. package/dist/workspace/projectContext.js +93 -0
  85. package/dist/workspace/projectContext.js.map +1 -0
  86. package/dist/workspace/projectIdentity.js +50 -98
  87. package/dist/workspace/projectIdentity.js.map +1 -1
  88. package/dist/workspace/template.js +208 -83
  89. package/dist/workspace/template.js.map +1 -1
  90. package/package.json +4 -2
  91. package/dist/publish/siteConfig.js +0 -649
  92. package/dist/publish/siteConfig.js.map +0 -1
  93. package/dist/template/scripts/android_snapshot_canary.py +0 -145
  94. package/dist/template/scripts/build_android.sh +0 -366
  95. package/dist/template/scripts/build_ios.sh +0 -299
  96. package/dist/template/scripts/build_site.sh +0 -43
  97. package/dist/template/scripts/lib/workspace.sh +0 -43
  98. package/dist/template/scripts/serve_web.py +0 -80
  99. package/dist/template/scripts/super_build_orchestrator.py +0 -421
  100. package/dist/template/scripts/supersdk_template.py +0 -1161
  101. package/dist/template/scripts/web_host_validator.py +0 -103
  102. package/dist/template/scripts/web_package.py +0 -295
  103. package/dist/template/scripts/ws03_shared_schema_merge_guard.py +0 -73
  104. package/dist/template/super_build.sh +0 -144
  105. package/dist/template/supersdk.json +0 -35
  106. package/dist/workspace/inspect.js +0 -274
  107. package/dist/workspace/inspect.js.map +0 -1
@@ -1,299 +0,0 @@
1
- #!/usr/bin/env bash
2
- set -euo pipefail
3
-
4
- SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
5
- # shellcheck source=lib/workspace.sh
6
- . "$SCRIPT_DIR/lib/workspace.sh"
7
- WORKSPACE_ROOT="$(supersdk_require_workspace_root "$SCRIPT_DIR")"
8
- PYTHON_BIN="${PYTHON_BIN:-python3}"
9
- HELPER="$WORKSPACE_ROOT/scripts/supersdk_template.py"
10
- CONFIG="$WORKSPACE_ROOT/supersdk.json"
11
- PROJECT_DIR=""
12
-
13
- usage() {
14
- echo "Usage: scripts/build_ios.sh --build [--project DIR]" >&2
15
- }
16
-
17
- build_seen=false
18
- while [ "$#" -gt 0 ]; do
19
- case "$1" in
20
- --build)
21
- if [ "$build_seen" = true ]; then
22
- usage
23
- exit 2
24
- fi
25
- build_seen=true
26
- shift
27
- ;;
28
- --project)
29
- if [ "$#" -lt 2 ]; then
30
- usage
31
- exit 2
32
- fi
33
- PROJECT_DIR="$2"
34
- shift 2
35
- ;;
36
- *)
37
- echo "error: expected --build" >&2
38
- exit 2
39
- ;;
40
- esac
41
- done
42
-
43
- if [ -z "$PROJECT_DIR" ]; then
44
- PROJECT_DIR="$("$PYTHON_BIN" "$HELPER" resolve --config "$CONFIG" --platform ios)"
45
- fi
46
-
47
- IOS_DIR="$WORKSPACE_ROOT/$PROJECT_DIR"
48
- GENERATED_DIR="$IOS_DIR/.supersdk/generated"
49
- GENERATED_SPEC="$GENERATED_DIR/project.generated.yml"
50
-
51
- "$PYTHON_BIN" - "$WORKSPACE_ROOT" "$PROJECT_DIR" <<'PY'
52
- import json
53
- import importlib.util
54
- import re
55
- import sys
56
- from pathlib import Path
57
-
58
-
59
- def fail_invalid(reason):
60
- print(f"ios managed config invalid: {reason}", file=sys.stderr)
61
- sys.exit(1)
62
-
63
-
64
- def quote(value):
65
- return json.dumps(str(value))
66
-
67
-
68
- def swift_string(value):
69
- # spid 被下面的 ASCII 约束限死,所以 json.dumps 就是一个合法的 Swift 字符串字面量。
70
- return json.dumps(str(value))
71
-
72
-
73
- def load_template_contract(workspace_root):
74
- helper_path = workspace_root / "scripts" / "supersdk_template.py"
75
- spec = importlib.util.spec_from_file_location("supersdk_template_contract", helper_path)
76
- if spec is None or spec.loader is None:
77
- fail_invalid("supersdk_template.py contract cannot be loaded")
78
- module = importlib.util.module_from_spec(spec)
79
- spec.loader.exec_module(module)
80
- return module
81
-
82
-
83
- workspace_root = Path(sys.argv[1]).resolve()
84
- project_dir = sys.argv[2]
85
- template_contract = load_template_contract(workspace_root)
86
- ios_dir = workspace_root / project_dir
87
- managed_path = ios_dir / ".supersdk" / "managed" / "config.json"
88
- generated_dir = ios_dir / ".supersdk" / "generated"
89
- generated_spec = generated_dir / "project.generated.yml"
90
- simple_config = generated_dir / "SimpleConfig.swift"
91
-
92
- if not managed_path.is_file():
93
- print(
94
- f"ios managed config missing; run scripts/supersdk_template.py inject --platform ios --project {project_dir} "
95
- "or ./super_build.sh --platform ios --build",
96
- file=sys.stderr,
97
- )
98
- sys.exit(1)
99
-
100
- try:
101
- managed_text = managed_path.read_text(encoding="utf-8")
102
- except (OSError, UnicodeDecodeError) as exc:
103
- fail_invalid(f"config.json is unreadable: {exc}")
104
-
105
- try:
106
- managed = json.loads(managed_text)
107
- except json.JSONDecodeError:
108
- fail_invalid("config.json is not valid JSON")
109
-
110
- if not isinstance(managed, dict):
111
- fail_invalid("root must be an object")
112
- if set(managed) != set(template_contract.MANAGED_CONFIG_KEYS):
113
- fail_invalid("top-level keys must match managed config schema")
114
- if managed.get("managed_by") != template_contract.MANAGED_BY:
115
- fail_invalid(f"managed_by must be {template_contract.MANAGED_BY}")
116
- if managed.get("schema_version") != template_contract.MANAGED_CONFIG_SCHEMA_VERSION:
117
- fail_invalid(f"schema_version must be {template_contract.MANAGED_CONFIG_SCHEMA_VERSION}")
118
- # 类型名就是 managed config 里的 platform 值;这个字面量必须跟着 PROJECT_TYPES 走。
119
- if managed.get("platform") != "ios":
120
- fail_invalid("platform must be ios")
121
-
122
- platform_config = managed.get("platform_config")
123
- if not isinstance(platform_config, dict):
124
- fail_invalid("platform_config must be an object")
125
- spm_mode = platform_config.get("spmMode", "mock")
126
-
127
- if spm_mode == "real":
128
- print("spmMode=real is not supported until SDK-F040 remote SPM is ready", file=sys.stderr)
129
- sys.exit(1)
130
- if spm_mode != "mock":
131
- fail_invalid("platform_config.spmMode must be mock")
132
-
133
- spid = managed.get("spid")
134
- if not isinstance(spid, str) or not re.fullmatch(template_contract.SPID_PATTERN, spid):
135
- fail_invalid(f"spid must match ^{template_contract.SPID_PATTERN}$")
136
- # 🔴 这里**没有** environment:它是运行时 init 参数,不进构建产物。
137
- # demo 在运行时切换环境(见 Sources/App/EnvironmentStore.swift)。
138
- name = managed.get("name")
139
- if not isinstance(name, str) or not name:
140
- fail_invalid("name must be a non-empty string")
141
- version = managed.get("version")
142
- if not isinstance(version, dict):
143
- fail_invalid("version must be an object")
144
- version_name = version.get("name")
145
- if not isinstance(version_name, str) or not version_name:
146
- fail_invalid("version.name must be a non-empty string")
147
- version_code = version.get("code")
148
- if not template_contract.is_positive_int(version_code):
149
- fail_invalid("version.code must be a positive integer")
150
-
151
- # 没有推导兜底:App 身份来自 projects[].bundleId,否则构建失败。
152
- bundle_id = platform_config.get("bundleId")
153
- if bundle_id is None:
154
- fail_invalid(
155
- f"platform_config.bundleId is required; set bundleId on the ios project in {template_contract.CONFIG_FILENAME}"
156
- )
157
- if not isinstance(bundle_id, str) or not re.fullmatch(template_contract.BUNDLE_ID_PATTERN, bundle_id):
158
- fail_invalid("platform_config.bundleId must be a bundle identifier")
159
- mock_package = (ios_dir / "MockSuperSDKPackage").resolve()
160
- source_spec = ios_dir / "project.yml"
161
-
162
- if not source_spec.is_file():
163
- print("ios project.yml missing", file=sys.stderr)
164
- sys.exit(1)
165
-
166
- generated_dir.mkdir(parents=True, exist_ok=True)
167
- simple_config.write_text(
168
- """import Foundation
169
-
170
- enum SimpleConfig {
171
- static let spid = %s
172
- }
173
- """ % swift_string(spid),
174
- encoding="utf-8",
175
- )
176
-
177
- template = source_spec.read_text(encoding="utf-8")
178
- replacements = {
179
- "__SUPERSDK_MOCK_PACKAGE_PATH__": quote(mock_package),
180
- "__SUPERSDK_APP_SOURCES_PATH__": quote(ios_dir / "Sources" / "App"),
181
- "__SUPERSDK_SIMPLE_CONFIG_PATH__": quote(simple_config),
182
- "__SUPERSDK_RESOURCES_PATH__": quote(ios_dir / "Resources"),
183
- "__SUPERSDK_BUNDLE_ID__": quote(bundle_id),
184
- "__SUPERSDK_VERSION_NAME__": quote(version_name),
185
- "__SUPERSDK_VERSION_CODE__": str(version_code),
186
- "__SUPERSDK_INFOPLIST_PATH__": quote(ios_dir / "Resources" / "Info.plist"),
187
- "__SUPERSDK_DISPLAY_NAME__": quote(name),
188
- "__SUPERSDK_TEST_SOURCES_PATH__": quote(ios_dir / "Tests" / "SimpleAppTests"),
189
- }
190
- template_tokens = set(re.findall(r"__SUPERSDK_[A-Z0-9_]+__", template))
191
- unknown_tokens = sorted(template_tokens - set(replacements))
192
- if unknown_tokens:
193
- print(f"ios project.yml has unresolved placeholder(s): {', '.join(unknown_tokens)}", file=sys.stderr)
194
- sys.exit(1)
195
- rendered = re.sub(r"__SUPERSDK_[A-Z0-9_]+__", lambda match: replacements[match.group(0)], template)
196
-
197
- generated_spec.write_text(rendered, encoding="utf-8")
198
- print(f"rendered {generated_spec}")
199
- PY
200
-
201
- # SDK 内部的 Teamwork 评审闸门。第三方模板用户不应设置 SUPERSDK_CI_GATE;
202
- # 本地正常构建走下面的模拟器自动选择。
203
- if [ "${SUPERSDK_CI_GATE:-}" = "ws03-ios-xcodegen-generate" ] && [ "$(uname -s)" != "Darwin" ]; then
204
- echo "IOS_MACOS_EXECUTOR_MISSING: ws03-ios-xcodegen-generate requires a macOS/Xcode runner" >&2
205
- exit 1
206
- fi
207
-
208
- if ! command -v xcodegen >/dev/null 2>&1; then
209
- if [ "${SUPERSDK_CI_GATE:-}" = "ws03-ios-xcodegen-generate" ]; then
210
- echo "xcodegen missing for non-skippable ws03-ios-xcodegen-generate gate" >&2
211
- exit 1
212
- fi
213
- echo "SKIP_XCODEGEN_MISSING: xcodegen not installed" >&2
214
- exit 0
215
- fi
216
-
217
- xcodegen generate --project "$IOS_DIR" --spec "$GENERATED_SPEC"
218
-
219
- if ! command -v xcodebuild >/dev/null 2>&1; then
220
- if [ "${SUPERSDK_CI_GATE:-}" = "ws03-ios-xcodegen-generate" ]; then
221
- echo "xcodebuild missing for non-skippable ws03-ios-xcodegen-generate gate" >&2
222
- exit 1
223
- fi
224
- echo "SKIP_XCODEBUILD_MISSING: xcodebuild not installed" >&2
225
- exit 0
226
- fi
227
-
228
- if ! command -v xcrun >/dev/null 2>&1 || ! xcrun simctl list devices available >/dev/null 2>&1; then
229
- if [ "${SUPERSDK_CI_GATE:-}" = "ws03-ios-xcodegen-generate" ]; then
230
- echo "SKIP_SIMULATOR_MISSING invalid for ws03-ios-xcodegen-generate gate" >&2
231
- exit 1
232
- fi
233
- echo "SKIP_SIMULATOR_MISSING: no available iOS simulator" >&2
234
- exit 0
235
- fi
236
-
237
- SIMULATOR_DESTINATION="$("$PYTHON_BIN" - <<'PY' || true
238
- import json
239
- import os
240
- import subprocess
241
- import sys
242
-
243
- GATE_NAME = "ws03-ios-xcodegen-generate"
244
- GATE_DESTINATION = "platform=iOS Simulator,name=iPhone 17,OS=26.5"
245
- GATE_DEVICE = "iPhone 17"
246
- GATE_RUNTIME_SUFFIX = "iOS-26-5"
247
-
248
- try:
249
- raw = subprocess.check_output(
250
- ["xcrun", "simctl", "list", "devices", "available", "-j"],
251
- stderr=subprocess.DEVNULL,
252
- text=True,
253
- )
254
- devices_by_runtime = json.loads(raw).get("devices", {})
255
- except Exception:
256
- sys.exit(1)
257
-
258
- gate = os.environ.get("SUPERSDK_CI_GATE") == GATE_NAME
259
- for runtime, devices in devices_by_runtime.items():
260
- runtime_key = runtime.replace(".", "-")
261
- if gate and not runtime_key.endswith(GATE_RUNTIME_SUFFIX):
262
- continue
263
- if not gate and "iOS-" not in runtime_key:
264
- continue
265
- for device in devices:
266
- if not device.get("isAvailable", True):
267
- continue
268
- name = device.get("name")
269
- if gate:
270
- if name == GATE_DEVICE:
271
- print(GATE_DESTINATION)
272
- sys.exit(0)
273
- continue
274
- udid = device.get("udid")
275
- if name and udid:
276
- print(f"platform=iOS Simulator,id={udid}")
277
- sys.exit(0)
278
- sys.exit(1)
279
- PY
280
- )"
281
- if [ -z "$SIMULATOR_DESTINATION" ]; then
282
- if [ "${SUPERSDK_CI_GATE:-}" = "ws03-ios-xcodegen-generate" ]; then
283
- echo "SKIP_SIMULATOR_MISSING invalid for ws03-ios-xcodegen-generate gate: missing iPhone 17 OS 26.5" >&2
284
- exit 1
285
- fi
286
- echo "SKIP_SIMULATOR_MISSING: no available iOS simulator destination" >&2
287
- exit 0
288
- fi
289
- if [ "${SUPERSDK_CI_GATE:-}" != "ws03-ios-xcodegen-generate" ]; then
290
- echo "Using iOS simulator destination: $SIMULATOR_DESTINATION" >&2
291
- fi
292
-
293
- xcodebuild \
294
- -project "$IOS_DIR/SimpleApp.xcodeproj" \
295
- -scheme SimpleApp \
296
- -configuration Debug \
297
- -destination "$SIMULATOR_DESTINATION" \
298
- CODE_SIGNING_ALLOWED=NO \
299
- test
@@ -1,43 +0,0 @@
1
- #!/usr/bin/env bash
2
- set -euo pipefail
3
-
4
- SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
5
- # shellcheck source=lib/workspace.sh
6
- . "$SCRIPT_DIR/lib/workspace.sh"
7
- WORKSPACE_ROOT="$(supersdk_require_workspace_root "$SCRIPT_DIR")"
8
- PYTHON_BIN="${PYTHON_BIN:-python3}"
9
- HELPER="$WORKSPACE_ROOT/scripts/supersdk_template.py"
10
- CONFIG="$WORKSPACE_ROOT/supersdk.json"
11
- PROJECT_DIR=""
12
-
13
- build_seen=false
14
- while [ "$#" -gt 0 ]; do
15
- case "$1" in
16
- --build)
17
- if [ "$build_seen" = true ]; then
18
- echo "error: expected --build" >&2
19
- exit 2
20
- fi
21
- build_seen=true
22
- shift
23
- ;;
24
- --project)
25
- if [ "$#" -lt 2 ]; then
26
- echo "error: --project requires a value" >&2
27
- exit 2
28
- fi
29
- PROJECT_DIR="$2"
30
- shift 2
31
- ;;
32
- *)
33
- echo "error: expected --build" >&2
34
- exit 2
35
- ;;
36
- esac
37
- done
38
-
39
- if [ -z "$PROJECT_DIR" ]; then
40
- PROJECT_DIR="$("$PYTHON_BIN" "$HELPER" resolve --config "$CONFIG" --platform site)"
41
- fi
42
-
43
- exec "$PYTHON_BIN" "$WORKSPACE_ROOT/scripts/web_package.py" package-managed --root "$WORKSPACE_ROOT" --project "$PROJECT_DIR"
@@ -1,43 +0,0 @@
1
- #!/usr/bin/env bash
2
- # 工作空间定位的单一实现,被 super_build.sh 与所有 build_<type>.sh 复用。
3
- #
4
- # 🔴 为什么不能再用 `APP_ROOT="$(dirname "$0")/.."`:项目目录由用户在 supersdk.json 的
5
- # `projects[].dir` 里指定,深度不定(`android`、`apps/android`、`clients/mobile/android`…)。
6
- # 任何「固定层级」的反推都会在第一个非默认布局上算错根目录,而且是**静默**算错 ——
7
- # 注入会写到一个不存在的地方,构建照样退出 0。
8
- #
9
- # 定位语义与 CLI 侧一致:向上查找 `supersdk.json`,它所在的目录就是工作空间根。
10
- # `scripts/` 与 `super_build.sh` 是工作空间级共享物,所以脚本自身位置永远在根之下。
11
-
12
- supersdk_find_workspace_root() {
13
- dir="$1"
14
- while :; do
15
- if [ -f "$dir/supersdk.json" ]; then
16
- printf '%s\n' "$dir"
17
- return 0
18
- fi
19
- parent="$(dirname "$dir")"
20
- if [ "$parent" = "$dir" ]; then
21
- return 1
22
- fi
23
- dir="$parent"
24
- done
25
- }
26
-
27
- # 允许调用方(编排器/CI)显式钉住根目录;否则从给定起点向上查找。
28
- supersdk_workspace_root() {
29
- if [ -n "${SUPERSDK_WORKSPACE_ROOT:-}" ]; then
30
- printf '%s\n' "$SUPERSDK_WORKSPACE_ROOT"
31
- return 0
32
- fi
33
- supersdk_find_workspace_root "$1"
34
- }
35
-
36
- supersdk_require_workspace_root() {
37
- root="$(supersdk_workspace_root "$1" || true)"
38
- if [ -z "$root" ]; then
39
- echo "error: no supersdk.json found at or above $1; run this script from inside a SuperSDK workspace" >&2
40
- exit 1
41
- fi
42
- printf '%s\n' "$root"
43
- }
@@ -1,80 +0,0 @@
1
- #!/usr/bin/env python3
2
- import argparse
3
- import functools
4
- import http.server
5
- import sys
6
- from pathlib import Path
7
-
8
- sys.path.insert(0, str(Path(__file__).resolve().parent))
9
- import supersdk_template
10
-
11
-
12
- def resolve_site_dir(root, project):
13
- """站点目录来自配置,不是写死的 `web/`。"""
14
- if project:
15
- return project
16
- return supersdk_template.resolve_single(Path(root) / supersdk_template.CONFIG_FILENAME, "site", None)
17
-
18
-
19
- def main(argv=None):
20
- parser = argparse.ArgumentParser(description="Serve a SuperSDK site project for local HTTP debugging")
21
- parser.add_argument("--root", default=Path(__file__).resolve().parents[1], type=Path)
22
- parser.add_argument("--project", default=None, help="Site project directory; resolved from supersdk.json when omitted.")
23
- parser.add_argument(
24
- "--host",
25
- default="127.0.0.1",
26
- help="Bind host. The default is loopback-only; non-loopback binds expose the workspace root.",
27
- )
28
- parser.add_argument("--port", default=8000, type=int)
29
- parser.add_argument(
30
- "--allow-remote",
31
- action="store_true",
32
- help="Allow non-loopback binds for dev-only remote debugging.",
33
- )
34
- args = parser.parse_args(argv)
35
-
36
- root = args.root.resolve()
37
- try:
38
- site_dir = resolve_site_dir(root, args.project)
39
- except supersdk_template.ConfigError as exc:
40
- print(str(exc), file=sys.stderr)
41
- return 1
42
-
43
- # Prefer the built package root: only <project>/dist carries the generated
44
- # supersdk-config.json that the browser fallback path reads.
45
- entry = None
46
- for relative in (f"{site_dir}/dist/index.html", f"{site_dir}/src/index.html"):
47
- if (root / relative).is_file():
48
- entry = relative
49
- break
50
- if entry is None:
51
- print(f"{site_dir}/dist/index.html or {site_dir}/src/index.html not found under {root}", file=sys.stderr)
52
- return 1
53
- if entry.endswith("/src/index.html"):
54
- print(
55
- f"warning: serving unbuilt {site_dir}/src; run scripts/build_site.sh --build for the browser config fallback",
56
- file=sys.stderr,
57
- )
58
-
59
- remote_bind = args.host not in {"127.0.0.1", "localhost", "::1"}
60
- if remote_bind and not args.allow_remote:
61
- print("refusing non-loopback bind; pass --allow-remote to expose the full workspace root", file=sys.stderr)
62
- return 1
63
- if remote_bind:
64
- print("warning: serving the full workspace root on a non-loopback host", file=sys.stderr)
65
-
66
- handler = functools.partial(http.server.SimpleHTTPRequestHandler, directory=str(root))
67
- server = http.server.ThreadingHTTPServer((args.host, args.port), handler)
68
- host, port = server.server_address
69
- print(f"http://{host}:{port}/{entry}", flush=True)
70
- try:
71
- server.serve_forever()
72
- except KeyboardInterrupt:
73
- pass
74
- finally:
75
- server.server_close()
76
- return 0
77
-
78
-
79
- if __name__ == "__main__":
80
- raise SystemExit(main())