|
| 1 | +/** @file Shared application options schema and helpers. */ |
| 2 | + |
| 3 | +import { z } from 'zod' |
| 4 | + |
| 5 | +const DEFAULT_PROFILING_TIME = 120 |
| 6 | +const DEFAULT_PORT = 8080 |
| 7 | + |
| 8 | +/** Schema for app-wide configuration options. */ |
| 9 | +export const OptionsSchema = z.object({ |
| 10 | + version: z.boolean().default(false), |
| 11 | + displayWindow: z.boolean().default(true), |
| 12 | + useServer: z.boolean().default(true), |
| 13 | + engineEnabled: z.boolean().default(true), |
| 14 | + useJvm: z.boolean().default(false), |
| 15 | + startup: z |
| 16 | + .object({ |
| 17 | + project: z.string().default(''), |
| 18 | + displayedProjectName: z.string().default(''), |
| 19 | + }) |
| 20 | + .default({}), |
| 21 | + authentication: z |
| 22 | + .object({ |
| 23 | + enabled: z.boolean().default(true), |
| 24 | + email: z.string().default(''), |
| 25 | + }) |
| 26 | + .default({}), |
| 27 | + server: z |
| 28 | + .object({ |
| 29 | + port: z.number().int().default(DEFAULT_PORT), |
| 30 | + }) |
| 31 | + .default({}), |
| 32 | + engine: z |
| 33 | + .object({ |
| 34 | + projectManagerPath: z.string().default(''), |
| 35 | + projectManagerUrl: z.string().default(''), |
| 36 | + ydocUrl: z.string().default(''), |
| 37 | + }) |
| 38 | + .default({}), |
| 39 | + debug: z |
| 40 | + .object({ |
| 41 | + info: z.boolean().default(false), |
| 42 | + verbose: z.boolean().default(false), |
| 43 | + devTools: z.boolean().default(false), |
| 44 | + profile: z.boolean().default(false), |
| 45 | + profileTime: z.number().int().default(DEFAULT_PROFILING_TIME), |
| 46 | + }) |
| 47 | + .default({}), |
| 48 | +}) |
| 49 | + |
| 50 | +/** Global configuration of Enso Studio, parsed from CLI arguments and passed to web application via URL params. */ |
| 51 | +export type Options = z.infer<typeof OptionsSchema> |
| 52 | + |
| 53 | +/** Default values for all configuration options. */ |
| 54 | +export function defaultOptions(): Options { |
| 55 | + return OptionsSchema.parse({}) |
| 56 | +} |
| 57 | + |
| 58 | +/** |
| 59 | + * Dotted keys which should be synced to the web application via URL params. |
| 60 | + * Other options are only used by Electron and not passed to the web application. |
| 61 | + */ |
| 62 | +export const PASS_TO_WEB: ReadonlySet<string> = new Set([ |
| 63 | + 'startup.project', |
| 64 | + 'startup.displayedProjectName', |
| 65 | + 'authentication.enabled', |
| 66 | + 'authentication.email', |
| 67 | + 'engine.projectManagerUrl', |
| 68 | + 'engine.ydocUrl', |
| 69 | +]) |
| 70 | + |
| 71 | +/** Possible values for options. */ |
| 72 | +export type OptionValue = string | number | boolean |
| 73 | + |
| 74 | +const phantomKey = Symbol('flattened') |
| 75 | +type Flat<T> = Record<string, OptionValue> & { readonly [phantomKey]?: T } |
| 76 | + |
| 77 | +/** Flatten a nested object into a dotted-key record. */ |
| 78 | +export function flattenObject<T extends object>(obj: T, prefix = ''): Flat<T> { |
| 79 | + const out: Flat<T> = {} |
| 80 | + if (obj != null) { |
| 81 | + for (const [k, v] of Object.entries(obj)) { |
| 82 | + const key = prefix ? `${prefix}.${k}` : k |
| 83 | + if (v != null && typeof v === 'object' && !Array.isArray(v)) { |
| 84 | + Object.assign(out, flattenObject(v, key)) |
| 85 | + } else { |
| 86 | + out[key] = v |
| 87 | + } |
| 88 | + } |
| 89 | + } |
| 90 | + return out |
| 91 | +} |
| 92 | + |
| 93 | +/** Turn a flat dotted-record into a nested object. */ |
| 94 | +export function unflattenObject<T extends object>(flat: Flat<T>): T { |
| 95 | + const out: any = {} |
| 96 | + for (const [k, v] of Object.entries(flat)) { |
| 97 | + const pathSegments = k.split('.') |
| 98 | + let cur = out |
| 99 | + for (const segment of pathSegments.slice(0, -1)) { |
| 100 | + cur[segment] ??= {} |
| 101 | + cur = cur[segment] |
| 102 | + } |
| 103 | + const last = pathSegments[pathSegments.length - 1]! |
| 104 | + cur[last] = v |
| 105 | + } |
| 106 | + return out as T |
| 107 | +} |
| 108 | + |
| 109 | +/** Build URLSearchParams for non-default pass-to-web options. */ |
| 110 | +export function buildWebAppURLSearchParamsFromArgs(args: Options): URLSearchParams { |
| 111 | + const params = new URLSearchParams() |
| 112 | + const entries = collectWebAppOptionsFromArgs(args) |
| 113 | + Object.entries(entries).forEach(([key, val]) => params.append(key, String(val))) |
| 114 | + return params |
| 115 | +} |
| 116 | + |
| 117 | +/** Collect non-default values of pass-to-web options from parsed args. */ |
| 118 | +function collectWebAppOptionsFromArgs(options: Options): Record<string, string | number | boolean> { |
| 119 | + const result: Record<string, string | number | boolean> = {} |
| 120 | + const defaults = defaultOptions() |
| 121 | + const flatDefaults = flattenObject(defaults) |
| 122 | + const flatOptions = flattenObject(options) |
| 123 | + for (const key of PASS_TO_WEB) { |
| 124 | + const value = flatOptions[key] |
| 125 | + if (value == null) |
| 126 | + throw new Error(`Option ${key} not found, but needs to be passed to web application`) |
| 127 | + const def = flatDefaults[key] |
| 128 | + if (value != null && JSON.stringify(value) !== JSON.stringify(def)) { |
| 129 | + result[key] = value |
| 130 | + } |
| 131 | + } |
| 132 | + return result |
| 133 | +} |
| 134 | + |
| 135 | +/** Parse pass-to-web options from URLSearchParams. For missing values, defaults are used. */ |
| 136 | +export function parseWebAppOptionsFromSearchParams(params: URLSearchParams): Options { |
| 137 | + const out = flattenObject(defaultOptions()) |
| 138 | + for (const key of PASS_TO_WEB) { |
| 139 | + const value = params.get(key) |
| 140 | + if (value == null) continue |
| 141 | + const def = out[key] |
| 142 | + switch (typeof def) { |
| 143 | + case 'boolean': { |
| 144 | + const b = coerceBoolean(value) |
| 145 | + if (b != null) { |
| 146 | + out[key] = b |
| 147 | + } else { |
| 148 | + console.warn(`Invalid boolean value for option ${key}: ${value}, using default ${def}.`) |
| 149 | + } |
| 150 | + break |
| 151 | + } |
| 152 | + case 'number': { |
| 153 | + const n = Number(value) |
| 154 | + if (!Number.isNaN(n)) { |
| 155 | + out[key] = n |
| 156 | + } else { |
| 157 | + console.warn(`Invalid number value for option ${key}: ${value}, using default ${def}.`) |
| 158 | + } |
| 159 | + break |
| 160 | + } |
| 161 | + case 'string': { |
| 162 | + out[key] = value |
| 163 | + break |
| 164 | + } |
| 165 | + default: |
| 166 | + throw new Error(`Invalid option type for option ${key}: ${typeof def}.`) |
| 167 | + } |
| 168 | + } |
| 169 | + return unflattenObject<Options>(out) |
| 170 | +} |
| 171 | + |
| 172 | +/** |
| 173 | + * Coerce a string to a boolean. |
| 174 | + * |
| 175 | + * true, 1, yes, enabled → true |
| 176 | + * false, 0, no, disabled → false |
| 177 | + * anything else → undefined |
| 178 | + */ |
| 179 | +function coerceBoolean(v: string): boolean | undefined { |
| 180 | + switch (v.toLowerCase()) { |
| 181 | + case 'true': |
| 182 | + case '1': |
| 183 | + case 'yes': |
| 184 | + case 'enabled': |
| 185 | + return true |
| 186 | + case 'false': |
| 187 | + case '0': |
| 188 | + case 'no': |
| 189 | + case 'disabled': |
| 190 | + return false |
| 191 | + default: |
| 192 | + return undefined |
| 193 | + } |
| 194 | +} |
0 commit comments