openspec-cn 0.23.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 (235) hide show
  1. package/LICENSE +22 -0
  2. package/README.md +153 -0
  3. package/bin/openspec.js +3 -0
  4. package/dist/cli/index.d.ts +2 -0
  5. package/dist/cli/index.js +480 -0
  6. package/dist/commands/change.d.ts +35 -0
  7. package/dist/commands/change.js +277 -0
  8. package/dist/commands/completion.d.ts +72 -0
  9. package/dist/commands/completion.js +257 -0
  10. package/dist/commands/config.d.ts +8 -0
  11. package/dist/commands/config.js +198 -0
  12. package/dist/commands/feedback.d.ts +9 -0
  13. package/dist/commands/feedback.js +183 -0
  14. package/dist/commands/schema.d.ts +6 -0
  15. package/dist/commands/schema.js +869 -0
  16. package/dist/commands/show.d.ts +14 -0
  17. package/dist/commands/show.js +132 -0
  18. package/dist/commands/spec.d.ts +15 -0
  19. package/dist/commands/spec.js +225 -0
  20. package/dist/commands/validate.d.ts +24 -0
  21. package/dist/commands/validate.js +294 -0
  22. package/dist/commands/workflow/index.d.ts +17 -0
  23. package/dist/commands/workflow/index.js +12 -0
  24. package/dist/commands/workflow/instructions.d.ts +29 -0
  25. package/dist/commands/workflow/instructions.js +381 -0
  26. package/dist/commands/workflow/new-change.d.ts +11 -0
  27. package/dist/commands/workflow/new-change.js +44 -0
  28. package/dist/commands/workflow/schemas.d.ts +10 -0
  29. package/dist/commands/workflow/schemas.js +34 -0
  30. package/dist/commands/workflow/shared.d.ts +52 -0
  31. package/dist/commands/workflow/shared.js +111 -0
  32. package/dist/commands/workflow/status.d.ts +14 -0
  33. package/dist/commands/workflow/status.js +58 -0
  34. package/dist/commands/workflow/templates.d.ts +16 -0
  35. package/dist/commands/workflow/templates.js +68 -0
  36. package/dist/core/archive.d.ts +11 -0
  37. package/dist/core/archive.js +280 -0
  38. package/dist/core/artifact-graph/graph.d.ts +56 -0
  39. package/dist/core/artifact-graph/graph.js +141 -0
  40. package/dist/core/artifact-graph/index.d.ts +7 -0
  41. package/dist/core/artifact-graph/index.js +13 -0
  42. package/dist/core/artifact-graph/instruction-loader.d.ts +143 -0
  43. package/dist/core/artifact-graph/instruction-loader.js +214 -0
  44. package/dist/core/artifact-graph/resolver.d.ts +81 -0
  45. package/dist/core/artifact-graph/resolver.js +257 -0
  46. package/dist/core/artifact-graph/schema.d.ts +13 -0
  47. package/dist/core/artifact-graph/schema.js +108 -0
  48. package/dist/core/artifact-graph/state.d.ts +12 -0
  49. package/dist/core/artifact-graph/state.js +54 -0
  50. package/dist/core/artifact-graph/types.d.ts +45 -0
  51. package/dist/core/artifact-graph/types.js +43 -0
  52. package/dist/core/command-generation/adapters/amazon-q.d.ts +13 -0
  53. package/dist/core/command-generation/adapters/amazon-q.js +26 -0
  54. package/dist/core/command-generation/adapters/antigravity.d.ts +13 -0
  55. package/dist/core/command-generation/adapters/antigravity.js +26 -0
  56. package/dist/core/command-generation/adapters/auggie.d.ts +13 -0
  57. package/dist/core/command-generation/adapters/auggie.js +27 -0
  58. package/dist/core/command-generation/adapters/claude.d.ts +13 -0
  59. package/dist/core/command-generation/adapters/claude.js +50 -0
  60. package/dist/core/command-generation/adapters/cline.d.ts +14 -0
  61. package/dist/core/command-generation/adapters/cline.js +27 -0
  62. package/dist/core/command-generation/adapters/codebuddy.d.ts +13 -0
  63. package/dist/core/command-generation/adapters/codebuddy.js +28 -0
  64. package/dist/core/command-generation/adapters/codex.d.ts +13 -0
  65. package/dist/core/command-generation/adapters/codex.js +27 -0
  66. package/dist/core/command-generation/adapters/continue.d.ts +13 -0
  67. package/dist/core/command-generation/adapters/continue.js +28 -0
  68. package/dist/core/command-generation/adapters/costrict.d.ts +13 -0
  69. package/dist/core/command-generation/adapters/costrict.js +27 -0
  70. package/dist/core/command-generation/adapters/crush.d.ts +13 -0
  71. package/dist/core/command-generation/adapters/crush.js +30 -0
  72. package/dist/core/command-generation/adapters/cursor.d.ts +14 -0
  73. package/dist/core/command-generation/adapters/cursor.js +44 -0
  74. package/dist/core/command-generation/adapters/factory.d.ts +13 -0
  75. package/dist/core/command-generation/adapters/factory.js +27 -0
  76. package/dist/core/command-generation/adapters/gemini.d.ts +13 -0
  77. package/dist/core/command-generation/adapters/gemini.js +26 -0
  78. package/dist/core/command-generation/adapters/github-copilot.d.ts +13 -0
  79. package/dist/core/command-generation/adapters/github-copilot.js +26 -0
  80. package/dist/core/command-generation/adapters/iflow.d.ts +13 -0
  81. package/dist/core/command-generation/adapters/iflow.js +29 -0
  82. package/dist/core/command-generation/adapters/index.d.ts +27 -0
  83. package/dist/core/command-generation/adapters/index.js +27 -0
  84. package/dist/core/command-generation/adapters/kilocode.d.ts +14 -0
  85. package/dist/core/command-generation/adapters/kilocode.js +23 -0
  86. package/dist/core/command-generation/adapters/opencode.d.ts +13 -0
  87. package/dist/core/command-generation/adapters/opencode.js +26 -0
  88. package/dist/core/command-generation/adapters/qoder.d.ts +13 -0
  89. package/dist/core/command-generation/adapters/qoder.js +30 -0
  90. package/dist/core/command-generation/adapters/qwen.d.ts +13 -0
  91. package/dist/core/command-generation/adapters/qwen.js +26 -0
  92. package/dist/core/command-generation/adapters/roocode.d.ts +14 -0
  93. package/dist/core/command-generation/adapters/roocode.js +27 -0
  94. package/dist/core/command-generation/adapters/windsurf.d.ts +14 -0
  95. package/dist/core/command-generation/adapters/windsurf.js +51 -0
  96. package/dist/core/command-generation/generator.d.ts +21 -0
  97. package/dist/core/command-generation/generator.js +27 -0
  98. package/dist/core/command-generation/index.d.ts +22 -0
  99. package/dist/core/command-generation/index.js +24 -0
  100. package/dist/core/command-generation/registry.d.ts +36 -0
  101. package/dist/core/command-generation/registry.js +88 -0
  102. package/dist/core/command-generation/types.d.ts +55 -0
  103. package/dist/core/command-generation/types.js +8 -0
  104. package/dist/core/completions/command-registry.d.ts +7 -0
  105. package/dist/core/completions/command-registry.js +456 -0
  106. package/dist/core/completions/completion-provider.d.ts +60 -0
  107. package/dist/core/completions/completion-provider.js +102 -0
  108. package/dist/core/completions/factory.d.ts +64 -0
  109. package/dist/core/completions/factory.js +75 -0
  110. package/dist/core/completions/generators/bash-generator.d.ts +32 -0
  111. package/dist/core/completions/generators/bash-generator.js +174 -0
  112. package/dist/core/completions/generators/fish-generator.d.ts +32 -0
  113. package/dist/core/completions/generators/fish-generator.js +157 -0
  114. package/dist/core/completions/generators/powershell-generator.d.ts +33 -0
  115. package/dist/core/completions/generators/powershell-generator.js +207 -0
  116. package/dist/core/completions/generators/zsh-generator.d.ts +44 -0
  117. package/dist/core/completions/generators/zsh-generator.js +250 -0
  118. package/dist/core/completions/installers/bash-installer.d.ts +87 -0
  119. package/dist/core/completions/installers/bash-installer.js +318 -0
  120. package/dist/core/completions/installers/fish-installer.d.ts +43 -0
  121. package/dist/core/completions/installers/fish-installer.js +143 -0
  122. package/dist/core/completions/installers/powershell-installer.d.ts +88 -0
  123. package/dist/core/completions/installers/powershell-installer.js +327 -0
  124. package/dist/core/completions/installers/zsh-installer.d.ts +125 -0
  125. package/dist/core/completions/installers/zsh-installer.js +449 -0
  126. package/dist/core/completions/templates/bash-templates.d.ts +6 -0
  127. package/dist/core/completions/templates/bash-templates.js +24 -0
  128. package/dist/core/completions/templates/fish-templates.d.ts +7 -0
  129. package/dist/core/completions/templates/fish-templates.js +39 -0
  130. package/dist/core/completions/templates/powershell-templates.d.ts +6 -0
  131. package/dist/core/completions/templates/powershell-templates.js +25 -0
  132. package/dist/core/completions/templates/zsh-templates.d.ts +6 -0
  133. package/dist/core/completions/templates/zsh-templates.js +36 -0
  134. package/dist/core/completions/types.d.ts +79 -0
  135. package/dist/core/completions/types.js +2 -0
  136. package/dist/core/config-prompts.d.ts +9 -0
  137. package/dist/core/config-prompts.js +34 -0
  138. package/dist/core/config-schema.d.ts +76 -0
  139. package/dist/core/config-schema.js +200 -0
  140. package/dist/core/config.d.ts +17 -0
  141. package/dist/core/config.js +30 -0
  142. package/dist/core/converters/json-converter.d.ts +6 -0
  143. package/dist/core/converters/json-converter.js +51 -0
  144. package/dist/core/global-config.d.ts +39 -0
  145. package/dist/core/global-config.js +115 -0
  146. package/dist/core/index.d.ts +2 -0
  147. package/dist/core/index.js +3 -0
  148. package/dist/core/init.d.ts +32 -0
  149. package/dist/core/init.js +433 -0
  150. package/dist/core/legacy-cleanup.d.ts +162 -0
  151. package/dist/core/legacy-cleanup.js +501 -0
  152. package/dist/core/list.d.ts +9 -0
  153. package/dist/core/list.js +171 -0
  154. package/dist/core/parsers/change-parser.d.ts +13 -0
  155. package/dist/core/parsers/change-parser.js +193 -0
  156. package/dist/core/parsers/markdown-parser.d.ts +22 -0
  157. package/dist/core/parsers/markdown-parser.js +187 -0
  158. package/dist/core/parsers/requirement-blocks.d.ts +37 -0
  159. package/dist/core/parsers/requirement-blocks.js +201 -0
  160. package/dist/core/project-config.d.ts +64 -0
  161. package/dist/core/project-config.js +223 -0
  162. package/dist/core/schemas/base.schema.d.ts +13 -0
  163. package/dist/core/schemas/base.schema.js +13 -0
  164. package/dist/core/schemas/change.schema.d.ts +73 -0
  165. package/dist/core/schemas/change.schema.js +31 -0
  166. package/dist/core/schemas/index.d.ts +4 -0
  167. package/dist/core/schemas/index.js +4 -0
  168. package/dist/core/schemas/spec.schema.d.ts +18 -0
  169. package/dist/core/schemas/spec.schema.js +15 -0
  170. package/dist/core/shared/index.d.ts +8 -0
  171. package/dist/core/shared/index.js +8 -0
  172. package/dist/core/shared/skill-generation.d.ts +41 -0
  173. package/dist/core/shared/skill-generation.js +74 -0
  174. package/dist/core/shared/tool-detection.d.ts +66 -0
  175. package/dist/core/shared/tool-detection.js +140 -0
  176. package/dist/core/specs-apply.d.ts +73 -0
  177. package/dist/core/specs-apply.js +384 -0
  178. package/dist/core/styles/palette.d.ts +7 -0
  179. package/dist/core/styles/palette.js +8 -0
  180. package/dist/core/templates/index.d.ts +8 -0
  181. package/dist/core/templates/index.js +9 -0
  182. package/dist/core/templates/skill-templates.d.ts +112 -0
  183. package/dist/core/templates/skill-templates.js +2893 -0
  184. package/dist/core/update.d.ts +42 -0
  185. package/dist/core/update.js +306 -0
  186. package/dist/core/validation/constants.d.ts +34 -0
  187. package/dist/core/validation/constants.js +40 -0
  188. package/dist/core/validation/types.d.ts +18 -0
  189. package/dist/core/validation/types.js +2 -0
  190. package/dist/core/validation/validator.d.ts +33 -0
  191. package/dist/core/validation/validator.js +409 -0
  192. package/dist/core/view.d.ts +8 -0
  193. package/dist/core/view.js +168 -0
  194. package/dist/index.d.ts +3 -0
  195. package/dist/index.js +3 -0
  196. package/dist/prompts/searchable-multi-select.d.ts +27 -0
  197. package/dist/prompts/searchable-multi-select.js +149 -0
  198. package/dist/telemetry/config.d.ts +32 -0
  199. package/dist/telemetry/config.js +68 -0
  200. package/dist/telemetry/index.d.ts +31 -0
  201. package/dist/telemetry/index.js +145 -0
  202. package/dist/ui/ascii-patterns.d.ts +16 -0
  203. package/dist/ui/ascii-patterns.js +133 -0
  204. package/dist/ui/welcome-screen.d.ts +10 -0
  205. package/dist/ui/welcome-screen.js +146 -0
  206. package/dist/utils/change-metadata.d.ts +51 -0
  207. package/dist/utils/change-metadata.js +147 -0
  208. package/dist/utils/change-utils.d.ts +62 -0
  209. package/dist/utils/change-utils.js +121 -0
  210. package/dist/utils/file-system.d.ts +36 -0
  211. package/dist/utils/file-system.js +281 -0
  212. package/dist/utils/index.d.ts +5 -0
  213. package/dist/utils/index.js +7 -0
  214. package/dist/utils/interactive.d.ts +18 -0
  215. package/dist/utils/interactive.js +21 -0
  216. package/dist/utils/item-discovery.d.ts +4 -0
  217. package/dist/utils/item-discovery.js +72 -0
  218. package/dist/utils/match.d.ts +3 -0
  219. package/dist/utils/match.js +22 -0
  220. package/dist/utils/shell-detection.d.ts +20 -0
  221. package/dist/utils/shell-detection.js +41 -0
  222. package/dist/utils/task-progress.d.ts +8 -0
  223. package/dist/utils/task-progress.js +36 -0
  224. package/package.json +84 -0
  225. package/schemas/spec-driven/schema.yaml +148 -0
  226. package/schemas/spec-driven/templates/design.md +19 -0
  227. package/schemas/spec-driven/templates/proposal.md +23 -0
  228. package/schemas/spec-driven/templates/spec.md +8 -0
  229. package/schemas/spec-driven/templates/tasks.md +9 -0
  230. package/schemas/tdd/schema.yaml +213 -0
  231. package/schemas/tdd/templates/docs.md +15 -0
  232. package/schemas/tdd/templates/implementation.md +11 -0
  233. package/schemas/tdd/templates/spec.md +11 -0
  234. package/schemas/tdd/templates/test.md +11 -0
  235. package/scripts/postinstall.js +147 -0
