|
| 1 | +/*--------------------------------------------------------------------------------------------- |
| 2 | + * Sky Build Debugger |
| 3 | + * -------------------------------------------------------------------------------------------- |
| 4 | + * This script is designed to run during the build phase of the Sky webview (the client-side |
| 5 | + * of the Astro-based application). |
| 6 | + * |
| 7 | + * Its primary responsibilities are: |
| 8 | + * 1. To inspect, resolve, and normalize all environment variables required for the build. |
| 9 | + * 2. To provide a "Single Source of Truth" for build configuration constants (e.g., Path, |
| 10 | + * Platform flags, Bundling modes). |
| 11 | + * 3. To output an EXTENSIVE, structured, and color-coded report to the build terminal, |
| 12 | + * ensuring developers have full visibility into the build context (Env, Path, |
| 13 | + * Flags) before the main Astro/Vite process takes over. |
| 14 | + * |
| 15 | + * Usage: |
| 16 | + * This file is imported by `astro.config.ts`. The act of importing it triggers the |
| 17 | + * console logging side-effects immediately. |
| 18 | + *--------------------------------------------------------------------------------------------*/ |
| 19 | + |
| 20 | +import type { ViteStaticCopyOptions } from "vite-plugin-static-copy"; |
| 21 | + |
| 22 | +// ----------------------------------------------------------------------------- |
| 23 | +// 1. Core Utilities & Exports |
| 24 | +// ----------------------------------------------------------------------------- |
| 25 | +export const { readFile } = await import("node:fs/promises"); |
| 26 | + |
| 27 | +// ----------------------------------------------------------------------------- |
| 28 | +// 2. Environment Variable Resolution |
| 29 | +// ----------------------------------------------------------------------------- |
| 30 | + |
| 31 | +export const Bundle = process.env["Bundle"] === "true"; |
| 32 | + |
| 33 | +export const Browser = process.env["Browser"] === "true"; |
| 34 | + |
| 35 | +export const Dependency = process.env["Dependency"] ?? "CodeEditorLand/Editor"; |
| 36 | + |
| 37 | +export const Tauri = typeof process.env["TAURI_ENV_ARCH"] !== "undefined"; |
| 38 | + |
| 39 | +export const Platform = ((Platform) => { |
| 40 | + switch (Platform?.toLowerCase()) { |
| 41 | + case "windows": |
| 42 | + return "Windows"; |
| 43 | + |
| 44 | + case "darwin": |
| 45 | + return "Mac"; |
| 46 | + |
| 47 | + case "linux": |
| 48 | + return "Linux"; |
| 49 | + |
| 50 | + case "android": |
| 51 | + return "Android"; |
| 52 | + |
| 53 | + case "ios": |
| 54 | + return "iOS"; |
| 55 | + |
| 56 | + default: |
| 57 | + return "Windows"; |
| 58 | + } |
| 59 | +})(process.env["TAURI_ENV_PLATFORM"]); |
| 60 | + |
| 61 | +/** |
| 62 | + * "On" represents the active development/debugging state. |
| 63 | + * True if NODE_ENV is 'development' OR TAURI_ENV_DEBUG is set. |
| 64 | + */ |
| 65 | +export const On = |
| 66 | + process.env["NODE_ENV"] === "development" || |
| 67 | + process.env["TAURI_ENV_DEBUG"] === "true"; |
| 68 | + |
| 69 | +// ----------------------------------------------------------------------------- |
| 70 | +// 3. Constants & Path |
| 71 | +// ----------------------------------------------------------------------------- |
| 72 | + |
| 73 | +export const Link = [ |
| 74 | + "@codeeditorland/common", |
| 75 | + |
| 76 | + "@codeeditorland/output", |
| 77 | + |
| 78 | + // "@codeeditorland/wind", |
| 79 | + |
| 80 | + "@codeeditorland/worker", |
| 81 | +]; |
| 82 | + |
| 83 | +export const External = ["@microsoft/1ds-core-js", "@microsoft/1ds-post-js"]; |
| 84 | + |
| 85 | +export const Host = process.env["TAURI_DEV_HOST"] |
| 86 | + ? `https://${process.env["TAURI_DEV_HOST"]}` |
| 87 | + : On |
| 88 | + ? "http://localhost" |
| 89 | + : Tauri |
| 90 | + ? "https://tauri.localhost" |
| 91 | + : "https://editor.land"; |
| 92 | + |
| 93 | +export const ApplicationStatic = "Static/Application"; |
| 94 | + |
| 95 | +export const VSCodeOutput = |
| 96 | + "node_modules/@codeeditorland/output/Target/Microsoft/VSCode"; |
| 97 | + |
| 98 | +export const KeyboardLayouts = |
| 99 | + "vs/workbench/services/keybinding/browser/keyboardLayouts"; |
| 100 | + |
| 101 | +// ----------------------------------------------------------------------------- |
| 102 | +// 4. Static Asset Logic |
| 103 | +// ----------------------------------------------------------------------------- |
| 104 | + |
| 105 | +export const Static: ViteStaticCopyOptions = { |
| 106 | + targets: [], |
| 107 | + |
| 108 | + structured: false, |
| 109 | +}; |
| 110 | + |
| 111 | +// Logic to populate Static.targets based on the environment |
| 112 | +if (Bundle) { |
| 113 | + switch (Platform) { |
| 114 | + case "Windows": |
| 115 | + Static.targets.push({ |
| 116 | + src: `${VSCodeOutput}/${KeyboardLayouts}/*.win.js`, |
| 117 | + |
| 118 | + dest: `${ApplicationStatic}/${KeyboardLayouts}/`, |
| 119 | + }); |
| 120 | + |
| 121 | + break; |
| 122 | + |
| 123 | + case "Mac": |
| 124 | + Static.targets.push({ |
| 125 | + src: `${VSCodeOutput}/${KeyboardLayouts}/*.darwin.js`, |
| 126 | + |
| 127 | + dest: `${ApplicationStatic}/${KeyboardLayouts}/`, |
| 128 | + }); |
| 129 | + |
| 130 | + break; |
| 131 | + |
| 132 | + case "Linux": |
| 133 | + Static.targets.push({ |
| 134 | + src: `${VSCodeOutput}/${KeyboardLayouts}/*.linux.js`, |
| 135 | + |
| 136 | + dest: `${ApplicationStatic}/${KeyboardLayouts}/`, |
| 137 | + }); |
| 138 | + |
| 139 | + break; |
| 140 | + |
| 141 | + default: |
| 142 | + break; |
| 143 | + } |
| 144 | + |
| 145 | + Static.targets.push( |
| 146 | + ...[ |
| 147 | + { |
| 148 | + src: `node_modules/@codeeditorland/output/Target/${Dependency}/${On ? "vs/" : ""}${On ? "nls.js" : "nls.messages.js"}`, |
| 149 | + |
| 150 | + dest: `${ApplicationStatic}/${On ? "vs/" : ""}`, |
| 151 | + }, |
| 152 | + |
| 153 | + { |
| 154 | + src: `${VSCodeOutput}/${KeyboardLayouts}/_.contribution.js`, |
| 155 | + |
| 156 | + dest: `${ApplicationStatic}/${KeyboardLayouts}/`, |
| 157 | + }, |
| 158 | + |
| 159 | + { |
| 160 | + src: "node_modules/@codeeditorland/worker/Target/Worker.js", |
| 161 | + |
| 162 | + dest: ".", |
| 163 | + }, |
| 164 | + ], |
| 165 | + ); |
| 166 | +} else { |
| 167 | + Static.targets.push( |
| 168 | + ...[ |
| 169 | + { |
| 170 | + src: `${VSCodeOutput}/*`, |
| 171 | + |
| 172 | + dest: ApplicationStatic, |
| 173 | + }, |
| 174 | + |
| 175 | + { |
| 176 | + src: "node_modules/@codeeditorland/worker/Target/*", |
| 177 | + |
| 178 | + dest: ".", |
| 179 | + }, |
| 180 | + ], |
| 181 | + ); |
| 182 | +} |
| 183 | + |
| 184 | +Browser |
| 185 | + ? External.push( |
| 186 | + ...[ |
| 187 | + "@codeeditorland/output/vs/code/electron-browser/workbench/workbench.js", |
| 188 | + ], |
| 189 | + ) |
| 190 | + : {}; |
| 191 | + |
| 192 | +// ----------------------------------------------------------------------------- |
| 193 | +// 5. EXTENSIVE DEBUG LOGGING |
| 194 | +// ----------------------------------------------------------------------------- |
| 195 | +// This self-executing block runs immediately on import to output the build context. |
| 196 | +(() => { |
| 197 | + const Color = { |
| 198 | + Reset: "\x1b[0m", |
| 199 | + Bright: "\x1b[1m", |
| 200 | + Dim: "\x1b[2m", |
| 201 | + Cyan: "\x1b[36m", |
| 202 | + Green: "\x1b[32m", |
| 203 | + Yellow: "\x1b[33m", |
| 204 | + Red: "\x1b[31m", |
| 205 | + Magenta: "\x1b[35m", |
| 206 | + Blue: "\x1b[34m", |
| 207 | + }; |
| 208 | + |
| 209 | + const Header = (Text: string) => |
| 210 | + `\n${Color.Bright}${Color.Cyan}ββββ ${Text} ββββ${Color.Reset}`; |
| 211 | + |
| 212 | + const Output = (Key: string, Value: any) => |
| 213 | + ` ${Color.Cyan}${Key.padEnd(18)}${Color.Reset}: ${Value}`; |
| 214 | + |
| 215 | + console.log( |
| 216 | + `\n${Color.Bright}${Color.Magenta}ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ${Color.Reset}`, |
| 217 | + ); |
| 218 | + |
| 219 | + console.log( |
| 220 | + `${Color.Bright}${Color.Magenta}β SKY BUILD CONTEXT DEBUGGER β${Color.Reset}`, |
| 221 | + ); |
| 222 | + |
| 223 | + console.log( |
| 224 | + `${Color.Bright}${Color.Magenta}ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ${Color.Reset}`, |
| 225 | + ); |
| 226 | + |
| 227 | + console.log(Header("Resolved Flags")); |
| 228 | + |
| 229 | + console.log( |
| 230 | + Output("Bundle", Bundle ? Color.Green + "TRUE" : Color.Dim + "false"), |
| 231 | + ); |
| 232 | + |
| 233 | + console.log( |
| 234 | + Output("Browser", Browser ? Color.Green + "TRUE" : Color.Dim + "false"), |
| 235 | + ); |
| 236 | + |
| 237 | + console.log( |
| 238 | + Output("Tauri", Tauri ? Color.Green + "TRUE" : Color.Dim + "false"), |
| 239 | + ); |
| 240 | + |
| 241 | + console.log( |
| 242 | + Output( |
| 243 | + "Debug Mode (On)", |
| 244 | + On ? Color.Red + "ACTIVE" : Color.Green + "Inactive (Production)", |
| 245 | + ), |
| 246 | + ); |
| 247 | + |
| 248 | + console.log(Output("Platform", `${Color.Yellow}${Platform}${Color.Reset}`)); |
| 249 | + |
| 250 | + console.log(Output("Host", `${Color.Blue}${Host}${Color.Reset}`)); |
| 251 | + |
| 252 | + console.log(Output("Dependency", Dependency)); |
| 253 | + |
| 254 | + console.log(Header("Process Environment (Raw)")); |
| 255 | + |
| 256 | + console.log( |
| 257 | + Output("NODE_ENV", process.env["NODE_ENV"] || Color.Dim + "undefined"), |
| 258 | + ); |
| 259 | + |
| 260 | + console.log( |
| 261 | + Output( |
| 262 | + "TAURI_ENV_ARCH", |
| 263 | + process.env["TAURI_ENV_ARCH"] || Color.Dim + "undefined", |
| 264 | + ), |
| 265 | + ); |
| 266 | + |
| 267 | + console.log( |
| 268 | + Output( |
| 269 | + "TAURI_ENV_PLATFORM", |
| 270 | + process.env["TAURI_ENV_PLATFORM"] || Color.Dim + "undefined", |
| 271 | + ), |
| 272 | + ); |
| 273 | + |
| 274 | + console.log( |
| 275 | + Output( |
| 276 | + "TAURI_ENV_DEBUG", |
| 277 | + process.env["TAURI_ENV_DEBUG"] || Color.Dim + "undefined", |
| 278 | + ), |
| 279 | + ); |
| 280 | + |
| 281 | + console.log( |
| 282 | + Output( |
| 283 | + "TAURI_DEV_HOST", |
| 284 | + process.env["TAURI_DEV_HOST"] || Color.Dim + "undefined", |
| 285 | + ), |
| 286 | + ); |
| 287 | + |
| 288 | + console.log(Header("Build Path")); |
| 289 | + |
| 290 | + console.log(Output("ApplicationStatic", ApplicationStatic)); |
| 291 | + |
| 292 | + console.log(Output("VSCodeOutput", VSCodeOutput)); |
| 293 | + |
| 294 | + console.log(Output("KeyboardLayouts", KeyboardLayouts)); |
| 295 | + |
| 296 | + console.log(Header("Module Configuration")); |
| 297 | + |
| 298 | + console.log( |
| 299 | + ` ${Color.Cyan}External Modules${Color.Reset} : [ ${External.map((e) => Color.Yellow + e + Color.Reset).join(", ")} ]`, |
| 300 | + ); |
| 301 | + |
| 302 | + console.log( |
| 303 | + ` ${Color.Cyan}Linked Packages${Color.Reset} : [ ${Link.map((l) => Color.Yellow + l + Color.Reset).join(", ")} ]`, |
| 304 | + ); |
| 305 | + |
| 306 | + console.log(Header("Static Asset Copy Rules")); |
| 307 | + |
| 308 | + if (Static.targets.length === 0) { |
| 309 | + console.log(` ${Color.Dim}(No static targets defined)${Color.Reset}`); |
| 310 | + } else { |
| 311 | + Static.targets.forEach((Target, i) => { |
| 312 | + // @ts-ignore |
| 313 | + const Source = Target.src; |
| 314 | + |
| 315 | + // @ts-ignore |
| 316 | + const Destination = Target.dest; |
| 317 | + |
| 318 | + console.log( |
| 319 | + ` ${Color.Dim}[${i + 1}]${Color.Reset} ${Color.Green}${Source}${Color.Reset}`, |
| 320 | + ); |
| 321 | + |
| 322 | + console.log(` ${Color.Dim}β${Color.Reset} ${Destination}`); |
| 323 | + }); |
| 324 | + } |
| 325 | + |
| 326 | + console.log( |
| 327 | + `\n${Color.Bright}${Color.Magenta}ββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ${Color.Reset}\n`, |
| 328 | + ); |
| 329 | +})(); |
0 commit comments