-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Expand file tree
/
Copy pathchange-utils.ts
More file actions
163 lines (146 loc) · 5.27 KB
/
change-utils.ts
File metadata and controls
163 lines (146 loc) · 5.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import path from 'path';
import { FileSystemUtils } from './file-system.js';
import { writeChangeMetadata, validateSchemaName } from './change-metadata.js';
import { readProjectConfig } from '../core/project-config.js';
const DEFAULT_SCHEMA = 'spec-driven';
/**
* Options for creating a change.
*/
export interface CreateChangeOptions {
/** The workflow schema to use (default: 'spec-driven') */
schema?: string;
}
/**
* Result of creating a change.
*/
export interface CreateChangeResult {
/** The schema that was actually used (resolved from options, config, or default) */
schema: string;
}
/**
* Result of validating a change name.
*/
export interface ValidationResult {
valid: boolean;
error?: string;
}
/**
* Validates that a change name follows kebab-case conventions.
*
* Valid names:
* - Start with a lowercase letter
* - Contain only lowercase letters, numbers, hyphens, and dots
* - Do not start or end with a hyphen
* - Do not contain consecutive hyphens
*
* @param name - The change name to validate
* @returns Validation result with `valid: true` or `valid: false` with an error message
*
* @example
* validateChangeName('add-auth') // { valid: true }
* validateChangeName('Add-Auth') // { valid: false, error: '...' }
*/
export function validateChangeName(name: string): ValidationResult {
// Pattern: starts with lowercase letter, followed by lowercase letters/numbers,
// optionally followed by hyphen/dot + lowercase letters/numbers (repeatable)
const kebabCasePattern = /^[a-z][a-z0-9]*([-.][a-z0-9]+)*$/;
if (!name) {
return { valid: false, error: 'Change name cannot be empty' };
}
if (!kebabCasePattern.test(name)) {
// Provide specific error messages for common mistakes
if (/[A-Z]/.test(name)) {
return { valid: false, error: 'Change name must be lowercase (use kebab-case)' };
}
if (/\s/.test(name)) {
return { valid: false, error: 'Change name cannot contain spaces (use hyphens instead)' };
}
if (/_/.test(name)) {
return { valid: false, error: 'Change name cannot contain underscores (use hyphens instead)' };
}
if (name.startsWith('-')) {
return { valid: false, error: 'Change name cannot start with a hyphen' };
}
if (name.endsWith('-')) {
return { valid: false, error: 'Change name cannot end with a hyphen' };
}
if (/--/.test(name)) {
return { valid: false, error: 'Change name cannot contain consecutive hyphens' };
}
if (/\.\./.test(name)) {
return { valid: false, error: 'Change name cannot contain consecutive dots' };
}
if (/[^a-z0-9.\-]/.test(name)) {
return { valid: false, error: 'Change name can only contain lowercase letters, numbers, hyphens, and dots' };
}
if (/^[0-9]/.test(name)) {
return { valid: false, error: 'Change name must start with a letter' };
}
return { valid: false, error: 'Change name must follow kebab-case convention (e.g., add-auth, refactor-db)' };
}
return { valid: true };
}
/**
* Creates a new change directory with metadata file.
*
* @param projectRoot - The root directory of the project (where `openspec/` lives)
* @param name - The change name (must be valid kebab-case)
* @param options - Optional settings for the change
* @throws Error if the change name is invalid
* @throws Error if the schema name is invalid
* @throws Error if the change directory already exists
*
* @returns Result containing the resolved schema name
*
* @example
* // Creates openspec/changes/add-auth/ with default schema
* const result = await createChange('/path/to/project', 'add-auth')
* console.log(result.schema) // 'spec-driven' or value from config
*
* @example
* // Creates openspec/changes/add-auth/ with custom schema
* const result = await createChange('/path/to/project', 'add-auth', { schema: 'my-workflow' })
* console.log(result.schema) // 'my-workflow'
*/
export async function createChange(
projectRoot: string,
name: string,
options: CreateChangeOptions = {}
): Promise<CreateChangeResult> {
// Validate the name first
const validation = validateChangeName(name);
if (!validation.valid) {
throw new Error(validation.error);
}
// Determine schema: explicit option → project config → hardcoded default
let schemaName: string;
if (options.schema) {
schemaName = options.schema;
} else {
// Try to read from project config
try {
const config = readProjectConfig(projectRoot);
schemaName = config?.schema ?? DEFAULT_SCHEMA;
} catch {
// If config read fails, use default
schemaName = DEFAULT_SCHEMA;
}
}
// Validate the resolved schema
validateSchemaName(schemaName, projectRoot);
// Build the change directory path
const changeDir = path.join(projectRoot, 'openspec', 'changes', name);
// Check if change already exists
if (await FileSystemUtils.directoryExists(changeDir)) {
throw new Error(`Change '${name}' already exists at ${changeDir}`);
}
// Create the directory (including parent directories if needed)
await FileSystemUtils.createDirectory(changeDir);
// Write metadata file with schema and creation date
const today = new Date().toISOString().split('T')[0];
writeChangeMetadata(changeDir, {
schema: schemaName,
created: today,
}, projectRoot);
return { schema: schemaName };
}