@gtkx/config 2.0.0-beta.12 → 2.0.0-beta.13

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/src/config.ts CHANGED
@@ -3,16 +3,16 @@ import { defu } from "defu";
3
3
  import { resolve } from "node:path";
4
4
  import { z } from "zod";
5
5
  import { configError, isRecord } from "./config-error.ts";
6
- import { deploySchema } from "./deploy.ts";
6
+ import { type DeployOptions, deploySchema } from "./deploy.ts";
7
7
  import { girLibrary, text } from "./schema-text.ts";
8
8
  import { resolveUserEventSignals } from "./user-event-signals.ts";
9
9
 
10
10
  /** Object form of the `reactCompiler` config key, forwarded to `babel-plugin-react-compiler`. */
11
11
  type ReactCompilerOptions = {
12
- /** Which functions the compiler processes. */
13
- compilationMode?: (typeof COMPILATION_MODES)[number];
14
- /** Which compiler diagnostics fail the build. */
15
- panicThreshold?: (typeof PANIC_THRESHOLDS)[number];
12
+ /** Which functions the React Compiler processes. */
13
+ compilationMode?: "all" | "annotation" | "infer" | "syntax" | undefined;
14
+ /** Which React Compiler diagnostics fail the build. */
15
+ panicThreshold?: "all_errors" | "critical_errors" | "none" | undefined;
16
16
  };
17
17
 
18
18
  /**
@@ -27,11 +27,127 @@ type ResolvedReactCompilerOptions = ReactCompilerOptions & {
27
27
  /**
28
28
  * GTKX project configuration authored in `gtkx.config.ts` and validated when loaded.
29
29
  */
30
- type Config = z.infer<typeof configSchema>;
31
- type ModuleExport = z.infer<typeof moduleExportSchema>;
32
- type ElementPropsExport = z.infer<typeof elementPropsSchema>;
30
+ type Config = {
31
+ /**
32
+ * Additional GIR libraries to bind, such as `WebKit-6.0`.
33
+ * Adwaita and GTK are included implicitly and must not be listed.
34
+ */
35
+ libraries?: string[] | undefined;
36
+ /** Additional GIR search directories. Relative paths are resolved from the project root. */
37
+ girPath?: string[] | undefined;
38
+ /**
39
+ * GApplication identifier, such as `com.example.Tasks`. Required to run, build or deploy the project as an
40
+ * application; a package that is not an application, such as a component library, leaves it out.
41
+ */
42
+ applicationId?: string | undefined;
43
+ /** Whether to enable the React Compiler, or options passed to it. Enabled by default. */
44
+ reactCompiler?: boolean | ReactCompilerOptions | undefined;
45
+ /**
46
+ * Whether GTKX generates project bindings. Set to `false` to reuse an installed binding store.
47
+ * Defaults to `true`.
48
+ */
49
+ codegen?: boolean | undefined;
50
+ /**
51
+ * Additional signals to suppress during React property updates, keyed by GLib type name.
52
+ * Entries extend the built-in signal lists.
53
+ */
54
+ userEventSignals?: Record<string, string[]> | undefined;
55
+ /** Custom element behaviors and overrides for generated components, props, and documentation. */
56
+ elements?: ElementsOptions | undefined;
57
+ /** Path to an icon theme directory or a single application icon file, relative to the project root. */
58
+ applicationIcon?: string | undefined;
59
+ /** Application metadata, bundled runtime settings, and packaging options used by `gtkx deploy`. */
60
+ deploy?: DeployOptions | undefined;
61
+ /** Controls the project instructions and reference files generated for coding agents. */
62
+ agents?: AgentsOptions | undefined;
63
+ /** Default tool selection and access mode for the project's MCP server. Command-line flags take precedence. */
64
+ mcp?: McpOptions | undefined;
65
+ /** Controls warnings for deprecated configuration and behavior. */
66
+ deprecations?: DeprecationsOptions | undefined;
67
+ };
68
+ /** A {@link Config} whose `applicationId` is known, as loaded for an application. */
69
+ type ApplicationConfig = Config & { applicationId: string };
70
+
71
+ /** A named export of a module, which `elements.config` entries reference for components and props. */
72
+ type ModuleExport = {
73
+ /** Module specifier containing the named export; relative paths start at the project root. */
74
+ module: string;
75
+ /** Name of the export to import from the module. */
76
+ export: string;
77
+ };
78
+
79
+ /** The props type an `elements.config` entry adds to a generated element, and how it combines with them. */
80
+ type ElementPropsExport = {
81
+ /** Module specifier containing the named export; relative paths start at the project root. */
82
+ module: string;
83
+ /** Name of the props type to import from the module. */
84
+ export: string;
85
+ /**
86
+ * How the exported props are combined: `intersection` is inherited; `factory` applies only to this type.
87
+ * Defaults to `intersection`.
88
+ */
89
+ composition?: "factory" | "intersection" | undefined;
90
+ /** Props that can only be set when the element is created; changing them afterward throws. */
91
+ constructOnly?: string[] | undefined;
92
+ };
93
+
33
94
  /** Overrides for one GLib type in `elements.config`, keyed by its registered type name. */
