@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.
- package/dist/build/artifactIntegrity.js +275 -0
- package/dist/build/artifactIntegrity.js.map +1 -0
- package/dist/build/projectBuild.js +335 -0
- package/dist/build/projectBuild.js.map +1 -0
- package/dist/cli.js +20 -11
- package/dist/cli.js.map +1 -1
- package/dist/commands/add.js +111 -57
- package/dist/commands/add.js.map +1 -1
- package/dist/commands/analytics.js +15 -9
- package/dist/commands/analytics.js.map +1 -1
- package/dist/commands/build.js +533 -0
- package/dist/commands/build.js.map +1 -0
- package/dist/commands/db.js +24 -19
- package/dist/commands/db.js.map +1 -1
- package/dist/commands/deploy.js +434 -751
- package/dist/commands/deploy.js.map +1 -1
- package/dist/commands/env.js +8 -8
- package/dist/commands/env.js.map +1 -1
- package/dist/commands/image.js +31 -20
- package/dist/commands/image.js.map +1 -1
- package/dist/commands/init.js +81 -57
- package/dist/commands/init.js.map +1 -1
- package/dist/commands/logs.js +16 -7
- package/dist/commands/logs.js.map +1 -1
- package/dist/commands/publishSite.js +28 -943
- package/dist/commands/publishSite.js.map +1 -1
- package/dist/commands/register.js +84 -218
- package/dist/commands/register.js.map +1 -1
- package/dist/commands/settings.js +10 -6
- package/dist/commands/settings.js.map +1 -1
- package/dist/config/appConfig.js +48 -171
- package/dist/config/appConfig.js.map +1 -1
- package/dist/config/superConfig.js +457 -0
- package/dist/config/superConfig.js.map +1 -0
- package/dist/deploy/adapterTypes.js +3 -0
- package/dist/deploy/adapterTypes.js.map +1 -0
- package/dist/deploy/manifest.js +403 -0
- package/dist/deploy/manifest.js.map +1 -0
- package/dist/deploy/preflight.js +201 -0
- package/dist/deploy/preflight.js.map +1 -0
- package/dist/deploy/resultWriter.js +178 -0
- package/dist/deploy/resultWriter.js.map +1 -0
- package/dist/deploy/runAdapter.js +302 -0
- package/dist/deploy/runAdapter.js.map +1 -0
- package/dist/deploy/siteAdapter.js +182 -0
- package/dist/deploy/siteAdapter.js.map +1 -0
- package/dist/docker/dockerCli.js +110 -53
- package/dist/docker/dockerCli.js.map +1 -1
- package/dist/output/envelope.js +5 -1
- package/dist/output/envelope.js.map +1 -1
- package/dist/output/resultPrinter.js +3 -0
- package/dist/output/resultPrinter.js.map +1 -1
- package/dist/publish/responseClassifier.js +4 -4
- package/dist/publish/responseClassifier.js.map +1 -1
- package/dist/publish/runClient.js +11 -11
- package/dist/publish/runClient.js.map +1 -1
- package/dist/publish/siteZip.js +4 -5
- package/dist/publish/siteZip.js.map +1 -1
- package/dist/template/README.md +26 -257
- package/dist/template/android/app/src/main/java/com/supersdk/simple/EnvironmentStore.java +1 -1
- package/dist/template/android/app/src/main/java/com/supersdk/simple/SimpleApplication.java +2 -2
- package/dist/template/android/gradle/supersdk-config-reader.gradle +13 -61
- package/dist/template/android/scripts/build.sh +11 -0
- package/dist/template/android/supersdk-default.config.json +3 -2
- package/dist/template/ios/Sources/App/AppDelegate.swift +1 -1
- package/dist/template/ios/Sources/App/SimpleSDKBootstrap.swift +1 -1
- package/dist/template/ios/project.yml +1 -1
- package/dist/template/ios/scripts/build.sh +19 -0
- package/dist/template/ios/scripts/render_project.py +60 -0
- package/dist/template/ios/supersdk-default.config.json +6 -0
- package/dist/template/service/Dockerfile +2 -0
- package/dist/template/service/scripts/build.sh +11 -0
- package/dist/template/super.json +62 -0
- package/dist/template/template_manifest.json +6 -9
- package/dist/template/web/package.json +8 -0
- package/dist/template/web/scripts/build.sh +8 -0
- package/dist/template/web/scripts/web_package.py +490 -0
- package/dist/template/web/src/js/app.js +2 -2
- package/dist/util/shell.js +3 -3
- package/dist/workspace/config.js +69 -183
- package/dist/workspace/config.js.map +1 -1
- package/dist/workspace/identity.js +10 -18
- package/dist/workspace/identity.js.map +1 -1
- package/dist/workspace/projectContext.js +93 -0
- package/dist/workspace/projectContext.js.map +1 -0
- package/dist/workspace/projectIdentity.js +50 -98
- package/dist/workspace/projectIdentity.js.map +1 -1
- package/dist/workspace/template.js +208 -83
- package/dist/workspace/template.js.map +1 -1
- package/package.json +4 -2
- package/dist/publish/siteConfig.js +0 -649
- package/dist/publish/siteConfig.js.map +0 -1
- package/dist/template/scripts/android_snapshot_canary.py +0 -145
- package/dist/template/scripts/build_android.sh +0 -366
- package/dist/template/scripts/build_ios.sh +0 -299
- package/dist/template/scripts/build_site.sh +0 -43
- package/dist/template/scripts/lib/workspace.sh +0 -43
- package/dist/template/scripts/serve_web.py +0 -80
- package/dist/template/scripts/super_build_orchestrator.py +0 -421
- package/dist/template/scripts/supersdk_template.py +0 -1161
- package/dist/template/scripts/web_host_validator.py +0 -103
- package/dist/template/scripts/web_package.py +0 -295
- package/dist/template/scripts/ws03_shared_schema_merge_guard.py +0 -73
- package/dist/template/super_build.sh +0 -144
- package/dist/template/supersdk.json +0 -35
- package/dist/workspace/inspect.js +0 -274
- package/dist/workspace/inspect.js.map +0 -1
|
@@ -0,0 +1,490 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
"""Build this Web project's publishable directory and offline package."""
|
|
3
|
+
|
|
4
|
+
import argparse
|
|
5
|
+
import hashlib
|
|
6
|
+
import ipaddress
|
|
7
|
+
import json
|
|
8
|
+
import os
|
|
9
|
+
import re
|
|
10
|
+
import secrets
|
|
11
|
+
import shutil
|
|
12
|
+
import stat
|
|
13
|
+
import sys
|
|
14
|
+
import time
|
|
15
|
+
import zipfile
|
|
16
|
+
from dataclasses import dataclass
|
|
17
|
+
from pathlib import Path
|
|
18
|
+
from urllib.parse import urlparse
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
REQUIRED_ASSETS = ("index.html", "js/app.js", "js/supersdk.js", "css/style.css")
|
|
22
|
+
EXCLUDED_ASSETS = {"__super_meta.json", "js/supersdk.provenance.json"}
|
|
23
|
+
PROVENANCE_FIELDS = {"package", "version", "integrity", "file", "sha256"}
|
|
24
|
+
VERSION_PATTERN = re.compile(r"^[0-9]+\.[0-9]+\.[0-9]+$")
|
|
25
|
+
INTEGRITY_PATTERN = re.compile(r"^sha512-[A-Za-z0-9+/]+={0,2}$")
|
|
26
|
+
SHA256_PATTERN = re.compile(r"^[0-9a-f]{64}$")
|
|
27
|
+
SPID_PATTERN = re.compile(r"^[a-z0-9]{8}$")
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
class WebBuildError(Exception):
|
|
31
|
+
pass
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
@dataclass
|
|
35
|
+
class VerifiedDirectory:
|
|
36
|
+
path: Path
|
|
37
|
+
real_path: Path
|
|
38
|
+
device: int
|
|
39
|
+
inode: int
|
|
40
|
+
fd: int
|
|
41
|
+
owned: bool = False
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
def _directory_open_flags():
|
|
45
|
+
flags = os.O_RDONLY
|
|
46
|
+
flags |= getattr(os, "O_DIRECTORY", 0)
|
|
47
|
+
flags |= getattr(os, "O_NOFOLLOW", 0)
|
|
48
|
+
return flags
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
def open_verified_root(path):
|
|
52
|
+
try:
|
|
53
|
+
before = os.lstat(path)
|
|
54
|
+
except OSError as exc:
|
|
55
|
+
raise WebBuildError(f"cannot inspect Web project directory: {path}") from exc
|
|
56
|
+
if stat.S_ISLNK(before.st_mode) or not stat.S_ISDIR(before.st_mode):
|
|
57
|
+
raise WebBuildError(f"Web project directory must be a real directory: {path}")
|
|
58
|
+
try:
|
|
59
|
+
fd = os.open(path, _directory_open_flags())
|
|
60
|
+
except OSError as exc:
|
|
61
|
+
raise WebBuildError(f"cannot open Web project directory without following symlinks: {path}") from exc
|
|
62
|
+
try:
|
|
63
|
+
opened = os.fstat(fd)
|
|
64
|
+
real_path = path.resolve(strict=True)
|
|
65
|
+
after = os.lstat(real_path)
|
|
66
|
+
if (before.st_dev, before.st_ino) != (opened.st_dev, opened.st_ino):
|
|
67
|
+
raise WebBuildError(f"Web project directory changed during validation: {path}")
|
|
68
|
+
if (after.st_dev, after.st_ino) != (opened.st_dev, opened.st_ino):
|
|
69
|
+
raise WebBuildError(f"Web project real path changed during validation: {path}")
|
|
70
|
+
return VerifiedDirectory(real_path, real_path, opened.st_dev, opened.st_ino, fd)
|
|
71
|
+
except Exception:
|
|
72
|
+
os.close(fd)
|
|
73
|
+
raise
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
def assert_verified_directory(directory):
|
|
77
|
+
assert_verified_directory_handle(directory)
|
|
78
|
+
try:
|
|
79
|
+
current = os.lstat(directory.path)
|
|
80
|
+
current_real = directory.path.resolve(strict=True)
|
|
81
|
+
except (OSError, RuntimeError) as exc:
|
|
82
|
+
raise WebBuildError(f"verified directory is no longer accessible: {directory.path}") from exc
|
|
83
|
+
expected = (directory.device, directory.inode)
|
|
84
|
+
if stat.S_ISLNK(current.st_mode) or not stat.S_ISDIR(current.st_mode):
|
|
85
|
+
raise WebBuildError(f"verified directory was replaced by a symlink or non-directory: {directory.path}")
|
|
86
|
+
if (current.st_dev, current.st_ino) != expected:
|
|
87
|
+
raise WebBuildError(f"verified directory identity changed: {directory.path}")
|
|
88
|
+
if current_real != directory.real_path:
|
|
89
|
+
raise WebBuildError(f"verified directory real path changed: {directory.path}")
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
def assert_verified_directory_handle(directory):
|
|
93
|
+
try:
|
|
94
|
+
opened = os.fstat(directory.fd)
|
|
95
|
+
except OSError as exc:
|
|
96
|
+
raise WebBuildError(f"verified directory handle is no longer accessible: {directory.path}") from exc
|
|
97
|
+
if not stat.S_ISDIR(opened.st_mode) or (opened.st_dev, opened.st_ino) != (directory.device, directory.inode):
|
|
98
|
+
raise WebBuildError(f"verified directory handle identity changed: {directory.path}")
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
def open_or_create_child_directory(parent, name, mode=0o755, require_new=False):
|
|
102
|
+
assert_verified_directory(parent)
|
|
103
|
+
created = False
|
|
104
|
+
try:
|
|
105
|
+
entry = os.lstat(name, dir_fd=parent.fd)
|
|
106
|
+
if require_new:
|
|
107
|
+
raise WebBuildError(f"scratch directory already exists: {parent.path / name}")
|
|
108
|
+
except FileNotFoundError:
|
|
109
|
+
try:
|
|
110
|
+
os.mkdir(name, mode=mode, dir_fd=parent.fd)
|
|
111
|
+
except FileExistsError as exc:
|
|
112
|
+
raise WebBuildError(f"directory appeared during exclusive creation: {parent.path / name}") from exc
|
|
113
|
+
except OSError as exc:
|
|
114
|
+
raise WebBuildError(f"cannot create directory: {parent.path / name}") from exc
|
|
115
|
+
created = True
|
|
116
|
+
entry = os.lstat(name, dir_fd=parent.fd)
|
|
117
|
+
if stat.S_ISLNK(entry.st_mode) or not stat.S_ISDIR(entry.st_mode):
|
|
118
|
+
raise WebBuildError(f"path component must be a real directory: {parent.path / name}")
|
|
119
|
+
try:
|
|
120
|
+
fd = os.open(name, _directory_open_flags(), dir_fd=parent.fd)
|
|
121
|
+
except OSError as exc:
|
|
122
|
+
raise WebBuildError(f"cannot open path component without following symlinks: {parent.path / name}") from exc
|
|
123
|
+
try:
|
|
124
|
+
opened = os.fstat(fd)
|
|
125
|
+
after = os.lstat(name, dir_fd=parent.fd)
|
|
126
|
+
expected_real = parent.real_path / name
|
|
127
|
+
resolved = (parent.path / name).resolve(strict=True)
|
|
128
|
+
identity = (opened.st_dev, opened.st_ino)
|
|
129
|
+
if identity != (entry.st_dev, entry.st_ino) or identity != (after.st_dev, after.st_ino):
|
|
130
|
+
raise WebBuildError(f"directory identity changed during validation: {parent.path / name}")
|
|
131
|
+
if resolved != expected_real:
|
|
132
|
+
raise WebBuildError(f"directory escaped its verified parent: {parent.path / name}")
|
|
133
|
+
child = VerifiedDirectory(expected_real, expected_real, opened.st_dev, opened.st_ino, fd, owned=created)
|
|
134
|
+
assert_verified_directory(parent)
|
|
135
|
+
assert_verified_directory(child)
|
|
136
|
+
return child
|
|
137
|
+
except Exception:
|
|
138
|
+
os.close(fd)
|
|
139
|
+
raise
|
|
140
|
+
|
|
141
|
+
|
|
142
|
+
def create_owned_scratch(parent):
|
|
143
|
+
for _ in range(16):
|
|
144
|
+
name = f"web-build-{os.getpid()}-{secrets.token_hex(8)}"
|
|
145
|
+
try:
|
|
146
|
+
return open_or_create_child_directory(parent, name, mode=0o700, require_new=True)
|
|
147
|
+
except WebBuildError as exc:
|
|
148
|
+
if "already exists" not in str(exc):
|
|
149
|
+
raise
|
|
150
|
+
raise WebBuildError("cannot allocate a unique Web build scratch directory")
|
|
151
|
+
|
|
152
|
+
|
|
153
|
+
def validate_version(value):
|
|
154
|
+
if not isinstance(value, str) or not VERSION_PATTERN.fullmatch(value):
|
|
155
|
+
raise WebBuildError("package.json version must match x.y.z")
|
|
156
|
+
parts = value.split(".")
|
|
157
|
+
if any(len(part) > 1 and part.startswith("0") for part in parts):
|
|
158
|
+
raise WebBuildError("package.json version must match x.y.z")
|
|
159
|
+
if int(parts[1]) > 99 or int(parts[2]) > 99:
|
|
160
|
+
raise WebBuildError("package.json version minor and patch must be <= 99")
|
|
161
|
+
return value
|
|
162
|
+
|
|
163
|
+
|
|
164
|
+
def normalize_host(value):
|
|
165
|
+
diagnostic = "package.json supersdk.offlineHost must be a deployable domain or HTTP(S) URL"
|
|
166
|
+
if not isinstance(value, str) or not value or value != value.strip() or any(ch.isspace() for ch in value):
|
|
167
|
+
raise WebBuildError(diagnostic)
|
|
168
|
+
try:
|
|
169
|
+
value.encode("ascii")
|
|
170
|
+
except UnicodeEncodeError as exc:
|
|
171
|
+
raise WebBuildError(diagnostic) from exc
|
|
172
|
+
if "://" in value:
|
|
173
|
+
parsed = urlparse(value)
|
|
174
|
+
if parsed.scheme.lower() not in {"http", "https"} or not parsed.hostname:
|
|
175
|
+
raise WebBuildError(diagnostic)
|
|
176
|
+
host = parsed.hostname.lower()
|
|
177
|
+
else:
|
|
178
|
+
if ":" in value:
|
|
179
|
+
raise WebBuildError(diagnostic)
|
|
180
|
+
host = value.lower()
|
|
181
|
+
try:
|
|
182
|
+
ipaddress.ip_address(host)
|
|
183
|
+
except ValueError:
|
|
184
|
+
pass
|
|
185
|
+
else:
|
|
186
|
+
raise WebBuildError(diagnostic)
|
|
187
|
+
if host == "localhost" or host.startswith(".") or host.endswith(".") or ".." in host or len(host) > 253:
|
|
188
|
+
raise WebBuildError(diagnostic)
|
|
189
|
+
labels = host.split(".")
|
|
190
|
+
label_pattern = re.compile(r"^[a-z0-9]([a-z0-9-]*[a-z0-9])?$")
|
|
191
|
+
if len(labels) < 2 or labels[-1].isdigit():
|
|
192
|
+
raise WebBuildError(diagnostic)
|
|
193
|
+
if any(len(label) > 63 or not label_pattern.fullmatch(label) for label in labels):
|
|
194
|
+
raise WebBuildError(diagnostic)
|
|
195
|
+
return host
|
|
196
|
+
|
|
197
|
+
|
|
198
|
+
def read_package_settings(project_root):
|
|
199
|
+
package_path = project_root / "package.json"
|
|
200
|
+
try:
|
|
201
|
+
package = json.loads(package_path.read_text(encoding="utf-8"))
|
|
202
|
+
except FileNotFoundError as exc:
|
|
203
|
+
raise WebBuildError("package.json is missing") from exc
|
|
204
|
+
except (OSError, UnicodeDecodeError, json.JSONDecodeError) as exc:
|
|
205
|
+
raise WebBuildError("package.json is not valid JSON") from exc
|
|
206
|
+
if not isinstance(package, dict):
|
|
207
|
+
raise WebBuildError("package.json must contain a JSON object")
|
|
208
|
+
settings = package.get("supersdk")
|
|
209
|
+
if not isinstance(settings, dict):
|
|
210
|
+
raise WebBuildError("package.json supersdk must contain a JSON object")
|
|
211
|
+
return validate_version(package.get("version")), normalize_host(settings.get("offlineHost"))
|
|
212
|
+
|
|
213
|
+
|
|
214
|
+
def validate_provenance(source_root):
|
|
215
|
+
provenance_path = source_root / "js" / "supersdk.provenance.json"
|
|
216
|
+
vendored_path = source_root / "js" / "supersdk.js"
|
|
217
|
+
try:
|
|
218
|
+
provenance = json.loads(provenance_path.read_text(encoding="utf-8"))
|
|
219
|
+
except FileNotFoundError as exc:
|
|
220
|
+
raise WebBuildError("supersdk.provenance.json is missing") from exc
|
|
221
|
+
except (OSError, UnicodeDecodeError, json.JSONDecodeError) as exc:
|
|
222
|
+
raise WebBuildError("supersdk.provenance.json is not valid JSON") from exc
|
|
223
|
+
if not isinstance(provenance, dict) or set(provenance) != PROVENANCE_FIELDS:
|
|
224
|
+
raise WebBuildError("supersdk.provenance.json fields do not match the public artifact schema")
|
|
225
|
+
if provenance.get("package") != "@supersdk/jssdk":
|
|
226
|
+
raise WebBuildError("supersdk.provenance.json package must be @supersdk/jssdk")
|
|
227
|
+
if not isinstance(provenance.get("version"), str) or not VERSION_PATTERN.fullmatch(provenance["version"]):
|
|
228
|
+
raise WebBuildError("supersdk.provenance.json version must match x.y.z")
|
|
229
|
+
if not isinstance(provenance.get("integrity"), str) or not INTEGRITY_PATTERN.fullmatch(provenance["integrity"]):
|
|
230
|
+
raise WebBuildError("supersdk.provenance.json integrity must be sha512 SRI")
|
|
231
|
+
if provenance.get("file") != "dist/supersdk.js":
|
|
232
|
+
raise WebBuildError("supersdk.provenance.json file must be dist/supersdk.js")
|
|
233
|
+
expected_hash = provenance.get("sha256")
|
|
234
|
+
if not isinstance(expected_hash, str) or not SHA256_PATTERN.fullmatch(expected_hash):
|
|
235
|
+
raise WebBuildError("supersdk.provenance.json sha256 must be 64 lowercase hex characters")
|
|
236
|
+
actual_hash = hashlib.sha256(vendored_path.read_bytes()).hexdigest()
|
|
237
|
+
if actual_hash != expected_hash:
|
|
238
|
+
raise WebBuildError("supersdk.provenance.json sha256 mismatch")
|
|
239
|
+
|
|
240
|
+
|
|
241
|
+
def validate_source(source_root):
|
|
242
|
+
if not source_root.is_dir():
|
|
243
|
+
raise WebBuildError(f"web source directory is missing: {source_root}")
|
|
244
|
+
for relative in REQUIRED_ASSETS:
|
|
245
|
+
path = source_root / relative
|
|
246
|
+
if path.is_symlink() or not path.is_file():
|
|
247
|
+
raise WebBuildError(f"web source asset is missing or not a regular file: {relative}")
|
|
248
|
+
validate_provenance(source_root)
|
|
249
|
+
|
|
250
|
+
|
|
251
|
+
def iter_publishable_files(root):
|
|
252
|
+
files = []
|
|
253
|
+
for path in root.rglob("*"):
|
|
254
|
+
relative = path.relative_to(root).as_posix()
|
|
255
|
+
if path.is_symlink():
|
|
256
|
+
raise WebBuildError(f"web source must not contain symlinks: {relative}")
|
|
257
|
+
if path.is_dir():
|
|
258
|
+
continue
|
|
259
|
+
if not path.is_file():
|
|
260
|
+
raise WebBuildError(f"web source contains a non-regular file: {relative}")
|
|
261
|
+
if relative in EXCLUDED_ASSETS or path.name == ".DS_Store":
|
|
262
|
+
continue
|
|
263
|
+
files.append((relative, path))
|
|
264
|
+
return sorted(files, key=lambda item: item[0])
|
|
265
|
+
|
|
266
|
+
|
|
267
|
+
def render_dist(source_root, staging_dist, spid):
|
|
268
|
+
staging_dist.mkdir(parents=True)
|
|
269
|
+
for relative, source in iter_publishable_files(source_root):
|
|
270
|
+
target = staging_dist / relative
|
|
271
|
+
target.parent.mkdir(parents=True, exist_ok=True)
|
|
272
|
+
shutil.copy2(source, target)
|
|
273
|
+
(staging_dist / "supersdk-config.json").write_text(
|
|
274
|
+
json.dumps({"spid": spid}, separators=(",", ":")) + "\n",
|
|
275
|
+
encoding="utf-8",
|
|
276
|
+
)
|
|
277
|
+
for relative in REQUIRED_ASSETS:
|
|
278
|
+
if not (staging_dist / relative).is_file():
|
|
279
|
+
raise WebBuildError(f"publishable dist asset is missing: {relative}")
|
|
280
|
+
if (staging_dist / "js" / "supersdk.provenance.json").exists():
|
|
281
|
+
raise WebBuildError("publishable dist must not contain supersdk.provenance.json")
|
|
282
|
+
|
|
283
|
+
|
|
284
|
+
def build_offline_zip(staging_dist, target, version):
|
|
285
|
+
files = iter_publishable_files(staging_dist)
|
|
286
|
+
file_map = {relative: {"length": path.stat().st_size} for relative, path in files}
|
|
287
|
+
meta = {
|
|
288
|
+
"__ver": 1,
|
|
289
|
+
"app_version": version,
|
|
290
|
+
"build_time": int(time.time()),
|
|
291
|
+
"file_map": file_map,
|
|
292
|
+
}
|
|
293
|
+
target.parent.mkdir(parents=True, exist_ok=True)
|
|
294
|
+
with zipfile.ZipFile(target, "w", compression=zipfile.ZIP_DEFLATED) as archive:
|
|
295
|
+
for relative, path in files:
|
|
296
|
+
archive.write(path, relative)
|
|
297
|
+
archive.writestr("__super_meta.json", json.dumps(meta, separators=(",", ":")) + "\n")
|
|
298
|
+
|
|
299
|
+
|
|
300
|
+
def install_outputs(scratch, staged_dist, staged_zip, project):
|
|
301
|
+
outputs = [
|
|
302
|
+
(staged_dist.name, project.root, "dist", "directory"),
|
|
303
|
+
(staged_zip.name, project.artifacts, "site-offline.zip", "file"),
|
|
304
|
+
]
|
|
305
|
+
backups = []
|
|
306
|
+
installed = []
|
|
307
|
+
try:
|
|
308
|
+
for index, (_, parent, target_name, expected_kind) in enumerate(outputs):
|
|
309
|
+
assert_verified_directory_handle(parent)
|
|
310
|
+
try:
|
|
311
|
+
target = os.lstat(target_name, dir_fd=parent.fd)
|
|
312
|
+
except FileNotFoundError:
|
|
313
|
+
continue
|
|
314
|
+
if stat.S_ISLNK(target.st_mode):
|
|
315
|
+
raise WebBuildError(f"refuse to replace symlink output: {parent.path / target_name}")
|
|
316
|
+
if expected_kind == "directory" and not stat.S_ISDIR(target.st_mode):
|
|
317
|
+
raise WebBuildError(f"build output must be a directory: {parent.path / target_name}")
|
|
318
|
+
if expected_kind == "file" and not stat.S_ISREG(target.st_mode):
|
|
319
|
+
raise WebBuildError(f"build output must be a regular file: {parent.path / target_name}")
|
|
320
|
+
backup_name = f"backup-{index}"
|
|
321
|
+
os.replace(target_name, backup_name, src_dir_fd=parent.fd, dst_dir_fd=scratch.fd)
|
|
322
|
+
backups.append((backup_name, parent, target_name))
|
|
323
|
+
for staged_name, parent, target_name, _ in outputs:
|
|
324
|
+
assert_verified_directory_handle(parent)
|
|
325
|
+
assert_verified_directory_handle(scratch)
|
|
326
|
+
os.replace(staged_name, target_name, src_dir_fd=scratch.fd, dst_dir_fd=parent.fd)
|
|
327
|
+
installed.append((parent, target_name))
|
|
328
|
+
except Exception:
|
|
329
|
+
for index, (parent, target_name) in enumerate(reversed(installed)):
|
|
330
|
+
assert_verified_directory_handle(parent)
|
|
331
|
+
assert_verified_directory_handle(scratch)
|
|
332
|
+
os.replace(target_name, f"rollback-installed-{index}", src_dir_fd=parent.fd, dst_dir_fd=scratch.fd)
|
|
333
|
+
for backup_name, parent, target_name in reversed(backups):
|
|
334
|
+
assert_verified_directory_handle(parent)
|
|
335
|
+
assert_verified_directory_handle(scratch)
|
|
336
|
+
try:
|
|
337
|
+
os.lstat(backup_name, dir_fd=scratch.fd)
|
|
338
|
+
except FileNotFoundError:
|
|
339
|
+
continue
|
|
340
|
+
os.replace(backup_name, target_name, src_dir_fd=scratch.fd, dst_dir_fd=parent.fd)
|
|
341
|
+
raise
|
|
342
|
+
for backup_name, _, _ in backups:
|
|
343
|
+
try:
|
|
344
|
+
os.lstat(backup_name, dir_fd=scratch.fd)
|
|
345
|
+
except FileNotFoundError:
|
|
346
|
+
continue
|
|
347
|
+
remove_entry_at(scratch.fd, backup_name)
|
|
348
|
+
|
|
349
|
+
|
|
350
|
+
@dataclass
|
|
351
|
+
class WebOutputDirectories:
|
|
352
|
+
root: VerifiedDirectory
|
|
353
|
+
state: VerifiedDirectory
|
|
354
|
+
artifacts: VerifiedDirectory
|
|
355
|
+
|
|
356
|
+
|
|
357
|
+
def open_output_directories(project_root):
|
|
358
|
+
root = open_verified_root(project_root)
|
|
359
|
+
state_dir = None
|
|
360
|
+
artifacts_dir = None
|
|
361
|
+
try:
|
|
362
|
+
state_dir = open_or_create_child_directory(root, ".supersdk")
|
|
363
|
+
artifacts_dir = open_or_create_child_directory(state_dir, "artifacts")
|
|
364
|
+
return WebOutputDirectories(root, state_dir, artifacts_dir)
|
|
365
|
+
except Exception:
|
|
366
|
+
if artifacts_dir is not None:
|
|
367
|
+
os.close(artifacts_dir.fd)
|
|
368
|
+
if state_dir is not None:
|
|
369
|
+
os.close(state_dir.fd)
|
|
370
|
+
os.close(root.fd)
|
|
371
|
+
raise
|
|
372
|
+
|
|
373
|
+
|
|
374
|
+
def close_output_directories(directories):
|
|
375
|
+
os.close(directories.artifacts.fd)
|
|
376
|
+
os.close(directories.state.fd)
|
|
377
|
+
os.close(directories.root.fd)
|
|
378
|
+
|
|
379
|
+
|
|
380
|
+
def remove_entry_at(directory_fd, name):
|
|
381
|
+
entry = os.lstat(name, dir_fd=directory_fd)
|
|
382
|
+
if stat.S_ISDIR(entry.st_mode) and not stat.S_ISLNK(entry.st_mode):
|
|
383
|
+
child_fd = os.open(name, _directory_open_flags(), dir_fd=directory_fd)
|
|
384
|
+
try:
|
|
385
|
+
opened = os.fstat(child_fd)
|
|
386
|
+
after = os.lstat(name, dir_fd=directory_fd)
|
|
387
|
+
if (opened.st_dev, opened.st_ino) != (entry.st_dev, entry.st_ino):
|
|
388
|
+
raise WebBuildError(f"scratch child changed during cleanup: {name}")
|
|
389
|
+
if (opened.st_dev, opened.st_ino) != (after.st_dev, after.st_ino):
|
|
390
|
+
raise WebBuildError(f"scratch child identity changed during cleanup: {name}")
|
|
391
|
+
remove_directory_contents(child_fd)
|
|
392
|
+
finally:
|
|
393
|
+
os.close(child_fd)
|
|
394
|
+
os.rmdir(name, dir_fd=directory_fd)
|
|
395
|
+
else:
|
|
396
|
+
os.unlink(name, dir_fd=directory_fd)
|
|
397
|
+
|
|
398
|
+
|
|
399
|
+
def remove_directory_contents(directory_fd):
|
|
400
|
+
scan_fd = os.open(".", _directory_open_flags(), dir_fd=directory_fd)
|
|
401
|
+
try:
|
|
402
|
+
names = os.listdir(scan_fd)
|
|
403
|
+
finally:
|
|
404
|
+
os.close(scan_fd)
|
|
405
|
+
for name in names:
|
|
406
|
+
remove_entry_at(directory_fd, name)
|
|
407
|
+
|
|
408
|
+
|
|
409
|
+
def cleanup_owned_scratch(parent, scratch):
|
|
410
|
+
if not scratch.owned:
|
|
411
|
+
raise WebBuildError("refuse to clean a scratch directory not owned by this invocation")
|
|
412
|
+
assert_verified_directory_handle(parent)
|
|
413
|
+
assert_verified_directory_handle(scratch)
|
|
414
|
+
scratch_name = scratch.path.name
|
|
415
|
+
try:
|
|
416
|
+
entry = os.lstat(scratch_name, dir_fd=parent.fd)
|
|
417
|
+
except FileNotFoundError as exc:
|
|
418
|
+
raise WebBuildError("owned scratch entry disappeared before cleanup") from exc
|
|
419
|
+
if stat.S_ISLNK(entry.st_mode) or not stat.S_ISDIR(entry.st_mode):
|
|
420
|
+
raise WebBuildError("owned scratch entry was replaced by a symlink or non-directory")
|
|
421
|
+
if (entry.st_dev, entry.st_ino) != (scratch.device, scratch.inode):
|
|
422
|
+
raise WebBuildError("owned scratch entry identity changed before cleanup")
|
|
423
|
+
try:
|
|
424
|
+
remove_directory_contents(scratch.fd)
|
|
425
|
+
after = os.lstat(scratch_name, dir_fd=parent.fd)
|
|
426
|
+
if (after.st_dev, after.st_ino) != (scratch.device, scratch.inode):
|
|
427
|
+
raise WebBuildError("owned scratch entry identity changed during cleanup")
|
|
428
|
+
os.rmdir(scratch_name, dir_fd=parent.fd)
|
|
429
|
+
finally:
|
|
430
|
+
os.close(scratch.fd)
|
|
431
|
+
scratch.fd = -1
|
|
432
|
+
|
|
433
|
+
|
|
434
|
+
def build(project_root, spid):
|
|
435
|
+
if not SPID_PATTERN.fullmatch(spid):
|
|
436
|
+
raise WebBuildError("spid must be 8 lowercase letters or digits")
|
|
437
|
+
version, host = read_package_settings(project_root)
|
|
438
|
+
source_root = project_root / "src"
|
|
439
|
+
validate_source(source_root)
|
|
440
|
+
original_cwd_fd = os.open(".", _directory_open_flags())
|
|
441
|
+
try:
|
|
442
|
+
directories = open_output_directories(project_root)
|
|
443
|
+
except Exception:
|
|
444
|
+
os.close(original_cwd_fd)
|
|
445
|
+
raise
|
|
446
|
+
scratch = None
|
|
447
|
+
try:
|
|
448
|
+
scratch = create_owned_scratch(directories.state)
|
|
449
|
+
os.fchdir(scratch.fd)
|
|
450
|
+
staged_dist = Path("dist")
|
|
451
|
+
staged_zip = Path("site-offline.zip")
|
|
452
|
+
render_dist(source_root, staged_dist, spid)
|
|
453
|
+
build_offline_zip(staged_dist, staged_zip, version)
|
|
454
|
+
assert_verified_directory_handle(scratch)
|
|
455
|
+
install_outputs(scratch, staged_dist, staged_zip, directories)
|
|
456
|
+
finally:
|
|
457
|
+
try:
|
|
458
|
+
os.fchdir(original_cwd_fd)
|
|
459
|
+
finally:
|
|
460
|
+
os.close(original_cwd_fd)
|
|
461
|
+
try:
|
|
462
|
+
if scratch is not None:
|
|
463
|
+
cleanup_owned_scratch(directories.state, scratch)
|
|
464
|
+
finally:
|
|
465
|
+
close_output_directories(directories)
|
|
466
|
+
return {
|
|
467
|
+
"dist": "dist",
|
|
468
|
+
"offlineArtifact": ".supersdk/artifacts/site-offline.zip",
|
|
469
|
+
"offlineHost": host,
|
|
470
|
+
"version": version,
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
|
|
474
|
+
def main(argv=None):
|
|
475
|
+
parser = argparse.ArgumentParser(description="Build the SuperSDK Simple Web project")
|
|
476
|
+
subcommands = parser.add_subparsers(dest="command", required=True)
|
|
477
|
+
build_command = subcommands.add_parser("build")
|
|
478
|
+
build_command.add_argument("--spid", required=True)
|
|
479
|
+
args = parser.parse_args(argv)
|
|
480
|
+
try:
|
|
481
|
+
result = build(Path(__file__).resolve().parent.parent, args.spid)
|
|
482
|
+
except (WebBuildError, OSError, shutil.Error) as exc:
|
|
483
|
+
print(f"web build failed: {exc}", file=sys.stderr)
|
|
484
|
+
return 1
|
|
485
|
+
print(json.dumps(result, sort_keys=True))
|
|
486
|
+
return 0
|
|
487
|
+
|
|
488
|
+
|
|
489
|
+
if __name__ == "__main__":
|
|
490
|
+
raise SystemExit(main())
|
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"use strict";
|
|
3
3
|
|
|
4
4
|
// Package-root relative on purpose: the file is emitted into <project>/dist by
|
|
5
|
-
// scripts/
|
|
5
|
+
// scripts/build.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
|
|
|
@@ -99,7 +99,7 @@
|
|
|
99
99
|
|
|
100
100
|
function loadConfig() {
|
|
101
101
|
if (!window.location || window.location.protocol === "file:") {
|
|
102
|
-
return Promise.reject(new Error("Run scripts/
|
|
102
|
+
return Promise.reject(new Error("Run scripts/build.sh then serve the built dist over the local HTTP server"));
|
|
103
103
|
}
|
|
104
104
|
var url = new URL(CONFIG_PATH, window.location.href).toString();
|
|
105
105
|
return fetch(url).then(function(response) {
|
package/dist/util/shell.js
CHANGED
|
@@ -2,12 +2,12 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.shellQuote = shellQuote;
|
|
4
4
|
/**
|
|
5
|
-
* `shlex.quote()`
|
|
5
|
+
* POSIX `shlex.quote()` 语义的移植 —— 建议命令里出现的路径和取值必须能被**照抄进 shell
|
|
6
6
|
* 直接执行**。
|
|
7
7
|
*
|
|
8
8
|
* 🔴 不能用 `JSON.stringify`:它产出的是双引号,shell 会在双引号里展开 `$VAR`、`` `cmd` ``、
|
|
9
|
-
* `\`。路径里带 `$` 的目录(`/tmp/a$HOME b
|
|
10
|
-
*
|
|
9
|
+
* `\`。路径里带 `$` 的目录(`/tmp/a$HOME b`)会被展开成完全不同的路径。这里采用通用
|
|
10
|
+
* shlex 规则:
|
|
11
11
|
* 安全字符集直接原样输出,否则单引号包裹,内部单引号用 `'"'"'` 拼接。
|
|
12
12
|
*/
|
|
13
13
|
const SHELL_SAFE = /^[A-Za-z0-9_@%+=:,./-]+$/;
|