@supersdk/supercli 0.2.1-staging.21 → 0.3.0-staging.22
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/dist/cli.js +4 -2
- package/dist/cli.js.map +1 -1
- package/dist/commands/add.js +519 -0
- package/dist/commands/add.js.map +1 -0
- package/dist/commands/init.js +234 -0
- package/dist/commands/init.js.map +1 -0
- package/dist/commands/publishSite.js +354 -71
- package/dist/commands/publishSite.js.map +1 -1
- package/dist/config/appConfig.js +160 -45
- package/dist/config/appConfig.js.map +1 -1
- package/dist/publish/siteConfig.js +278 -63
- package/dist/publish/siteConfig.js.map +1 -1
- package/dist/template/README.md +144 -85
- package/dist/template/android/app/build.gradle.kts +0 -3
- package/dist/template/android/app/src/main/java/com/supersdk/simple/EnvironmentStore.java +41 -0
- package/dist/template/android/app/src/main/java/com/supersdk/simple/MainActivity.java +57 -4
- package/dist/template/android/app/src/main/java/com/supersdk/simple/SimpleApplication.java +3 -1
- package/dist/template/android/gradle/supersdk-config-reader.gradle +5 -3
- package/dist/template/android/supersdk-default.config.json +4 -3
- package/dist/template/ios/Sources/App/EnvironmentStore.swift +35 -0
- package/dist/template/ios/Sources/App/SimpleSDKBootstrap.swift +2 -1
- package/dist/template/ios/Sources/App/SimpleSDKProbe.swift +15 -0
- package/dist/template/ios/Sources/App/SimpleViewController.swift +52 -8
- package/dist/template/ios/Tests/SimpleAppTests/SimpleSDKInitializationTests.swift +12 -1
- package/dist/template/scripts/android_snapshot_canary.py +17 -8
- package/dist/template/scripts/build_android.sh +36 -9
- package/dist/template/scripts/build_ios.sh +63 -24
- package/dist/template/scripts/build_site.sh +43 -0
- package/dist/template/scripts/lib/workspace.sh +43 -0
- package/dist/template/scripts/serve_web.py +26 -9
- package/dist/template/scripts/super_build_orchestrator.py +49 -28
- package/dist/template/scripts/supersdk_template.py +759 -240
- package/dist/template/scripts/web_package.py +44 -20
- package/dist/template/super_build.sh +40 -12
- package/dist/template/supersdk.json +25 -13
- package/dist/template/template_manifest.json +25 -0
- package/dist/template/web/src/css/style.css +28 -0
- package/dist/template/web/src/index.html +8 -0
- package/dist/template/web/src/js/app.js +92 -19
- package/dist/workspace/config.js +440 -0
- package/dist/workspace/config.js.map +1 -0
- package/dist/workspace/identity.js +106 -0
- package/dist/workspace/identity.js.map +1 -0
- package/dist/workspace/interactive.js +96 -0
- package/dist/workspace/interactive.js.map +1 -0
- package/dist/workspace/template.js +357 -0
- package/dist/workspace/template.js.map +1 -0
- package/package.json +1 -1
- package/dist/commands/createApp.js +0 -448
- package/dist/commands/createApp.js.map +0 -1
- package/dist/createApp/copyTemplate.js +0 -561
- package/dist/createApp/copyTemplate.js.map +0 -1
- package/dist/template/scripts/build_web.sh +0 -12
|
@@ -20,11 +20,12 @@ HEX64 = set("0123456789abcdef")
|
|
|
20
20
|
VERSION_PATTERN = re.compile(r"^[0-9]+\.[0-9]+\.[0-9]+$")
|
|
21
21
|
CONFIG_FILENAME = "supersdk.json"
|
|
22
22
|
|
|
23
|
-
#
|
|
24
|
-
# It is what the offline zip is built from AND what `supercli publish-site <dir>`
|
|
23
|
+
# `<project>/src` is authored input; `<project>/dist` is the single publishable build
|
|
24
|
+
# output. It is what the offline zip is built from AND what `supercli publish-site <dir>`
|
|
25
25
|
# takes: a directory with a root index.html and no build-only sidecars.
|
|
26
26
|
# Same source tree, not identical content: the zip additionally carries the offline
|
|
27
27
|
# manifest `__super_meta.json` at its root, which never belongs in a published site.
|
|
28
|
+
SRC_DIRNAME = "src"
|
|
28
29
|
DIST_DIRNAME = "dist"
|
|
29
30
|
# Runtime config lives INSIDE the package root. The old `../.supersdk/managed/config.json`
|
|
30
31
|
# path sat outside the zip/site root and was dropped by the publisher's dotdir rule.
|
|
@@ -140,8 +141,8 @@ def zip_staging(staging, output_dir, host, version):
|
|
|
140
141
|
temp_zip.unlink()
|
|
141
142
|
|
|
142
143
|
|
|
143
|
-
def build_dist(
|
|
144
|
-
"""Render
|
|
144
|
+
def build_dist(project_root, managed_config):
|
|
145
|
+
"""Render <project>/src + the managed config into the publishable <project>/dist.
|
|
145
146
|
|
|
146
147
|
Built in a sibling temp dir and swapped in, so a failed build never leaves a
|
|
147
148
|
half-written dist behind for `publish-site` to upload.
|
|
@@ -150,9 +151,9 @@ def build_dist(app_root, managed_config):
|
|
|
150
151
|
dist because `write_meta` adds the offline manifest `__super_meta.json` to the
|
|
151
152
|
package root. Same source tree, one extra offline-only file.
|
|
152
153
|
"""
|
|
153
|
-
|
|
154
|
-
source_root = validate_source(
|
|
155
|
-
dist =
|
|
154
|
+
project_root = Path(project_root)
|
|
155
|
+
source_root = validate_source(project_root / SRC_DIRNAME)
|
|
156
|
+
dist = project_root / DIST_DIRNAME
|
|
156
157
|
dist.parent.mkdir(parents=True, exist_ok=True)
|
|
157
158
|
staging_parent = Path(tempfile.mkdtemp(prefix=".supersdk-web-dist-", dir=dist.parent))
|
|
158
159
|
staging = staging_parent / DIST_DIRNAME
|
|
@@ -195,33 +196,55 @@ def package(src, host, version, output_dir, require_provenance=True):
|
|
|
195
196
|
}
|
|
196
197
|
|
|
197
198
|
|
|
198
|
-
def package_managed(root):
|
|
199
|
-
|
|
200
|
-
|
|
199
|
+
def package_managed(root, project):
|
|
200
|
+
workspace_root = Path(root)
|
|
201
|
+
project_root = workspace_root / project
|
|
202
|
+
config_path = project_root / ".supersdk" / "managed" / "config.json"
|
|
201
203
|
if not config_path.is_file():
|
|
202
204
|
raise WebPackageError(
|
|
203
|
-
"
|
|
205
|
+
f"site managed config missing; run scripts/supersdk_template.py inject --platform site --project {project} "
|
|
206
|
+
"or ./super_build.sh --platform site --build"
|
|
204
207
|
)
|
|
205
208
|
try:
|
|
206
209
|
config = json.loads(config_path.read_text(encoding="utf-8"))
|
|
207
210
|
except (OSError, json.JSONDecodeError, UnicodeDecodeError) as exc:
|
|
208
|
-
raise WebPackageError("
|
|
209
|
-
|
|
210
|
-
|
|
211
|
+
raise WebPackageError("site managed config invalid") from exc
|
|
212
|
+
# 类型名就是 managed config 里的 platform 值;这个字面量必须跟着 PROJECT_TYPES 走。
|
|
213
|
+
if config.get("platform") != "site":
|
|
214
|
+
raise WebPackageError("site managed config invalid")
|
|
211
215
|
platform_config = config.get("platform_config")
|
|
212
216
|
version = config.get("version")
|
|
213
217
|
if not isinstance(platform_config, dict) or not isinstance(version, dict):
|
|
214
|
-
raise WebPackageError("
|
|
218
|
+
raise WebPackageError("site managed config invalid")
|
|
219
|
+
|
|
220
|
+
host = platform_config.get("host")
|
|
221
|
+
# 🔴 offline.host 是可选的、由服务端回填的:首次发布成功后 CLI 才把服务端签发的域名
|
|
222
|
+
# 记回配置。CLI 绝不自己拼域名,所以没有它就**跳过离线打包并显式说明原因** ——
|
|
223
|
+
# dist 照常产出(publish-site 要的就是它),静默跳过才是不可接受的那一种。
|
|
224
|
+
if host is None:
|
|
225
|
+
# host 缺席不代表可以少校验:版本号照样要合法,dist 照样要产出。
|
|
226
|
+
validate_version(version.get("name"))
|
|
227
|
+
validate_source(project_root / SRC_DIRNAME)
|
|
228
|
+
dist = build_dist(project_root, config)
|
|
229
|
+
reason = (
|
|
230
|
+
f"offline package not built for {project}: projects[].offline.host is not set. "
|
|
231
|
+
"Publish the site once so the CLI records the server-issued host, or set it by hand in supersdk.json."
|
|
232
|
+
)
|
|
233
|
+
print(reason, file=sys.stderr)
|
|
234
|
+
return {"host": None, "version": version.get("name"), "zip": None, "dist": str(dist), "offline_skipped": reason}
|
|
235
|
+
|
|
215
236
|
try:
|
|
216
237
|
# Validate host/version/source before anything is written, so a bad host cannot
|
|
217
238
|
# leave a stale or partly-rendered dist on disk.
|
|
218
|
-
validate_inputs(
|
|
219
|
-
dist = build_dist(
|
|
220
|
-
|
|
239
|
+
validate_inputs(project_root / SRC_DIRNAME, host, version.get("name"))
|
|
240
|
+
dist = build_dist(project_root, config)
|
|
241
|
+
# 🔴 output/ 是工作空间级的,但同名 zip 会被 unlink 后 replace:两个站点
|
|
242
|
+
# 同 host + 同 version 就会互相顶掉。按项目目录分仓,冲突从根上消失。
|
|
243
|
+
result = package(dist, host, version.get("name"), workspace_root / "output" / project, require_provenance=False)
|
|
221
244
|
except WebPackageError as exc:
|
|
222
245
|
message = str(exc)
|
|
223
246
|
if message.startswith("web.host:"):
|
|
224
|
-
raise WebPackageError(f"
|
|
247
|
+
raise WebPackageError(f"offline.host (from {CONFIG_FILENAME}):{message[len('web.host:'):]}") from exc
|
|
225
248
|
raise
|
|
226
249
|
result["dist"] = str(dist)
|
|
227
250
|
return result
|
|
@@ -239,10 +262,11 @@ def main(argv=None):
|
|
|
239
262
|
cmd.add_argument("--output-dir", required=True)
|
|
240
263
|
managed = sub.add_parser("package-managed")
|
|
241
264
|
managed.add_argument("--root", required=True)
|
|
265
|
+
managed.add_argument("--project", required=True)
|
|
242
266
|
args = parser.parse_args(argv)
|
|
243
267
|
try:
|
|
244
268
|
if args.command == "package-managed":
|
|
245
|
-
print(json.dumps(package_managed(args.root), sort_keys=True))
|
|
269
|
+
print(json.dumps(package_managed(args.root, args.project), sort_keys=True))
|
|
246
270
|
else:
|
|
247
271
|
source_root, host, version = validate_inputs(args.src, args.host, args.version)
|
|
248
272
|
if args.command == "validate":
|
|
@@ -1,30 +1,36 @@
|
|
|
1
1
|
#!/usr/bin/env bash
|
|
2
2
|
set -euo pipefail
|
|
3
3
|
|
|
4
|
-
|
|
4
|
+
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
|
|
5
|
+
# shellcheck source=scripts/lib/workspace.sh
|
|
6
|
+
. "$SCRIPT_DIR/scripts/lib/workspace.sh"
|
|
7
|
+
WORKSPACE_ROOT="$(supersdk_require_workspace_root "$SCRIPT_DIR")"
|
|
5
8
|
PYTHON_BIN="${PYTHON_BIN:-python3}"
|
|
6
|
-
HELPER="$
|
|
7
|
-
ORCHESTRATOR="$
|
|
9
|
+
HELPER="$WORKSPACE_ROOT/scripts/supersdk_template.py"
|
|
10
|
+
ORCHESTRATOR="$WORKSPACE_ROOT/scripts/super_build_orchestrator.py"
|
|
8
11
|
|
|
9
12
|
usage() {
|
|
10
13
|
cat <<'EOF'
|
|
11
|
-
Usage: ./super_build.sh [--platform all|android|ios|
|
|
14
|
+
Usage: ./super_build.sh [--platform all|android|ios|site] [--project DIR] [--plan|--build] [--force] [--config path]
|
|
12
15
|
|
|
13
16
|
Options:
|
|
14
|
-
--platform VALUE
|
|
17
|
+
--platform VALUE Project type to plan/build. Default: all.
|
|
18
|
+
--project DIR Single project directory (as written in supersdk.json projects[].dir).
|
|
15
19
|
--plan Validate and print the build plan. Default when no mode is set.
|
|
16
|
-
--build Validate, inject managed config, then call
|
|
20
|
+
--build Validate, inject managed config, then call project build helpers.
|
|
17
21
|
--force Allow replacing unmanaged target files during injection.
|
|
18
|
-
--config PATH Config file. Default:
|
|
22
|
+
--config PATH Config file. Default: <workspace root>/supersdk.json.
|
|
19
23
|
--help Show this help.
|
|
20
24
|
EOF
|
|
21
25
|
}
|
|
22
26
|
|
|
23
27
|
platform="all"
|
|
24
|
-
|
|
28
|
+
project=""
|
|
29
|
+
config="$WORKSPACE_ROOT/supersdk.json"
|
|
25
30
|
mode="plan"
|
|
26
31
|
mode_set=false
|
|
27
32
|
platform_set=false
|
|
33
|
+
project_set=false
|
|
28
34
|
config_set=false
|
|
29
35
|
force_set=false
|
|
30
36
|
force=false
|
|
@@ -48,6 +54,19 @@ while [ "$#" -gt 0 ]; do
|
|
|
48
54
|
platform_set=true
|
|
49
55
|
shift 2
|
|
50
56
|
;;
|
|
57
|
+
--project)
|
|
58
|
+
if [ "$project_set" = true ]; then
|
|
59
|
+
echo "error: --project specified more than once" >&2
|
|
60
|
+
exit 2
|
|
61
|
+
fi
|
|
62
|
+
if [ "$#" -lt 2 ]; then
|
|
63
|
+
echo "error: --project requires a value" >&2
|
|
64
|
+
exit 2
|
|
65
|
+
fi
|
|
66
|
+
project="$2"
|
|
67
|
+
project_set=true
|
|
68
|
+
shift 2
|
|
69
|
+
;;
|
|
51
70
|
--config)
|
|
52
71
|
if [ "$config_set" = true ]; then
|
|
53
72
|
echo "error: --config specified more than once" >&2
|
|
@@ -96,21 +115,30 @@ while [ "$#" -gt 0 ]; do
|
|
|
96
115
|
done
|
|
97
116
|
|
|
98
117
|
case "$platform" in
|
|
99
|
-
all|android|ios|
|
|
118
|
+
all|android|ios|site) ;;
|
|
100
119
|
*)
|
|
101
120
|
echo "error: invalid platform $platform" >&2
|
|
102
121
|
exit 2
|
|
103
122
|
;;
|
|
104
123
|
esac
|
|
105
124
|
|
|
125
|
+
selection=(--platform "$platform")
|
|
126
|
+
if [ "$project_set" = true ]; then
|
|
127
|
+
selection+=(--project "$project")
|
|
128
|
+
fi
|
|
129
|
+
|
|
130
|
+
# 引擎版本比对:managed config 记录了写它的那一版引擎。模板是 vendored 进每个工作空间的,
|
|
131
|
+
# 升级后不重新投影就会拿旧形状去喂新构建脚本。只 warn,不阻断。
|
|
132
|
+
"$PYTHON_BIN" "$HELPER" engine-check --root "$WORKSPACE_ROOT" --config "$config" || true
|
|
133
|
+
|
|
106
134
|
if [ "$mode" = "plan" ]; then
|
|
107
|
-
exec "$PYTHON_BIN" "$HELPER" plan --root "$
|
|
135
|
+
exec "$PYTHON_BIN" "$HELPER" plan --root "$WORKSPACE_ROOT" --config "$config" "${selection[@]}"
|
|
108
136
|
fi
|
|
109
137
|
|
|
110
|
-
inject_args=(inject --root "$
|
|
138
|
+
inject_args=(inject --root "$WORKSPACE_ROOT" --config "$config" "${selection[@]}")
|
|
111
139
|
if [ "$force" = true ]; then
|
|
112
140
|
inject_args+=(--force)
|
|
113
141
|
fi
|
|
114
142
|
"$PYTHON_BIN" "$HELPER" "${inject_args[@]}"
|
|
115
143
|
|
|
116
|
-
exec "$PYTHON_BIN" "$ORCHESTRATOR" --root "$
|
|
144
|
+
exec "$PYTHON_BIN" "$ORCHESTRATOR" --root "$WORKSPACE_ROOT" --config "$config" "${selection[@]}"
|
|
@@ -1,23 +1,35 @@
|
|
|
1
1
|
{
|
|
2
|
-
"schemaVersion":
|
|
2
|
+
"schemaVersion": 3,
|
|
3
3
|
"spid": "demo0001",
|
|
4
4
|
"name": "SuperSDK Simple",
|
|
5
|
-
"environment": "prod",
|
|
6
|
-
"android": {
|
|
7
|
-
"package": "com.supersdk.simple.host"
|
|
8
|
-
},
|
|
9
|
-
"ios": {
|
|
10
|
-
"bundleId": "com.supersdk.simple",
|
|
11
|
-
"spmMode": "mock"
|
|
12
|
-
},
|
|
13
|
-
"web": {
|
|
14
|
-
"host": "simple.supersdk.com"
|
|
15
|
-
},
|
|
16
5
|
"version": {
|
|
17
6
|
"name": "1.0.0",
|
|
18
7
|
"code": 1
|
|
19
8
|
},
|
|
20
9
|
"sdk": {
|
|
21
10
|
"androidVersion": "1.0.0.54-SNAPSHOT"
|
|
22
|
-
}
|
|
11
|
+
},
|
|
12
|
+
"projects": [
|
|
13
|
+
{
|
|
14
|
+
"type": "android",
|
|
15
|
+
"dir": "android",
|
|
16
|
+
"name": "SuperSDK Simple",
|
|
17
|
+
"package": "com.supersdk.simple.host"
|
|
18
|
+
},
|
|
19
|
+
{
|
|
20
|
+
"type": "ios",
|
|
21
|
+
"dir": "ios",
|
|
22
|
+
"name": "SuperSDK Simple",
|
|
23
|
+
"bundleId": "com.supersdk.simple",
|
|
24
|
+
"spmMode": "mock"
|
|
25
|
+
},
|
|
26
|
+
{
|
|
27
|
+
"type": "site",
|
|
28
|
+
"dir": "web",
|
|
29
|
+
"name": "SuperSDK Simple Web",
|
|
30
|
+
"offline": {
|
|
31
|
+
"host": "simple.supersdk.com"
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
]
|
|
23
35
|
}
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
{
|
|
2
|
+
"engineVersion": "3.0.0",
|
|
3
|
+
"schemaVersion": 3,
|
|
4
|
+
"source": "app_repo_example",
|
|
5
|
+
"comment": [
|
|
6
|
+
"模板的拆分契约,给 supercli 消费:`init` 落地 workspace 级共享物一次,`add <type> <dir>` 拷贝对应类型的项目模板。",
|
|
7
|
+
"workspace: 工作空间级共享物(super_build.sh 与 scripts/ 是所有项目共用的编排与引擎)。",
|
|
8
|
+
"projects: 类型 -> app_repo_example 里该类型的项目模板目录。目标目录由用户在 `add` 时指定,不必与这里同名。",
|
|
9
|
+
"generated: 不是拷贝来的,而是 `supercli init` 按 spid 生成的。"
|
|
10
|
+
],
|
|
11
|
+
"workspace": [
|
|
12
|
+
"super_build.sh",
|
|
13
|
+
"scripts",
|
|
14
|
+
"README.md",
|
|
15
|
+
".gitignore"
|
|
16
|
+
],
|
|
17
|
+
"projects": {
|
|
18
|
+
"android": "android",
|
|
19
|
+
"ios": "ios",
|
|
20
|
+
"site": "web"
|
|
21
|
+
},
|
|
22
|
+
"generated": [
|
|
23
|
+
"supersdk.json"
|
|
24
|
+
]
|
|
25
|
+
}
|
|
@@ -64,6 +64,34 @@ dd {
|
|
|
64
64
|
font-size: 15px;
|
|
65
65
|
}
|
|
66
66
|
|
|
67
|
+
.switcher {
|
|
68
|
+
display: flex;
|
|
69
|
+
align-items: center;
|
|
70
|
+
gap: 10px;
|
|
71
|
+
margin: 20px 0 0;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
.switcher label {
|
|
75
|
+
color: #637083;
|
|
76
|
+
font-size: 12px;
|
|
77
|
+
font-weight: 700;
|
|
78
|
+
text-transform: uppercase;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
.switcher select {
|
|
82
|
+
border: 1px solid #d9e1ef;
|
|
83
|
+
border-radius: 6px;
|
|
84
|
+
background: #ffffff;
|
|
85
|
+
padding: 6px 10px;
|
|
86
|
+
font: inherit;
|
|
87
|
+
font-size: 14px;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
.switcher select:disabled {
|
|
91
|
+
background: #f1f4f9;
|
|
92
|
+
color: #94a3b8;
|
|
93
|
+
}
|
|
94
|
+
|
|
67
95
|
.diagnostic {
|
|
68
96
|
margin: 20px 0 0;
|
|
69
97
|
border-left: 4px solid #c2410c;
|
|
@@ -34,6 +34,14 @@
|
|
|
34
34
|
<dd id="version">-</dd>
|
|
35
35
|
</div>
|
|
36
36
|
</dl>
|
|
37
|
+
<p class="switcher">
|
|
38
|
+
<label for="environmentPicker">Switch environment</label>
|
|
39
|
+
<select id="environmentPicker">
|
|
40
|
+
<option value="prod">prod</option>
|
|
41
|
+
<option value="staging">staging</option>
|
|
42
|
+
<option value="dev">dev</option>
|
|
43
|
+
</select>
|
|
44
|
+
</p>
|
|
37
45
|
<p id="diagnostic" class="diagnostic" hidden></p>
|
|
38
46
|
</section>
|
|
39
47
|
</main>
|
|
@@ -1,39 +1,89 @@
|
|
|
1
1
|
(function() {
|
|
2
2
|
"use strict";
|
|
3
3
|
|
|
4
|
-
// Package-root relative on purpose: the file is emitted into
|
|
5
|
-
// scripts/
|
|
4
|
+
// Package-root relative on purpose: the file is emitted into <project>/dist by
|
|
5
|
+
// scripts/build_site.sh, so the same path resolves in the offline package, on a
|
|
6
6
|
// published site, and on the local dev server.
|
|
7
7
|
var CONFIG_PATH = "supersdk-config.json";
|
|
8
8
|
|
|
9
|
+
// 🔴 environment 不在 supersdk-config.json 里,因为它不是构建配置:它是
|
|
10
|
+
// SuperSDK 初始化的**运行时**参数。Web 端的运行时选择就存在浏览器里,
|
|
11
|
+
// 三端(Android / iOS / Web)在这一点上是同一个模型。
|
|
12
|
+
var ENVIRONMENT_KEY = "supersdk.simple.environment";
|
|
13
|
+
var ENVIRONMENT_DEFAULT = "prod";
|
|
14
|
+
var ENVIRONMENT_PATTERN = /^[a-z0-9]{1,10}$/;
|
|
15
|
+
|
|
16
|
+
function element(id) {
|
|
17
|
+
return document.getElementById(id);
|
|
18
|
+
}
|
|
19
|
+
|
|
9
20
|
function text(id, value) {
|
|
10
|
-
var
|
|
11
|
-
if (
|
|
12
|
-
|
|
21
|
+
var node = element(id);
|
|
22
|
+
if (node) {
|
|
23
|
+
node.textContent = value == null || value === "" ? "-" : String(value);
|
|
13
24
|
}
|
|
14
25
|
}
|
|
15
26
|
|
|
16
27
|
function diagnostic(message) {
|
|
17
|
-
var
|
|
18
|
-
if (
|
|
19
|
-
|
|
20
|
-
|
|
28
|
+
var node = element("diagnostic");
|
|
29
|
+
if (node) {
|
|
30
|
+
node.hidden = false;
|
|
31
|
+
node.textContent = message;
|
|
21
32
|
}
|
|
22
33
|
}
|
|
23
34
|
|
|
24
35
|
function clearDiagnostic() {
|
|
25
|
-
var
|
|
26
|
-
if (
|
|
27
|
-
|
|
28
|
-
|
|
36
|
+
var node = element("diagnostic");
|
|
37
|
+
if (node) {
|
|
38
|
+
node.hidden = true;
|
|
39
|
+
node.textContent = "";
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function isValidEnvironment(value) {
|
|
44
|
+
return typeof value === "string" && ENVIRONMENT_PATTERN.test(value);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function readEnvironment() {
|
|
48
|
+
try {
|
|
49
|
+
var stored = window.localStorage ? window.localStorage.getItem(ENVIRONMENT_KEY) : null;
|
|
50
|
+
return isValidEnvironment(stored) ? stored : ENVIRONMENT_DEFAULT;
|
|
51
|
+
} catch (error) {
|
|
52
|
+
// Private mode and file:// can make localStorage throw on access.
|
|
53
|
+
return ENVIRONMENT_DEFAULT;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function writeEnvironment(value) {
|
|
58
|
+
if (!isValidEnvironment(value)) {
|
|
59
|
+
return false;
|
|
60
|
+
}
|
|
61
|
+
try {
|
|
62
|
+
if (window.localStorage) {
|
|
63
|
+
window.localStorage.setItem(ENVIRONMENT_KEY, value);
|
|
64
|
+
}
|
|
65
|
+
} catch (error) {
|
|
66
|
+
return false;
|
|
67
|
+
}
|
|
68
|
+
return true;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function setPickerEnabled(enabled, selected) {
|
|
72
|
+
var picker = element("environmentPicker");
|
|
73
|
+
if (!picker) {
|
|
74
|
+
return;
|
|
75
|
+
}
|
|
76
|
+
picker.disabled = !enabled;
|
|
77
|
+
if (selected != null) {
|
|
78
|
+
picker.value = selected;
|
|
29
79
|
}
|
|
30
80
|
}
|
|
31
81
|
|
|
32
|
-
function renderConfig(config) {
|
|
82
|
+
function renderConfig(config, environment) {
|
|
33
83
|
var version = config && config.version ? config.version : {};
|
|
34
84
|
var platformConfig = config && config.platform_config ? config.platform_config : {};
|
|
35
85
|
text("spid", config && config.spid);
|
|
36
|
-
text("environment",
|
|
86
|
+
text("environment", environment);
|
|
37
87
|
text("appName", config && config.name);
|
|
38
88
|
text("host", platformConfig.host);
|
|
39
89
|
text("version", version.name);
|
|
@@ -49,7 +99,7 @@
|
|
|
49
99
|
|
|
50
100
|
function loadConfig() {
|
|
51
101
|
if (!window.location || window.location.protocol === "file:") {
|
|
52
|
-
return Promise.reject(new Error("Run scripts/
|
|
102
|
+
return Promise.reject(new Error("Run scripts/build_site.sh then serve the built dist over the local HTTP server"));
|
|
53
103
|
}
|
|
54
104
|
var url = new URL(CONFIG_PATH, window.location.href).toString();
|
|
55
105
|
return fetch(url).then(function(response) {
|
|
@@ -64,23 +114,29 @@
|
|
|
64
114
|
return window.SuperSDK.getEnv().then(function(env) {
|
|
65
115
|
clearDiagnostic();
|
|
66
116
|
renderEnv(env || {});
|
|
117
|
+
// In-app: the host App owns the environment, so the web picker must not pretend
|
|
118
|
+
// it can change it.
|
|
119
|
+
setPickerEnabled(false, env && env.environment);
|
|
67
120
|
return { mode: "bridge", env: env || {} };
|
|
68
121
|
}).catch(function(error) {
|
|
69
122
|
diagnostic("bridge diagnostic: " + (error && error.message ? error.message : "getEnv failed"));
|
|
123
|
+
setPickerEnabled(false, null);
|
|
70
124
|
return { mode: "bridge-error" };
|
|
71
125
|
});
|
|
72
126
|
}
|
|
73
127
|
|
|
74
128
|
function runFallback() {
|
|
129
|
+
var environment = readEnvironment();
|
|
130
|
+
setPickerEnabled(true, environment);
|
|
75
131
|
return loadConfig().then(function(config) {
|
|
76
132
|
window.SuperSDK.initFallback({
|
|
77
133
|
spid: config.spid,
|
|
78
|
-
environment:
|
|
134
|
+
environment: environment,
|
|
79
135
|
appName: config.name
|
|
80
136
|
});
|
|
81
137
|
clearDiagnostic();
|
|
82
|
-
renderConfig(config);
|
|
83
|
-
return { mode: "fallback", config: config };
|
|
138
|
+
renderConfig(config, environment);
|
|
139
|
+
return { mode: "fallback", config: config, environment: environment };
|
|
84
140
|
}).catch(function(error) {
|
|
85
141
|
diagnostic("config diagnostic: " + (error && error.message ? error.message : "unavailable"));
|
|
86
142
|
return { mode: "config-error" };
|
|
@@ -98,6 +154,23 @@
|
|
|
98
154
|
return runFallback();
|
|
99
155
|
}
|
|
100
156
|
|
|
157
|
+
function bindPicker() {
|
|
158
|
+
var picker = element("environmentPicker");
|
|
159
|
+
if (!picker || typeof picker.addEventListener !== "function") {
|
|
160
|
+
return;
|
|
161
|
+
}
|
|
162
|
+
picker.addEventListener("change", function() {
|
|
163
|
+
if (!writeEnvironment(picker.value)) {
|
|
164
|
+
diagnostic("environment diagnostic: must match ^[a-z0-9]{1,10}$");
|
|
165
|
+
return;
|
|
166
|
+
}
|
|
167
|
+
// Re-init in place: on Web the fallback config is a runtime object, so switching
|
|
168
|
+
// environment does not need a rebuild or even a reload.
|
|
169
|
+
window.__superReady = boot();
|
|
170
|
+
});
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
bindPicker();
|
|
101
174
|
window.__superReady = boot().catch(function(error) {
|
|
102
175
|
diagnostic("runtime diagnostic: " + (error && error.message ? error.message : "unknown"));
|
|
103
176
|
return { mode: "runtime-error" };
|