34
- type ElementConfigOptions = z.infer<typeof elementConfigSchema>;
95
+ type ElementConfigOptions = {
96
+ /** Component export that wraps the generated element. */
97
+ component?: ModuleExport | undefined;
98
+ /** Additional props type to combine with the generated element props. */
99
+ props?: ElementPropsExport | undefined;
100
+ /** Whether the parent container creates this element's GObject instead of the element constructing its own. */
101
+ isLazy?: boolean | undefined;
102
+ /** Accepted child GLib type names to show in the generated element documentation. */
103
+ acceptedChildTypes?: string[] | undefined;
104
+ /** GObject properties to leave out of generated props, such as properties managed by an element behavior. */
105
+ omittedProps?: string[] | undefined;
106
+ };
107
+
108
+ /** The `elements` options: custom element behaviors and per-type overrides for generated elements. */
109
+ type ElementsOptions = {
110
+ /** Path, relative to the project root, of the module exporting the element behavior map. */
111
+ behaviors?: string | undefined;
112
+ /** Element configuration overrides keyed by GLib type name, such as `GtkWidget`. */
113
+ config?: Record<string, ElementConfigOptions> | undefined;
114
+ };
115
+
116
+ /** The `agents` options: which files codegen maintains for coding agents. */
117
+ type AgentsOptions = {
118
+ /**
119
+ * Whether codegen maintains the GTKX rules in `AGENTS.md` and creates a missing `CLAUDE.md` import.
120
+ * `false` removes rules written earlier, deleting `AGENTS.md` and the `CLAUDE.md` import when nothing else
121
+ * remains. Defaults to `true` for an application and `false` for a package without an `applicationId`.
122
+ */
123
+ rules?: boolean | undefined;
124
+ /**
125
+ * Whether codegen writes the generated element reference to `.gtkx/reference` and points the rules at it.
126
+ * `false` removes a reference generated earlier. Defaults to `true` for an application and `false` for a
127
+ * package without an `applicationId`.
128
+ */
129
+ reference?: boolean | undefined;
130
+ };
131
+
132
+ /** The `mcp` options: the tools `gtkx mcp` offers by default and whether it may interact with the app. */
133
+ type McpOptions = {
134
+ /**
135
+ * Tool name patterns applied in order: `*` matches any text and a leading `!` excludes matches.
136
+ * Omitted, empty, or exclusion-only lists start with all tools; other lists start with none.
137
+ */
138
+ tools?: string[] | undefined;
139
+ /**
140
+ * Whether the MCP server exposes only inspection and reference tools, excluding app interactions.
141
+ * Defaults to `false`.
142
+ */
143
+ readOnly?: boolean | undefined;
144
+ };
145
+
146
+ /** The `deprecations` options: which deprecation warnings stay silent. */
147
+ type DeprecationsOptions = {
148
+ /** Deprecation identifiers whose warnings are suppressed. There are currently no accepted identifiers. */
149
+ silence?: never[] | undefined;
150
+ };
35
151
 