@@ -0,0 +1,146 @@
1
+ /**
2
+ * Animated welcome screen for the experimental artifact workflow setup.
3
+ * Shows side-by-side layout with animated ASCII art on left and welcome text on right.
4
+ */
5
+ import chalk from 'chalk';
6
+ import { WELCOME_ANIMATION } from './ascii-patterns.js';
7
+ // Minimum terminal width for side-by-side layout
8
+ const MIN_WIDTH = 60;
9
+ // Width of the ASCII art column (with padding)
10
+ const ART_COLUMN_WIDTH = 24;
11
+ /**
12
+ * Welcome text content (right column)
13
+ */
14
+ function getWelcomeText() {
15
+ return [
16
+ chalk.white.bold('Welcome to OpenSpec'),
17
+ chalk.dim('A lightweight spec-driven framework'),
18
+ '',
19
+ chalk.white('This setup will configure:'),
20
+ chalk.dim(' • Agent Skills for AI tools'),
21
+ chalk.dim(' • /opsx:* slash commands'),
22
+ '',
23
+ chalk.white('Quick start after setup:'),
24
+ ` ${chalk.yellow('/opsx:new')} ${chalk.dim('Create a change')}`,
25
+ ` ${chalk.yellow('/opsx:continue')} ${chalk.dim('Next artifact')}`,
26
+ ` ${chalk.yellow('/opsx:apply')} ${chalk.dim('Implement tasks')}`,
27
+ '',
28
+ chalk.cyan('Press Enter to select tools...'),
29
+ ];
30
+ }
31
+ /**
32
+ * Renders a single frame with side-by-side layout
33
+ */
34
+ function renderFrame(artLines, textLines) {
35
+ const maxLines = Math.max(artLines.length, textLines.length);
36
+ const lines = [];
37
+ for (let i = 0; i < maxLines; i++) {
38
+ const artLine = artLines[i] || '';
39
+ const textLine = textLines[i] || '';
40
+ // Pad the art column to fixed width
41
+ const paddedArt = artLine.padEnd(ART_COLUMN_WIDTH);
42
+ // Color the ASCII art with cyan for visual appeal
43
+ const coloredArt = chalk.cyan(paddedArt);
44
+ // Clear line before writing to prevent residual characters
45
+ lines.push(`\x1b[2K${coloredArt}${textLine}`);
46
+ }
47
+ return lines.join('\n');
48
+ }
49
+ /**
50
+ * Checks if the terminal supports animation
51
+ */
52
+ function canAnimate() {
53
+ // Must be TTY
54
+ if (!process.stdout.isTTY)
55
+ return false;
56
+ // Respect NO_COLOR
57
+ if (process.env.NO_COLOR)
58
+ return false;
59
+ // Check terminal width
60
+ const columns = process.stdout.columns || 80;
61
+ if (columns < MIN_WIDTH)
62
+ return false;
63
+ return true;
64
+ }
65
+ /**
66
+ * Wait for Enter key press
67
+ */
68
+ function waitForEnter() {
69
+ return new Promise((resolve) => {
70
+ const { stdin } = process;
71
+ // Handle non-TTY gracefully
72
+ if (!stdin.isTTY) {
73
+ resolve();
74
+ return;
75
+ }
76
+ const wasRaw = stdin.isRaw;
77
+ stdin.setRawMode(true);
78
+ stdin.resume();
79
+ const onData = (data) => {
80
+ const char = data.toString();
81
+ // Enter key or Ctrl+C
82
+ if (char === '\r' || char === '\n' || char === '\u0003') {
83
+ stdin.removeListener('data', onData);
84
+ stdin.setRawMode(wasRaw);
85
+ stdin.pause();
86
+ // Handle Ctrl+C
87
+ if (char === '\u0003') {
88
+ process.stdout.write('\n');
89
+ process.exit(0);
90
+ }
91
+ resolve();
92
+ }
93
+ };
94
+ stdin.on('data', onData);
95
+ });
96
+ }
97
+ /**
98
+ * Shows the animated welcome screen.
99
+ * Returns when user presses Enter.
100
+ */
101
+ export async function showWelcomeScreen() {
102
+ const textLines = getWelcomeText();
103
+ if (!canAnimate()) {
104
+ // Fallback: show static welcome
105
+ const frame = WELCOME_ANIMATION.frames[3]; // Peak frame
106
+ process.stdout.write('\n' + renderFrame(frame, textLines) + '\n\n');
107
+ return;
108
+ }
109
+ let frameIndex = 0;
110
+ let running = true;
111
+ let isFirstRender = true;
112
+ // Content height for cursor movement between frames
113
+ const numContentLines = Math.max(WELCOME_ANIMATION.frames[0].length, textLines.length);
114
+ const frameHeight = numContentLines + 1; // internal newlines (11) + trailing newlines (2) = 13
115
+ // Total height including initial newline (for cleanup)
116
+ const totalHeight = frameHeight + 1; // 14
117
+ // Initial render
118
+ process.stdout.write('\n');
119
+ // Animation loop
120
+ const interval = setInterval(() => {
121
+ if (!running)
122
+ return;
123
+ const frame = WELCOME_ANIMATION.frames[frameIndex];
124
+ // Move cursor up to overwrite previous frame (always after first render)
125
+ if (!isFirstRender) {
126
+ process.stdout.write(`\x1b[${frameHeight}A`);
127
+ }
128
+ isFirstRender = false;
129
+ // Render current frame
130
+ process.stdout.write(renderFrame(frame, textLines) + '\n\n');
131
+ // Advance to next frame
132
+ frameIndex = (frameIndex + 1) % WELCOME_ANIMATION.frames.length;
133
+ }, WELCOME_ANIMATION.interval);
134
+ // Wait for Enter
135
+ await waitForEnter();
136
+ // Stop animation
137
+ running = false;
138
+ clearInterval(interval);
139
+ // Clear the welcome screen and move on
140
+ process.stdout.write(`\x1b[${totalHeight}A`);
141
+ for (let i = 0; i < totalHeight; i++) {
142
+ process.stdout.write('\x1b[2K\n'); // Clear line
143
+ }
144
+ process.stdout.write(`\x1b[${totalHeight}A`); // Move back up
145
+ }
146
+ //# sourceMappingURL=welcome-screen.js.map
@@ -0,0 +1,51 @@
1
+ import { type ChangeMetadata } from '../core/artifact-graph/types.js';
2
+ /**
3
+ * Error thrown when change metadata validation fails.
4
+ */
5
+ export declare class ChangeMetadataError extends Error {
6
+ readonly metadataPath: string;
7
+ readonly cause?: Error | undefined;
8
+ constructor(message: string, metadataPath: string, cause?: Error | undefined);
9
+ }
10
+ /**
11
+ * Validates that a schema name is valid (exists in available schemas).
12
+ *
13
+ * @param schemaName - The schema name to validate
14
+ * @param projectRoot - Optional project root for project-local schema resolution
15
+ * @returns The validated schema name
16
+ * @throws Error if schema is not found
17
+ */
18
+ export declare function validateSchemaName(schemaName: string, projectRoot?: string): string;
19
+ /**
20
+ * Writes change metadata to .openspec.yaml in the change directory.
21
+ *
22
+ * @param changeDir - The path to the change directory
23
+ * @param metadata - The metadata to write
24
+ * @param projectRoot - Optional project root for project-local schema resolution
25
+ * @throws ChangeMetadataError if validation fails or write fails
26
+ */
27
+ export declare function writeChangeMetadata(changeDir: string, metadata: ChangeMetadata, projectRoot?: string): void;
28
+ /**
29
+ * Reads change metadata from .openspec.yaml in the change directory.
30
+ *
31
+ * @param changeDir - The path to the change directory
32
+ * @param projectRoot - Optional project root for project-local schema resolution
33
+ * @returns The validated metadata, or null if no metadata file exists
34
+ * @throws ChangeMetadataError if the file exists but is invalid
35
+ */
36
+ export declare function readChangeMetadata(changeDir: string, projectRoot?: string): ChangeMetadata | null;
37
+ /**
38
+ * Resolves the schema for a change, with explicit override taking precedence.
39
+ *
40
+ * Resolution order:
41
+ * 1. Explicit schema (if provided)
42
+ * 2. Schema from .openspec.yaml metadata (if exists)
43
+ * 3. Schema from openspec/config.yaml (if exists)
44
+ * 4. Default 'spec-driven'
45
+ *
46
+ * @param changeDir - The path to the change directory
47
+ * @param explicitSchema - Optional explicit schema override
48
+ * @returns The resolved schema name
49
+ */
50
+ export declare function resolveSchemaForChange(changeDir: string, explicitSchema?: string): string;
51
+ //# sourceMappingURL=change-metadata.d.ts.map
@@ -0,0 +1,147 @@
1
+ import * as fs from 'node:fs';
2
+ import * as path from 'node:path';
3
+ import * as yaml from 'yaml';
4
+ import { ChangeMetadataSchema } from '../core/artifact-graph/types.js';
5
+ import { listSchemas } from '../core/artifact-graph/resolver.js';
6
+ import { readProjectConfig } from '../core/project-config.js';
7
+ const METADATA_FILENAME = '.openspec.yaml';
8
+ /**
9
+ * Error thrown when change metadata validation fails.
10
+ */
11
+ export class ChangeMetadataError extends Error {
12
+ metadataPath;
13
+ cause;
14
+ constructor(message, metadataPath, cause) {
15
+ super(message);
16
+ this.metadataPath = metadataPath;
17
+ this.cause = cause;
18
+ this.name = 'ChangeMetadataError';
19
+ }
20
+ }
21
+ /**
22
+ * Validates that a schema name is valid (exists in available schemas).
23
+ *
24
+ * @param schemaName - The schema name to validate
25
+ * @param projectRoot - Optional project root for project-local schema resolution
26
+ * @returns The validated schema name
27
+ * @throws Error if schema is not found
28
+ */
29
+ export function validateSchemaName(schemaName, projectRoot) {
30
+ const availableSchemas = listSchemas(projectRoot);
31
+ if (!availableSchemas.includes(schemaName)) {
32
+ throw new Error(`Unknown schema '${schemaName}'. Available: ${availableSchemas.join(', ')}`);
33
+ }
34
+ return schemaName;
35
+ }
36
+ /**
37
+ * Writes change metadata to .openspec.yaml in the change directory.
38
+ *
39
+ * @param changeDir - The path to the change directory
40
+ * @param metadata - The metadata to write
41
+ * @param projectRoot - Optional project root for project-local schema resolution
42
+ * @throws ChangeMetadataError if validation fails or write fails
43
+ */
44
+ export function writeChangeMetadata(changeDir, metadata, projectRoot) {
45
+ const metaPath = path.join(changeDir, METADATA_FILENAME);
46
+ // Validate schema exists
47
+ validateSchemaName(metadata.schema, projectRoot);
48
+ // Validate with Zod
49
+ const parseResult = ChangeMetadataSchema.safeParse(metadata);
50
+ if (!parseResult.success) {
51
+ throw new ChangeMetadataError(`Invalid metadata: ${parseResult.error.message}`, metaPath);
52
+ }
53
+ // Write YAML file
54
+ const content = yaml.stringify(parseResult.data);
55
+ try {
56
+ fs.writeFileSync(metaPath, content, 'utf-8');
57
+ }
58
+ catch (err) {
59
+ const ioError = err instanceof Error ? err : new Error(String(err));
60
+ throw new ChangeMetadataError(`Failed to write metadata: ${ioError.message}`, metaPath, ioError);
61
+ }
62
+ }
63
+ /**
64
+ * Reads change metadata from .openspec.yaml in the change directory.
65
+ *
66
+ * @param changeDir - The path to the change directory
67
+ * @param projectRoot - Optional project root for project-local schema resolution
68
+ * @returns The validated metadata, or null if no metadata file exists
69
+ * @throws ChangeMetadataError if the file exists but is invalid
70
+ */
71
+ export function readChangeMetadata(changeDir, projectRoot) {
72
+ const metaPath = path.join(changeDir, METADATA_FILENAME);
73
+ if (!fs.existsSync(metaPath)) {
74
+ return null;
75
+ }
76
+ let content;
77
+ try {
78
+ content = fs.readFileSync(metaPath, 'utf-8');
79
+ }
80
+ catch (err) {
81
+ const ioError = err instanceof Error ? err : new Error(String(err));
82
+ throw new ChangeMetadataError(`Failed to read metadata: ${ioError.message}`, metaPath, ioError);
83
+ }
84
+ let parsed;
85
+ try {
86
+ parsed = yaml.parse(content);
87
+ }
88
+ catch (err) {
89
+ const parseError = err instanceof Error ? err : new Error(String(err));
90
+ throw new ChangeMetadataError(`Invalid YAML in metadata file: ${parseError.message}`, metaPath, parseError);
91
+ }
92
+ // Validate with Zod
93
+ const parseResult = ChangeMetadataSchema.safeParse(parsed);
94
+ if (!parseResult.success) {
95
+ throw new ChangeMetadataError(`Invalid metadata: ${parseResult.error.message}`, metaPath);
96
+ }
97
+ // Validate that the schema exists
98
+ const availableSchemas = listSchemas(projectRoot);
99
+ if (!availableSchemas.includes(parseResult.data.schema)) {
100
+ throw new ChangeMetadataError(`Unknown schema '${parseResult.data.schema}'. Available: ${availableSchemas.join(', ')}`, metaPath);
101
+ }
102
+ return parseResult.data;
103
+ }
104
+ /**
105
+ * Resolves the schema for a change, with explicit override taking precedence.
106
+ *
107
+ * Resolution order:
108
+ * 1. Explicit schema (if provided)
109
+ * 2. Schema from .openspec.yaml metadata (if exists)
110
+ * 3. Schema from openspec/config.yaml (if exists)
111
+ * 4. Default 'spec-driven'
112
+ *
113
+ * @param changeDir - The path to the change directory
114
+ * @param explicitSchema - Optional explicit schema override
115
+ * @returns The resolved schema name
116
+ */
117
+ export function resolveSchemaForChange(changeDir, explicitSchema) {
118
+ // Derive project root from changeDir (changeDir is typically projectRoot/openspec/changes/change-name)
119
+ const projectRoot = path.resolve(changeDir, '../../..');
120
+ // 1. Explicit override wins
121
+ if (explicitSchema) {
122
+ return explicitSchema;
123
+ }
124
+ // 2. Try reading from metadata
125
+ try {
126
+ const metadata = readChangeMetadata(changeDir, projectRoot);
127
+ if (metadata?.schema) {
128
+ return metadata.schema;
129
+ }
130
+ }
131
+ catch {
132
+ // If metadata read fails, continue to next option
133
+ }
134
+ // 3. Try reading from project config
135
+ try {
136
+ const config = readProjectConfig(projectRoot);
137
+ if (config?.schema) {
138
+ return config.schema;
139
+ }
140
+ }
141
+ catch {
142
+ // If config read fails, fall back to default
143
+ }
144
+ // 4. Default
145
+ return 'spec-driven';
146
+ }
147
+ //# sourceMappingURL=change-metadata.js.map
@@ -0,0 +1,62 @@
1
+ /**
2
+ * Options for creating a change.
3
+ */
4
+ export interface CreateChangeOptions {
5
+ /** The workflow schema to use (default: 'spec-driven') */
6
+ schema?: string;
7
+ }
8
+ /**
9
+ * Result of creating a change.
10
+ */
11
+ export interface CreateChangeResult {
12
+ /** The schema that was actually used (resolved from options, config, or default) */
13
+ schema: string;
14
+ }
15
+ /**
16
+ * Result of validating a change name.
17
+ */
18
+ export interface ValidationResult {
19
+ valid: boolean;
20
+ error?: string;
21
+ }
22
+ /**
23
+ * Validates that a change name follows kebab-case conventions.
24
+ *
25
+ * Valid names:
26
+ * - Start with a lowercase letter
27
+ * - Contain only lowercase letters, numbers, and hyphens
28
+ * - Do not start or end with a hyphen
29
+ * - Do not contain consecutive hyphens
30
+ *
31
+ * @param name - The change name to validate
32
+ * @returns Validation result with `valid: true` or `valid: false` with an error message
33
+ *
34
+ * @example
35
+ * validateChangeName('add-auth') // { valid: true }
36
+ * validateChangeName('Add-Auth') // { valid: false, error: '...' }
37
+ */
38
+ export declare function validateChangeName(name: string): ValidationResult;
39
+ /**
40
+ * Creates a new change directory with metadata file.
41
+ *
42
+ * @param projectRoot - The root directory of the project (where `openspec/` lives)
43
+ * @param name - The change name (must be valid kebab-case)
44
+ * @param options - Optional settings for the change
45
+ * @throws Error if the change name is invalid
46
+ * @throws Error if the schema name is invalid
47
+ * @throws Error if the change directory already exists
48
+ *
49
+ * @returns Result containing the resolved schema name
50
+ *
51
+ * @example
52
+ * // Creates openspec/changes/add-auth/ with default schema
53
+ * const result = await createChange('/path/to/project', 'add-auth')
54
+ * console.log(result.schema) // 'spec-driven' or value from config
55
+ *
56
+ * @example
57
+ * // Creates openspec/changes/add-auth/ with TDD schema
58
+ * const result = await createChange('/path/to/project', 'add-auth', { schema: 'tdd' })
59
+ * console.log(result.schema) // 'tdd'
60
+ */
61
+ export declare function createChange(projectRoot: string, name: string, options?: CreateChangeOptions): Promise<CreateChangeResult>;
62
+ //# sourceMappingURL=change-utils.d.ts.map
@@ -0,0 +1,121 @@
1
+ import path from 'path';
2
+ import { FileSystemUtils } from './file-system.js';
3
+ import { writeChangeMetadata, validateSchemaName } from './change-metadata.js';
4
+ import { readProjectConfig } from '../core/project-config.js';
5
+ const DEFAULT_SCHEMA = 'spec-driven';
6
+ /**
7
+ * Validates that a change name follows kebab-case conventions.
8
+ *
9
+ * Valid names:
10
+ * - Start with a lowercase letter
11
+ * - Contain only lowercase letters, numbers, and hyphens
12
+ * - Do not start or end with a hyphen
13
+ * - Do not contain consecutive hyphens
14
+ *
15
+ * @param name - The change name to validate
16
+ * @returns Validation result with `valid: true` or `valid: false` with an error message
17
+ *
18
+ * @example
19
+ * validateChangeName('add-auth') // { valid: true }
20
+ * validateChangeName('Add-Auth') // { valid: false, error: '...' }
21
+ */
22
+ export function validateChangeName(name) {
23
+ // Pattern: starts with lowercase letter, followed by lowercase letters/numbers,
24
+ // optionally followed by hyphen + lowercase letters/numbers (repeatable)
25
+ const kebabCasePattern = /^[a-z][a-z0-9]*(-[a-z0-9]+)*$/;
26
+ if (!name) {
27
+ return { valid: false, error: 'Change name cannot be empty' };
28
+ }
29
+ if (!kebabCasePattern.test(name)) {
30
+ // Provide specific error messages for common mistakes
31
+ if (/[A-Z]/.test(name)) {
32
+ return { valid: false, error: 'Change name must be lowercase (use kebab-case)' };
33
+ }
34
+ if (/\s/.test(name)) {
35
+ return { valid: false, error: 'Change name cannot contain spaces (use hyphens instead)' };
36
+ }
37
+ if (/_/.test(name)) {
38
+ return { valid: false, error: 'Change name cannot contain underscores (use hyphens instead)' };
39
+ }
40
+ if (name.startsWith('-')) {
41
+ return { valid: false, error: 'Change name cannot start with a hyphen' };
42
+ }
43
+ if (name.endsWith('-')) {
44
+ return { valid: false, error: 'Change name cannot end with a hyphen' };
45
+ }
46
+ if (/--/.test(name)) {
47
+ return { valid: false, error: 'Change name cannot contain consecutive hyphens' };
48
+ }
49
+ if (/[^a-z0-9-]/.test(name)) {
50
+ return { valid: false, error: 'Change name can only contain lowercase letters, numbers, and hyphens' };
51
+ }
52
+ if (/^[0-9]/.test(name)) {
53
+ return { valid: false, error: 'Change name must start with a letter' };
54
+ }
55
+ return { valid: false, error: 'Change name must follow kebab-case convention (e.g., add-auth, refactor-db)' };
56
+ }
57
+ return { valid: true };
58
+ }
59
+ /**
60
+ * Creates a new change directory with metadata file.
61
+ *
62
+ * @param projectRoot - The root directory of the project (where `openspec/` lives)
63
+ * @param name - The change name (must be valid kebab-case)
64
+ * @param options - Optional settings for the change
65
+ * @throws Error if the change name is invalid
66
+ * @throws Error if the schema name is invalid
67
+ * @throws Error if the change directory already exists
68
+ *
69
+ * @returns Result containing the resolved schema name
70
+ *
71
+ * @example
72
+ * // Creates openspec/changes/add-auth/ with default schema
73
+ * const result = await createChange('/path/to/project', 'add-auth')
74
+ * console.log(result.schema) // 'spec-driven' or value from config
75
+ *
76
+ * @example
77
+ * // Creates openspec/changes/add-auth/ with TDD schema
78
+ * const result = await createChange('/path/to/project', 'add-auth', { schema: 'tdd' })
79
+ * console.log(result.schema) // 'tdd'
80
+ */
81
+ export async function createChange(projectRoot, name, options = {}) {
82
+ // Validate the name first
83
+ const validation = validateChangeName(name);
84
+ if (!validation.valid) {
85
+ throw new Error(validation.error);
86
+ }
87
+ // Determine schema: explicit option → project config → hardcoded default
88
+ let schemaName;
89
+ if (options.schema) {
90
+ schemaName = options.schema;
91
+ }
92
+ else {
93
+ // Try to read from project config
94
+ try {
95
+ const config = readProjectConfig(projectRoot);
96
+ schemaName = config?.schema ?? DEFAULT_SCHEMA;
97
+ }
98
+ catch {
99
+ // If config read fails, use default
100
+ schemaName = DEFAULT_SCHEMA;
101
+ }
102
+ }
103
+ // Validate the resolved schema
104
+ validateSchemaName(schemaName, projectRoot);
105
+ // Build the change directory path
106
+ const changeDir = path.join(projectRoot, 'openspec', 'changes', name);
107
+ // Check if change already exists
108
+ if (await FileSystemUtils.directoryExists(changeDir)) {
109
+ throw new Error(`Change '${name}' already exists at ${changeDir}`);
110
+ }
111
+ // Create the directory (including parent directories if needed)
112
+ await FileSystemUtils.createDirectory(changeDir);
113
+ // Write metadata file with schema and creation date
114
+ const today = new Date().toISOString().split('T')[0];
115
+ writeChangeMetadata(changeDir, {
116
+ schema: schemaName,
117
+ created: today,
118
+ }, projectRoot);
119
+ return { schema: schemaName };
120
+ }
121
+ //# sourceMappingURL=change-utils.js.map
@@ -0,0 +1,36 @@
1
+ export declare class FileSystemUtils {
2
+ /**
3
+ * Converts a path to use forward slashes (POSIX style).
4
+ * Essential for cross-platform compatibility with glob libraries like fast-glob.
5
+ */
6
+ static toPosixPath(p: string): string;
7
+ private static isWindowsBasePath;
8
+ private static normalizeSegments;
9
+ static joinPath(basePath: string, ...segments: string[]): string;
10
+ static createDirectory(dirPath: string): Promise<void>;
11
+ static fileExists(filePath: string): Promise<boolean>;
12
+ /**
13
+ * Finds the first existing parent directory by walking up the directory tree.
14
+ * @param dirPath Starting directory path
15
+ * @returns The first existing directory path, or null if root is reached without finding one
16
+ */
17
+ private static findFirstExistingDirectory;
18
+ static canWriteFile(filePath: string): Promise<boolean>;
19
+ static directoryExists(dirPath: string): Promise<boolean>;
20
+ static writeFile(filePath: string, content: string): Promise<void>;
21
+ static readFile(filePath: string): Promise<string>;
22
+ static updateFileWithMarkers(filePath: string, content: string, startMarker: string, endMarker: string): Promise<void>;
23
+ static ensureWritePermissions(dirPath: string): Promise<boolean>;
24
+ }
25
+ /**
26
+ * Removes a marker block from file content.
27
+ * Only removes markers that are on their own lines (ignores inline mentions).
28
+ * Cleans up double blank lines that may result from removal.
29
+ *
30
+ * @param content - File content with markers
31
+ * @param startMarker - The start marker string
32
+ * @param endMarker - The end marker string
33
+ * @returns Content with marker block removed, or original content if markers not found/invalid
34
+ */
35
+ export declare function removeMarkerBlock(content: string, startMarker: string, endMarker: string): string;
36
+ //# sourceMappingURL=file-system.d.ts.map