@levu/snap 0.1.0 → 0.2.0

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 (138) hide show
  1. package/CHANGELOG.md +41 -0
  2. package/README.md +33 -2
  3. package/dist/cli/cli-runner.d.ts +37 -0
  4. package/dist/cli/cli-runner.js +161 -0
  5. package/dist/cli-entry.js +6 -64
  6. package/dist/core/contracts/action-contract.d.ts +1 -3
  7. package/dist/core/contracts/tui-contract.d.ts +47 -0
  8. package/dist/core/contracts/tui-contract.js +1 -0
  9. package/dist/core/registry/action-registry.js +3 -1
  10. package/dist/dx/args/env.d.ts +6 -0
  11. package/dist/dx/args/env.js +15 -0
  12. package/dist/dx/args/index.d.ts +4 -0
  13. package/dist/dx/args/index.js +3 -0
  14. package/dist/dx/args/readers.d.ts +5 -0
  15. package/dist/dx/args/readers.js +36 -0
  16. package/dist/dx/args/types.d.ts +5 -0
  17. package/dist/dx/args/types.js +1 -0
  18. package/dist/dx/help/builder.d.ts +10 -0
  19. package/dist/dx/help/builder.js +11 -0
  20. package/dist/dx/help/index.d.ts +4 -0
  21. package/dist/dx/help/index.js +2 -0
  22. package/dist/dx/help/schema.d.ts +14 -0
  23. package/dist/dx/help/schema.js +33 -0
  24. package/dist/dx/runtime/action-result.d.ts +12 -0
  25. package/dist/dx/runtime/action-result.js +35 -0
  26. package/dist/dx/runtime/flow.d.ts +9 -0
  27. package/dist/dx/runtime/flow.js +19 -0
  28. package/dist/dx/runtime/index.d.ts +4 -0
  29. package/dist/dx/runtime/index.js +2 -0
  30. package/dist/dx/terminal/index.d.ts +3 -0
  31. package/dist/dx/terminal/index.js +3 -0
  32. package/dist/dx/terminal/intro-outro.d.ts +4 -0
  33. package/dist/dx/terminal/intro-outro.js +44 -0
  34. package/dist/dx/terminal/output.d.ts +19 -0
  35. package/dist/dx/terminal/output.js +59 -0
  36. package/dist/dx/tui/components.d.ts +6 -0
  37. package/dist/dx/tui/components.js +40 -0
  38. package/dist/dx/tui/flow.d.ts +2 -0
  39. package/dist/dx/tui/flow.js +14 -0
  40. package/dist/dx/tui/index.d.ts +16 -0
  41. package/dist/dx/tui/index.js +15 -0
  42. package/dist/dx/tui/no-result.d.ts +13 -0
  43. package/dist/dx/tui/no-result.js +18 -0
  44. package/dist/help/help-renderer.js +5 -1
  45. package/dist/help/hierarchy-resolver.js +1 -1
  46. package/dist/index.d.ts +16 -0
  47. package/dist/index.js +11 -0
  48. package/dist/runtime/dispatch.d.ts +2 -1
  49. package/dist/runtime/engine.d.ts +2 -1
  50. package/dist/runtime/engine.js +22 -1
  51. package/dist/runtime/mode-resolver.d.ts +3 -2
  52. package/dist/runtime/runtime-context.d.ts +8 -1
  53. package/dist/tui/component-adapters/autocomplete.d.ts +15 -0
  54. package/dist/tui/component-adapters/autocomplete.js +34 -0
  55. package/dist/tui/component-adapters/cancel.d.ts +6 -0
  56. package/dist/tui/component-adapters/cancel.js +20 -0
  57. package/dist/tui/component-adapters/confirm.d.ts +2 -0
  58. package/dist/tui/component-adapters/confirm.js +13 -1
  59. package/dist/tui/component-adapters/multiselect.d.ts +4 -0
  60. package/dist/tui/component-adapters/multiselect.js +23 -3
  61. package/dist/tui/component-adapters/note.d.ts +7 -0
  62. package/dist/tui/component-adapters/note.js +23 -0
  63. package/dist/tui/component-adapters/password.d.ts +7 -0
  64. package/dist/tui/component-adapters/password.js +24 -0
  65. package/dist/tui/component-adapters/progress.d.ts +7 -0
  66. package/dist/tui/component-adapters/progress.js +44 -0
  67. package/dist/tui/component-adapters/readline-utils.d.ts +1 -0
  68. package/dist/tui/component-adapters/readline-utils.js +2 -0
  69. package/dist/tui/component-adapters/select.d.ts +2 -0
  70. package/dist/tui/component-adapters/select.js +25 -3
  71. package/dist/tui/component-adapters/spinner.d.ts +10 -0
  72. package/dist/tui/component-adapters/spinner.js +48 -0
  73. package/dist/tui/component-adapters/tasks.d.ts +9 -0
  74. package/dist/tui/component-adapters/tasks.js +31 -0
  75. package/dist/tui/component-adapters/text.d.ts +2 -0
  76. package/dist/tui/component-adapters/text.js +21 -4
  77. package/dist/tui/custom/custom-prompt.d.ts +16 -0
  78. package/dist/tui/custom/custom-prompt.js +72 -0
  79. package/dist/tui/custom/index.d.ts +2 -0
  80. package/dist/tui/custom/index.js +1 -0
  81. package/dist/tui/prompt-toolkit.d.ts +15 -0
  82. package/dist/tui/prompt-toolkit.js +17 -0
  83. package/docs/component-reference.md +474 -0
  84. package/docs/getting-started.md +242 -0
  85. package/docs/integration-examples.md +677 -0
  86. package/docs/module-authoring-guide.md +105 -1
  87. package/docs/snap-args.md +323 -0
  88. package/docs/snap-help.md +372 -0
  89. package/docs/snap-runtime.md +394 -0
  90. package/docs/snap-terminal.md +410 -0
  91. package/docs/snap-tui.md +529 -0
  92. package/package.json +15 -2
  93. package/.github/workflows/ci.yml +0 -26
  94. package/plans/260209-1547-hub-dual-runtime-framework/phase-01-foundation-and-contracts.md +0 -71
  95. package/plans/260209-1547-hub-dual-runtime-framework/phase-02-runtime-and-state-machine.md +0 -76
  96. package/plans/260209-1547-hub-dual-runtime-framework/phase-03-tui-components-and-policies.md +0 -71
  97. package/plans/260209-1547-hub-dual-runtime-framework/phase-04-help-system-and-ai-readability.md +0 -69
  98. package/plans/260209-1547-hub-dual-runtime-framework/phase-05-testing-and-quality-gates.md +0 -79
  99. package/plans/260209-1547-hub-dual-runtime-framework/phase-06-sample-modules-and-adoption.md +0 -75
  100. package/plans/260209-1547-hub-dual-runtime-framework/plan.md +0 -105
  101. package/plans/260209-1547-hub-dual-runtime-framework/reports/planner-report.md +0 -27
  102. package/plans/260209-1547-hub-dual-runtime-framework/research/researcher-01-report.md +0 -166
  103. package/plans/260209-1547-hub-dual-runtime-framework/research/researcher-02-report.md +0 -87
  104. package/plans/260209-1547-hub-dual-runtime-framework/scout/scout-01-report.md +0 -24
  105. package/src/cli/help-command.ts +0 -1
  106. package/src/cli-entry.ts +0 -83
  107. package/src/core/contracts/action-contract.ts +0 -30
  108. package/src/core/contracts/help-contract.ts +0 -20
  109. package/src/core/contracts/module-contract.ts +0 -7
  110. package/src/core/errors/framework-errors.ts +0 -26
  111. package/src/core/registry/action-registry.ts +0 -94
  112. package/src/help/help-command.ts +0 -32
  113. package/src/help/help-model.ts +0 -10
  114. package/src/help/help-renderer.ts +0 -21
  115. package/src/help/hierarchy-resolver.ts +0 -54
  116. package/src/index.ts +0 -10
  117. package/src/modules/sample-content/module.ts +0 -66
  118. package/src/modules/sample-system/module.ts +0 -74
  119. package/src/runtime/dispatch.ts +0 -64
  120. package/src/runtime/engine.ts +0 -59
  121. package/src/runtime/mode-resolver.ts +0 -18
  122. package/src/runtime/resume-store.ts +0 -53
  123. package/src/runtime/runtime-context.ts +0 -10
  124. package/src/runtime/state-machine.ts +0 -77
  125. package/src/tui/accessibility-footer.ts +0 -11
  126. package/src/tui/component-adapters/confirm.ts +0 -8
  127. package/src/tui/component-adapters/group.ts +0 -12
  128. package/src/tui/component-adapters/multiselect.ts +0 -22
  129. package/src/tui/component-adapters/select.ts +0 -18
  130. package/src/tui/component-adapters/text.ts +0 -13
  131. package/src/tui/interrupt-handlers.ts +0 -15
  132. package/tests/e2e/cli-smoke.e2e.test.ts +0 -19
  133. package/tests/integration/runtime-dispatch.integration.test.ts +0 -23
  134. package/tests/transcript/help.transcript.test.ts +0 -20
  135. package/tests/unit/state-machine.test.ts +0 -22
  136. package/tsconfig.build.json +0 -9
  137. package/tsconfig.json +0 -17
  138. package/vitest.config.ts +0 -8