36
152
  type McpSettings = {
37
153
  tools: string[];
@@ -42,6 +158,8 @@ type McpSettings = {
42
158
  type ResolvedConfig = {
43
159
  /** The GApplication identifier the app registers under. */
44
160
  applicationId: string;
161
+ /** The gettext domain, which is the `applicationId` production builds use. */
162
+ translationDomain: string;
45
163
  /** React Compiler options for the build, or `null` when the compiler is disabled. */
46
164
  reactCompiler: ResolvedReactCompilerOptions | null;
47
165
  /**
@@ -57,6 +175,7 @@ type ResolvedConfig = {
57
175
 
58
176
  const APPLICATION_ID_PATTERN = /^[A-Za-z_][A-Za-z0-9_-]*(\.[A-Za-z_][A-Za-z0-9_-]*)+$/;
59
177
  const APPLICATION_ID_MAX_LENGTH = 255;
178
+ const LIBRARY_APPLICATION_ID = "org.gtkx.Library";
60
179
  const IMPLICIT_LIBRARIES: Set<string> = new Set(["Adw-1", "Gtk-4.0"]);
61
180
  /** Compilation modes `babel-plugin-react-compiler` accepts. */
62
181
  const COMPILATION_MODES = ["infer", "syntax", "annotation", "all"] as const;
@@ -74,20 +193,22 @@ const librariesSchema = z
74
193
  .min(1, { error: "must be a non-empty string array or omitted" });
75
194
 
76
195
  const applicationIdSchema = z
77
- .string({ error: "must satisfy g_application_id_is_valid" })
196
+ .string({
197
+ error: (issue) =>
198
+ issue.input === undefined
199
+ ? 'is required to run, build or deploy an application, such as "org.example.MyApp"'
200
+ : "must satisfy g_application_id_is_valid",
201
+ })
78
202
  .refine((value) => isValidApplicationId(value), {
79
203
  error: 'must satisfy g_application_id_is_valid, such as "org.example.MyApp"',
80
204
  });
81
205
 
82
- const reactCompilerSchema = z.union([
83
- z.boolean(),
84
- z.strictObject({
85
- /** Which functions the React Compiler processes. */
86
- compilationMode: z.enum(COMPILATION_MODES).optional(),
87
- /** Which React Compiler diagnostics fail the build. */
88
- panicThreshold: z.enum(PANIC_THRESHOLDS).optional(),
89
- }),
90
- ]);
206
+ const reactCompilerOptionsSchema = z.strictObject({
207
+ compilationMode: z.enum(COMPILATION_MODES).optional(),
208
+ panicThreshold: z.enum(PANIC_THRESHOLDS).optional(),
209
+ }) satisfies z.ZodType<ReactCompilerOptions>;
210
+
211
+ const reactCompilerSchema = z.union([z.boolean(), reactCompilerOptionsSchema]);
91
212
 
92
213
  const userEventSignalsSchema = z.record(
93
214
  z.string(),
@@ -102,35 +223,22 @@ const userEventSignalsSchema = z.record(
102
223
 
103
224
  const moduleExportSchema = z.strictObject(
104
225
  {
105
- /** Module specifier containing the named export. */
106
226
  module: z.string({ error: "must be a module specifier" }).min(1, { error: "must be a module specifier" }),
107
- /** Name of the export to import from the module. */
108
227
  export: z.string({ error: "must be an export name" }).min(1, { error: "must be an export name" }),
109
228
  },
110
229
  { error: "must be a { module, export } object" },
111
- );
230
+ ) satisfies z.ZodType<ModuleExport>;
112
231
 
113
232
  const elementPropsSchema = moduleExportSchema.extend({
114
- /**
115
- * How the exported props are combined: `intersection` is inherited; `factory` applies only to this type.
116
- * Defaults to `intersection`.
117
- */
118
233
  composition: z.enum(["factory", "intersection"]).optional(),
119
- /** Props that can only be set when the element is created; changing them afterward throws. */
120
234
  constructOnly: z.array(z.string()).optional(),
121
- });
235
+ }) satisfies z.ZodType<ElementPropsExport>;
122
236
 
123
- /** Schema for one element override and the source of {@link ElementConfigOptions}. */
124
237
  const elementConfigSchema = z.strictObject({
125
- /** Component export that wraps the generated element. */
126
238
  component: moduleExportSchema.optional(),
127
- /** Additional props type to combine with the generated element props. */
128
239
  props: elementPropsSchema.optional(),
129
- /** Whether the parent container creates this element's GObject instead of the element constructing its own. */
130
240
  isLazy: z.boolean({ error: "must be a boolean" }).optional(),
131
- /** Accepted child GLib type names to show in the generated element documentation. */
132
241
  acceptedChildTypes: z.array(z.string()).optional(),
133
- /** GObject properties to leave out of generated props, such as properties managed by an element behavior. */
134
242
  omittedProps: z
135
243
  .array(
136
244
  z.string({ error: "must be a non-empty property name" }).min(1, {
@@ -139,47 +247,29 @@ const elementConfigSchema = z.strictObject({
139
247
  { error: "must be an array of property names" },
140
248
  )
141
249
  .optional(),
142
- });
250
+ }) satisfies z.ZodType<ElementConfigOptions>;
143
251
 
144
252
  const elementsSchema = z.strictObject({
145
- /** Path, relative to the project root, of the module exporting the element behavior map. */
146
253
  behaviors: z
147
254
  .string({ error: "must be a path to a module exporting element behaviors" })
148
255
  .min(1, { error: "must be a path to a module exporting element behaviors" })
149
256
  .optional(),
150
- /**
151
- * Element configuration overrides keyed by GLib type name, such as `GtkWidget`.
152
- * @see {@link ElementConfigOptions} for the fields accepted by each entry.
153
- */
154
257
  config: z.record(z.string(), elementConfigSchema).optional(),
155
- });
258
+ }) satisfies z.ZodType<ElementsOptions>;
156
259
 
157
260
  const agentsSchema = z.strictObject({
158
- /**
159
- * Whether codegen maintains the GTKX rules in `AGENTS.md` and creates a missing `CLAUDE.md` import.
160
- * Defaults to `true`.
161
- */
162
261
  rules: z.boolean({ error: "must be a boolean" }).optional(),
163
- /** Whether codegen writes the generated element reference to `.gtkx/reference`. Defaults to `true`. */
164
262
  reference: z.boolean({ error: "must be a boolean" }).optional(),
165
- });
263
+ }) satisfies z.ZodType<AgentsOptions>;
166
264
 
