lecodes-cli 0.18.2 → 0.19.2

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 (224) hide show
  1. package/dist/index.js +1866 -301
  2. package/package.json +14 -12
  3. package/runtime/scene-harness.json +1 -1
  4. package/runtime/sdk-types.json +1 -1
  5. package/src/api.ts +302 -0
  6. package/src/browserAuth.ts +87 -0
  7. package/src/cmgenTool.ts +104 -0
  8. package/src/commands/app.ts +891 -0
  9. package/src/commands/appAndroid.ts +603 -0
  10. package/src/commands/appDesktop.ts +326 -0
  11. package/src/commands/appDesktopMac.ts +470 -0
  12. package/src/commands/appIcon.ts +187 -0
  13. package/src/commands/appShared.ts +448 -0
  14. package/src/commands/appTemplates.ts +883 -0
  15. package/src/commands/appTemplatesAndroid.ts +599 -0
  16. package/src/commands/appTemplatesGradlew.ts +9 -0
  17. package/src/commands/assets.ts +28 -0
  18. package/src/commands/clone.ts +60 -0
  19. package/src/commands/compile.ts +253 -0
  20. package/src/commands/create.ts +59 -0
  21. package/src/commands/design.ts +863 -0
  22. package/src/commands/designTemplates.ts +11 -0
  23. package/src/commands/desktop.ts +122 -0
  24. package/src/commands/dev.ts +214 -0
  25. package/src/commands/diff.ts +59 -0
  26. package/src/commands/init.ts +191 -0
  27. package/src/commands/install.ts +198 -0
  28. package/src/commands/lightmap.ts +290 -0
  29. package/src/commands/link.ts +147 -0
  30. package/src/commands/login.ts +24 -0
  31. package/src/commands/navmesh.ts +225 -0
  32. package/src/commands/pn.ts +305 -0
  33. package/src/commands/projectTemplates.ts +129 -0
  34. package/src/commands/pull.ts +109 -0
  35. package/src/commands/push.ts +173 -0
  36. package/src/commands/render.ts +414 -0
  37. package/src/commands/scene.ts +148 -0
  38. package/src/commands/shaders.ts +157 -0
  39. package/src/commands/status.ts +29 -0
  40. package/src/commands/test.ts +370 -0
  41. package/src/commands/thumbs.ts +178 -0
  42. package/src/commands/types.ts +87 -0
  43. package/src/commands/update.ts +190 -0
  44. package/src/compile/assetIcons.ts +214 -0
  45. package/src/compile/collect.ts +46 -0
  46. package/src/compile/collectLocal.ts +37 -0
  47. package/src/compile/designCompile.ts +104 -0
  48. package/src/compile/fonts.ts +156 -0
  49. package/src/compile/headlessBundle.ts +129 -0
  50. package/src/compile/nativeStack.ts +21 -0
  51. package/src/compile/projectCompile.ts +109 -0
  52. package/src/compile/sceneCompile.ts +113 -0
  53. package/src/compile/screenEntry.ts +127 -0
  54. package/src/compile/shaders.ts +245 -0
  55. package/src/config.ts +42 -0
  56. package/src/designMeta.ts +35 -0
  57. package/src/desktopRenderer.ts +532 -0
  58. package/src/desktopScript.ts +276 -0
  59. package/src/dev/clientTemplates.ts +160 -0
  60. package/src/dev/devServer.ts +292 -0
  61. package/src/dev/wsServer.ts +144 -0
  62. package/src/distRoot.ts +20 -0
  63. package/src/ignore.ts +163 -0
  64. package/src/index.ts +491 -0
  65. package/src/lecodes-3d-editor.d.ts +41 -0
  66. package/src/lecodes-assets.d.ts +7 -0
  67. package/src/lecodes-design.d.ts +191 -0
  68. package/src/lecodes-renderer.d.ts +131 -0
  69. package/src/localFiles.ts +144 -0
  70. package/src/manifest.ts +46 -0
  71. package/src/matcTool.ts +137 -0
  72. package/src/peers.ts +40 -0
  73. package/src/project.ts +61 -0
  74. package/src/projectEnv.ts +94 -0
  75. package/src/qrcode-terminal.d.ts +9 -0
  76. package/src/releases.ts +125 -0
  77. package/src/serverDiff.ts +78 -0
  78. package/src/textDiff.ts +103 -0
  79. package/src/types.ts +0 -0
  80. package/src/util.ts +146 -0
  81. package/runtime/sdk/animate/animate.ts +0 -238
  82. package/runtime/sdk/animate/bezier.ts +0 -138
  83. package/runtime/sdk/animate/easings.ts +0 -126
  84. package/runtime/sdk/canvas/Canvas.ts +0 -305
  85. package/runtime/sdk/core/Aspect.ts +0 -512
  86. package/runtime/sdk/core/InspectorUI.ts +0 -212
  87. package/runtime/sdk/core/color.ts +0 -66
  88. package/runtime/sdk/core/compWrite.ts +0 -42
  89. package/runtime/sdk/core/events.ts +0 -38
  90. package/runtime/sdk/core/fields.ts +0 -120
  91. package/runtime/sdk/core/registry.ts +0 -23
  92. package/runtime/sdk/core/signals.ts +0 -277
  93. package/runtime/sdk/core/time.ts +0 -81
  94. package/runtime/sdk/g2/Camera2D.ts +0 -40
  95. package/runtime/sdk/g2/CharacterController2D.ts +0 -276
  96. package/runtime/sdk/g2/Node2D.ts +0 -267
  97. package/runtime/sdk/g2/OneWay2D.ts +0 -66
  98. package/runtime/sdk/g2/Physics2D.ts +0 -346
  99. package/runtime/sdk/g2/Scene2D.ts +0 -209
  100. package/runtime/sdk/g2/Shape2D.ts +0 -259
  101. package/runtime/sdk/g2/Sprite.ts +0 -89
  102. package/runtime/sdk/g2/SpriteAnimation.ts +0 -171
  103. package/runtime/sdk/g2/SpriteSheet.ts +0 -166
  104. package/runtime/sdk/g2/Texture2D.ts +0 -47
  105. package/runtime/sdk/g2/Tilemap.ts +0 -41
  106. package/runtime/sdk/g2/Tileset.ts +0 -71
  107. package/runtime/sdk/g2/Trigger2D.ts +0 -77
  108. package/runtime/sdk/g2/autotile.ts +0 -433
  109. package/runtime/sdk/g2/cells.ts +0 -91
  110. package/runtime/sdk/g2/defineScene2d.ts +0 -381
  111. package/runtime/sdk/g2/groups2d.ts +0 -106
  112. package/runtime/sdk/g2/loop.ts +0 -50
  113. package/runtime/sdk/g2/scenarios2d.ts +0 -69
  114. package/runtime/sdk/g2/touch.ts +0 -83
  115. package/runtime/sdk/gl/Camera.ts +0 -160
  116. package/runtime/sdk/gl/CameraPlace.ts +0 -52
  117. package/runtime/sdk/gl/CharacterController.ts +0 -238
  118. package/runtime/sdk/gl/Gearbox.ts +0 -212
  119. package/runtime/sdk/gl/Geometry.ts +0 -279
  120. package/runtime/sdk/gl/IK.ts +0 -193
  121. package/runtime/sdk/gl/InstancedMesh.ts +0 -132
  122. package/runtime/sdk/gl/Light.ts +0 -99
  123. package/runtime/sdk/gl/Lightmap.ts +0 -179
  124. package/runtime/sdk/gl/Material.ts +0 -245
  125. package/runtime/sdk/gl/Mesh.ts +0 -83
  126. package/runtime/sdk/gl/Model.ts +0 -64
  127. package/runtime/sdk/gl/Node.ts +0 -350
  128. package/runtime/sdk/gl/Noise.ts +0 -30
  129. package/runtime/sdk/gl/Particles.ts +0 -676
  130. package/runtime/sdk/gl/Physics.ts +0 -222
  131. package/runtime/sdk/gl/Plane.ts +0 -53
  132. package/runtime/sdk/gl/Ray.ts +0 -16
  133. package/runtime/sdk/gl/Scene.ts +0 -479
  134. package/runtime/sdk/gl/Shape.ts +0 -377
  135. package/runtime/sdk/gl/Texture.ts +0 -46
  136. package/runtime/sdk/gl/Trigger.ts +0 -45
  137. package/runtime/sdk/gl/Vehicle.ts +0 -473
  138. package/runtime/sdk/gl/Wheel.ts +0 -240
  139. package/runtime/sdk/gl/animation/AnimationClip.ts +0 -204
  140. package/runtime/sdk/gl/animation/Animator.ts +0 -87
  141. package/runtime/sdk/gl/animation/Layer.ts +0 -29
  142. package/runtime/sdk/gl/animation/Loop.ts +0 -25
  143. package/runtime/sdk/gl/animation/Playback.ts +0 -43
  144. package/runtime/sdk/gl/animation/core.ts +0 -294
  145. package/runtime/sdk/gl/controls.ts +0 -95
  146. package/runtime/sdk/gl/physicsEvents.ts +0 -20
  147. package/runtime/sdk/gl/scenarios.ts +0 -291
  148. package/runtime/sdk/gl/state.ts +0 -6
  149. package/runtime/sdk/gl/touch.ts +0 -68
  150. package/runtime/sdk/inject.ts +0 -186
  151. package/runtime/sdk/math/Mathf.ts +0 -118
  152. package/runtime/sdk/math/mat4.ts +0 -278
  153. package/runtime/sdk/math/quat.ts +0 -232
  154. package/runtime/sdk/math/vec.ts +0 -255
  155. package/runtime/sdk/plugins/camera.ts +0 -81
  156. package/runtime/sdk/plugins/geolocation.ts +0 -123
  157. package/runtime/sdk/plugins/oauth.ts +0 -61
  158. package/runtime/sdk/plugins/permission.ts +0 -7
  159. package/runtime/sdk/plugins/push.ts +0 -132
  160. package/runtime/sdk/plugins/qr.ts +0 -73
  161. package/runtime/sdk/plugins/service.ts +0 -47
  162. package/runtime/sdk/runtime/app.ts +0 -101
  163. package/runtime/sdk/runtime/appEvents.ts +0 -54
  164. package/runtime/sdk/runtime/channel.ts +0 -61
  165. package/runtime/sdk/runtime/clipboard.ts +0 -20
  166. package/runtime/sdk/runtime/datetime.ts +0 -329
  167. package/runtime/sdk/runtime/device.ts +0 -293
  168. package/runtime/sdk/runtime/fetch.ts +0 -77
  169. package/runtime/sdk/runtime/files.ts +0 -23
  170. package/runtime/sdk/runtime/input.ts +0 -175
  171. package/runtime/sdk/runtime/media.ts +0 -111
  172. package/runtime/sdk/runtime/misc.ts +0 -16
  173. package/runtime/sdk/runtime/net.ts +0 -36
  174. package/runtime/sdk/runtime/rpc.ts +0 -218
  175. package/runtime/sdk/runtime/service.ts +0 -83
  176. package/runtime/sdk/runtime/share.ts +0 -9
  177. package/runtime/sdk/runtime/storage.ts +0 -13
  178. package/runtime/sdk/runtime/touch.ts +0 -76
  179. package/runtime/sdk/scene/defineScene.ts +0 -1227
  180. package/runtime/sdk/scene/editorPlugins.ts +0 -92
  181. package/runtime/sdk/scene/gizmos.ts +0 -148
  182. package/runtime/sdk/scene/grammar.ts +0 -120
  183. package/runtime/sdk/scene/material.ts +0 -188
  184. package/runtime/sdk/server/auth/appConfig.ts +0 -12
  185. package/runtime/sdk/server/auth/global.ts +0 -80
  186. package/runtime/sdk/server/auth/host.ts +0 -318
  187. package/runtime/sdk/server/auth/models.ts +0 -83
  188. package/runtime/sdk/server/auth/types.ts +0 -50
  189. package/runtime/sdk/server/channel.ts +0 -56
  190. package/runtime/sdk/server/context.ts +0 -36
  191. package/runtime/sdk/server/db/defineDb.ts +0 -237
  192. package/runtime/sdk/server/db/fields.ts +0 -132
  193. package/runtime/sdk/server/db/httpTransport.ts +0 -93
  194. package/runtime/sdk/server/db/index.ts +0 -7
  195. package/runtime/sdk/server/db/marci/query.ts +0 -412
  196. package/runtime/sdk/server/db/types.ts +0 -202
  197. package/runtime/sdk/server/errors.ts +0 -12
  198. package/runtime/sdk/server/host.ts +0 -74
  199. package/runtime/sdk/server/inject.ts +0 -13
  200. package/runtime/sdk/server/runtime.ts +0 -133
  201. package/runtime/sdk/server/validate.ts +0 -87
  202. package/runtime/sdk/ui/NativeView.ts +0 -142
  203. package/runtime/sdk/ui/UI.ts +0 -39
  204. package/runtime/sdk/ui/UIBottomSheet.ts +0 -139
  205. package/runtime/sdk/ui/UIButton.ts +0 -101
  206. package/runtime/sdk/ui/UIContainer.ts +0 -60
  207. package/runtime/sdk/ui/UIImage.ts +0 -83
  208. package/runtime/sdk/ui/UIInput.ts +0 -185
  209. package/runtime/sdk/ui/UIModal.ts +0 -139
  210. package/runtime/sdk/ui/UINode.ts +0 -826
  211. package/runtime/sdk/ui/UIPager.ts +0 -362
  212. package/runtime/sdk/ui/UIPopover.ts +0 -100
  213. package/runtime/sdk/ui/UIScreen.ts +0 -123
  214. package/runtime/sdk/ui/UIScrollable.ts +0 -87
  215. package/runtime/sdk/ui/UISpacer.ts +0 -14
  216. package/runtime/sdk/ui/UITabs.ts +0 -236
  217. package/runtime/sdk/ui/UIText.ts +0 -51
  218. package/runtime/sdk/ui/UIVideo.ts +0 -88
  219. package/runtime/sdk/ui/UIVirtualizedList.ts +0 -241
  220. package/runtime/sdk/ui/UIWidget.ts +0 -127
  221. package/runtime/sdk/ui/fonts.ts +0 -13
  222. package/runtime/sdk/ui/presentable.ts +0 -117
  223. package/runtime/sdk/ui/router.ts +0 -132
  224. package/runtime/sdk/ui/theme.ts +0 -84
