wds-init-scenario.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. // wds-init-scenario.js — WDS scaffold: initialize new scenario folder
  2. // Usage: node src/scripts/wds-init-scenario.js --scenario "01 Onboarding" --description "New user first visit to account creation"
  3. 'use strict';
  4. const fs = require('node:fs');
  5. const path = require('node:path');
  6. function parseArgs(argv) {
  7. const args = {};
  8. for (let i = 0; i < argv.length; i++) {
  9. if (argv[i].startsWith('--')) {
  10. const key = argv[i].slice(2);
  11. const value = argv[i + 1] && !argv[i + 1].startsWith('--') ? argv[i + 1] : true;
  12. args[key] = value;
  13. if (value !== true) i++;
  14. }
  15. }
  16. return args;
  17. }
  18. function toSlug(str) {
  19. return str.toLowerCase().replaceAll(/\s+/g, '-');
  20. }
  21. function printUsage() {
  22. process.stdout.write(
  23. [
  24. 'Usage: node src/scripts/wds-init-scenario.js --scenario "01 Onboarding" [options]',
  25. '',
  26. 'Required:',
  27. ' --scenario Scenario name with number, e.g. "01 New User Onboarding"',
  28. '',
  29. 'Optional:',
  30. ' --description Short description of the scenario',
  31. ' --output Base path to write to (default: current directory)',
  32. '',
  33. ].join('\n'),
  34. );
  35. }
  36. function buildReadme({ scenarioName, scenarioSlug, description }) {
  37. const scenarioNumber = scenarioName.split(' ')[0];
  38. const desc = description || '—';
  39. return [
  40. `# Scenario ${scenarioNumber}: ${scenarioName}`,
  41. '',
  42. `**Description:** ${desc}`,
  43. '',
  44. '**Trigger Map:** [Link to trigger map]()',
  45. '',
  46. '---',
  47. '',
  48. '## Pages',
  49. '',
  50. '| # | Page | File | Status |',
  51. '|---|------|------|--------|',
  52. '| — | (no pages yet) | — | — |',
  53. '',
  54. '---',
  55. '',
  56. '## Notes',
  57. '',
  58. '- Add pages with: `node src/scripts/wds-init-page.js --scenario "' + scenarioName + '" --page "01 Start"`',
  59. '- Update navigation after adding pages: `node src/scripts/wds-nav.js --scenario "' + scenarioName + '"`',
  60. '- Validate pages: `node src/scripts/wds-validate.js --scenario "' + scenarioName + '"`',
  61. '',
  62. ].join('\n');
  63. }
  64. function main() {
  65. const args = parseArgs(process.argv.slice(2));
  66. if (args.help) {
  67. printUsage();
  68. process.exit(0);
  69. }
  70. if (!args.scenario) {
  71. process.stderr.write('Error: --scenario is required.\n\n');
  72. printUsage();
  73. process.exit(1);
  74. }
  75. const scenarioName = args.scenario;
  76. const description = args.description || '';
  77. const outputBase = args.output || process.cwd();
  78. const scenarioSlug = toSlug(scenarioName);
  79. const scenarioDir = path.join(outputBase, 'C-UX-Scenarios', scenarioSlug);
  80. const readmeFile = path.join(scenarioDir, 'README.md');
  81. if (fs.existsSync(scenarioDir)) {
  82. process.stderr.write(`Error: Scenario folder already exists: ${scenarioDir}\n`);
  83. process.exit(1);
  84. }
  85. try {
  86. fs.mkdirSync(scenarioDir, { recursive: true });
  87. } catch (error) {
  88. process.stderr.write(`Error creating directory: ${error.message}\n`);
  89. process.exit(1);
  90. }
  91. const content = buildReadme({ scenarioName, scenarioSlug, description });
  92. try {
  93. fs.writeFileSync(readmeFile, content, 'utf8');
  94. } catch (error) {
  95. process.stderr.write(`Error writing README: ${error.message}\n`);
  96. process.exit(1);
  97. }
  98. process.stdout.write(`✓ Created scenario ${scenarioSlug}/\n`);
  99. process.stdout.write(` Path: ${scenarioDir}\n`);
  100. process.stdout.write(` README: ${readmeFile}\n`);
  101. }
  102. main();