167
265
  const mcpSchema = z.strictObject({
168
- /**
169
- * Tool name patterns applied in order: `*` matches any text and a leading `!` excludes matches.
170
- * Omitted, empty, or exclusion-only lists start with all tools; other lists start with none.
171
- */
172
266
  tools: z
173
267
  .array(z.string({ error: "must be a tool name pattern" }).min(1, { error: "must be a tool name pattern" }), {
174
268
  error: "must be an array of tool name patterns",
175
269
  })
176
270
  .optional(),
177
- /**
178
- * Whether the MCP server exposes only inspection and reference tools, excluding app interactions.
179
- * Defaults to `false`.
180
- */
181
271
  readOnly: z.boolean({ error: "must be a boolean" }).optional(),
182
- });
272
+ }) satisfies z.ZodType<McpOptions>;
183
273
 
184
274
  const graduatedFutureSchema = z
185
275
  .object({
@@ -194,57 +284,38 @@ const graduatedFutureSchema = z
194
284
  .strict();
195
285
 
196
286
  const deprecationsSchema = z.strictObject({
197
- /** Deprecation identifiers whose warnings are suppressed. There are currently no accepted identifiers. */
198
287
  silence: z
199
288
  .array(z.never({ error: "does not name a current deprecation" }), {
200
289
  error: "must be an array of current deprecation ids",
201
290
  })
202
291
  .optional(),
203
- });
292
+ }) satisfies z.ZodType<DeprecationsOptions>;
204
293
 
