@wix/web50-cli 0.1.0 → 0.1.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 (134) hide show
  1. package/bin/web5.js +1 -2
  2. package/dist/cjs/auth/deviceFlow.js +175 -16
  3. package/dist/cjs/auth/deviceFlow.js.map +1 -1
  4. package/dist/cjs/auth/index.js +93 -10
  5. package/dist/cjs/auth/index.js.map +1 -1
  6. package/dist/cjs/auth/secretStore.js.map +1 -1
  7. package/dist/cjs/cli.js +29 -1
  8. package/dist/cjs/cli.js.map +1 -1
  9. package/dist/cjs/commands/bundle.js +103 -0
  10. package/dist/cjs/commands/bundle.js.map +1 -0
  11. package/dist/cjs/commands/conversation.js +50 -0
  12. package/dist/cjs/commands/conversation.js.map +1 -0
  13. package/dist/cjs/commands/conversationWizard.js +528 -0
  14. package/dist/cjs/commands/conversationWizard.js.map +1 -0
  15. package/dist/cjs/commands/deploy.js +237 -0
  16. package/dist/cjs/commands/deploy.js.map +1 -0
  17. package/dist/cjs/commands/ecom.js +239 -0
  18. package/dist/cjs/commands/ecom.js.map +1 -0
  19. package/dist/cjs/commands/embed.js +118 -0
  20. package/dist/cjs/commands/embed.js.map +1 -0
  21. package/dist/cjs/commands/init.js +65 -29
  22. package/dist/cjs/commands/init.js.map +1 -1
  23. package/dist/cjs/commands/instructions.js +456 -0
  24. package/dist/cjs/commands/instructions.js.map +1 -0
  25. package/dist/cjs/commands/login.js +63 -4
  26. package/dist/cjs/commands/login.js.map +1 -1
  27. package/dist/cjs/commands/logout.js +16 -0
  28. package/dist/cjs/commands/logout.js.map +1 -0
  29. package/dist/cjs/commands/serve.js +122 -0
  30. package/dist/cjs/commands/serve.js.map +1 -0
  31. package/dist/cjs/commands/storybook.js +102 -0
  32. package/dist/cjs/commands/storybook.js.map +1 -0
  33. package/dist/cjs/commands/validate.js +617 -17
  34. package/dist/cjs/commands/validate.js.map +1 -1
  35. package/dist/cjs/commands/whoami.js +48 -0
  36. package/dist/cjs/commands/whoami.js.map +1 -0
  37. package/dist/cjs/templates/aiInstructionsSchema.js +5 -1
  38. package/dist/cjs/templates/aiInstructionsSchema.js.map +1 -1
  39. package/dist/cjs/templates/cmsMappingSchema.js +132 -0
  40. package/dist/cjs/templates/cmsMappingSchema.js.map +1 -0
  41. package/dist/cjs/utils/print.js +12 -0
  42. package/dist/cjs/utils/print.js.map +1 -1
  43. package/dist/cjs/utils/project.js +24 -0
  44. package/dist/cjs/utils/project.js.map +1 -1
  45. package/dist/cjs/utils/wixApi.js +57 -0
  46. package/dist/cjs/utils/wixApi.js.map +1 -0
  47. package/dist/esm/auth/deviceFlow.js +182 -17
  48. package/dist/esm/auth/deviceFlow.js.map +1 -1
  49. package/dist/esm/auth/index.js +98 -11
  50. package/dist/esm/auth/index.js.map +1 -1
  51. package/dist/esm/auth/secretStore.js.map +1 -1
  52. package/dist/esm/cli.js +29 -1
  53. package/dist/esm/cli.js.map +1 -1
  54. package/dist/esm/commands/bundle.js +100 -0
  55. package/dist/esm/commands/bundle.js.map +1 -0
  56. package/dist/esm/commands/conversation.js +44 -0
  57. package/dist/esm/commands/conversation.js.map +1 -0
  58. package/dist/esm/commands/conversationWizard.js +529 -0
  59. package/dist/esm/commands/conversationWizard.js.map +1 -0
  60. package/dist/esm/commands/deploy.js +239 -0
  61. package/dist/esm/commands/deploy.js.map +1 -0
  62. package/dist/esm/commands/ecom.js +234 -0
  63. package/dist/esm/commands/ecom.js.map +1 -0
  64. package/dist/esm/commands/embed.js +112 -0
  65. package/dist/esm/commands/embed.js.map +1 -0
  66. package/dist/esm/commands/init.js +66 -30
  67. package/dist/esm/commands/init.js.map +1 -1
  68. package/dist/esm/commands/instructions.js +459 -0
  69. package/dist/esm/commands/instructions.js.map +1 -0
  70. package/dist/esm/commands/login.js +66 -6
  71. package/dist/esm/commands/login.js.map +1 -1
  72. package/dist/esm/commands/logout.js +12 -0
  73. package/dist/esm/commands/logout.js.map +1 -0
  74. package/dist/esm/commands/serve.js +117 -0
  75. package/dist/esm/commands/serve.js.map +1 -0
  76. package/dist/esm/commands/storybook.js +97 -0
  77. package/dist/esm/commands/storybook.js.map +1 -0
  78. package/dist/esm/commands/validate.js +623 -19
  79. package/dist/esm/commands/validate.js.map +1 -1
  80. package/dist/esm/commands/whoami.js +44 -0
  81. package/dist/esm/commands/whoami.js.map +1 -0
  82. package/dist/esm/templates/aiInstructionsSchema.js +5 -1
  83. package/dist/esm/templates/aiInstructionsSchema.js.map +1 -1
  84. package/dist/esm/templates/cmsMappingSchema.js +128 -0
  85. package/dist/esm/templates/cmsMappingSchema.js.map +1 -0
  86. package/dist/esm/utils/print.js +10 -0
  87. package/dist/esm/utils/print.js.map +1 -1
  88. package/dist/esm/utils/project.js +23 -0
  89. package/dist/esm/utils/project.js.map +1 -1
  90. package/dist/esm/utils/wixApi.js +53 -0
  91. package/dist/esm/utils/wixApi.js.map +1 -0
  92. package/dist/types/auth/deviceFlow.d.ts +3 -1
  93. package/dist/types/auth/deviceFlow.d.ts.map +1 -1
  94. package/dist/types/auth/index.d.ts +6 -1
  95. package/dist/types/auth/index.d.ts.map +1 -1
  96. package/dist/types/auth/secretStore.d.ts +2 -0
  97. package/dist/types/auth/secretStore.d.ts.map +1 -1
  98. package/dist/types/commands/bundle.d.ts +10 -0
  99. package/dist/types/commands/bundle.d.ts.map +1 -0
  100. package/dist/types/commands/conversation.d.ts +3 -0
  101. package/dist/types/commands/conversation.d.ts.map +1 -0
  102. package/dist/types/commands/conversationWizard.d.ts +3 -0
  103. package/dist/types/commands/conversationWizard.d.ts.map +1 -0
  104. package/dist/types/commands/deploy.d.ts +3 -0
  105. package/dist/types/commands/deploy.d.ts.map +1 -0
  106. package/dist/types/commands/ecom.d.ts +3 -0
  107. package/dist/types/commands/ecom.d.ts.map +1 -0
  108. package/dist/types/commands/embed.d.ts +3 -0
  109. package/dist/types/commands/embed.d.ts.map +1 -0
  110. package/dist/types/commands/init.d.ts.map +1 -1
  111. package/dist/types/commands/instructions.d.ts +3 -0
  112. package/dist/types/commands/instructions.d.ts.map +1 -0
  113. package/dist/types/commands/login.d.ts.map +1 -1
  114. package/dist/types/commands/logout.d.ts +3 -0
  115. package/dist/types/commands/logout.d.ts.map +1 -0
  116. package/dist/types/commands/serve.d.ts +3 -0
  117. package/dist/types/commands/serve.d.ts.map +1 -0
  118. package/dist/types/commands/storybook.d.ts +3 -0
  119. package/dist/types/commands/storybook.d.ts.map +1 -0
  120. package/dist/types/commands/validate.d.ts +7 -0
  121. package/dist/types/commands/validate.d.ts.map +1 -1
  122. package/dist/types/commands/whoami.d.ts +3 -0
  123. package/dist/types/commands/whoami.d.ts.map +1 -0
  124. package/dist/types/templates/aiInstructionsSchema.d.ts.map +1 -1
  125. package/dist/types/templates/cmsMappingSchema.d.ts +2 -0
  126. package/dist/types/templates/cmsMappingSchema.d.ts.map +1 -0
  127. package/dist/types/utils/print.d.ts +3 -0
  128. package/dist/types/utils/print.d.ts.map +1 -1
  129. package/dist/types/utils/project.d.ts +12 -0
  130. package/dist/types/utils/project.d.ts.map +1 -1
  131. package/dist/types/utils/wixApi.d.ts +9 -0
  132. package/dist/types/utils/wixApi.d.ts.map +1 -0
  133. package/package.json +5 -5
  134. package/defaults/package.json +0 -42
