@wp-typia/project-tools 0.22.8 → 0.22.10

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 (50) hide show
  1. package/dist/runtime/ai-artifacts.js +3 -4
  2. package/dist/runtime/ai-feature-artifacts.js +2 -4
  3. package/dist/runtime/cli-add-block.js +12 -1
  4. package/dist/runtime/cli-add-filesystem.js +2 -15
  5. package/dist/runtime/cli-add-types.d.ts +8 -0
  6. package/dist/runtime/cli-add-types.js +13 -0
  7. package/dist/runtime/cli-add.d.ts +3 -2
  8. package/dist/runtime/cli-add.js +2 -2
  9. package/dist/runtime/cli-core.d.ts +4 -2
  10. package/dist/runtime/cli-core.js +3 -2
  11. package/dist/runtime/cli-doctor-workspace-shared.js +3 -0
  12. package/dist/runtime/cli-doctor-workspace.d.ts +1 -1
  13. package/dist/runtime/cli-doctor-workspace.js +8 -3
  14. package/dist/runtime/cli-doctor.js +1 -1
  15. package/dist/runtime/cli-scaffold.js +3 -3
  16. package/dist/runtime/create-template-validation.js +2 -28
  17. package/dist/runtime/fs-async.d.ts +7 -0
  18. package/dist/runtime/fs-async.js +11 -2
  19. package/dist/runtime/id-suggestions.d.ts +21 -0
  20. package/dist/runtime/id-suggestions.js +48 -0
  21. package/dist/runtime/index.d.ts +5 -2
  22. package/dist/runtime/index.js +3 -1
  23. package/dist/runtime/migration-maintenance-verify.js +5 -0
  24. package/dist/runtime/migration-render-generated.js +4 -0
  25. package/dist/runtime/package-versions.js +3 -7
  26. package/dist/runtime/scaffold-repository-reference.js +3 -7
  27. package/dist/runtime/scaffold.js +3 -3
  28. package/dist/runtime/template-builtins.js +11 -8
  29. package/dist/runtime/template-layers.js +2 -2
  30. package/dist/runtime/template-source-external.d.ts +3 -0
  31. package/dist/runtime/template-source-external.js +5 -2
  32. package/dist/runtime/template-source-remote.d.ts +6 -0
  33. package/dist/runtime/template-source-remote.js +8 -2
  34. package/dist/runtime/template-source-seeds.d.ts +13 -0
  35. package/dist/runtime/template-source-seeds.js +36 -8
  36. package/dist/runtime/template-source.js +2 -2
  37. package/dist/runtime/typia-llm.js +3 -4
  38. package/dist/runtime/workspace-inventory-mutations.d.ts +24 -0
  39. package/dist/runtime/workspace-inventory-mutations.js +132 -0
  40. package/dist/runtime/workspace-inventory-parser.d.ts +52 -0
  41. package/dist/runtime/workspace-inventory-parser.js +511 -0
  42. package/dist/runtime/workspace-inventory-read.d.ts +44 -0
  43. package/dist/runtime/workspace-inventory-read.js +91 -0
  44. package/dist/runtime/workspace-inventory-templates.d.ts +35 -0
  45. package/dist/runtime/workspace-inventory-templates.js +198 -0
  46. package/dist/runtime/workspace-inventory-types.d.ts +171 -0
  47. package/dist/runtime/workspace-inventory-types.js +1 -0
  48. package/dist/runtime/workspace-inventory.d.ts +5 -238
  49. package/dist/runtime/workspace-inventory.js +4 -901
  50. package/package.json +7 -2