205
- /** Schema every `gtkx.config.ts` is validated against, and the source of the {@link Config} type. */
294
+ /** Schema every `gtkx.config.ts` is validated against. */
206
295
  const configSchema = z.strictObject({
207
- /**
208
- * Additional GIR libraries to bind, such as `WebKit-6.0`.
209
- * Adwaita and GTK are included implicitly and must not be listed.
210
- */
211
296
  libraries: librariesSchema.optional(),
212
- /** Additional GIR search directories. Relative paths are resolved from the project root. */
213
297
  girPath: z.array(z.string(), { error: "must be an array of strings if provided" }).optional(),
214
- /** Required GApplication identifier, such as `com.example.Tasks`. */
215
- applicationId: applicationIdSchema,
216
- /** Whether to enable the React Compiler, or options passed to it. Enabled by default. */
298
+ applicationId: applicationIdSchema.optional(),
217
299
  reactCompiler: reactCompilerSchema.optional(),
218
- /**
219
- * Whether GTKX generates project bindings. Set to `false` to reuse an installed binding store.
220
- * Defaults to `true`.
221
- */
222
300
  codegen: z.boolean({ error: "must be a boolean" }).optional(),
223
- /**
224
- * Additional signals to suppress during React property updates, keyed by GLib type name.
225
- * Entries extend the built-in signal lists.
226
- */
227
301
  userEventSignals: userEventSignalsSchema.optional(),
228
- /** Custom element behaviors and overrides for generated components, props, and documentation. */
229
302
  elements: elementsSchema.optional(),
230
- /** Path to an icon theme directory or a single application icon file, relative to the project root. */
231
303
  applicationIcon: text("must be a path to an icon theme directory or a single icon file").optional(),
232
- /** Application metadata, bundled runtime settings, and packaging options used by `gtkx deploy`. */
233
304
  deploy: deploySchema.optional(),
234
- /** Controls the project instructions and reference files generated for coding agents. */
235
305
  agents: agentsSchema.optional(),
236
- /** Default tool selection and access mode for the project's MCP server. Command-line flags take precedence. */
237
306
  mcp: mcpSchema.optional(),
238
- /** Controls warnings for deprecated configuration and behavior. */
239
307
  deprecations: deprecationsSchema.optional(),
240
- });
308
+ }) satisfies z.ZodType<Config>;
241
309
 
242
310
  /**
243
311
  * Returns the given configuration unchanged, typed as {@link Config}, so `gtkx.config.ts` gets
244
312
  * autocompletion and type checking.
245
313
  */
246
314
  const defineConfig: DefineConfig<Config> = createDefineConfig<Config>();
247
- const validationSchema = configSchema.extend({ future: graduatedFutureSchema.optional() });
315
+ const validationSchema = configSchema.extend({
316
+ applicationId: applicationIdSchema,
317
+ future: graduatedFutureSchema.optional(),
318
+ });
248
319
 
249
320
  const isValidApplicationId = (applicationId: string): boolean =>
250
321
  applicationId.length <= APPLICATION_ID_MAX_LENGTH && APPLICATION_ID_PATTERN.test(applicationId);
@@ -263,7 +334,10 @@ const resolveReactCompilerOptions = (setting: Config["reactCompiler"]): Resolved
263
334
  };
264
335
  };
265
336
 