@@ -1,18 +1,19 @@
1
1
  import { Command } from 'commander';
2
2
  import Ajv from 'ajv/dist/2020';
3
3
  import { load as yamlLoad } from 'js-yaml';
4
- import { readFileSync, existsSync, readdirSync } from 'fs';
4
+ import { readFileSync, existsSync, readdirSync, watch as fsWatch } from 'fs';
5
5
  import * as path from 'path';
6
6
  import chalk from 'chalk';
7
7
  import { error, info } from '../utils/print';
8
8
  import { cmsSchemaSchemaTemplate } from '../templates/cmsSchemaSchema';
9
+ import { cmsMappingSchemaTemplate } from '../templates/cmsMappingSchema';
9
10
  import { aiInstructionsSchemaTemplate } from '../templates/aiInstructionsSchema';
10
11
  import { actionYamlSchemaTemplate } from '../templates/actionYamlSchema';
11
-
12
12
  // ── Bundled schemas (never exposed to user's project) ────────────────────────
13
13
 
14
14
  const SCHEMAS = {
15
15
  'cms-schema.yaml': yamlLoad(cmsSchemaSchemaTemplate()),
16
+ 'cms-mapping.yaml': yamlLoad(cmsMappingSchemaTemplate()),
16
17
  'prompt-instructions.yaml': yamlLoad(aiInstructionsSchemaTemplate()),
17
18
  'action.yaml': yamlLoad(actionYamlSchemaTemplate())
18
19
  };