@@ -0,0 +1,326 @@
1
+ import { execFileSync, spawn } from "node:child_process"
2
+ import { cpSync, existsSync, mkdirSync, readdirSync, rmSync, statSync, writeFileSync } from "node:fs"
3
+ import { basename, dirname, join, resolve } from "node:path"
4
+ import { buildBundle } from "./compile"
5
+ import { resolveDesktopExe, selectedDesktopVersion } from "../desktopRenderer"
6
+ import { CliError, c, flagBool, flagStr, log, note, success, warn, type Args } from "../util"
7
+ import { assertRendererSupportsBundle, projectRoot, readJson, resolveDesktopRenderer, writeJson, type AppConfig } from "./appShared"
8
+ import { loadIconMaster } from "./appIcon"
9
+ import { buildMacApp, macHostProvidedNames } from "./appDesktopMac"
10
+
11
+ /*
12
+ * `lecodes app init|build desktop` — the desktop arm of the native-app commands
13
+ * (docs/desktop-target-plan.md phase 3). Unlike iOS/Android there is no user-side toolchain:
14
+ * `build` stages the verified cached host + the compiled bundle + its assets into one portable
15
+ * folder that runs on a double-click. On macOS the same command produces a signed (and, with a
16
+ * notarytool profile, notarized) .app instead — the staging half is shared, the bundle half lives
17
+ * in appDesktopMac.ts.
18
+ *
19
+ * The staged folder is SELF-DESCRIBING: a pruned app.json (name + desktop.window) ships next to
20
+ * app.js, and the host (≥1.3.0) reads it for title/size/min-size/resizable/fullscreen — no
21
+ * launcher, no envs, works the same on Windows and Linux. Field-proven ancestor: the quiz
22
+ * kiosk's tools/pack-desktop.mjs (its BUG-REPORT.md drove the host fixes this relies on).
23
+ */
24
+
25
+ const mb = (bytes: number) => `${(bytes / 1024 / 1024).toFixed(1)} MB`
26
+
27
+ const dirSize = (dir: string): number =>
28
+ readdirSync(dir, { withFileTypes: true }).reduce((sum, e) => {
29
+ const p = join(dir, e.name)
30
+ return sum + (e.isDirectory() ? dirSize(p) : statSync(p).size)
31
+ }, 0)
32
+
33
+ /* --------------------------------------- init ---------------------------------------- */
34
+
35
+ /** `lecodes app init desktop` — declare the desktop target in app.json (mirrors
36
+ * `lecodes init --desktop` for existing projects). Purely additive; re-running keeps edits. */
37
+ export const initDesktop = async (args: Args) => {
38
+ const root = projectRoot()
39
+ const existing = readJson<AppConfig>(join(root, "app.json")) ?? {}
40
+ const name = flagStr(args, "name") ?? existing.name ?? basename(root)
41
+ const window = {
42
+ width: 1280, height: 800, minWidth: 640, minHeight: 480,
43
+ ...existing.desktop?.window,
44
+ ...(flagBool(args, "fullscreen") ? { fullscreen: true } : {}),
45
+ }
46
+ const config: AppConfig = { ...existing, name, desktop: { ...existing.desktop, window } }
47
+ writeJson(join(root, "app.json"), config)
48
+ log(`${c.green("written")} app.json (desktop.window ${window.width}×${window.height}${window.fullscreen ? ", fullscreen" : ""})`)
49
+ log(`Next: ${c.bold("lecodes dev --desktop")} to develop, ${c.bold("lecodes app build desktop")} to package.`)
50
+ }
51
+
52
+ /* --------------------------------------- build --------------------------------------- */
53
+
54
+ /** Windows/file-system-clean name; non-ASCII is fine (the host is UTF-8-clean since 1.3.0). */
55
+ const cleanFileName = (name: string) =>
56
+ name.replace(/[<>:"/\\|?*\u0000-\u001f]/g, "").replace(/[. ]+$/, "").trim() || "app"
57
+
58
+ /** Where a built folder keeps the two kinds of file. Both are a HOST contract (see onFetchLocal and
59
+ * the runtime-assets probe in desktop/src/main.cpp) — change them on both sides or not at all. */
60
+ const ASSETS_DIR = "assets" // the project's own files
61
+ const RUNTIME_DIR = "runtime" // the host's engine files (initial.js, built-in materials, IBL)
62
+
63
+ export const buildDesktop = async (args: Args) => {
64
+ const root = projectRoot()
65
+ const config = readJson<AppConfig>(join(root, "app.json")) ?? {}
66
+ const name = (config.name ?? basename(root)).trim() || "app"
67
+ const slug = flagStr(args, "slug") ?? cleanFileName(name)
68
+ const out = resolve(root, flagStr(args, "o", "out") ?? join(root, "dist", slug))
69
+
70
+ // Only a previous build output (or an empty/absent dir) may be replaced — never wipe a
71
+ // directory this command didn't produce.
72
+ if (existsSync(out)) {
73
+ const entries = readdirSync(out)
74
+ const looksLikeBuild = entries.length === 0 || entries.includes("app.js") || entries.some((e) => e.endsWith(".app"))
75
+ if (!looksLikeBuild) {
76
+ throw new CliError(`${out} exists and doesn't look like a previous desktop build — pick another -o or clear it yourself.`)
77
+ }
78
+ }
79
+
80
+ // Per-target entry (app.json desktop.entry); an explicit --entry wins.
81
+ if (config.desktop?.entry && flagStr(args, "entry") === undefined) args.flags.entry = config.desktop.entry
82
+
83
+ // ONE renderer decision: it selects the host binary staged into the folder AND the single
84
+ // .filamat variant compiled beside the bundle. Two independent reads of the config is how a
85
+ // build ships a Vulkan exe next to an OpenGL shader (a Filament abort at the first material).
86
+ const renderer = resolveDesktopRenderer(root, args)
87
+
88
+ const mac = process.platform === "darwin"
89
+ note(`Building "${name}" for ${mac ? "macOS" : "desktop"}${renderer === "vulkan" ? " (Vulkan)" : ""}…`)
90
+ const { js, resources } = await buildBundle(
91
+ args,
92
+ { remotePatterns: config.resources?.remote ?? [] },
93
+ { localShaders: true, desktopRenderer: renderer },
94
+ )
95
+
96
+ // The Vulkan host has no 2D engine — refuse now rather than shipping a folder that opens a blank
97
+ // window (see assertRendererSupportsBundle).
98
+ assertRendererSupportsBundle(renderer, js)
99
+
100
+ // Only what the bundle actually references ships — the compile inventory lists EVERY project
101
+ // resource (design/ references included), which would multiply the build size. asset() compiles
102
+ // to fetchLocal("<basename>"), so the bundle itself is the reference list.
103
+ const wanted = new Set([...js.matchAll(/fetchLocal\("([^"]+)"\)/g)].map((m) => m[1]))
104
+ // Compiled shader artifacts always ship: their name is built at runtime ("<hash>_" +
105
+ // _creator.backend + ".filamat" — one bundle serves every native backend), so no literal for
106
+ // the scan above to find. They only exist here when a shader was actually compiled locally.
107
+ const shipped = resources.filter((r) => !r.remote && (r.shader || wanted.has(r.name)))
108
+
109
+ // Duplicate basenames alias silently in a built app (asset() collapses to fetchLocal(basename),
110
+ // quiz BUG-REPORT §4) — a hard error for every referenced name, not a warning.
111
+ const byName = new Map<string, Set<string>>()
112
+ for (const r of resources) {
113
+ if (r.remote || r.shader || !wanted.has(r.name)) continue
114
+ const set = byName.get(r.name) ?? new Set<string>()
115
+ set.add(r.path)
116
+ byName.set(r.name, set)
117
+ }
118
+ const collisions = [...byName.entries()].filter(([, paths]) => paths.size > 1)
119
+ if (collisions.length > 0) {
120
+ throw new CliError(
121
+ "Asset file names must be unique across the whole project — the bundle refers to assets " +
122
+ "by BASENAME, so same-named files silently replace each other in the built app:\n" +
123
+ collisions.map(([n, paths]) => ` ${n}\n` + [...paths].map((p) => ` ${p}`).join("\n")).join("\n"))
124
+ }
125
+ // The host: same verified cache `render --desktop` uses; LECODES_DESKTOP_EXE overrides
126
+ // (points builds at a locally built host).
127
+ const exe = await resolveDesktopExe(renderer)
128
+ const hostDir = dirname(exe)
129
+
130
+ // A referenced name with no local file would 404 at runtime — fail here instead. The host
131
+ // supplies two classes of name itself, so neither counts as missing: the files in its assets/
132
+ // dir (the built-in materials `Material.lit`/`unlit`/… fetch — onFetchLocal falls back there
133
+ // after the app.js dir), and the engine-synthesized preloads. Same split the iOS/Android
134
+ // shells make with sdkProvided + IMPLICIT_PRELOADS.
135
+ const IMPLICIT_PRELOADS = new Set(["uberarchive", "capture"])
136
+ const hostProvided = mac ? macHostProvidedNames(exe) : new Set<string>()
137
+ if (!mac) {
138
+ try {
139
+ for (const f of readdirSync(join(hostDir, "assets"))) hostProvided.add(f)
140
+ } catch { /* checked properly below (assets/initial.js) */ }
141
+ }
142
+ const shippedNames = new Set(shipped.map((r) => r.name))
143
+ const missing = [...wanted].filter((n) => !shippedNames.has(n) && !hostProvided.has(n) && !IMPLICIT_PRELOADS.has(n))
144
+ if (missing.length > 0) {
145
+ throw new CliError(`The bundle references assets with no local file: ${missing.join(", ")}`)
146
+ }
147
+
148
+ // macOS: one signed .app rather than a folder — see appDesktopMac.ts.
149
+ if (mac) return buildMacApp({ root, config, name, slug, out, js, shipped, hostExe: exe, args })
150
+ if (flagBool(args, "dmg")) warn("--dmg is a macOS format — ignored on this platform.")
151
+
152
+ const version = process.env.LECODES_DESKTOP_EXE ? "local build" : selectedDesktopVersion()
153
+
154
+ rmSync(out, { recursive: true, force: true })
155
+ mkdirSync(out, { recursive: true })
156
+
157
+ // 1) Host files — a WHITELIST (exe + runtime DLLs + its assets/fonts dirs), so a
158
+ // LECODES_DESKTOP_EXE override pointing into a build tree doesn't stage the whole ninja
159
+ // output, and .pdb/README never ship. The exe is renamed to the app — the host is
160
+ // name-agnostic and auto-runs the app.js next to itself.
161
+ //
162
+ // The host's own assets land in `runtime/`, NOT `assets/`: that name belongs to the project's
163
+ // files (step 2), and one shared folder would let a project `logo.png` overwrite the host's
164
+ // splash — or a project `initial.js` stop the app booting at all. The host (≥1.5.0) looks for
165
+ // `runtime/initial.js` first and falls back to `assets/`, so a downloaded older host cache
166
+ // still stages correctly.
167
+ for (const entry of readdirSync(hostDir, { withFileTypes: true })) {
168
+ if (entry.isFile() && entry.name.toLowerCase().endsWith(".dll")) {
169
+ cpSync(join(hostDir, entry.name), join(out, entry.name))
170
+ } else if (entry.isDirectory() && entry.name === "assets") {
171
+ cpSync(join(hostDir, entry.name), join(out, RUNTIME_DIR), { recursive: true })
172
+ } else if (entry.isDirectory() && entry.name === "fonts") {
173
+ cpSync(join(hostDir, entry.name), join(out, entry.name), { recursive: true })
174
+ }
175
+ }
176
+ if (!existsSync(join(out, RUNTIME_DIR, "initial.js"))) {
177
+ throw new CliError(`The host at ${hostDir} has no assets/initial.js next to it — a staged build can't boot. ` +
178
+ `(LECODES_DESKTOP_EXE must point at an exe with its runtime assets/ beside it.)`)
179
+ }
180
+ const exeName = process.platform === "win32" ? `${cleanFileName(name)}.exe` : cleanFileName(name)
181
+ cpSync(exe, join(out, exeName))
182
+ note(` host: LeCodes Desktop ${version}`)
183
+
184
+ // 2) The bundle next to the exe (the host auto-runs it), its assets in `assets/` beside it —
185
+ // fetchLocal resolves by BASENAME and searches `<app>/assets/` before the flat dir, so the
186
+ // shipped folder stays readable instead of scattering the whole asset library in the root.
187
+ writeFileSync(join(out, "app.js"), js)
188
+ let assetBytes = 0, assetCount = 0
189
+ if (shipped.length > 0) mkdirSync(join(out, ASSETS_DIR), { recursive: true })
190
+ for (const r of shipped) {
191
+ if (!r.absPath || !existsSync(r.absPath) || !statSync(r.absPath).isFile()) {
192
+ warn(`asset "${r.name}" (${r.path}) has no local file — skipped`)
193
+ continue
194
+ }
195
+ cpSync(r.absPath, join(out, ASSETS_DIR, r.name))
196
+ assetBytes += statSync(r.absPath).size
197
+ assetCount++
198
+ }
199
+ note(` assets: ${assetCount} files, ${mb(assetBytes)} → ${ASSETS_DIR}/`)
200
+
201
+ // 3) Pruned app.json — the host (≥1.3.0) reads name + desktop.window next to app.js, so the
202
+ // folder carries its own window config (title/size/min/resizable/fullscreen).
203
+ writeJson(join(out, "app.json"), { name, ...(config.desktop ? { desktop: { window: config.desktop.window } } : {}) })
204
+
205
+ // 4) Window/taskbar icon (host ≥1.3.0 loads app.ico next to the exe at runtime).
206
+ if (config.icon) {
207
+ await writeIco(root, config, join(out, "app.ico"))
208
+ note(" app.ico rendered from " + config.icon)
209
+ }
210
+
211
+ // 5) A short README (BOM so double-click-Notepad shows it right).
212
+ writeFileSync(join(out, "README.txt"), "\uFEFF" + readmeText(name, exeName, version))
213
+
214
+ success(`Portable build: ${out} (${mb(dirSize(out))})`)
215
+
216
+ if (flagBool(args, "probe")) await probeBuild(out, exeName, config, args)
217
+ if (flagBool(args, "zip")) zipBuild(out)
218
+ if (!flagBool(args, "probe")) note(`Tip: --probe renders the staged folder headless to verify exactly what ships.`)
219
+ }
220
+
221
+ /** app.ico from the app.json icon master: PNG-compressed frames (fine on the Win10+ the
222
+ * distribution requires) at the sizes Windows asks for — no image tooling needed. */
223
+ const writeIco = async (root: string, config: AppConfig, dest: string) => {
224
+ const { image } = await loadIconMaster(root, config)
225
+ const sizes = [16, 24, 32, 48, 64, 256]
226
+ const pngs: Buffer[] = []
227
+ for (const s of sizes) {
228
+ const frame = image.clone()
229
+ frame.resize({ w: s, h: s })
230
+ pngs.push(Buffer.from(await frame.getBuffer("image/png")))
231
+ }
232
+ const header = Buffer.alloc(6 + sizes.length * 16)
233
+ header.writeUInt16LE(0, 0) // reserved
234
+ header.writeUInt16LE(1, 2) // type: icon
235
+ header.writeUInt16LE(sizes.length, 4)
236
+ let offset = header.length
237
+ sizes.forEach((s, i) => {
238
+ const e = 6 + i * 16
239
+ header.writeUInt8(s === 256 ? 0 : s, e) // width (0 encodes 256)
240
+ header.writeUInt8(s === 256 ? 0 : s, e + 1) // height
241
+ header.writeUInt16LE(1, e + 4) // color planes
242
+ header.writeUInt16LE(32, e + 6) // bits per pixel
243
+ header.writeUInt32LE(pngs[i].length, e + 8)
244
+ header.writeUInt32LE(offset, e + 12)
245
+ offset += pngs[i].length
246
+ })
247
+ writeFileSync(dest, Buffer.concat([header, ...pngs]))
248
+ }
249
+
250
+ /** `--probe`: run the STAGED folder hidden and dump a PNG next to it — verifies exactly what
251
+ * ships (the staged host, its assets, the bundle), not the dev environment. */
252
+ const probeBuild = async (out: string, exeName: string, config: AppConfig, args: Args) => {
253
+ const win = config.desktop?.window
254
+ const frames = Number(flagStr(args, "frames")) || 90
255
+ const shot = join(dirname(out), `${basename(out)}-probe.png`)
256
+ note(`Probe: hidden render of the staged folder (${frames} frames)…`)
257
+ await new Promise<void>((done, fail) => {
258
+ const child = spawn(join(out, exeName), [], {
259
+ cwd: out,
260
+ env: {
261
+ ...process.env,
262
+ CREATOR_HIDDEN: "1",
263
+ CREATOR_MAX_FRAMES: String(frames),
264
+ CREATOR_DUMP_PNG: shot,
265
+ // Explicit size keeps the probe meaningful on pre-1.3.0 hosts too (no sibling-config read).
266
+ CREATOR_WINDOW_SIZE: `${win?.width ?? 1280}x${win?.height ?? 720}`,
267
+ },
268
+ stdio: ["ignore", "pipe", "pipe"],
269
+ })
270
+ const tail: string[] = []
271
+ const sink = (d: unknown) => { tail.push(String(d)); while (tail.length > 60) tail.shift() }
272
+ child.stdout.on("data", sink)
273
+ child.stderr.on("data", sink)
274
+ const killer = setTimeout(() => {
275
+ child.kill()
276
+ fail(new CliError("The probe timed out after 120s — raise --frames headroom or check the app."))
277
+ }, 120_000)
278
+ child.on("error", (e) => { clearTimeout(killer); fail(new CliError(`Probe couldn't run: ${e.message}`)) })
279
+ child.on("exit", (code) => {
280
+ clearTimeout(killer)
281
+ if (code === 0 && existsSync(shot)) { success(`Probe: ${shot}`); done() }
282
+ else {
283
+ const detail = tail.length > 0 ? `\n${tail.join("").trimEnd()}` : ""
284
+ fail(new CliError(`Probe failed (exit ${code}).${detail}`))
285
+ }
286
+ })
287
+ })
288
+ }
289
+
290
+ /** `--zip`: <out>.zip with UTF-8 names + forward-slash entries. `Compress-Archive` writes
291
+ * backslash entry paths and `tar -a` OEM-encodes non-ASCII names — both corrupt on
292
+ * macOS/Linux unpackers, so on Windows the entries are written by hand. */
293
+ const zipBuild = (out: string) => {
294
+ const zip = `${out}.zip`
295
+ rmSync(zip, { force: true })
296
+ note("Zipping…")
297
+ if (process.platform === "win32") {
298
+ const q = (s: string) => s.replaceAll("'", "''")
299
+ execFileSync("powershell", ["-NoProfile", "-Command", [
300
+ "Add-Type -AssemblyName System.IO.Compression.FileSystem;",
301
+ `$src='${q(out)}'; $base=Split-Path $src -Parent;`,
302
+ `$zip=[System.IO.Compression.ZipFile]::Open('${q(zip)}','Create');`,
303
+ "Get-ChildItem $src -Recurse -File | ForEach-Object {",
304
+ " $rel=$_.FullName.Substring($base.Length+1).Replace('\\','/');",
305
+ " [void][System.IO.Compression.ZipFileExtensions]::CreateEntryFromFile($zip,$_.FullName,$rel,'Optimal') };",
306
+ "$zip.Dispose()",
307
+ ].join(" ")], { stdio: ["ignore", "ignore", "pipe"] })
308
+ } else {
309
+ execFileSync("zip", ["-rq", zip, basename(out)], { cwd: dirname(out) })
310
+ }
311
+ success(`Zip: ${zip} (${mb(statSync(zip).size)})`)
312
+ }
313
+
314
+ const readmeText = (name: string, exeName: string, version: string) => `${name}
315
+ ${"=".repeat(Math.max(name.length, 4))}
316
+
317
+ A LeCodes desktop app. Nothing to install: unpack the WHOLE folder anywhere and run ${exeName}.
318
+ Keep the files together: assets/ holds the app's own content, runtime/ the engine it needs.
319
+
320
+ On first run Windows may show a SmartScreen prompt (any unsigned app gets one):
321
+ choose "More info", then "Run anyway".
322
+
323
+ Requires 64-bit Windows 10/11.
324
+
325
+ Built with LeCodes Desktop ${version}.
326
+ `