266
- const validateConfig = (config: unknown, configFile?: string): void => {
337
+ const validateConfig: (config: unknown, configFile?: string) => asserts config is ApplicationConfig = (
338
+ config,
339
+ configFile,
340
+ ) => {
267
341
  const result = validationSchema.safeParse(config);
268
342
 
269
343
  if (!result.success) {
@@ -310,11 +384,16 @@ const elementEntryValues = <T>(
310
384
  }),
311
385
  );
312
386
 
313
- const resolveElementComponents = (elements: Config["elements"]): Record<string, ModuleExport> =>
314
- elementEntryValues(elements, (entry) => entry.component);
387
+ const resolveModuleSpecifier = <T extends { module: string }>(reference: T, root: string): T =>
388
+ reference.module.startsWith("./") || reference.module.startsWith("../")
389
+ ? { ...reference, module: resolve(root, reference.module) }
390
+ : reference;
391
+
392
+ const resolveElementComponents = (elements: Config["elements"], root: string): Record<string, ModuleExport> =>
393
+ elementEntryValues(elements, (entry) => entry.component && resolveModuleSpecifier(entry.component, root));
315
394
 
316
- const resolveElementProps = (elements: Config["elements"]): Record<string, ElementPropsExport> =>
317
- elementEntryValues(elements, (entry) => entry.props);
395
+ const resolveElementProps = (elements: Config["elements"], root: string): Record<string, ElementPropsExport> =>
396
+ elementEntryValues(elements, (entry) => entry.props && resolveModuleSpecifier(entry.props, root));
318
397
 
319
398
  const resolveAcceptedChildTypes = (elements: Config["elements"]): Record<string, string[]> =>
320
399
  elementEntryValues(elements, (entry) => entry.acceptedChildTypes);
@@ -322,16 +401,18 @@ const resolveAcceptedChildTypes = (elements: Config["elements"]): Record<string,
322
401
  const resolveOmittedProps = (elements: Config["elements"]): Record<string, string[]> =>
323
402
  elementEntryValues(elements, (entry) => entry.omittedProps);
324
403
 
325
- const isAgentRulesEnabled = (config: Config): boolean => config.agents?.rules !== false;
326
- const isAgentReferenceEnabled = (config: Config): boolean => config.agents?.reference !== false;
404
+ const isAgentRulesEnabled = (config: Config, isApplication: boolean): boolean => config.agents?.rules ?? isApplication;
405
+ const isAgentReferenceEnabled = (config: Config, isApplication: boolean): boolean =>
406
+ config.agents?.reference ?? isApplication;
327
407
 
328
408
  const resolveMcpSettings = (config: Config): McpSettings => ({
329
409
  tools: config.mcp?.tools ?? [],
330
410
  isReadOnly: config.mcp?.readOnly === true,
331
411
  });
332
412
 
333
- const resolveConfig = (config: Config, root?: string): ResolvedConfig => ({
413
+ const resolveConfig = (config: ApplicationConfig, translationDomain: string, root?: string): ResolvedConfig => ({
334
414
  applicationId: config.applicationId,
415
+ translationDomain,
335
416
  reactCompiler: resolveReactCompilerOptions(config.reactCompiler),
336
417
  userEventSignals: resolveUserEventSignals(config.userEventSignals),
337
418
  elements: resolveElementsModule(config.elements?.behaviors, root),
@@ -339,7 +420,9 @@ const resolveConfig = (config: Config, root?: string): ResolvedConfig => ({
339
420
  });
340
421
 
341
422
  export {
423
+ configSchema,
342
424
  APPLICATION_ID_MAX_LENGTH,
425
+ LIBRARY_APPLICATION_ID,
343
426
  defineConfig,
344
427
  isAgentReferenceEnabled,
345
428
  isAgentRulesEnabled,
@@ -354,8 +437,14 @@ export {
354
437
  resolveMcpSettings,
355
438
  resolveOmittedProps,
356
439
  resolveConfig,
440
+ type AgentsOptions,
441
+ type DeprecationsOptions,
442
+ type ElementsOptions,
443
+ type McpOptions,
357
444
  type McpSettings,
445
+ type ReactCompilerOptions,
358
446
  type ResolvedReactCompilerOptions,
447
+ type ApplicationConfig,
359
448
  type Config,
360
449
  type ElementConfigOptions,
361
450
  type ElementPropsExport,