@@ -293,6 +294,91 @@ function buildCmsSchemaDomain(root) {
293
294
  }]
294
295
  };
295
296
  }
297
+ function buildCmsMappingDomain(root) {
298
+ const filePath = path.join(root, 'src', 'configuration', 'cms', 'cms-mapping.yaml');
299
+ const fileName = 'cms-mapping.yaml';
300
+ const ajv = new Ajv({
301
+ allErrors: true,
302
+ strict: false
303
+ });
304
+ const schema = SCHEMAS[fileName];
305
+ let parsed;
306
+ let fileExists = false;
307
+ let yamlOk = false;
308
+ return {
309
+ name: 'CMS Mapping',
310
+ checks: [{
311
+ label: `${fileName} — file exists`,
312
+ run() {
313
+ fileExists = existsSync(filePath);
314
+ if (fileExists) {
315
+ return {
316
+ ok: true,
317
+ errors: []
318
+ };
319
+ }
320
+ return {
321
+ ok: false,
322
+ errors: [{
323
+ message: `File not found: ${filePath}`,
324
+ fix: `Create 'src/configuration/cms/cms-mapping.yaml' in your project.`
325
+ }]
326
+ };
327
+ }
328
+ }, {
329
+ label: `${fileName} — valid YAML`,
330
+ run() {
331
+ if (!fileExists) {
332
+ return {
333
+ ok: false,
334
+ errors: [{
335
+ message: 'skipped — file does not exist'
336
+ }]
337
+ };
338
+ }
339
+ const result = tryParseYaml(filePath);
340
+ if (result.err !== undefined) {
341
+ return {
342
+ ok: false,
343
+ errors: [{
344
+ message: result.err,
345
+ fix: 'Fix the YAML syntax error above.'
346
+ }]
347
+ };
348
+ }
349
+ parsed = result.data;
350
+ yamlOk = true;
351
+ return {
352
+ ok: true,
353
+ errors: []
354
+ };
355
+ }
356
+ }, {
357
+ label: `${fileName} — schema valid`,
358
+ run() {
359
+ if (!yamlOk) {
360
+ return {
361
+ ok: false,
362
+ errors: [{
363
+ message: 'skipped — YAML parse failed'
364
+ }]
365
+ };
366
+ }
367
+ const validate = ajv.compile(schema);
368
+ if (validate(parsed)) {
369
+ return {
370
+ ok: true,
371
+ errors: []
372
+ };
373
+ }
374
+ return {
375
+ ok: false,
376
+ errors: (validate.errors ?? []).map(e => formatAjvError(e))
377
+ };
378
+ }
379
+ }]
380
+ };
381
+ }
296
382
  function buildActionsDomain(root) {
297
383
  const actionsDir = path.join(root, 'src', 'actions');
298
384
  const registryPath = path.join(root, 'src', 'createRegistry.ts');
@@ -666,27 +752,278 @@ function buildActionsDomain(root) {
666
752
  };
667
753
  }