@@ -1,901 +1,4 @@
1
- import { readFileSync } from "node:fs";
2
- import path from "node:path";
3
- import { readFile, writeFile } from "node:fs/promises";
4
- import ts from "typescript";
5
- import { REST_RESOURCE_METHOD_IDS } from "./cli-add-shared.js";
6
- import { escapeRegex } from "./php-utils.js";
7
- import { getPropertyNameText } from "./ts-property-names.js";
8
- function defineInventoryEntryParser(descriptor) {
9
- return descriptor;
10
- }
11
- export const BLOCK_CONFIG_ENTRY_MARKER = "\t// wp-typia add block entries";
12
- export const VARIATION_CONFIG_ENTRY_MARKER = "\t// wp-typia add variation entries";
13
- export const BLOCK_STYLE_CONFIG_ENTRY_MARKER = "\t// wp-typia add style entries";
14
- export const BLOCK_TRANSFORM_CONFIG_ENTRY_MARKER = "\t// wp-typia add transform entries";
15
- export const PATTERN_CONFIG_ENTRY_MARKER = "\t// wp-typia add pattern entries";
16
- export const BINDING_SOURCE_CONFIG_ENTRY_MARKER = "\t// wp-typia add binding-source entries";
17
- export const REST_RESOURCE_CONFIG_ENTRY_MARKER = "\t// wp-typia add rest-resource entries";
18
- export const ABILITY_CONFIG_ENTRY_MARKER = "\t// wp-typia add ability entries";
19
- export const AI_FEATURE_CONFIG_ENTRY_MARKER = "\t// wp-typia add ai-feature entries";
20
- export const ADMIN_VIEW_CONFIG_ENTRY_MARKER = "\t// wp-typia add admin-view entries";
21
- /**
22
- * Marker used to append generated editor-plugin entries into `EDITOR_PLUGINS`.
23
- */
24
- export const EDITOR_PLUGIN_CONFIG_ENTRY_MARKER = "\t// wp-typia add editor-plugin entries";
25
- const VARIATIONS_INTERFACE_SECTION = `
26
-
27
- export interface WorkspaceVariationConfig {
28
- \tblock: string;
29
- \tfile: string;
30
- \tslug: string;
31
- }
32
- `;
33
- const VARIATIONS_CONST_SECTION = `
34
-
35
- export const VARIATIONS: WorkspaceVariationConfig[] = [
36
- \t// wp-typia add variation entries
37
- ];
38
- `;
39
- const BLOCK_STYLES_INTERFACE_SECTION = `
40
-
41
- export interface WorkspaceBlockStyleConfig {
42
- \tblock: string;
43
- \tfile: string;
44
- \tslug: string;
45
- }
46
- `;
47
- const BLOCK_STYLES_CONST_SECTION = `
48
-
49
- export const BLOCK_STYLES: WorkspaceBlockStyleConfig[] = [
50
- \t// wp-typia add style entries
51
- ];
52
- `;
53
- const BLOCK_TRANSFORMS_INTERFACE_SECTION = `
54
-
55
- export interface WorkspaceBlockTransformConfig {
56
- \tblock: string;
57
- \tfile: string;
58
- \tfrom: string;
59
- \tslug: string;
60
- \tto: string;
61
- }
62
- `;
63
- const BLOCK_TRANSFORMS_CONST_SECTION = `
64
-
65
- export const BLOCK_TRANSFORMS: WorkspaceBlockTransformConfig[] = [
66
- \t// wp-typia add transform entries
67
- ];
68
- `;
69
- const PATTERNS_INTERFACE_SECTION = `
70
-
71
- export interface WorkspacePatternConfig {
72
- \tfile: string;
73
- \tslug: string;
74
- }
75
- `;
76
- const PATTERNS_CONST_SECTION = `
77
-
78
- export const PATTERNS: WorkspacePatternConfig[] = [
79
- \t// wp-typia add pattern entries
80
- ];
81
- `;
82
- const BINDING_SOURCES_INTERFACE_SECTION = `
83
-
84
- export interface WorkspaceBindingSourceConfig {
85
- \tattribute?: string;
86
- \tblock?: string;
87
- \teditorFile: string;
88
- \tserverFile: string;
89
- \tslug: string;
90
- }
91
- `;
92
- const BINDING_SOURCES_CONST_SECTION = `
93
-
94
- export const BINDING_SOURCES: WorkspaceBindingSourceConfig[] = [
95
- \t// wp-typia add binding-source entries
96
- ];
97
- `;
98
- const REST_RESOURCES_INTERFACE_SECTION = `
99
-
100
- export interface WorkspaceRestResourceConfig {
101
- \tapiFile: string;
102
- \tclientFile: string;
103
- \tdataFile: string;
104
- \tmethods: Array< 'list' | 'read' | 'create' | 'update' | 'delete' >;
105
- \tnamespace: string;
106
- \topenApiFile: string;
107
- \tphpFile: string;
108
- \trestManifest?: ReturnType<
109
- \t\ttypeof import( '@wp-typia/block-runtime/metadata-core' ).defineEndpointManifest
110
- \t>;
111
- \tslug: string;
112
- \ttypesFile: string;
113
- \tvalidatorsFile: string;
114
- }
115
- `;
116
- const REST_RESOURCES_CONST_SECTION = `
117
-
118
- export const REST_RESOURCES: WorkspaceRestResourceConfig[] = [
119
- \t// wp-typia add rest-resource entries
120
- ];
121
- `;
122
- const WORKSPACE_COMPATIBILITY_CONFIG_FIELD = `\tcompatibility?: {
123
- \t\thardMinimums: {
124
- \t\t\tphp?: string;
125
- \t\t\twordpress?: string;
126
- \t\t};
127
- \t\tmode: 'baseline' | 'optional' | 'required';
128
- \t\toptionalFeatureIds: string[];
129
- \t\toptionalFeatures: string[];
130
- \t\trequiredFeatureIds: string[];
131
- \t\trequiredFeatures: string[];
132
- \t\truntimeGates: string[];
133
- \t};
134
- `;
135
- const ABILITIES_INTERFACE_SECTION = `
136
-
137
- export interface WorkspaceAbilityConfig {
138
- \tclientFile: string;
139
- ${WORKSPACE_COMPATIBILITY_CONFIG_FIELD}\tconfigFile: string;
140
- \tdataFile: string;
141
- \tinputSchemaFile: string;
142
- \tinputTypeName: string;
143
- \toutputSchemaFile: string;
144
- \toutputTypeName: string;
145
- \tphpFile: string;
146
- \tslug: string;
147
- \ttypesFile: string;
148
- }
149
- `;
150
- const ABILITIES_CONST_SECTION = `
151
-
152
- export const ABILITIES: WorkspaceAbilityConfig[] = [
153
- \t// wp-typia add ability entries
154
- ];
155
- `;
156
- const AI_FEATURES_INTERFACE_SECTION = `
157
-
158
- export interface WorkspaceAiFeatureConfig {
159
- \taiSchemaFile: string;
160
- \tapiFile: string;
161
- \tclientFile: string;
162
- ${WORKSPACE_COMPATIBILITY_CONFIG_FIELD}\tdataFile: string;
163
- \tnamespace: string;
164
- \topenApiFile: string;
165
- \tphpFile: string;
166
- \trestManifest?: ReturnType<
167
- \t\ttypeof import( '@wp-typia/block-runtime/metadata-core' ).defineEndpointManifest
168
- \t>;
169
- \tslug: string;
170
- \ttypesFile: string;
171
- \tvalidatorsFile: string;
172
- }
173
- `;
174
- const AI_FEATURES_CONST_SECTION = `
175
-
176
- export const AI_FEATURES: WorkspaceAiFeatureConfig[] = [
177
- \t// wp-typia add ai-feature entries
178
- ];
179
- `;
180
- const ADMIN_VIEWS_INTERFACE_SECTION = `
181
-
182
- export interface WorkspaceAdminViewConfig {
183
- \tfile: string;
184
- \tphpFile: string;
185
- \tslug: string;
186
- \tsource?: string;
187
- }
188
- `;
189
- const ADMIN_VIEWS_CONST_SECTION = `
190
-
191
- export const ADMIN_VIEWS: WorkspaceAdminViewConfig[] = [
192
- \t// wp-typia add admin-view entries
193
- ];
194
- `;
195
- const EDITOR_PLUGINS_INTERFACE_SECTION = `
196
-
197
- export interface WorkspaceEditorPluginConfig {
198
- \tfile: string;
199
- \tslug: string;
200
- \tslot: string;
201
- }
202
- `;
203
- const EDITOR_PLUGINS_CONST_SECTION = `
204
-
205
- export const EDITOR_PLUGINS: WorkspaceEditorPluginConfig[] = [
206
- \t// wp-typia add editor-plugin entries
207
- ];
208
- `;
209
- const BLOCK_INVENTORY_SECTION = {
210
- append: {
211
- marker: BLOCK_CONFIG_ENTRY_MARKER,
212
- optionKey: "blockEntries",
213
- },
214
- parse: {
215
- entriesKey: "blocks",
216
- entry: defineInventoryEntryParser({
217
- entryName: "BLOCKS",
218
- fields: [
219
- { key: "apiTypesFile" },
220
- { key: "attributeTypeName" },
221
- { key: "openApiFile" },
222
- { key: "slug", required: true },
223
- { key: "typesFile", required: true },
224
- ],
225
- }),
226
- exportName: "BLOCKS",
227
- required: true,
228
- },
229
- };
230
- const INVENTORY_SECTIONS = [
231
- {
232
- append: {
233
- marker: VARIATION_CONFIG_ENTRY_MARKER,
234
- optionKey: "variationEntries",
235
- },
236
- interface: {
237
- name: "WorkspaceVariationConfig",
238
- section: VARIATIONS_INTERFACE_SECTION,
239
- },
240
- parse: {
241
- entriesKey: "variations",
242
- entry: defineInventoryEntryParser({
243
- entryName: "VARIATIONS",
244
- fields: [
245
- { key: "block", required: true },
246
- { key: "file", required: true },
247
- { key: "slug", required: true },
248
- ],
249
- }),
250
- hasSectionKey: "hasVariationsSection",
251
- },
252
- value: {
253
- name: "VARIATIONS",
254
- section: VARIATIONS_CONST_SECTION,
255
- },
256
- },
257
- {
258
- append: {
259
- marker: BLOCK_STYLE_CONFIG_ENTRY_MARKER,
260
- optionKey: "blockStyleEntries",
261
- },
262
- interface: {
263
- name: "WorkspaceBlockStyleConfig",
264
- section: BLOCK_STYLES_INTERFACE_SECTION,
265
- },
266
- parse: {
267
- entriesKey: "blockStyles",
268
- entry: defineInventoryEntryParser({
269
- entryName: "BLOCK_STYLES",
270
- fields: [
271
- { key: "block", required: true },
272
- { key: "file", required: true },
273
- { key: "slug", required: true },
274
- ],
275
- }),
276
- hasSectionKey: "hasBlockStylesSection",
277
- },
278
- value: {
279
- name: "BLOCK_STYLES",
280
- section: BLOCK_STYLES_CONST_SECTION,
281
- },
282
- },
283
- {
284
- append: {
285
- marker: BLOCK_TRANSFORM_CONFIG_ENTRY_MARKER,
286
- optionKey: "blockTransformEntries",
287
- },
288
- interface: {
289
- name: "WorkspaceBlockTransformConfig",
290
- section: BLOCK_TRANSFORMS_INTERFACE_SECTION,
291
- },
292
- parse: {
293
- entriesKey: "blockTransforms",
294
- entry: defineInventoryEntryParser({
295
- entryName: "BLOCK_TRANSFORMS",
296
- fields: [
297
- { key: "block", required: true },
298
- { key: "file", required: true },
299
- { key: "from", required: true },
300
- { key: "slug", required: true },
301
- { key: "to", required: true },
302
- ],
303
- }),
304
- hasSectionKey: "hasBlockTransformsSection",
305
- },
306
- value: {
307
- name: "BLOCK_TRANSFORMS",
308
- section: BLOCK_TRANSFORMS_CONST_SECTION,
309
- },
310
- },
311
- {
312
- append: {
313
- marker: PATTERN_CONFIG_ENTRY_MARKER,
314
- optionKey: "patternEntries",
315
- },
316
- interface: {
317
- name: "WorkspacePatternConfig",
318
- section: PATTERNS_INTERFACE_SECTION,
319
- },
320
- parse: {
321
- entriesKey: "patterns",
322
- entry: defineInventoryEntryParser({
323
- entryName: "PATTERNS",
324
- fields: [
325
- { key: "file", required: true },
326
- { key: "slug", required: true },
327
- ],
328
- }),
329
- hasSectionKey: "hasPatternsSection",
330
- },
331
- value: {
332
- name: "PATTERNS",
333
- section: PATTERNS_CONST_SECTION,
334
- },
335
- },
336
- {
337
- append: {
338
- marker: BINDING_SOURCE_CONFIG_ENTRY_MARKER,
339
- optionKey: "bindingSourceEntries",
340
- },
341
- interface: {
342
- name: "WorkspaceBindingSourceConfig",
343
- section: BINDING_SOURCES_INTERFACE_SECTION,
344
- },
345
- parse: {
346
- entriesKey: "bindingSources",
347
- entry: defineInventoryEntryParser({
348
- entryName: "BINDING_SOURCES",
349
- fields: [
350
- { key: "attribute" },
351
- { key: "block" },
352
- { key: "editorFile", required: true },
353
- { key: "serverFile", required: true },
354
- { key: "slug", required: true },
355
- ],
356
- }),
357
- hasSectionKey: "hasBindingSourcesSection",
358
- },
359
- value: {
360
- name: "BINDING_SOURCES",
361
- section: BINDING_SOURCES_CONST_SECTION,
362
- },
363
- },
364
- {
365
- append: {
366
- marker: REST_RESOURCE_CONFIG_ENTRY_MARKER,
367
- optionKey: "restResourceEntries",
368
- },
369
- interface: {
370
- name: "WorkspaceRestResourceConfig",
371
- section: REST_RESOURCES_INTERFACE_SECTION,
372
- },
373
- parse: {
374
- entriesKey: "restResources",
375
- entry: defineInventoryEntryParser({
376
- entryName: "REST_RESOURCES",
377
- fields: [
378
- { key: "apiFile", required: true },
379
- { key: "clientFile", required: true },
380
- { key: "dataFile", required: true },
381
- {
382
- key: "methods",
383
- kind: "stringArray",
384
- required: true,
385
- validate: (value, context) => {
386
- const methods = Array.isArray(value) ? value : [];
387
- const invalidMethods = methods.filter((method) => !REST_RESOURCE_METHOD_IDS.includes(method));
388
- if (invalidMethods.length > 0) {
389
- throw new Error(`${context.entryName}[${context.elementIndex}].${context.key} includes unsupported values: ${invalidMethods.join(", ")}.`);
390
- }
391
- },
392
- },
393
- { key: "namespace", required: true },
394
- { key: "openApiFile", required: true },
395
- { key: "phpFile", required: true },
396
- { key: "slug", required: true },
397
- { key: "typesFile", required: true },
398
- { key: "validatorsFile", required: true },
399
- ],
400
- }),
401
- hasSectionKey: "hasRestResourcesSection",
402
- },
403
- value: {
404
- name: "REST_RESOURCES",
405
- section: REST_RESOURCES_CONST_SECTION,
406
- },
407
- },
408
- {
409
- append: {
410
- marker: ABILITY_CONFIG_ENTRY_MARKER,
411
- optionKey: "abilityEntries",
412
- },
413
- interface: {
414
- name: "WorkspaceAbilityConfig",
415
- section: ABILITIES_INTERFACE_SECTION,
416
- },
417
- parse: {
418
- entriesKey: "abilities",
419
- entry: defineInventoryEntryParser({
420
- entryName: "ABILITIES",
421
- fields: [
422
- { key: "clientFile", required: true },
423
- { key: "configFile", required: true },
424
- { key: "dataFile", required: true },
425
- { key: "inputSchemaFile", required: true },
426
- { key: "inputTypeName", required: true },
427
- { key: "outputSchemaFile", required: true },
428
- { key: "outputTypeName", required: true },
429
- { key: "phpFile", required: true },
430
- { key: "slug", required: true },
431
- { key: "typesFile", required: true },
432
- ],
433
- }),
434
- hasSectionKey: "hasAbilitiesSection",
435
- },
436
- value: {
437
- name: "ABILITIES",
438
- section: ABILITIES_CONST_SECTION,
439
- },
440
- },
441
- {
442
- append: {
443
- marker: AI_FEATURE_CONFIG_ENTRY_MARKER,
444
- optionKey: "aiFeatureEntries",
445
- },
446
- interface: {
447
- name: "WorkspaceAiFeatureConfig",
448
- section: AI_FEATURES_INTERFACE_SECTION,
449
- },
450
- parse: {
451
- entriesKey: "aiFeatures",
452
- entry: defineInventoryEntryParser({
453
- entryName: "AI_FEATURES",
454
- fields: [
455
- { key: "aiSchemaFile", required: true },
456
- { key: "apiFile", required: true },
457
- { key: "clientFile", required: true },
458
- { key: "dataFile", required: true },
459
- { key: "namespace", required: true },
460
- { key: "openApiFile", required: true },
461
- { key: "phpFile", required: true },
462
- { key: "slug", required: true },
463
- { key: "typesFile", required: true },
464
- { key: "validatorsFile", required: true },
465
- ],
466
- }),
467
- hasSectionKey: "hasAiFeaturesSection",
468
- },
469
- value: {
470
- name: "AI_FEATURES",
471
- section: AI_FEATURES_CONST_SECTION,
472
- },
473
- },
474
- {
475
- append: {
476
- marker: ADMIN_VIEW_CONFIG_ENTRY_MARKER,
477
- optionKey: "adminViewEntries",
478
- },
479
- interface: {
480
- name: "WorkspaceAdminViewConfig",
481
- section: ADMIN_VIEWS_INTERFACE_SECTION,
482
- },
483
- parse: {
484
- entriesKey: "adminViews",
485
- entry: defineInventoryEntryParser({
486
- entryName: "ADMIN_VIEWS",
487
- fields: [
488
- { key: "file", required: true },
489
- { key: "phpFile", required: true },
490
- { key: "slug", required: true },
491
- { key: "source" },
492
- ],
493
- }),
494
- hasSectionKey: "hasAdminViewsSection",
495
- },
496
- value: {
497
- name: "ADMIN_VIEWS",
498
- section: ADMIN_VIEWS_CONST_SECTION,
499
- },
500
- },
501
- {
502
- append: {
503
- marker: EDITOR_PLUGIN_CONFIG_ENTRY_MARKER,
504
- optionKey: "editorPluginEntries",
505
- },
506
- interface: {
507
- name: "WorkspaceEditorPluginConfig",
508
- section: EDITOR_PLUGINS_INTERFACE_SECTION,
509
- },
510
- parse: {
511
- entriesKey: "editorPlugins",
512
- entry: defineInventoryEntryParser({
513
- entryName: "EDITOR_PLUGINS",
514
- fields: [
515
- { key: "file", required: true },
516
- { key: "slug", required: true },
517
- { key: "slot", required: true },
518
- ],
519
- }),
520
- hasSectionKey: "hasEditorPluginsSection",
521
- },
522
- value: {
523
- name: "EDITOR_PLUGINS",
524
- section: EDITOR_PLUGINS_CONST_SECTION,
525
- },
526
- },
527
- ];
528
- function findExportedArrayLiteral(sourceFile, exportName) {
529
- for (const statement of sourceFile.statements) {
530
- if (!ts.isVariableStatement(statement)) {
531
- continue;
532
- }
533
- if (!statement.modifiers?.some((modifier) => modifier.kind === ts.SyntaxKind.ExportKeyword)) {
534
- continue;
535
- }
536
- for (const declaration of statement.declarationList.declarations) {
537
- if (!ts.isIdentifier(declaration.name) || declaration.name.text !== exportName) {
538
- continue;
539
- }
540
- if (declaration.initializer && ts.isArrayLiteralExpression(declaration.initializer)) {
541
- return {
542
- array: declaration.initializer,
543
- found: true,
544
- };
545
- }
546
- return {
547
- array: null,
548
- found: true,
549
- };
550
- }
551
- }
552
- return {
553
- array: null,
554
- found: false,
555
- };
556
- }
557
- function getOptionalStringProperty(entryName, elementIndex, objectLiteral, key) {
558
- for (const property of objectLiteral.properties) {
559
- if (!ts.isPropertyAssignment(property)) {
560
- continue;
561
- }
562
- const propertyName = getPropertyNameText(property.name);
563
- if (propertyName !== key) {
564
- continue;
565
- }
566
- if (ts.isStringLiteralLike(property.initializer)) {
567
- return property.initializer.text;
568
- }
569
- throw new Error(`${entryName}[${elementIndex}] must use a string literal for "${key}" in scripts/block-config.ts.`);
570
- }
571
- return undefined;
572
- }
573
- function getRequiredStringProperty(entryName, elementIndex, objectLiteral, key) {
574
- const value = getOptionalStringProperty(entryName, elementIndex, objectLiteral, key);
575
- if (!value) {
576
- throw new Error(`${entryName}[${elementIndex}] is missing required "${key}" in scripts/block-config.ts.`);
577
- }
578
- return value;
579
- }
580
- function getRequiredStringArrayProperty(entryName, elementIndex, objectLiteral, key) {
581
- for (const property of objectLiteral.properties) {
582
- if (!ts.isPropertyAssignment(property)) {
583
- continue;
584
- }
585
- const propertyName = getPropertyNameText(property.name);
586
- if (propertyName !== key) {
587
- continue;
588
- }
589
- if (!ts.isArrayLiteralExpression(property.initializer)) {
590
- throw new Error(`${entryName}[${elementIndex}] must use an array literal for "${key}" in scripts/block-config.ts.`);
591
- }
592
- return property.initializer.elements.map((element, itemIndex) => {
593
- if (!ts.isStringLiteralLike(element)) {
594
- throw new Error(`${entryName}[${elementIndex}].${key}[${itemIndex}] must use a string literal in scripts/block-config.ts.`);
595
- }
596
- return element.text;
597
- });
598
- }
599
- throw new Error(`${entryName}[${elementIndex}] is missing required "${key}" in scripts/block-config.ts.`);
600
- }
601
- function parseInventoryEntries(arrayLiteral, descriptor) {
602
- return arrayLiteral.elements.map((element, elementIndex) => {
603
- if (!ts.isObjectLiteralExpression(element)) {
604
- throw new Error(`${descriptor.entryName}[${elementIndex}] must be an object literal in scripts/block-config.ts.`);
605
- }
606
- const entry = {};
607
- for (const field of descriptor.fields) {
608
- const kind = field.kind ?? "string";
609
- const value = kind === "stringArray"
610
- ? getRequiredStringArrayProperty(descriptor.entryName, elementIndex, element, field.key)
611
- : field.required
612
- ? getRequiredStringProperty(descriptor.entryName, elementIndex, element, field.key)
613
- : getOptionalStringProperty(descriptor.entryName, elementIndex, element, field.key);
614
- field.validate?.(value, {
615
- elementIndex,
616
- entryName: descriptor.entryName,
617
- key: field.key,
618
- });
619
- entry[field.key] = value;
620
- }
621
- return entry;
622
- });
623
- }
624
- function parseInventorySection(sourceFile, descriptor) {
625
- if (!descriptor.parse) {
626
- return {
627
- entries: [],
628
- found: false,
629
- };
630
- }
631
- const exportName = descriptor.parse.exportName ?? descriptor.value?.name;
632
- if (!exportName) {
633
- throw new Error("Inventory parser descriptor is missing an export name.");
634
- }
635
- const exportedArray = findExportedArrayLiteral(sourceFile, exportName);
636
- if (!exportedArray.found) {
637
- if (descriptor.parse.required) {
638
- throw new Error(`scripts/block-config.ts must export a ${exportName} array.`);
639
- }
640
- return {
641
- entries: [],
642
- found: false,
643
- };
644
- }
645
- if (!exportedArray.array) {
646
- if (descriptor.parse.required) {
647
- throw new Error(`scripts/block-config.ts must export a ${exportName} array.`);
648
- }
649
- throw new Error(`scripts/block-config.ts must export ${exportName} as an array literal.`);
650
- }
651
- return {
652
- entries: parseInventoryEntries(exportedArray.array, descriptor.parse.entry),
653
- found: true,
654
- };
655
- }
656
- /**
657
- * Parse workspace inventory entries from the source of `scripts/block-config.ts`.
658
- *
659
- * @param source Raw TypeScript source from `scripts/block-config.ts`.
660
- * @returns Parsed inventory sections without the resolved `blockConfigPath`.
661
- * @throws {Error} When `BLOCKS` is missing or any inventory entry is malformed.
662
- */
663
- export function parseWorkspaceInventorySource(source) {
664
- const sourceFile = ts.createSourceFile("block-config.ts", source, ts.ScriptTarget.Latest, true, ts.ScriptKind.TS);
665
- const parsedInventory = {
666
- abilities: [],
667
- adminViews: [],
668
- aiFeatures: [],
669
- bindingSources: [],
670
- blockStyles: [],
671
- blockTransforms: [],
672
- blocks: parseInventorySection(sourceFile, BLOCK_INVENTORY_SECTION).entries,
673
- editorPlugins: [],
674
- hasAbilitiesSection: false,
675
- hasAdminViewsSection: false,
676
- hasAiFeaturesSection: false,
677
- hasBindingSourcesSection: false,
678
- hasBlockStylesSection: false,
679
- hasBlockTransformsSection: false,
680
- hasEditorPluginsSection: false,
681
- hasPatternsSection: false,
682
- hasRestResourcesSection: false,
683
- hasVariationsSection: false,
684
- patterns: [],
685
- restResources: [],
686
- source,
687
- variations: [],
688
- };
689
- const mutableInventory = parsedInventory;
690
- for (const section of INVENTORY_SECTIONS) {
691
- if (!section.parse) {
692
- continue;
693
- }
694
- const parsedSection = parseInventorySection(sourceFile, section);
695
- mutableInventory[section.parse.entriesKey] = parsedSection.entries;
696
- if (section.parse.hasSectionKey) {
697
- mutableInventory[section.parse.hasSectionKey] = parsedSection.found;
698
- }
699
- }
700
- return parsedInventory;
701
- }
702
- /**
703
- * Synchronously read and parse the canonical workspace inventory file.
704
- *
705
- * This compatibility helper is intentionally sync-only for callers that expose
706
- * synchronous APIs. Prefer `readWorkspaceInventoryAsync()` from async command
707
- * paths so workspace reads do not block the event loop.
708
- *
709
- * @param projectDir Workspace root directory.
710
- * @returns Parsed `WorkspaceInventory` including the resolved `blockConfigPath`.
711
- * @throws {Error} When `scripts/block-config.ts` is missing or invalid.
712
- */
713
- export function readWorkspaceInventory(projectDir) {
714
- const blockConfigPath = path.join(projectDir, "scripts", "block-config.ts");
715
- let source;
716
- try {
717
- source = readFileSync(blockConfigPath, "utf8");
718
- }
719
- catch (error) {
720
- if (typeof error === "object" &&
721
- error !== null &&
722
- "code" in error &&
723
- error.code === "ENOENT") {
724
- throw new Error(`Workspace inventory file is missing at ${blockConfigPath}. Expected scripts/block-config.ts to exist.`);
725
- }
726
- throw error;
727
- }
728
- return {
729
- blockConfigPath,
730
- ...parseWorkspaceInventorySource(source),
731
- };
732
- }
733
- /**
734
- * Asynchronously read and parse the canonical workspace inventory file.
735
- *
736
- * @param projectDir Workspace root directory.
737
- * @returns Parsed `WorkspaceInventory` including the resolved `blockConfigPath`.
738
- * @throws {Error} When `scripts/block-config.ts` is missing or invalid.
739
- */
740
- export async function readWorkspaceInventoryAsync(projectDir) {
741
- const blockConfigPath = path.join(projectDir, "scripts", "block-config.ts");
742
- let source;
743
- try {
744
- source = await readFile(blockConfigPath, "utf8");
745
- }
746
- catch (error) {
747
- if (typeof error === "object" &&
748
- error !== null &&
749
- "code" in error &&
750
- error.code === "ENOENT") {
751
- throw new Error(`Workspace inventory file is missing at ${blockConfigPath}. Expected scripts/block-config.ts to exist.`);
752
- }
753
- throw error;
754
- }
755
- return {
756
- blockConfigPath,
757
- ...parseWorkspaceInventorySource(source),
758
- };
759
- }
760
- /**
761
- * Return select options for the current workspace block inventory.
762
- *
763
- * The `description` field mirrors `block.typesFile`, while `name` and `value`
764
- * both map to the block slug for use in interactive add flows.
765
- *
766
- * @param projectDir Workspace root directory.
767
- * @returns Block options for variation-target selection.
768
- */
769
- export function getWorkspaceBlockSelectOptions(projectDir) {
770
- return readWorkspaceInventory(projectDir).blocks.map((block) => ({
771
- description: block.typesFile,
772
- name: block.slug,
773
- value: block.slug,
774
- }));
775
- }
776
- function ensureWorkspaceInventorySections(source) {
777
- let nextSource = source.trimEnd();
778
- for (const section of INVENTORY_SECTIONS) {
779
- if (section.interface &&
780
- !hasExportedInterface(nextSource, section.interface.name)) {
781
- nextSource += section.interface.section;
782
- }
783
- if (section.value && !hasExportedConst(nextSource, section.value.name)) {
784
- nextSource += section.value.section;
785
- }
786
- }
787
- return `${nextSource}\n`;
788
- }
789
- function hasExportedInterface(source, interfaceName) {
790
- return new RegExp(`export\\s+interface\\s+${escapeRegex(interfaceName)}\\b`, "u").test(source);
791
- }
792
- function hasExportedConst(source, constName) {
793
- return new RegExp(`export\\s+const\\s+${escapeRegex(constName)}\\b`, "u").test(source);
794
- }
795
- function appendEntriesAtMarker(source, marker, entries) {
796
- if (entries.length === 0) {
797
- return source;
798
- }
799
- if (!source.includes(marker)) {
800
- throw new Error(`Workspace inventory marker "${marker}" is missing in scripts/block-config.ts.`);
801
- }
802
- return source.replace(marker, `${entries.join("\n")}\n${marker}`);
803
- }
804
- function appendInventorySectionEntries(source, options) {
805
- let nextSource = source;
806
- for (const section of [BLOCK_INVENTORY_SECTION, ...INVENTORY_SECTIONS]) {
807
- if (!section.append) {
808
- continue;
809
- }
810
- nextSource = appendEntriesAtMarker(nextSource, section.append.marker, options[section.append.optionKey] ?? []);
811
- }
812
- return nextSource;
813
- }
814
- function ensureInterfaceField(source, interfaceName, fieldName, fieldSource) {
815
- const interfacePattern = new RegExp(`(export\\s+interface\\s+${escapeRegex(interfaceName)}\\s*\\{\\r?\\n)([\\s\\S]*?)(\\r?\\n\\})`, "u");
816
- return source.replace(interfacePattern, (match, start, body, end) => {
817
- if (new RegExp(`^[ \t]*${escapeRegex(fieldName)}\\??:`, "mu").test(body)) {
818
- return match;
819
- }
820
- const lineEnding = start.endsWith("\r\n") ? "\r\n" : "\n";
821
- const formattedFieldSource = `${fieldSource
822
- .replace(/\r?\n$/u, "")
823
- .split("\n")
824
- .join(lineEnding)}${lineEnding}`;
825
- const memberPattern = /^[ \t]*([A-Za-z_$][\w$]*)\??:/gmu;
826
- for (const member of body.matchAll(memberPattern)) {
827
- const memberIndex = member.index;
828
- const memberName = member[1];
829
- if (memberIndex === undefined || !memberName) {
830
- continue;
831
- }
832
- if (memberName.localeCompare(fieldName) > 0) {
833
- return `${start}${body.slice(0, memberIndex)}${formattedFieldSource}${body.slice(memberIndex)}${end}`;
834
- }
835
- }
836
- return `${start}${body}${body.length > 0 && !body.endsWith(lineEnding) ? lineEnding : ""}${formattedFieldSource}${end}`;
837
- });
838
- }
839
- function normalizeInterfaceFieldBlock(source, interfaceName, fieldName, fieldSource, requiredFragments) {
840
- const interfacePattern = new RegExp(`(export\\s+interface\\s+${escapeRegex(interfaceName)}\\s*\\{\\r?\\n)([\\s\\S]*?)(\\r?\\n\\})`, "u");
841
- return source.replace(interfacePattern, (match, start, body, end) => {
842
- const fieldPattern = new RegExp(`(^([ \\t]*)${escapeRegex(fieldName)}\\??:\\s*\\{[ \\t]*\\r?\\n)([\\s\\S]*?)(^\\2\\};\\r?\\n?)`, "mu");
843
- const fieldMatch = fieldPattern.exec(body);
844
- if (!fieldMatch) {
845
- return match;
846
- }
847
- const existingFieldSource = fieldMatch[0];
848
- if (requiredFragments.every((fragment) => existingFieldSource.includes(fragment))) {
849
- return match;
850
- }
851
- const lineEnding = start.endsWith("\r\n") ? "\r\n" : "\n";
852
- const formattedFieldSource = `${fieldSource
853
- .replace(/\r?\n$/u, "")
854
- .split("\n")
855
- .join(lineEnding)}${lineEnding}`;
856
- return `${start}${body.slice(0, fieldMatch.index)}${formattedFieldSource}${body.slice(fieldMatch.index + existingFieldSource.length)}${end}`;
857
- });
858
- }
859
- /**
860
- * Update `scripts/block-config.ts` source text with additional inventory entries.
861
- *
862
- * Missing inventory sections for variations, patterns, binding sources, REST
863
- * resources, workflow abilities, AI features, editor plugins, block styles, and
864
- * block transforms are created
865
- * automatically before new entries are appended at their marker comments.
866
- * When provided, `transformSource` runs before any entries are inserted.
867
- *
868
- * @param source Existing `scripts/block-config.ts` source.
869
- * @param options Entry lists plus an optional source transformer.
870
- * @returns Updated source text with all requested inventory entries appended.
871
- */
872
- export function updateWorkspaceInventorySource(source, options = {}) {
873
- let nextSource = ensureWorkspaceInventorySections(source);
874
- if (options.transformSource) {
875
- nextSource = options.transformSource(nextSource);
876
- }
877
- nextSource = appendInventorySectionEntries(nextSource, options);
878
- nextSource = ensureInterfaceField(nextSource, "WorkspaceBindingSourceConfig", "attribute", "\tattribute?: string;");
879
- nextSource = ensureInterfaceField(nextSource, "WorkspaceBindingSourceConfig", "block", "\tblock?: string;");
880
- nextSource = ensureInterfaceField(nextSource, "WorkspaceAbilityConfig", "compatibility", WORKSPACE_COMPATIBILITY_CONFIG_FIELD);
881
- nextSource = normalizeInterfaceFieldBlock(nextSource, "WorkspaceAbilityConfig", "compatibility", WORKSPACE_COMPATIBILITY_CONFIG_FIELD, ["optionalFeatureIds: string[];", "requiredFeatureIds: string[];"]);
882
- nextSource = ensureInterfaceField(nextSource, "WorkspaceAiFeatureConfig", "compatibility", WORKSPACE_COMPATIBILITY_CONFIG_FIELD);
883
- nextSource = normalizeInterfaceFieldBlock(nextSource, "WorkspaceAiFeatureConfig", "compatibility", WORKSPACE_COMPATIBILITY_CONFIG_FIELD, ["optionalFeatureIds: string[];", "requiredFeatureIds: string[];"]);
884
- return nextSource;
885
- }
886
- /**
887
- * Append new entries to the canonical workspace inventory file on disk.
888
- *
889
- * @param projectDir Workspace root directory.
890
- * @param options Entry lists and optional source transform passed through to
891
- * `updateWorkspaceInventorySource`.
892
- * @returns Resolves once `scripts/block-config.ts` has been updated if needed.
893
- */
894
- export async function appendWorkspaceInventoryEntries(projectDir, options) {
895
- const blockConfigPath = path.join(projectDir, "scripts", "block-config.ts");
896
- const source = await readFile(blockConfigPath, "utf8");
897
- const nextSource = updateWorkspaceInventorySource(source, options);
898
- if (nextSource !== source) {
899
- await writeFile(blockConfigPath, nextSource, "utf8");
900
- }
901
- }
1
+ export { ABILITY_CONFIG_ENTRY_MARKER, ADMIN_VIEW_CONFIG_ENTRY_MARKER, AI_FEATURE_CONFIG_ENTRY_MARKER, BINDING_SOURCE_CONFIG_ENTRY_MARKER, BLOCK_CONFIG_ENTRY_MARKER, BLOCK_STYLE_CONFIG_ENTRY_MARKER, BLOCK_TRANSFORM_CONFIG_ENTRY_MARKER, EDITOR_PLUGIN_CONFIG_ENTRY_MARKER, PATTERN_CONFIG_ENTRY_MARKER, REST_RESOURCE_CONFIG_ENTRY_MARKER, VARIATION_CONFIG_ENTRY_MARKER, } from "./workspace-inventory-templates.js";
2
+ export { parseWorkspaceInventorySource } from "./workspace-inventory-parser.js";
3
+ export { getWorkspaceBlockSelectOptions, getWorkspaceBlockSelectOptionsAsync, readWorkspaceInventory, readWorkspaceInventoryAsync, } from "./workspace-inventory-read.js";
4
+ export { appendWorkspaceInventoryEntries, updateWorkspaceInventorySource, } from "./workspace-inventory-mutations.js";