@@ -0,0 +1,372 @@
1
+ # SnapHelp - Schema-Driven Help Generation
2
+
3
+ `SnapHelp` provides helpers for defining argument schemas once and automatically generating both commandline contracts and help documentation.
4
+
5
+ ## Import
6
+
7
+ ```typescript
8
+ import * as SnapHelp from 'snap-framework';
9
+ ```
10
+
11
+ ## Core Concepts
12
+
13
+ ### Define Once, Use Everywhere
14
+
15
+ Define your argument schema once, and SnapHelp generates:
16
+
17
+ 1. **Commandline contract** - `requiredArgs` and `optionalArgs`
18
+ 2. **Help specification** - Formatted help arguments
19
+ 3. **Full help contract** - Complete help metadata
20
+
21
+ ## API Reference
22
+
23
+ ### `defineArgSchema(schema)`
24
+
25
+ Defines a typed argument schema map.
26
+
27
+ ```typescript
28
+ const schema = SnapHelp.defineArgSchema({
29
+ name: {
30
+ description: 'User name',
31
+ required: true,
32
+ example: '--name=Alice'
33
+ },
34
+ verbose: {
35
+ description: 'Enable verbose output',
36
+ required: false,
37
+ example: '--verbose'
38
+ }
39
+ });
40
+ ```
41
+
42
+ **Schema Options:**
43
+
44
+ | Option | Type | Default | Description |
45
+ |--------|------|---------|-------------|
46
+ | `description` | `string` | **required** | Human-readable description |
47
+ | `required` | `boolean` | `false` | Whether the argument is required |
48
+ | `example` | `string` | `undefined` | Example usage string |
49
+ | `includeInCommandline` | `boolean` | `true` | Include in commandline contract |
50
+ | `includeInHelp` | `boolean` | `true` | Include in help output |
51
+ | `helpName` | `string` | *(key name)* | Override display name in help |
52
+
53
+ ### `commandlineFromArgSchema(schema)`
54
+
55
+ Generates a `CommandlineContract` from an arg schema.
56
+
57
+ ```typescript
58
+ const commandline = SnapHelp.commandlineFromArgSchema(schema);
59
+ // Returns: { requiredArgs: string[], optionalArgs: string[] }
60
+ ```
61
+
62
+ **Usage Example:**
63
+
64
+ ```typescript
65
+ const DEPLOY_ARG_SCHEMA = SnapHelp.defineArgSchema({
66
+ environment: {
67
+ description: 'Target deployment environment',
68
+ required: true,
69
+ example: '--environment=production'
70
+ },
71
+ region: {
72
+ description: 'AWS region',
73
+ required: false,
74
+ example: '--region=us-east-1'
75
+ }
76
+ });
77
+
78
+ const deployAction: ActionContract = {
79
+ // ...
80
+ commandline: SnapHelp.commandlineFromArgSchema(DEPLOY_ARG_SCHEMA),
81
+ // ...
82
+ };
83
+ ```
84
+
85
+ ### `helpArgsFromArgSchema(schema)`
86
+
87
+ Generates help argument specs from an arg schema.
88
+
89
+ ```typescript
90
+ const helpArgs = SnapHelp.helpArgsFromArgSchema(schema);
91
+ // Returns: HelpArgumentSpec[]
92
+ ```
93
+
94
+ **Usage Example:**
95
+
96
+ ```typescript
97
+ const helpArgs = SnapHelp.helpArgsFromArgSchema(DEPLOY_ARG_SCHEMA);
98
+ // Returns:
99
+ // [
100
+ // { name: 'environment', required: true, description: '...', example: '...' },
101
+ // { name: 'region', required: false, description: '...', example: '...' }
102
+ // ]
103
+ ```
104
+
105
+ ### `buildHelpFromArgSchema(input)`
106
+
107
+ Generates a complete `HelpContract` from an arg schema.
108
+
109
+ ```typescript
110
+ const help = SnapHelp.buildHelpFromArgSchema({
111
+ summary: 'Deploy application to environment',
112
+ argSchema: DEPLOY_ARG_SCHEMA,
113
+ examples: ['mytool deploy --environment=production'],
114
+ useCases: [
115
+ {
116
+ name: 'production',
117
+ description: 'Deploy to production',
118
+ command: 'mytool deploy --environment=production'
119
+ }
120
+ ],
121
+ keybindings: ['Enter confirm', 'Esc cancel']
122
+ });
123
+ ```
124
+
125
+ **Input Options:**
126
+
127
+ | Option | Type | Required | Description |
128
+ |--------|------|----------|-------------|
129
+ | `summary` | `string` | Yes | Short action description |
130
+ | `argSchema` | `ArgSchemaMap` | Yes | Argument definition schema |
131
+ | `examples` | `string[]` | No | Command examples |
132
+ | `useCases` | `HelpUseCaseSpec[]` | No | Detailed use cases |
133
+ | `keybindings` | `string[]` | No | Key bindings (defaults to Enter/Esc) |
134
+
135
+ ## Complete Example
136
+
137
+ Here's a complete module using `SnapHelp` helpers:
138
+
139
+ ```typescript
140
+ import type { ModuleContract } from 'snap-framework';
141
+ import { ExitCode } from 'snap-framework';
142
+ import * as SnapHelp from 'snap-framework';
143
+
144
+ // Define argument schema once
145
+ const DEPLOY_ARG_SCHEMA = SnapHelp.defineArgSchema({
146
+ environment: {
147
+ description: 'Target deployment environment',
148
+ required: true,
149
+ example: '--environment=production'
150
+ },
151
+ region: {
152
+ description: 'AWS region for deployment',
153
+ required: false,
154
+ example: '--region=us-east-1'
155
+ },
156
+ 'skip-tests': {
157
+ description: 'Skip pre-deployment tests',
158
+ required: false,
159
+ example: '--skip-tests',
160
+ helpName: 'skip-tests'
161
+ },
162
+ rollback: {
163
+ description: 'Enable automatic rollback on failure',
164
+ required: false,
165
+ example: '--rollback=true'
166
+ }
167
+ });
168
+
169
+ // Generate help from schema
170
+ const DEPLOY_HELP = SnapHelp.buildHelpFromArgSchema({
171
+ summary: 'Deploy application to the specified environment with optional configuration.',
172
+ argSchema: DEPLOY_ARG_SCHEMA,
173
+ examples: [
174
+ 'mytool deploy start --environment=production',
175
+ 'mytool deploy start --environment=staging --region=us-west-2',
176
+ 'mytool deploy start --environment=production --skip-tests --rollback=true'
177
+ ],
178
+ useCases: [
179
+ {
180
+ name: 'production',
181
+ description: 'Standard production deployment',
182
+ command: 'mytool deploy start --environment=production'
183
+ },
184
+ {
185
+ name: 'staging-with-region',
186
+ description: 'Deploy to staging in specific region',
187
+ command: 'mytool deploy start --environment=staging --region=us-west-2'
188
+ },
189
+ {
190
+ name: 'fast-deploy',
191
+ description: 'Quick deployment skipping tests',
192
+ command: 'mytool deploy start --environment=production --skip-tests'
193
+ }
194
+ ]
195
+ });
196
+
197
+ const deployModule: ModuleContract = {
198
+ moduleId: 'deploy',
199
+ description: 'Deployment management',
200
+ actions: [
201
+ {
202
+ actionId: 'start',
203
+ description: 'Start a deployment',
204
+ tui: {
205
+ steps: ['collect-environment', 'collect-options', 'confirm']
206
+ },
207
+ // Generate commandline from schema
208
+ commandline: SnapHelp.commandlineFromArgSchema(DEPLOY_ARG_SCHEMA),
209
+ // Use pre-built help
210
+ help: DEPLOY_HELP,
211
+ run: async (context) => {
212
+ const { environment, region, skipTests, rollback } = context.args;
213
+
214
+ context.terminal.line(`Deploying to ${environment}...`);
215
+
216
+ if (region) {
217
+ context.terminal.line(`Region: ${region}`);
218
+ }
219
+
220
+ if (skipTests) {
221
+ context.terminal.line('Skipping pre-deployment tests');
222
+ }
223
+
224
+ if (rollback) {
225
+ context.terminal.line('Automatic rollback enabled');
226
+ }
227
+
228
+ // Deployment logic here...
229
+
230
+ return {
231
+ ok: true,
232
+ mode: context.mode,
233
+ exitCode: ExitCode.SUCCESS,
234
+ data: `Deployed to ${environment}`
235
+ };
236
+ }
237
+ }
238
+ ]
239
+ };
240
+
241
+ export default deployModule;
242
+ ```
243
+
244
+ ## Advanced Patterns
245
+
246
+ ### Schema Composition
247
+
248
+ Combine schemas for reusability:
249
+
250
+ ```typescript
251
+ const COMMON_ARGS = SnapHelp.defineArgSchema({
252
+ verbose: {
253
+ description: 'Enable verbose logging',
254
+ required: false,
255
+ example: '--verbose'
256
+ },
257
+ 'dry-run': {
258
+ description: 'Show what would be done without making changes',
259
+ required: false,
260
+ example: '--dry-run',
261
+ helpName: 'dry-run'
262
+ }
263
+ });
264
+
265
+ const DEPLOY_SPECIFIC_ARGS = SnapHelp.defineArgSchema({
266
+ environment: {
267
+ description: 'Target environment',
268
+ required: true,
269
+ example: '--environment=production'
270
+ }
271
+ });
272
+
273
+ // Combine for action help
274
+ const DEPLOY_HELP = SnapHelp.buildHelpFromArgSchema({
275
+ summary: 'Deploy to environment',
276
+ argSchema: { ...COMMON_ARGS, ...DEPLOY_SPECIFIC_ARGS },
277
+ examples: ['mytool deploy --environment=production --verbose']
278
+ });
279
+ ```
280
+
281
+ ### Conditional Schema Inclusion
282
+
283
+ Use `includeInCommandline` or `includeInHelp` to control where arguments appear:
284
+
285
+ ```typescript
286
+ const SCHEMA = SnapHelp.defineArgSchema({
287
+ // Appears in both commandline and help
288
+ name: {
289
+ description: 'Resource name',
290
+ required: true
291
+ },
292
+
293
+ // Only in help (e.g., for documentation)
294
+ 'api-key': {
295
+ description: 'API key for authentication',
296
+ required: false,
297
+ includeInCommandline: false // Not part of commandline contract
298
+ },
299
+
300
+ // Only in commandline (e.g., internal flag)
301
+ _internal: {
302
+ description: 'Internal flag',
303
+ required: false,
304
+ includeInHelp: false // Not shown in help
305
+ }
306
+ });
307
+ ```
308
+
309
+ ### Custom Help Names
310
+
311
+ Use `helpName` to override the display name in help:
312
+
313
+ ```typescript
314
+ const SCHEMA = SnapHelp.defineArgSchema({
315
+ 'skip-tests': {
316
+ description: 'Skip pre-deployment tests',
317
+ required: false,
318
+ helpName: 'skip-tests' // Use kebab-case in help
319
+ }
320
+ });
321
+ ```
322
+
323
+ ## Migration Pattern
324
+
325
+ Migrate existing actions to use SnapHelp:
326
+
327
+ **Before:**
328
+
329
+ ```typescript
330
+ {
331
+ commandline: {
332
+ requiredArgs: ['environment', 'region'],
333
+ optionalArgs: ['verbose', 'force']
334
+ },
335
+ help: {
336
+ summary: 'Deploy to environment',
337
+ args: [
338
+ { name: 'environment', required: true, description: '...' },
339
+ { name: 'region', required: true, description: '...' },
340
+ // ... more args
341
+ ]
342
+ }
343
+ }
344
+ ```
345
+
346
+ **After:**
347
+
348
+ ```typescript
349
+ const SCHEMA = SnapHelp.defineArgSchema({
350
+ environment: { description: '...', required: true },
351
+ region: { description: '...', required: true },
352
+ verbose: { description: '...', required: false },
353
+ force: { description: '...', required: false }
354
+ });
355
+
356
+ {
357
+ commandline: SnapHelp.commandlineFromArgSchema(SCHEMA),
358
+ help: SnapHelp.buildHelpFromArgSchema({
359
+ summary: 'Deploy to environment',
360
+ argSchema: SCHEMA
361
+ })
362
+ }
363
+ ```
364
+
365
+ ## Best Practices
366
+
367
+ 1. **Define schemas at module level** - Share schemas across actions
368
+ 2. **Use descriptive examples** - Help users understand expected values
369
+ 3. **Provide use cases** - Show real-world usage patterns
370
+ 4. **Consistent naming** - Use kebab-case for argument names
371
+ 5. **Document all flags** - Even boolean flags need descriptions
372
+ 6. **Use helpName** - When the arg key differs from display name