668
754
 
669
- // ── Command ───────────────────────────────────────────────────────────────────
755
+ // ── Sections helpers ──────────────────────────────────────────────────────────
670
756
 
671
- export const validateCommand = new Command('validate').description('Validate project config files against their schemas').action(() => {
672
- const root = findProjectRoot(process.cwd());
673
- if (!root) {
674
- error('No web5.config.json found run this command from inside a Web5 project');
675
- process.exit(1);
757
+ /**
758
+ * Extracts the text of every `.register(...)` call that has a component array.
759
+ * Uses bracket-counting to handle nesting correctly.
760
+ * Skips system registrations that have no `[` (e.g. SkipNodesSectionDefinition).
761
+ */
762
+ function extractRegisterBlocks(content) {
763
+ const blocks = [];
764
+ const marker = '.register(';
765
+ let searchFrom = 0;
766
+ let start = content.indexOf(marker, searchFrom);
767
+ while (start !== -1) {
768
+ let depth = 0;
769
+ let i = start + marker.length - 1; // position of the opening '('
770
+ while (i < content.length) {
771
+ const ch = content[i];
772
+ if (ch === '(') {
773
+ depth++;
774
+ } else if (ch === ')') {
775
+ depth--;
776
+ if (depth === 0) {
777
+ break;
778
+ }
779
+ }
780
+ i++;
781
+ }
782
+ const block = content.slice(start, i + 1);
783
+ if (block.includes('[')) {
784
+ blocks.push(block);
785
+ }
786
+ searchFrom = i + 1;
787
+ start = content.indexOf(marker, searchFrom);
676
788
  }
677
- info(`Project root: ${root}`);
678
- console.log('');
679
- const domains = [buildAiInstructionsDomain(root), buildCmsSchemaDomain(root), buildActionsDomain(root)];
680
- let hasErrors = false;
681
- for (const domain of domains) {
789
+ return blocks;
790
+ }
791
+
792
+ /**
793
+ * Scans all import lines that reference a `sections/` path and returns a
794
+ * Map<localName, stem>. Handles aliases and multi-name imports:
795
+ * import { KpiSection as KpiSectionComponent } from '...sections/KpiSection'
796
+ * → "KpiSectionComponent" → "KpiSection"
797
+ */
798
+ function buildSectionImportMap(content) {
799
+ const map = new Map();
800
+ const re = /import\s*\{([^}]+)\}\s*from\s*['"][^'"]*sections\/(\w+)['"]/g;
801
+ let m = re.exec(content);
802
+ while (m !== null) {
803
+ const stem = m[2];
804
+ const names = m[1].split(',');
805
+ for (const raw of names) {
806
+ const token = raw.trim();
807
+ if (!token) {
808
+ continue;
809
+ }
810
+ const asParts = token.split(/\s+as\s+/);
811
+ const localName = (asParts[1] ?? asParts[0]).trim();
812
+ if (localName) {
813
+ map.set(localName, stem);
814
+ }
815
+ }
816
+ m = re.exec(content);
817
+ }
818
+ return map;
819
+ }
820
+
821
+ // ── Sections domain ────────────────────────────────────────────────────────────
822
+
823
+ function buildSectionsDomain(root) {
824
+ const registryPath = path.join(root, 'src', 'createRegistry.ts');
825
+ const sectionsDir = path.join(root, 'src', 'components', 'sections');
826
+ let registryExists = false;
827
+ let registryContent = '';
828
+ let tsxFiles = [];
829
+ return {
830
+ name: 'Sections',
831
+ checks: [
832
+ // ── Check 1: createRegistry.ts exists ────────────────────────────────
833
+ {
834
+ label: 'createRegistry.ts — file exists',
835
+ run() {
836
+ registryExists = existsSync(registryPath);
837
+ if (!registryExists) {
838
+ return {
839
+ ok: false,
840
+ errors: [{
841
+ message: 'src/createRegistry.ts not found',
842
+ fix: 'Create src/createRegistry.ts and export a createRegistry() function that registers your section components.'
843
+ }]
844
+ };
845
+ }
846
+ registryContent = readText(registryPath);
847
+ tsxFiles = existsSync(sectionsDir) ? readdirSync(sectionsDir).filter(f => f.endsWith('.tsx')) : [];
848
+ return {
849
+ ok: true,
850
+ errors: []
851
+ };
852
+ }
853
+ },
854
+ // ── Check 2: all .tsx files imported in createRegistry.ts ─────────────
855
+ {
856
+ label: 'src/components/sections/ — all .tsx files imported in createRegistry.ts',
857
+ run() {
858
+ if (!registryExists) {
859
+ return {
860
+ ok: false,
861
+ errors: [{
862
+ message: 'skipped — createRegistry.ts not found'
863
+ }]
864
+ };
865
+ }
866
+ const errors = [];
867
+ for (const file of tsxFiles) {
868
+ const stem = path.basename(file, '.tsx');
869
+ if (!registryContent.includes(`sections/${stem}`)) {
870
+ errors.push({
871
+ message: `'${stem}.tsx' is not imported in src/createRegistry.ts`,
872
+ fix: `Add: import { ${stem} } from './components/sections/${stem}';`
873
+ });
874
+ }
875
+ }
876
+ return errors.length === 0 ? {
877
+ ok: true,
878
+ errors: []
879
+ } : {
880
+ ok: false,
881
+ errors
882
+ };
883
+ }
884
+ },
885
+ // ── Check 3: all .tsx files wired into a .register() call ─────────────
886
+ {
887
+ label: 'src/components/sections/ — all .tsx files wired into .register()',
888
+ run() {
889
+ if (!registryExists) {
890
+ return {
891
+ ok: false,
892
+ errors: [{
893
+ message: 'skipped — createRegistry.ts not found'
894
+ }]
895
+ };
896
+ }
897
+ const importMap = buildSectionImportMap(registryContent);
898
+ const blocks = extractRegisterBlocks(registryContent);
899
+ const blockText = blocks.join('\n');
900
+ const errors = [];
901
+ for (const file of tsxFiles) {
902
+ var _find;
903
+ const stem = path.basename(file, '.tsx');
904
+ // Find the local name for this stem (may have been aliased on import)
905
+ const localName = ((_find = [...importMap.entries()].find(_ref => {
906
+ let [, s] = _ref;
907
+ return s === stem;
908
+ })) == null ? void 0 : _find[0]) ?? stem;
909
+ if (!blockText.includes(localName)) {
910
+ errors.push({
911
+ message: `'${localName}' (${stem}.tsx) is imported but not passed to any .register() call`,
912
+ fix: `Add { component: ${localName} } inside a .register(new <Definition>(), [...]) call in src/createRegistry.ts.`
913
+ });
914
+ }
915
+ }
916
+ return errors.length === 0 ? {
917
+ ok: true,
918
+ errors: []
919
+ } : {
920
+ ok: false,
921
+ errors
922
+ };
923
+ }
924
+ },
925
+ // ── Check 4: every .register() block has a catch-all entry ────────────
926
+ {
927
+ label: 'createRegistry.ts — all .register() blocks have a catch-all component',
928
+ run() {
929
+ if (!registryExists) {
930
+ return {
931
+ ok: false,
932
+ errors: [{
933
+ message: 'skipped — createRegistry.ts not found'
934
+ }]
935
+ };
936
+ }
937
+ const blocks = extractRegisterBlocks(registryContent);
938
+ const errors = [];
939
+ for (const block of blocks) {
940
+ const defMatch = block.match(/\.register\(\s*new\s+(\w+)/);
941
+ const defName = defMatch ? defMatch[1] : '(unknown)';
942
+
943
+ // Split on "{ component:" to isolate each entry
944
+ const entries = block.split('{ component:').slice(1);
945
+ if (entries.length === 0) {
946
+ continue;
947
+ }
948
+ const allHaveIntent = entries.every(entry => entry.includes('intent:'));
949
+ if (allHaveIntent) {
950
+ errors.push({
951
+ message: `'${defName}' has no catch-all component — every entry specifies an intent`,
952
+ fix: `Add a catch-all entry to the .register(new ${defName}(), [...]) call:\n { component: YourDefaultComponent }`
953
+ });
954
+ }
955
+ }
956
+ return errors.length === 0 ? {
957
+ ok: true,
958
+ errors: []
959
+ } : {
960
+ ok: false,
961
+ errors
962
+ };
963
+ }
964
+ },
965
+ // ── Check 5: all component refs in .register() resolve to a .tsx file ─
966
+ {
967
+ label: 'createRegistry.ts — all .register() component references resolve to a .tsx file',
968
+ run() {
969
+ if (!registryExists) {
970
+ return {
971
+ ok: false,
972
+ errors: [{
973
+ message: 'skipped — createRegistry.ts not found'
974
+ }]
975
+ };
976
+ }
977
+ const importMap = buildSectionImportMap(registryContent);
978
+ const blocks = extractRegisterBlocks(registryContent);
979
+ const errors = [];
980
+ for (const block of blocks) {
981
+ const refs = [...block.matchAll(/\{\s*component:\s*(\w+)/g)].map(m => m[1]);
982
+ for (const ref of refs) {
983
+ const stem = importMap.get(ref);
984
+ if (stem === undefined) {
985
+ errors.push({
986
+ message: `Component '${ref}' used in .register() has no matching import from sections/`,
987
+ fix: `Add: import { ${ref} } from './components/sections/${ref}'; or check the import alias.`
988
+ });
989
+ continue;
990
+ }
991
+ const filePath = path.join(sectionsDir, `${stem}.tsx`);
992
+ if (!existsSync(filePath)) {
993
+ errors.push({
994
+ message: `Component '${ref}' maps to '${stem}.tsx' which does not exist in src/components/sections/`,
995
+ fix: `Create src/components/sections/${stem}.tsx, or remove the stale .register() entry from src/createRegistry.ts.`
996
+ });
997
+ }
998
+ }
999
+ }
1000
+ return errors.length === 0 ? {
1001
+ ok: true,
1002
+ errors: []
1003
+ } : {
1004
+ ok: false,
1005
+ errors
1006
+ };
1007
+ }
1008
+ }]
1009
+ };
1010
+ }
1011
+
1012
+ // ── Rendering ─────────────────────────────────────────────────────────────────
1013
+
1014
+ function printDomain(domain, results, verbose) {
1015
+ const failed = results.filter(r => !r.result.ok);
1016
+ const domainOk = failed.length === 0;
1017
+ if (verbose) {
682
1018
  console.log(domain.name);
683
- for (const check of domain.checks) {
684
- const result = check.run();
1019
+ for (const {
1020
+ label,
1021
+ result
1022
+ } of results) {
685
1023
  if (result.ok) {
686
- console.log(chalk.green(` \u2714 ${check.label}`));
1024
+ console.log(chalk.green(` \u2714 ${label}`));
687
1025
  } else {
688
- hasErrors = true;
689
- process.stderr.write(chalk.red(` \u2716 ${check.label}\n`));
1026
+ process.stderr.write(chalk.red(` \u2716 ${label}\n`));
690
1027
  for (const e_ of result.errors) {
691
1028
  process.stderr.write(` ${e_.message}\n`);
692
1029
  if (e_.fix) {
@@ -696,9 +1033,276 @@ export const validateCommand = new Command('validate').description('Validate pro
696
1033
  }
697
1034
  }
698
1035
  console.log('');
1036
+ return domainOk;
1037
+ }
1038
+
1039
+ // Slim mode
1040
+ if (domainOk) {
1041
+ console.log(chalk.green(`\u2714 ${domain.name} — valid`));
1042
+ return true;
1043
+ }
1044
+ console.log(domain.name);
1045
+ for (const {
1046
+ label,
1047
+ result
1048
+ } of failed) {
1049
+ process.stderr.write(chalk.red(` \u2716 ${label}\n`));
1050
+ for (const e_ of result.errors) {
1051
+ process.stderr.write(` ${e_.message}\n`);
1052
+ if (e_.fix) {
1053
+ process.stderr.write(` Fix: ${e_.fix}\n`);
1054
+ }
1055
+ }
1056
+ }
1057
+ console.log('');
1058
+ return false;
1059
+ }
1060
+
1061
+ // ── JSON output ───────────────────────────────────────────────────────────────
1062
+
1063
+ function collectDomainJson(domain, results) {
1064
+ return {
1065
+ name: domain.name,
1066
+ ok: results.every(r => r.result.ok),
1067
+ checks: results.map(_ref2 => {
1068
+ let {
1069
+ label,
1070
+ result
1071
+ } = _ref2;
1072
+ return {
1073
+ label,
1074
+ ok: result.ok,
1075
+ errors: result.errors
1076
+ };
1077
+ })
1078
+ };
1079
+ }
1080
+
1081
+ // ── Build scripts domain ──────────────────────────────────────────────────────
1082
+
1083
+ function buildScriptsDomain(root) {
1084
+ const pkgPath = path.join(root, 'package.json');
1085
+ let pkg = {};
1086
+ let fileExists = false;
1087
+ return {
1088
+ name: 'Build Scripts',
1089
+ checks: [{
1090
+ label: 'package.json — file exists',
1091
+ run() {
1092
+ fileExists = existsSync(pkgPath);
1093
+ if (!fileExists) {
1094
+ return {
1095
+ ok: false,
1096
+ errors: [{
1097
+ message: `File not found: ${pkgPath}`,
1098
+ fix: `Create a 'package.json' at the root of your project.`
1099
+ }]
1100
+ };
1101
+ }
1102
+ try {
1103
+ pkg = JSON.parse(readFileSync(pkgPath, 'utf8'));
1104
+ } catch (e) {
1105
+ fileExists = false;
1106
+ return {
1107
+ ok: false,
1108
+ errors: [{
1109
+ message: `Failed to parse package.json: ${e instanceof Error ? e.message : String(e)}`,
1110
+ fix: `Fix the JSON syntax in your package.json.`
1111
+ }]
1112
+ };
1113
+ }
1114
+ return {
1115
+ ok: true,
1116
+ errors: []
1117
+ };
1118
+ }
1119
+ }, {
1120
+ label: 'package.json — bundle script defined',
1121
+ run() {
1122
+ var _pkg$scripts;
1123
+ if (!fileExists) {
1124
+ return {
1125
+ ok: false,
1126
+ errors: [{
1127
+ message: 'skipped — package.json not readable'
1128
+ }]
1129
+ };
1130
+ }
1131
+ if (typeof ((_pkg$scripts = pkg.scripts) == null ? void 0 : _pkg$scripts.bundle) === 'string' && pkg.scripts.bundle.length > 0) {
1132
+ return {
1133
+ ok: true,
1134
+ errors: []
1135
+ };
1136
+ }
1137
+ return {
1138
+ ok: false,
1139
+ errors: [{
1140
+ message: `Missing 'bundle' script in package.json`,
1141
+ fix: `Add a 'bundle' script to your package.json scripts, e.g.:\n "bundle": "vite build --config src/vite.cdn.config.ts"`
1142
+ }]
1143
+ };
1144
+ }
1145
+ }, {
1146
+ label: 'package.json — build script defined',
1147
+ run() {
1148
+ var _pkg$scripts2;
1149
+ if (!fileExists) {
1150
+ return {
1151
+ ok: false,
1152
+ errors: [{
1153
+ message: 'skipped — package.json not readable'
1154
+ }]
1155
+ };
1156
+ }
1157
+ if (typeof ((_pkg$scripts2 = pkg.scripts) == null ? void 0 : _pkg$scripts2.build) === 'string' && pkg.scripts.build.length > 0) {
1158
+ return {
1159
+ ok: true,
1160
+ errors: []
1161
+ };
1162
+ }
1163
+ return {
1164
+ ok: false,
1165
+ errors: [{
1166
+ message: `Missing 'build' script in package.json`,
1167
+ fix: `Add a 'build' script to your package.json scripts, e.g.:\n "build": "tsc"`
1168
+ }]
1169
+ };
1170
+ }
1171
+ }]
1172
+ };
1173
+ }
1174
+
1175
+ // ── Domain selection ──────────────────────────────────────────────────────────
1176
+
1177
+ const DOMAIN_NAME_MAP = {
1178
+ ai: 'AI Instructions',
1179
+ cms: 'CMS Schema',
1180
+ 'cms-mapping': 'CMS Mapping',
1181
+ scripts: 'Build Scripts',
1182
+ actions: 'Actions',
1183
+ sections: 'Sections'
1184
+ };
1185
+ function selectDomains(root, filter) {
1186
+ const all = [buildAiInstructionsDomain(root), buildCmsSchemaDomain(root), buildCmsMappingDomain(root), buildActionsDomain(root), buildScriptsDomain(root), buildSectionsDomain(root)];
1187
+ if (!filter) {
1188
+ return all;
699
1189
  }
700
- if (hasErrors) {
1190
+ const filters = Array.isArray(filter) ? filter : [filter];
1191
+ const names = new Set(filters.map(f => DOMAIN_NAME_MAP[f]));
1192
+ return all.filter(d => names.has(d.name));
1193
+ }
1194
+
1195
+ // ── Core runner ───────────────────────────────────────────────────────────────
1196
+
1197
+ function runDomains(domains, verbose, format) {
1198
+ let hasErrors = false;
1199
+ const jsonResults = [];
1200
+ for (const domain of domains) {
1201
+ const results = domain.checks.map(check => ({
1202
+ label: check.label,
1203
+ result: check.run()
1204
+ }));
1205
+ let domainOk;
1206
+ if (format === 'json') {
1207
+ const jr = collectDomainJson(domain, results);
1208
+ jsonResults.push(jr);
1209
+ domainOk = jr.ok;
1210
+ } else {
1211
+ domainOk = printDomain(domain, results, verbose);
1212
+ }
1213
+ if (!domainOk) {
1214
+ hasErrors = true;
1215
+ }
1216
+ }
1217
+ return {
1218
+ ok: !hasErrors,
1219
+ jsonResults
1220
+ };
1221
+ }
1222
+
1223
+ // ── Programmatic API ─────────────────────────────────────────────────────────
1224
+
1225
+ /**
1226
+ * Runs validation domains against the given project root.
1227
+ * Pass `filters` to validate only specific domains; omit to validate all.
1228
+ * Prints results in slim mode and returns true if everything passes.
1229
+ */
1230
+ export function runValidation(root, filters) {
1231
+ const domains = selectDomains(root, filters);
1232
+ const {
1233
+ ok
1234
+ } = runDomains(domains, false, 'text');
1235
+ return ok;
1236
+ }
1237
+
1238
+ // ── Watch mode ────────────────────────────────────────────────────────────────
1239
+
1240
+ function startWatch(root, run) {
1241
+ const watchDirs = [path.join(root, 'src', 'configuration'), path.join(root, 'src', 'actions'), path.join(root, 'src', 'components', 'sections'), path.join(root, 'src', 'createRegistry.ts')].filter(d => existsSync(d));
1242
+ if (watchDirs.length === 0) {
1243
+ error('No directories to watch found.');
1244
+ return;
1245
+ }
1246
+ let debounce = null;
1247
+ const trigger = () => {
1248
+ if (debounce) {
1249
+ clearTimeout(debounce);
1250
+ }
1251
+ debounce = setTimeout(() => {
1252
+ console.clear();
1253
+ info(`[${new Date().toLocaleTimeString()}] Re-validating...`);
1254
+ console.log('');
1255
+ run();
1256
+ }, 200);
1257
+ };
1258
+ for (const dir of watchDirs) {
1259
+ fsWatch(dir, {
1260
+ recursive: true
1261
+ }, trigger);
1262
+ }
1263
+ info(`Watching for changes in ${watchDirs.map(d => path.relative(root, d)).join(', ')}...`);
1264
+ info('Press Ctrl+C to stop.');
1265
+ console.log('');
1266
+ }
1267
+
1268
+ // ── Command ───────────────────────────────────────────────────────────────────
1269
+
1270
+ export const validateCommand = new Command('validate').description('Validate project config files against their schemas').option('--verbose', 'Show all checks, including passing ones').option('--domain <domain>', 'Only validate one domain: ai, cms, actions, scripts, or sections').option('--format <format>', 'Output format: text (default) or json', 'text').option('--watch', 'Re-validate on file changes (TTY only)').option('--strict', 'Exit with error even on warnings').option('--project <path>', 'Explicit project root (overrides cwd auto-detection)').action(opts => {
1271
+ const verbose = Boolean(opts.verbose);
1272
+ const format = opts.format ?? 'text';
1273
+ const domainFilter = opts.domain;
1274
+ const root = opts.project ? path.resolve(opts.project) : findProjectRoot(process.cwd());
1275
+ if (!root) {
1276
+ error('No web5.config.json found — run this command from inside a Web5 project');
701
1277
  process.exit(1);
702
1278
  }
1279
+ if (format === 'text') {
1280
+ info(`Project root: ${root}`);
1281
+ console.log('');
1282
+ }
1283
+ const run = () => {
1284
+ const domains = selectDomains(root, domainFilter);
1285
+ const {
1286
+ ok,
1287
+ jsonResults
1288
+ } = runDomains(domains, verbose, format);
1289
+ if (format === 'json') {
1290
+ console.log(JSON.stringify({
1291
+ ok,
1292
+ domains: jsonResults
1293
+ }, null, 2));
1294
+ }
1295
+ if (!ok && !opts.watch) {
1296
+ process.exit(1);
1297
+ }
1298
+ };
1299
+ run();
1300
+ if (opts.watch) {
1301
+ if (!process.stdout.isTTY) {
1302
+ error('--watch requires a TTY (interactive terminal).');
1303
+ process.exit(1);
1304
+ }
1305
+ startWatch(root, run);
1306
+ }
703
1307
  });
704
1308
  //# sourceMappingURL=validate.js.map