Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion openspec/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,9 @@ openspec/changes/[descriptive-name]/
- Affected specs: [list capabilities that will change]
- Affected code: [list key files/systems]

# 3. Create patches showing spec changes
# 3. Create patches for ALL spec changes
# - For EXISTING capabilities: show what changes (+ for additions, - for removals)
# - For NEW capabilities: show entire new spec with + prefix on every line
patches/
└── [capability]/
└── spec.md.diff
Expand Down
104 changes: 104 additions & 0 deletions openspec/changes/add-init-command/design.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
# Technical Design for Init Command

## Architecture Overview

The init command follows a modular architecture with clear separation of concerns:

```
CLI Layer (src/cli/index.ts)
Core Logic (src/core/init.ts)
Templates (src/core/templates/)
File System Utils (src/utils/file-system.ts)
```

## Key Design Decisions

### 1. Template Management

**Decision**: Store templates as TypeScript modules rather than separate files
**Rationale**:
- Ensures templates are bundled with the compiled code
- Allows for dynamic content insertion
- Type-safe template handling
- No need for complex file path resolution

### 2. Interactive vs Non-Interactive Mode

**Decision**: Support both interactive (default) and non-interactive modes
**Rationale**:
- Interactive mode for developer experience
- Non-interactive for CI/CD and automation
- Flags: `--yes` to accept defaults, `--no-input` for full automation

### 3. Directory Structure Creation

**Decision**: Create all directories upfront, then populate files
**Rationale**:
- Fail fast if permissions issues
- Clear transaction boundary
- Easier to clean up on failure

### 4. Error Handling Strategy

**Decision**: Implement rollback on failure
**Rationale**:
- Prevent partial installations
- Clear error states
- Better user experience

## Implementation Details

### File System Operations

```typescript
// Atomic directory creation with rollback
interface InitTransaction {
createdPaths: string[];
rollback(): Promise<void>;
commit(): Promise<void>;
}
```

### Template System

```typescript
interface Template {
path: string;
content: string | ((context: ProjectContext) => string);
}

interface ProjectContext {
projectName: string;
description: string;
techStack: string[];
conventions: string;
}
```

### CLI Command Structure

```bash
openspec init [path] # Initialize in specified path (default: current directory)
--yes # Accept all defaults
--no-input # Skip all prompts
--force # Overwrite existing OpenSpec directory
--dry-run # Show what would be created
```

## Security Considerations

1. **Path Traversal**: Sanitize all user-provided paths
2. **File Permissions**: Check write permissions before starting
3. **Existing Files**: Never overwrite without explicit --force flag
4. **Template Injection**: Sanitize user inputs in templates

## Future Extensibility

The design supports future enhancements:
- Custom template sources
- Project type presets (API, web app, library)
- Migration from other documentation systems
- Integration with version control systems
66 changes: 66 additions & 0 deletions openspec/changes/add-init-command/patches/cli-init/spec.md.diff
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
+# CLI Init Specification
+
+## Purpose
+
+The `openspec init` command SHALL create a complete OpenSpec directory structure in any project, enabling immediate adoption of OpenSpec conventions.
+
+## Behavior
+
+### Directory Creation
+
+WHEN `openspec init` is executed
+THEN create the following directory structure:
+```
+openspec/
+├── project.md
+├── README.md
+├── specs/
+└── changes/
+ └── archive/
+```
+
+### File Generation
+
+The command SHALL generate:
+- `README.md` containing complete OpenSpec instructions for AI assistants
+- `project.md` with customizable project context template
+
+### Interactive Mode (Default)
+
+WHEN run without flags
+THEN prompt user for:
+- Project name
+- Project description
+- Technology stack
+- Key conventions
+
+### Non-Interactive Mode
+
+WHEN run with `--yes` flag
+THEN use sensible defaults for all prompts
+
+WHEN run with `--no-input` flag
+THEN skip all prompts and use minimal defaults
+
+### Safety Checks
+
+WHEN `openspec/` directory already exists
+THEN exit with error unless `--force` flag is provided
+
+WHEN `--force` flag is provided
+THEN backup existing directory before overwriting
+
+### Exit Codes
+
+- 0: Success
+- 1: OpenSpec directory already exists
+- 2: Insufficient permissions
+- 3: User cancelled operation
+
+## Why
+
+Manual creation of OpenSpec structure is error-prone and creates adoption friction. A standardized init command ensures:
+- Consistent structure across all projects
+- Proper AI instruction files are always included
+- Quick onboarding for new projects
+- Clear conventions from the start
25 changes: 25 additions & 0 deletions openspec/changes/add-init-command/proposal.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Add Init Command for OpenSpec

## Why

Projects need a simple way to adopt OpenSpec conventions. Currently, users must manually create the directory structure and understand all the conventions, which creates friction for adoption. An init command would enable instant OpenSpec setup with proper structure and guidance.

## What Changes

- Add `openspec init` CLI command that creates the complete OpenSpec directory structure
- Generate template files (README.md with AI instructions, project.md template)
- Interactive prompts to gather project-specific information
- Validation to prevent overwriting existing OpenSpec structures
- Clear success/error messages to guide users

### Breaking Changes
- None - this is a new feature

## Impact

- Affected specs: None (new feature)
- Affected code:
- src/cli/index.ts (add init command)
- src/core/init.ts (new - initialization logic)
- src/core/templates/ (new - template files)
- src/utils/file-system.ts (new - file operations)
30 changes: 30 additions & 0 deletions openspec/changes/add-init-command/tasks.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Implementation Tasks for Init Command

## 1. Core Infrastructure
- [ ] 1.1 Create src/utils/file-system.ts with directory/file creation utilities
- [ ] 1.2 Create src/core/templates/index.ts for template management
- [ ] 1.3 Create src/core/init.ts with main initialization logic

## 2. Template Files
- [ ] 2.1 Create src/core/templates/readme-template.ts with OpenSpec README content
- [ ] 2.2 Create src/core/templates/project-template.ts with customizable project.md
- [ ] 2.3 Create src/core/templates/gitignore-template.ts for OpenSpec-specific ignores

## 3. Init Command Implementation
- [ ] 3.1 Add init command to src/cli/index.ts using Commander
- [ ] 3.2 Implement interactive prompts for project information
- [ ] 3.3 Add validation for existing OpenSpec directories
- [ ] 3.4 Implement directory structure creation logic
- [ ] 3.5 Implement file generation with templates

## 4. User Experience
- [ ] 4.1 Add colorful console output for better UX
- [ ] 4.2 Implement progress indicators during creation
- [ ] 4.3 Add success message with next steps
- [ ] 4.4 Add error handling with helpful messages

## 5. Testing and Documentation
- [ ] 5.1 Add unit tests for file system utilities
- [ ] 5.2 Add integration tests for init command
- [ ] 5.3 Update package.json with proper bin configuration
- [ ] 5.4 Test the built CLI command end-to-end