Compare commits

...
Author SHA1 Message Date
Tabish Bidiwale 3267006c7c Add changeset for config command dynamic import fix 2025-12-23 09:53:56 +11:00
Tabish Bidiwale 6d84924c18 fix(cli): use dynamic import for @inquirer/prompts in config command (#392)
* fix(cli): use dynamic import for @inquirer/prompts in config command

The config command (added in #382) reintroduced the pre-commit hook hang
issue that was fixed in #380. The static import of @inquirer/prompts at
module load time causes stdin event listeners to be registered even when
running non-interactive commands, preventing clean process exit when
stdin is piped (as pre-commit does).

Convert the static import to a dynamic import that only loads inquirer
when the `config reset` command is actually used interactively.

Fixes #367

* chore: add ESLint with no-restricted-imports rule for @inquirer

Add ESLint configuration that prevents static imports of @inquirer/*
modules. This prevents future regressions of the pre-commit hook hang
issue fixed in this PR.

The rule shows a helpful error message pointing to issue #367 for context.
init.ts is exempted since it's already dynamically imported from the CLI.

* ci: add ESLint step to lint job

Run `pnpm lint` in CI to enforce the no-restricted-imports rule
that prevents static @inquirer imports.
2025-12-23 09:50:54 +11:00
Tabish Bidiwale 6de04f3b2b feat(ci): migrate to npm OIDC trusted publishing (#390)
Replace classic npm token authentication with OIDC trusted publishing:

- Add `id-token: write` permission for OIDC token generation
- Upgrade to Node 24 (includes npm 11.5.1+ required for OIDC)
- Remove NPM_TOKEN/NODE_AUTH_TOKEN env vars (OIDC replaces them)

This eliminates the need for rotating npm access tokens and provides
cryptographically verified publisher identity with automatic provenance
attestation.

Requires configuring trusted publisher on npmjs.com:
- Organization: Fission-AI
- Repository: OpenSpec
- Workflow: release-prepare.yml
2025-12-22 20:10:27 +11:00
github-actions[bot]andTabish Bidiwale c2a1a4c807 chore(release): version packages (#389)
* Version Packages

* chore: trigger CI

---------

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: Tabish Bidiwale <tabishbidiwale@gmail.com>
2025-12-22 18:44:57 +11:00
Tabish Bidiwale 2e71835d23 Add changeset for config command and shell completions (#388) 2025-12-22 18:35:29 +11:00
Tabish Bidiwale 971f8ca4a3 feat(cli): add openspec config command for global configuration management (#382)
* feat(cli): add openspec config command for global configuration management

Implements the `openspec config` command with subcommands:
- `path`: Show config file location
- `list [--json]`: Show all current settings
- `get <key>`: Get a specific value (raw output for scripting)
- `set <key> <value> [--string]`: Set a value with auto type coercion
- `unset <key>`: Remove a key (revert to default)
- `reset --all [-y]`: Reset configuration to defaults
- `edit`: Open config in $EDITOR/$VISUAL

Key features:
- Dot notation for nested key access (e.g., featureFlags.someFlag)
- Auto type coercion (true/false → boolean, numbers → number)
- --string flag to force string storage
- Zod schema validation with unknown field passthrough
- Reserved --scope flag for future project-local config
- Windows-compatible editor spawning with proper path quoting
- Shell completion registry integration

* test(config): add additional unit tests for validation and coercion

- Add tests for unknown fields with various types
- Add test to verify error message path for featureFlags
- Add test for number values rejection in featureFlags
- Add config set simulation tests to verify full coerce → set → validate flow

* fix(config): avoid shell parsing in config edit to handle paths with spaces

Use spawn with shell: false and pass configPath as an argument instead
of building a shell command string. This correctly handles spaces in
both the EDITOR path and config file path on all platforms.

* chore(openspec): archive add-config-command and create cli-config spec

Move completed change to archive and apply spec deltas to create
the cli-config specification documenting the config command interface.

* Validate config keys on set
2025-12-22 18:29:25 +11:00
Tabish Bidiwale 68e0a7e68e fix(cli): prevent hang in pre-commit hooks by using dynamic imports (#380)
Fixes #367

The CLI was hanging when run as a pre-commit hook because @inquirer/prompts
was statically imported at module load time. Even when prompts were never
called (e.g., `openspec validate --specs --no-interactive`), the import
itself could set up stdin references that prevented clean process exit
when stdin was piped.

Changes:
- Convert all static `@inquirer/prompts` imports to dynamic imports
- Dynamically import `InitCommand` (which uses `@inquirer/core`)
- Update `isInteractive()` to accept options object with both
  `noInteractive` and Commander's negated `interactive` property
- Handle empty validation queue with proper exit code

Now when running in non-interactive mode, the inquirer modules are never
loaded, allowing the process to exit cleanly after completion.
2025-12-21 18:10:53 +11:00
26 changed files with 2566 additions and 67 deletions
+9
View File
@@ -0,0 +1,9 @@
---
"@fission-ai/openspec": patch
---
Fix pre-commit hook hang issue in config command by using dynamic import for @inquirer/prompts
The config command was causing pre-commit hooks to hang indefinitely due to stdin event listeners being registered at module load time. This fix converts the static import to a dynamic import that only loads inquirer when the `config reset` command is actually used interactively.
Also adds ESLint with a rule to prevent static @inquirer imports, avoiding future regressions.
+3
View File
@@ -142,6 +142,9 @@ jobs:
- name: Type check
run: pnpm exec tsc --noEmit
- name: Lint
run: pnpm lint
- name: Check for build artifacts
run: |
if [ ! -d "dist" ]; then
+3 -5
View File
@@ -7,6 +7,7 @@ on:
permissions:
contents: write
pull-requests: write
id-token: write # Required for npm OIDC trusted publishing
concurrency:
group: release-${{ github.ref }}
@@ -27,11 +28,9 @@ jobs:
- uses: actions/setup-node@v4
with:
node-version: '20'
node-version: '24' # Node 24 includes npm 11.5.1+ required for OIDC
cache: 'pnpm'
registry-url: 'https://registry.npmjs.org'
scope: '@fission-ai'
always-auth: true
- run: pnpm install --frozen-lockfile
@@ -46,5 +45,4 @@ jobs:
publish: pnpm run release:ci
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
# npm authentication handled via OIDC trusted publishing (no token needed)
+22
View File
@@ -1,5 +1,27 @@
# @fission-ai/openspec
## 0.17.0
### Minor Changes
- 2e71835: ### New Features
- Add `openspec config` command for managing global configuration settings
- Implement global config directory with XDG Base Directory specification support
- Add Oh-my-zsh shell completions support for enhanced CLI experience
### Bug Fixes
- Fix hang in pre-commit hooks by using dynamic imports
- Respect XDG_CONFIG_HOME environment variable on all platforms
- Resolve Windows compatibility issues in zsh-installer tests
- Align cli-completion spec with implementation
- Remove hardcoded agent field from slash commands
### Documentation
- Alphabetize AI tools list in README and make it collapsible
## 0.16.0
### Minor Changes
+42
View File
@@ -0,0 +1,42 @@
import tseslint from 'typescript-eslint';
export default tseslint.config(
{
files: ['src/**/*.ts'],
extends: [...tseslint.configs.recommended],
rules: {
// Prevent static imports of @inquirer modules to avoid pre-commit hook hangs.
// These modules have side effects that can keep the Node.js event loop alive
// when stdin is piped. Use dynamic import() instead.
// See: https://github.com/Fission-AI/OpenSpec/issues/367
'no-restricted-imports': [
'error',
{
patterns: [
{
group: ['@inquirer/*'],
message:
'Use dynamic import() for @inquirer modules to prevent pre-commit hook hangs. See #367.',
},
],
},
],
// Disable rules that need broader cleanup - focus on critical issues only
'@typescript-eslint/no-explicit-any': 'off',
'@typescript-eslint/no-unused-vars': 'off',
'no-empty': 'off',
'prefer-const': 'off',
},
},
{
// init.ts is dynamically imported from cli/index.ts, so static @inquirer
// imports there are safe - they won't be loaded at CLI startup
files: ['src/core/init.ts'],
rules: {
'no-restricted-imports': 'off',
},
},
{
ignores: ['dist/**', 'node_modules/**', '*.js', '*.mjs'],
}
);
@@ -1,39 +0,0 @@
## Why
Users need a way to view and modify their global OpenSpec settings without manually editing JSON files. The `add-global-config-dir` change provides the foundation, but there's no user-facing interface to interact with the config. A dedicated `openspec config` command provides discoverability and ease of use.
## What Changes
Add `openspec config` subcommand with the following operations:
```bash
openspec config path # Show config file location
openspec config list # Show all current settings
openspec config get <key> # Get a specific value
openspec config set <key> <value> # Set a value
openspec config reset [key] # Reset to defaults (all or specific key)
```
**Example usage:**
```bash
$ openspec config path
/Users/me/.config/openspec/config.json
$ openspec config list
enableTelemetry: true
featureFlags: {}
$ openspec config set enableTelemetry false
Set enableTelemetry = false
$ openspec config get enableTelemetry
false
```
## Impact
- Affected specs: New `cli-config` capability
- Affected code:
- New `src/commands/config.ts`
- Update CLI entry point to register config command
- Dependencies: Requires `add-global-config-dir` to be implemented first
@@ -0,0 +1,89 @@
## Context
The `global-config` spec defines how OpenSpec reads/writes `config.json`, but users currently must edit it by hand. This command provides a CLI interface to that config.
## Goals / Non-Goals
**Goals:**
- Provide a discoverable CLI for config management
- Support scripting with machine-readable output
- Validate config changes with zod schema
- Handle nested keys gracefully
**Non-Goals:**
- Project-local config (reserved for future via `--scope` flag)
- Complex queries (JSONPath, filtering)
- Config file format migration
## Decisions
### Key Naming: camelCase with Dot Notation
**Decision:** Keys use camelCase matching the JSON structure, with dot notation for nesting.
**Rationale:**
- Matches the actual JSON keys (no translation layer)
- Dot notation is intuitive and widely used (lodash, jq, kubectl)
- Avoids complexity of supporting multiple casing styles
**Examples:**
```bash
openspec config get featureFlags # Returns object
openspec config get featureFlags.experimental # Returns nested value
openspec config set featureFlags.newFlag true
```
### Type Coercion: Auto-detect with `--string` Override
**Decision:** Parse values automatically; provide `--string` flag to force string storage.
**Rationale:**
- Most intuitive for common cases (`true`, `false`, `123`)
- Explicit override for edge cases (storing literal string "true")
- Follows npm/yarn config patterns
**Coercion rules:**
| Input | Stored As |
|-------|-----------|
| `true`, `false` | boolean |
| Numeric string (`123`, `3.14`) | number |
| Everything else | string |
| Any value with `--string` | string |
### Output Format: Raw by Default
**Decision:** `get` prints raw value only. `list` prints YAML-like format by default, JSON with `--json`.
**Rationale:**
- Raw output enables piping: `VAR=$(openspec config get key)`
- YAML-like is human-readable for inspection
- JSON for automation/scripting
### Schema Validation: Zod with Unknown Field Passthrough
**Decision:** Use zod for validation but preserve unknown fields per `global-config` spec.
**Rationale:**
- Type safety for known fields
- Forward compatibility (old CLI doesn't break new config)
- Follows existing `global-config` spec requirement
### Reserved Flag: `--scope`
**Decision:** Reserve `--scope global|project` but only implement `global` initially.
**Rationale:**
- Avoids breaking change if project-local config is added later
- Clear error message if someone tries `--scope project`
## Risks / Trade-offs
| Risk | Mitigation |
|------|------------|
| Dot notation conflicts with keys containing dots | Rare in practice; document limitation |
| Type coercion surprises | `--string` escape hatch; document rules |
| $EDITOR not set | Check and provide helpful error message |
## Open Questions
None - design is straightforward.
@@ -0,0 +1,60 @@
## Why
Users need a way to view and modify their global OpenSpec settings without manually editing JSON files. The `global-config` spec provides the foundation, but there's no user-facing interface to interact with the config. A dedicated `openspec config` command provides discoverability and ease of use.
## What Changes
Add `openspec config` subcommand with the following operations:
```bash
openspec config path # Show config file location
openspec config list [--json] # Show all current settings
openspec config get <key> # Get a specific value (raw, scriptable)
openspec config set <key> <value> [--string] # Set a value (auto-coerce types)
openspec config unset <key> # Remove a key (revert to default)
openspec config reset --all [-y] # Reset everything to defaults
openspec config edit # Open config in $EDITOR
```
**Key design decisions:**
- **Key naming**: Use camelCase to match JSON structure (e.g., `featureFlags.someFlag`)
- **Nested keys**: Support dot notation for nested access
- **Type coercion**: Auto-detect types by default; `--string` flag forces string storage
- **Scriptable output**: `get` prints raw value only (no labels) for easy piping
- **Zod validation**: Use zod for config schema validation and type safety
- **Future-proofing**: Reserve `--scope global|project` flag for potential project-local config
**Example usage:**
```bash
$ openspec config path
/Users/me/.config/openspec/config.json
$ openspec config list
featureFlags: {}
$ openspec config set featureFlags.enableTelemetry false
Set featureFlags.enableTelemetry = false
$ openspec config get featureFlags.enableTelemetry
false
$ openspec config list --json
{
"featureFlags": {}
}
$ openspec config unset featureFlags.enableTelemetry
Unset featureFlags.enableTelemetry (reverted to default)
$ openspec config edit
# Opens $EDITOR with config.json
```
## Impact
- Affected specs: New `cli-config` capability
- Affected code:
- New `src/commands/config.ts`
- New `src/core/config-schema.ts` (zod schema)
- Update CLI entry point to register config command
- Dependencies: Requires `global-config` spec (already implemented)
@@ -0,0 +1,213 @@
# cli-config Specification
## Purpose
Provide a CLI interface for viewing and modifying global OpenSpec configuration. Enables users to manage settings without manually editing JSON files, with support for scripting and automation.
## ADDED Requirements
### Requirement: Command Structure
The config command SHALL provide subcommands for all configuration operations.
#### Scenario: Available subcommands
- **WHEN** user executes `openspec config --help`
- **THEN** display available subcommands:
- `path` - Show config file location
- `list` - Show all current settings
- `get <key>` - Get a specific value
- `set <key> <value>` - Set a value
- `unset <key>` - Remove a key (revert to default)
- `reset` - Reset configuration to defaults
- `edit` - Open config in editor
### Requirement: Config Path
The config command SHALL display the config file location.
#### Scenario: Show config path
- **WHEN** user executes `openspec config path`
- **THEN** print the absolute path to the config file
- **AND** exit with code 0
### Requirement: Config List
The config command SHALL display all current configuration values.
#### Scenario: List config in human-readable format
- **WHEN** user executes `openspec config list`
- **THEN** display all config values in YAML-like format
- **AND** show nested objects with indentation
#### Scenario: List config as JSON
- **WHEN** user executes `openspec config list --json`
- **THEN** output the complete config as valid JSON
- **AND** output only JSON (no additional text)
### Requirement: Config Get
The config command SHALL retrieve specific configuration values.
#### Scenario: Get top-level key
- **WHEN** user executes `openspec config get <key>` with a valid top-level key
- **THEN** print the raw value only (no labels or formatting)
- **AND** exit with code 0
#### Scenario: Get nested key with dot notation
- **WHEN** user executes `openspec config get featureFlags.someFlag`
- **THEN** traverse the nested structure using dot notation
- **AND** print the value at that path
#### Scenario: Get non-existent key
- **WHEN** user executes `openspec config get <key>` with a key that does not exist
- **THEN** print nothing (empty output)
- **AND** exit with code 1
#### Scenario: Get object value
- **WHEN** user executes `openspec config get <key>` where the value is an object
- **THEN** print the object as JSON
### Requirement: Config Set
The config command SHALL set configuration values with automatic type coercion.
#### Scenario: Set string value
- **WHEN** user executes `openspec config set <key> <value>`
- **AND** value does not match boolean or number patterns
- **THEN** store value as a string
- **AND** display confirmation message
#### Scenario: Set boolean value
- **WHEN** user executes `openspec config set <key> true` or `openspec config set <key> false`
- **THEN** store value as boolean (not string)
- **AND** display confirmation message
#### Scenario: Set numeric value
- **WHEN** user executes `openspec config set <key> <value>`
- **AND** value is a valid number (integer or float)
- **THEN** store value as number (not string)
#### Scenario: Force string with --string flag
- **WHEN** user executes `openspec config set <key> <value> --string`
- **THEN** store value as string regardless of content
- **AND** this allows storing literal "true" or "123" as strings
#### Scenario: Set nested key
- **WHEN** user executes `openspec config set featureFlags.newFlag true`
- **THEN** create intermediate objects if they don't exist
- **AND** set the value at the nested path
### Requirement: Config Unset
The config command SHALL remove configuration overrides.
#### Scenario: Unset existing key
- **WHEN** user executes `openspec config unset <key>`
- **AND** the key exists in the config
- **THEN** remove the key from the config file
- **AND** the value reverts to its default
- **AND** display confirmation message
#### Scenario: Unset non-existent key
- **WHEN** user executes `openspec config unset <key>`
- **AND** the key does not exist in the config
- **THEN** display message indicating key was not set
- **AND** exit with code 0
### Requirement: Config Reset
The config command SHALL reset configuration to defaults.
#### Scenario: Reset all with confirmation
- **WHEN** user executes `openspec config reset --all`
- **THEN** prompt for confirmation before proceeding
- **AND** if confirmed, delete the config file or reset to defaults
- **AND** display confirmation message
#### Scenario: Reset all with -y flag
- **WHEN** user executes `openspec config reset --all -y`
- **THEN** reset without prompting for confirmation
#### Scenario: Reset without --all flag
- **WHEN** user executes `openspec config reset` without `--all`
- **THEN** display error indicating `--all` is required
- **AND** exit with code 1
### Requirement: Config Edit
The config command SHALL open the config file in the user's editor.
#### Scenario: Open editor successfully
- **WHEN** user executes `openspec config edit`
- **AND** `$EDITOR` or `$VISUAL` environment variable is set
- **THEN** open the config file in that editor
- **AND** create the config file with defaults if it doesn't exist
- **AND** wait for the editor to close before returning
#### Scenario: No editor configured
- **WHEN** user executes `openspec config edit`
- **AND** neither `$EDITOR` nor `$VISUAL` is set
- **THEN** display error message suggesting to set `$EDITOR`
- **AND** exit with code 1
### Requirement: Key Naming Convention
The config command SHALL use camelCase keys matching the JSON structure.
#### Scenario: Keys match JSON structure
- **WHEN** accessing configuration keys via CLI
- **THEN** use camelCase matching the actual JSON property names
- **AND** support dot notation for nested access (e.g., `featureFlags.someFlag`)
### Requirement: Schema Validation
The config command SHALL validate configuration writes against the config schema using zod, while allowing unknown fields for forward compatibility.
#### Scenario: Unknown key accepted
- **WHEN** user executes `openspec config set someFutureKey 123`
- **THEN** the value is saved successfully
- **AND** exit with code 0
#### Scenario: Invalid feature flag value rejected
- **WHEN** user executes `openspec config set featureFlags.someFlag notABoolean`
- **THEN** display a descriptive error message
- **AND** do not modify the config file
- **AND** exit with code 1
### Requirement: Reserved Scope Flag
The config command SHALL reserve the `--scope` flag for future extensibility.
#### Scenario: Scope flag defaults to global
- **WHEN** user executes any config command without `--scope`
- **THEN** operate on global configuration (default behavior)
#### Scenario: Project scope not yet implemented
- **WHEN** user executes `openspec config --scope project <subcommand>`
- **THEN** display error message: "Project-local config is not yet implemented"
- **AND** exit with code 1
@@ -0,0 +1,28 @@
## 1. Core Infrastructure
- [x] 1.1 Create zod schema for global config in `src/core/config-schema.ts`
- [x] 1.2 Add utility functions for dot-notation key access (get/set nested values)
- [x] 1.3 Add type coercion logic (auto-detect boolean/number/string)
## 2. Config Command Implementation
- [x] 2.1 Create `src/commands/config.ts` with Commander.js subcommands
- [x] 2.2 Implement `config path` subcommand
- [x] 2.3 Implement `config list` subcommand with `--json` flag
- [x] 2.4 Implement `config get <key>` subcommand (raw output)
- [x] 2.5 Implement `config set <key> <value>` with `--string` flag
- [x] 2.6 Implement `config unset <key>` subcommand
- [x] 2.7 Implement `config reset --all` with `-y` confirmation flag
- [x] 2.8 Implement `config edit` subcommand (spawn $EDITOR)
## 3. Integration
- [x] 3.1 Register config command in CLI entry point
- [x] 3.2 Update shell completion registry to include config subcommands
## 4. Testing
- [x] 4.1 Manual testing of all subcommands
- [x] 4.2 Verify zod validation rejects invalid keys/values
- [x] 4.3 Test nested key access with dot notation
- [x] 4.4 Test type coercion edge cases (true/false, numbers, strings)
+217
View File
@@ -0,0 +1,217 @@
# cli-config Specification
## Purpose
Provide a user-friendly CLI interface for viewing and modifying global OpenSpec configuration settings without manually editing JSON files.
## Requirements
### Requirement: Command Structure
The config command SHALL provide subcommands for all configuration operations.
#### Scenario: Available subcommands
- **WHEN** user executes `openspec config --help`
- **THEN** display available subcommands:
- `path` - Show config file location
- `list` - Show all current settings
- `get <key>` - Get a specific value
- `set <key> <value>` - Set a value
- `unset <key>` - Remove a key (revert to default)
- `reset` - Reset configuration to defaults
- `edit` - Open config in editor
### Requirement: Config Path
The config command SHALL display the config file location.
#### Scenario: Show config path
- **WHEN** user executes `openspec config path`
- **THEN** print the absolute path to the config file
- **AND** exit with code 0
### Requirement: Config List
The config command SHALL display all current configuration values.
#### Scenario: List config in human-readable format
- **WHEN** user executes `openspec config list`
- **THEN** display all config values in YAML-like format
- **AND** show nested objects with indentation
#### Scenario: List config as JSON
- **WHEN** user executes `openspec config list --json`
- **THEN** output the complete config as valid JSON
- **AND** output only JSON (no additional text)
### Requirement: Config Get
The config command SHALL retrieve specific configuration values.
#### Scenario: Get top-level key
- **WHEN** user executes `openspec config get <key>` with a valid top-level key
- **THEN** print the raw value only (no labels or formatting)
- **AND** exit with code 0
#### Scenario: Get nested key with dot notation
- **WHEN** user executes `openspec config get featureFlags.someFlag`
- **THEN** traverse the nested structure using dot notation
- **AND** print the value at that path
#### Scenario: Get non-existent key
- **WHEN** user executes `openspec config get <key>` with a key that does not exist
- **THEN** print nothing (empty output)
- **AND** exit with code 1
#### Scenario: Get object value
- **WHEN** user executes `openspec config get <key>` where the value is an object
- **THEN** print the object as JSON
### Requirement: Config Set
The config command SHALL set configuration values with automatic type coercion.
#### Scenario: Set string value
- **WHEN** user executes `openspec config set <key> <value>`
- **AND** value does not match boolean or number patterns
- **THEN** store value as a string
- **AND** display confirmation message
#### Scenario: Set boolean value
- **WHEN** user executes `openspec config set <key> true` or `openspec config set <key> false`
- **THEN** store value as boolean (not string)
- **AND** display confirmation message
#### Scenario: Set numeric value
- **WHEN** user executes `openspec config set <key> <value>`
- **AND** value is a valid number (integer or float)
- **THEN** store value as number (not string)
#### Scenario: Force string with --string flag
- **WHEN** user executes `openspec config set <key> <value> --string`
- **THEN** store value as string regardless of content
- **AND** this allows storing literal "true" or "123" as strings
#### Scenario: Set nested key
- **WHEN** user executes `openspec config set featureFlags.newFlag true`
- **THEN** create intermediate objects if they don't exist
- **AND** set the value at the nested path
### Requirement: Config Unset
The config command SHALL remove configuration overrides.
#### Scenario: Unset existing key
- **WHEN** user executes `openspec config unset <key>`
- **AND** the key exists in the config
- **THEN** remove the key from the config file
- **AND** the value reverts to its default
- **AND** display confirmation message
#### Scenario: Unset non-existent key
- **WHEN** user executes `openspec config unset <key>`
- **AND** the key does not exist in the config
- **THEN** display message indicating key was not set
- **AND** exit with code 0
### Requirement: Config Reset
The config command SHALL reset configuration to defaults.
#### Scenario: Reset all with confirmation
- **WHEN** user executes `openspec config reset --all`
- **THEN** prompt for confirmation before proceeding
- **AND** if confirmed, delete the config file or reset to defaults
- **AND** display confirmation message
#### Scenario: Reset all with -y flag
- **WHEN** user executes `openspec config reset --all -y`
- **THEN** reset without prompting for confirmation
#### Scenario: Reset without --all flag
- **WHEN** user executes `openspec config reset` without `--all`
- **THEN** display error indicating `--all` is required
- **AND** exit with code 1
### Requirement: Config Edit
The config command SHALL open the config file in the user's editor.
#### Scenario: Open editor successfully
- **WHEN** user executes `openspec config edit`
- **AND** `$EDITOR` or `$VISUAL` environment variable is set
- **THEN** open the config file in that editor
- **AND** create the config file with defaults if it doesn't exist
- **AND** wait for the editor to close before returning
#### Scenario: No editor configured
- **WHEN** user executes `openspec config edit`
- **AND** neither `$EDITOR` nor `$VISUAL` is set
- **THEN** display error message suggesting to set `$EDITOR`
- **AND** exit with code 1
### Requirement: Key Naming Convention
The config command SHALL use camelCase keys matching the JSON structure.
#### Scenario: Keys match JSON structure
- **WHEN** accessing configuration keys via CLI
- **THEN** use camelCase matching the actual JSON property names
- **AND** support dot notation for nested access (e.g., `featureFlags.someFlag`)
### Requirement: Schema Validation
The config command SHALL validate configuration writes against the config schema using zod, while rejecting unknown keys for `config set` unless explicitly overridden.
#### Scenario: Unknown key rejected by default
- **WHEN** user executes `openspec config set someFutureKey 123`
- **THEN** display a descriptive error message indicating the key is invalid
- **AND** do not modify the config file
- **AND** exit with code 1
#### Scenario: Unknown key accepted with override
- **WHEN** user executes `openspec config set someFutureKey 123 --allow-unknown`
- **THEN** the value is saved successfully
- **AND** exit with code 0
#### Scenario: Invalid feature flag value rejected
- **WHEN** user executes `openspec config set featureFlags.someFlag notABoolean`
- **THEN** display a descriptive error message
- **AND** do not modify the config file
- **AND** exit with code 1
### Requirement: Reserved Scope Flag
The config command SHALL reserve the `--scope` flag for future extensibility.
#### Scenario: Scope flag defaults to global
- **WHEN** user executes any config command without `--scope`
- **THEN** operate on global configuration (default behavior)
#### Scenario: Project scope not yet implemented
- **WHEN** user executes `openspec config --scope project <subcommand>`
- **THEN** display error message: "Project-local config is not yet implemented"
- **AND** exit with code 1
+4 -1
View File
@@ -1,6 +1,6 @@
{
"name": "@fission-ai/openspec",
"version": "0.16.0",
"version": "0.17.0",
"description": "AI-native system for spec-driven development",
"keywords": [
"openspec",
@@ -38,6 +38,7 @@
"!dist/**/*.map"
],
"scripts": {
"lint": "eslint src/",
"build": "node build.js",
"dev": "tsc --watch",
"dev:cli": "pnpm build && node bin/openspec.js",
@@ -62,7 +63,9 @@
"@changesets/cli": "^2.27.7",
"@types/node": "^24.2.0",
"@vitest/ui": "^3.2.4",
"eslint": "^9.39.2",
"typescript": "^5.9.3",
"typescript-eslint": "^8.50.1",
"vitest": "^3.2.4"
},
"dependencies": {
+762
View File
File diff suppressed because it is too large Load Diff
+4 -2
View File
@@ -3,7 +3,6 @@ import { createRequire } from 'module';
import ora from 'ora';
import path from 'path';
import { promises as fs } from 'fs';
import { InitCommand } from '../core/init.js';
import { AI_TOOLS } from '../core/config.js';
import { UpdateCommand } from '../core/update.js';
import { ListCommand } from '../core/list.js';
@@ -14,6 +13,7 @@ import { ChangeCommand } from '../commands/change.js';
import { ValidateCommand } from '../commands/validate.js';
import { ShowCommand } from '../commands/show.js';
import { CompletionCommand } from '../commands/completion.js';
import { registerConfigCommand } from '../commands/config.js';
const program = new Command();
const require = createRequire(import.meta.url);
@@ -30,7 +30,7 @@ program.option('--no-color', 'Disable color output');
// Apply global flags before any command runs
program.hook('preAction', (thisCommand) => {
const opts = thisCommand.opts();
if (opts.noColor) {
if (opts.color === false) {
process.env.NO_COLOR = '1';
}
});
@@ -63,6 +63,7 @@ program
}
}
const { InitCommand } = await import('../core/init.js');
const initCommand = new InitCommand({
tools: options?.tools,
});
@@ -200,6 +201,7 @@ program
});
registerSpecCommand(program);
registerConfigCommand(program);
// Top-level validate command
program
+4 -3
View File
@@ -1,6 +1,5 @@
import { promises as fs } from 'fs';
import path from 'path';
import { select } from '@inquirer/prompts';
import { JsonConverter } from '../core/converters/json-converter.js';
import { Validator } from '../core/validation/validator.js';
import { ChangeParser } from '../core/parsers/change-parser.js';
@@ -30,9 +29,10 @@ export class ChangeCommand {
const changesPath = path.join(process.cwd(), 'openspec', 'changes');
if (!changeName) {
const canPrompt = isInteractive(options?.noInteractive);
const canPrompt = isInteractive(options);
const changes = await this.getActiveChanges(changesPath);
if (canPrompt && changes.length > 0) {
const { select } = await import('@inquirer/prompts');
const selected = await select({
message: 'Select a change to show',
choices: changes.map(id => ({ name: id, value: id })),
@@ -186,9 +186,10 @@ export class ChangeCommand {
const changesPath = path.join(process.cwd(), 'openspec', 'changes');
if (!changeName) {
const canPrompt = isInteractive(options?.noInteractive);
const canPrompt = isInteractive(options);
const changes = await getActiveChangeIds();
if (canPrompt && changes.length > 0) {
const { select } = await import('@inquirer/prompts');
const selected = await select({
message: 'Select a change to validate',
choices: changes.map(id => ({ name: id, value: id })),
+1 -1
View File
@@ -1,5 +1,4 @@
import ora from 'ora';
import { confirm } from '@inquirer/prompts';
import { CompletionFactory } from '../core/completions/factory.js';
import { COMMAND_REGISTRY } from '../core/completions/command-registry.js';
import { detectShell, SupportedShell } from '../utils/shell-detection.js';
@@ -179,6 +178,7 @@ export class CompletionCommand {
// Prompt for confirmation unless --yes flag is provided
if (!skipConfirmation) {
const { confirm } = await import('@inquirer/prompts');
const confirmed = await confirm({
message: 'Remove OpenSpec configuration from ~/.zshrc?',
default: false,
+233
View File
@@ -0,0 +1,233 @@
import { Command } from 'commander';
import { spawn } from 'node:child_process';
import * as fs from 'node:fs';
import {
getGlobalConfigPath,
getGlobalConfig,
saveGlobalConfig,
GlobalConfig,
} from '../core/global-config.js';
import {
getNestedValue,
setNestedValue,
deleteNestedValue,
coerceValue,
formatValueYaml,
validateConfigKeyPath,
validateConfig,
DEFAULT_CONFIG,
} from '../core/config-schema.js';
/**
* Register the config command and all its subcommands.
*
* @param program - The Commander program instance
*/
export function registerConfigCommand(program: Command): void {
const configCmd = program
.command('config')
.description('View and modify global OpenSpec configuration')
.option('--scope <scope>', 'Config scope (only "global" supported currently)')
.hook('preAction', (thisCommand) => {
const opts = thisCommand.opts();
if (opts.scope && opts.scope !== 'global') {
console.error('Error: Project-local config is not yet implemented');
process.exit(1);
}
});
// config path
configCmd
.command('path')
.description('Show config file location')
.action(() => {
console.log(getGlobalConfigPath());
});
// config list
configCmd
.command('list')
.description('Show all current settings')
.option('--json', 'Output as JSON')
.action((options: { json?: boolean }) => {
const config = getGlobalConfig();
if (options.json) {
console.log(JSON.stringify(config, null, 2));
} else {
console.log(formatValueYaml(config));
}
});
// config get
configCmd
.command('get <key>')
.description('Get a specific value (raw, scriptable)')
.action((key: string) => {
const config = getGlobalConfig();
const value = getNestedValue(config as Record<string, unknown>, key);
if (value === undefined) {
process.exitCode = 1;
return;
}
if (typeof value === 'object' && value !== null) {
console.log(JSON.stringify(value));
} else {
console.log(String(value));
}
});
// config set
configCmd
.command('set <key> <value>')
.description('Set a value (auto-coerce types)')
.option('--string', 'Force value to be stored as string')
.option('--allow-unknown', 'Allow setting unknown keys')
.action((key: string, value: string, options: { string?: boolean; allowUnknown?: boolean }) => {
const allowUnknown = Boolean(options.allowUnknown);
const keyValidation = validateConfigKeyPath(key);
if (!keyValidation.valid && !allowUnknown) {
const reason = keyValidation.reason ? ` ${keyValidation.reason}.` : '';
console.error(`Error: Invalid configuration key "${key}".${reason}`);
console.error('Use "openspec config list" to see available keys.');
console.error('Pass --allow-unknown to bypass this check.');
process.exitCode = 1;
return;
}
const config = getGlobalConfig() as Record<string, unknown>;
const coercedValue = coerceValue(value, options.string || false);
// Create a copy to validate before saving
const newConfig = JSON.parse(JSON.stringify(config));
setNestedValue(newConfig, key, coercedValue);
// Validate the new config
const validation = validateConfig(newConfig);
if (!validation.success) {
console.error(`Error: Invalid configuration - ${validation.error}`);
process.exitCode = 1;
return;
}
// Apply changes and save
setNestedValue(config, key, coercedValue);
saveGlobalConfig(config as GlobalConfig);
const displayValue =
typeof coercedValue === 'string' ? `"${coercedValue}"` : String(coercedValue);
console.log(`Set ${key} = ${displayValue}`);
});
// config unset
configCmd
.command('unset <key>')
.description('Remove a key (revert to default)')
.action((key: string) => {
const config = getGlobalConfig() as Record<string, unknown>;
const existed = deleteNestedValue(config, key);
if (existed) {
saveGlobalConfig(config as GlobalConfig);
console.log(`Unset ${key} (reverted to default)`);
} else {
console.log(`Key "${key}" was not set`);
}
});
// config reset
configCmd
.command('reset')
.description('Reset configuration to defaults')
.option('--all', 'Reset all configuration (required)')
.option('-y, --yes', 'Skip confirmation prompts')
.action(async (options: { all?: boolean; yes?: boolean }) => {
if (!options.all) {
console.error('Error: --all flag is required for reset');
console.error('Usage: openspec config reset --all [-y]');
process.exitCode = 1;
return;
}
if (!options.yes) {
const { confirm } = await import('@inquirer/prompts');
const confirmed = await confirm({
message: 'Reset all configuration to defaults?',
default: false,
});
if (!confirmed) {
console.log('Reset cancelled.');
return;
}
}
saveGlobalConfig({ ...DEFAULT_CONFIG });
console.log('Configuration reset to defaults');
});
// config edit
configCmd
.command('edit')
.description('Open config in $EDITOR')
.action(async () => {
const editor = process.env.EDITOR || process.env.VISUAL;
if (!editor) {
console.error('Error: No editor configured');
console.error('Set the EDITOR or VISUAL environment variable to your preferred editor');
console.error('Example: export EDITOR=vim');
process.exitCode = 1;
return;
}
const configPath = getGlobalConfigPath();
// Ensure config file exists with defaults
if (!fs.existsSync(configPath)) {
saveGlobalConfig({ ...DEFAULT_CONFIG });
}
// Spawn editor and wait for it to close
// Avoid shell parsing to correctly handle paths with spaces in both
// the editor path and config path
const child = spawn(editor, [configPath], {
stdio: 'inherit',
shell: false,
});
await new Promise<void>((resolve, reject) => {
child.on('close', (code) => {
if (code === 0) {
resolve();
} else {
reject(new Error(`Editor exited with code ${code}`));
}
});
child.on('error', reject);
});
try {
const rawConfig = fs.readFileSync(configPath, 'utf-8');
const parsedConfig = JSON.parse(rawConfig);
const validation = validateConfig(parsedConfig);
if (!validation.success) {
console.error(`Error: Invalid configuration - ${validation.error}`);
process.exitCode = 1;
}
} catch (error) {
if ((error as NodeJS.ErrnoException).code === 'ENOENT') {
console.error(`Error: Config file not found at ${configPath}`);
} else if (error instanceof SyntaxError) {
console.error(`Error: Invalid JSON in ${configPath}`);
console.error(error.message);
} else {
console.error(`Error: Unable to validate configuration - ${error instanceof Error ? error.message : String(error)}`);
}
process.exitCode = 1;
}
});
}
+3 -4
View File
@@ -1,4 +1,3 @@
import { select } from '@inquirer/prompts';
import path from 'path';
import { isInteractive } from '../utils/interactive.js';
import { getActiveChangeIds, getSpecIds } from '../utils/item-discovery.js';
@@ -13,11 +12,12 @@ const SPEC_FLAG_KEYS = new Set(['requirements', 'scenarios', 'requirement']);
export class ShowCommand {
async execute(itemName?: string, options: { json?: boolean; type?: string; noInteractive?: boolean; [k: string]: any } = {}): Promise<void> {
const interactive = isInteractive(options.noInteractive);
const interactive = isInteractive(options);
const typeOverride = this.normalizeType(options.type);
if (!itemName) {
if (interactive) {
const { select } = await import('@inquirer/prompts');
const type = await select<ItemType>({
message: 'What would you like to show?',
choices: [
@@ -44,6 +44,7 @@ export class ShowCommand {
}
private async runInteractiveByType(type: ItemType, options: { json?: boolean; noInteractive?: boolean; [k: string]: any }): Promise<void> {
const { select } = await import('@inquirer/prompts');
if (type === 'change') {
const changes = await getActiveChangeIds();
if (changes.length === 0) {
@@ -135,5 +136,3 @@ export class ShowCommand {
return false;
}
}
+5 -4
View File
@@ -4,7 +4,6 @@ import { join } from 'path';
import { MarkdownParser } from '../core/parsers/markdown-parser.js';
import { Validator } from '../core/validation/validator.js';
import type { Spec } from '../core/schemas/index.js';
import { select } from '@inquirer/prompts';
import { isInteractive } from '../utils/interactive.js';
import { getSpecIds } from '../utils/item-discovery.js';
@@ -70,9 +69,10 @@ export class SpecCommand {
async show(specId?: string, options: ShowOptions = {}): Promise<void> {
if (!specId) {
const canPrompt = isInteractive(options?.noInteractive);
const canPrompt = isInteractive(options);
const specIds = await getSpecIds();
if (canPrompt && specIds.length > 0) {
const { select } = await import('@inquirer/prompts');
specId = await select({
message: 'Select a spec to show',
choices: specIds.map(id => ({ name: id, value: id })),
@@ -204,9 +204,10 @@ export function registerSpecCommand(rootProgram: typeof program) {
.action(async (specId: string | undefined, options: { strict?: boolean; json?: boolean; noInteractive?: boolean }) => {
try {
if (!specId) {
const canPrompt = isInteractive(options?.noInteractive);
const canPrompt = isInteractive(options);
const specIds = await getSpecIds();
if (canPrompt && specIds.length > 0) {
const { select } = await import('@inquirer/prompts');
specId = await select({
message: 'Select a spec to validate',
choices: specIds.map(id => ({ name: id, value: id })),
@@ -247,4 +248,4 @@ export function registerSpecCommand(rootProgram: typeof program) {
});
return specCommand;
}
}
+24 -4
View File
@@ -1,4 +1,3 @@
import { select } from '@inquirer/prompts';
import ora from 'ora';
import path from 'path';
import { Validator } from '../core/validation/validator.js';
@@ -29,7 +28,7 @@ interface BulkItemResult {
export class ValidateCommand {
async execute(itemName: string | undefined, options: ExecuteOptions = {}): Promise<void> {
const interactive = isInteractive(options.noInteractive);
const interactive = isInteractive(options);
// Handle bulk flags first
if (options.all || options.changes || options.specs) {
@@ -64,6 +63,7 @@ export class ValidateCommand {
}
private async runInteractiveSelector(opts: { strict: boolean; json: boolean; concurrency?: string }): Promise<void> {
const { select } = await import('@inquirer/prompts');
const choice = await select({
message: 'What would you like to validate?',
choices: [
@@ -212,6 +212,28 @@ export class ValidateCommand {
});
}
if (queue.length === 0) {
spinner?.stop();
const summary = {
totals: { items: 0, passed: 0, failed: 0 },
byType: {
...(scope.changes ? { change: { items: 0, passed: 0, failed: 0 } } : {}),
...(scope.specs ? { spec: { items: 0, passed: 0, failed: 0 } } : {}),
},
} as const;
if (opts.json) {
const out = { items: [] as BulkItemResult[], summary, version: '1.0' };
console.log(JSON.stringify(out, null, 2));
} else {
console.log('No items found to validate.');
}
process.exitCode = 0;
return;
}
const results: BulkItemResult[] = [];
let index = 0;
let running = 0;
@@ -301,5 +323,3 @@ function getPlannedType(index: number, changeIds: string[], specIds: string[]):
if (specIndex >= 0 && specIndex < specIds.length) return 'spec';
return undefined;
}
+4 -1
View File
@@ -1,6 +1,5 @@
import { promises as fs } from 'fs';
import path from 'path';
import { select, confirm } from '@inquirer/prompts';
import { FileSystemUtils } from '../utils/file-system.js';
import { getTaskProgressForChange, formatTaskStatus } from '../utils/task-progress.js';
import { Validator } from './validation/validator.js';
@@ -125,6 +124,7 @@ export class ArchiveCommand {
const timestamp = new Date().toISOString();
if (!options.yes) {
const { confirm } = await import('@inquirer/prompts');
const proceed = await confirm({
message: chalk.yellow('⚠️ WARNING: Skipping validation may archive invalid specs. Continue? (y/N)'),
default: false
@@ -149,6 +149,7 @@ export class ArchiveCommand {
const incompleteTasks = Math.max(progress.total - progress.completed, 0);
if (incompleteTasks > 0) {
if (!options.yes) {
const { confirm } = await import('@inquirer/prompts');
const proceed = await confirm({
message: `Warning: ${incompleteTasks} incomplete task(s) found. Continue?`,
default: false
@@ -179,6 +180,7 @@ export class ArchiveCommand {
let shouldUpdateSpecs = true;
if (!options.yes) {
const { confirm } = await import('@inquirer/prompts');
shouldUpdateSpecs = await confirm({
message: 'Proceed with spec updates?',
default: true
@@ -256,6 +258,7 @@ export class ArchiveCommand {
}
private async selectChange(changesDir: string): Promise<string | null> {
const { select } = await import('@inquirer/prompts');
// Get all directories in changes (excluding archive)
const entries = await fs.readdir(changesDir, { withFileTypes: true });
const changeDirs = entries
+73
View File
@@ -288,4 +288,77 @@ export const COMMAND_REGISTRY: CommandDefinition[] = [
},
],
},
{
name: 'config',
description: 'View and modify global OpenSpec configuration',
flags: [
{
name: 'scope',
description: 'Config scope (only "global" supported currently)',
takesValue: true,
values: ['global'],
},
],
subcommands: [
{
name: 'path',
description: 'Show config file location',
flags: [],
},
{
name: 'list',
description: 'Show all current settings',
flags: [
COMMON_FLAGS.json,
],
},
{
name: 'get',
description: 'Get a specific value (raw, scriptable)',
acceptsPositional: true,
flags: [],
},
{
name: 'set',
description: 'Set a value (auto-coerce types)',
acceptsPositional: true,
flags: [
{
name: 'string',
description: 'Force value to be stored as string',
},
{
name: 'allow-unknown',
description: 'Allow setting unknown keys',
},
],
},
{
name: 'unset',
description: 'Remove a key (revert to default)',
acceptsPositional: true,
flags: [],
},
{
name: 'reset',
description: 'Reset configuration to defaults',
flags: [
{
name: 'all',
description: 'Reset all configuration (required)',
},
{
name: 'yes',
short: 'y',
description: 'Skip confirmation prompts',
},
],
},
{
name: 'edit',
description: 'Open config in $EDITOR',
flags: [],
},
],
},
];
+230
View File
@@ -0,0 +1,230 @@
import { z } from 'zod';
/**
* Zod schema for global OpenSpec configuration.
* Uses passthrough() to preserve unknown fields for forward compatibility.
*/
export const GlobalConfigSchema = z
.object({
featureFlags: z
.record(z.string(), z.boolean())
.optional()
.default({}),
})
.passthrough();
export type GlobalConfigType = z.infer<typeof GlobalConfigSchema>;
/**
* Default configuration values.
*/
export const DEFAULT_CONFIG: GlobalConfigType = {
featureFlags: {},
};
const KNOWN_TOP_LEVEL_KEYS = new Set(Object.keys(DEFAULT_CONFIG));
/**
* Validate a config key path for CLI set operations.
* Unknown top-level keys are rejected unless explicitly allowed by the caller.
*/
export function validateConfigKeyPath(path: string): { valid: boolean; reason?: string } {
const rawKeys = path.split('.');
if (rawKeys.length === 0 || rawKeys.some((key) => key.trim() === '')) {
return { valid: false, reason: 'Key path must not be empty' };
}
const rootKey = rawKeys[0];
if (!KNOWN_TOP_LEVEL_KEYS.has(rootKey)) {
return { valid: false, reason: `Unknown top-level key "${rootKey}"` };
}
if (rootKey === 'featureFlags') {
if (rawKeys.length > 2) {
return { valid: false, reason: 'featureFlags values are booleans and do not support nested keys' };
}
return { valid: true };
}
if (rawKeys.length > 1) {
return { valid: false, reason: `"${rootKey}" does not support nested keys` };
}
return { valid: true };
}
/**
* Get a nested value from an object using dot notation.
*
* @param obj - The object to access
* @param path - Dot-separated path (e.g., "featureFlags.someFlag")
* @returns The value at the path, or undefined if not found
*/
export function getNestedValue(obj: Record<string, unknown>, path: string): unknown {
const keys = path.split('.');
let current: unknown = obj;
for (const key of keys) {
if (current === null || current === undefined) {
return undefined;
}
if (typeof current !== 'object') {
return undefined;
}
current = (current as Record<string, unknown>)[key];
}
return current;
}
/**
* Set a nested value in an object using dot notation.
* Creates intermediate objects as needed.
*
* @param obj - The object to modify (mutated in place)
* @param path - Dot-separated path (e.g., "featureFlags.someFlag")
* @param value - The value to set
*/
export function setNestedValue(obj: Record<string, unknown>, path: string, value: unknown): void {
const keys = path.split('.');
let current: Record<string, unknown> = obj;
for (let i = 0; i < keys.length - 1; i++) {
const key = keys[i];
if (current[key] === undefined || current[key] === null || typeof current[key] !== 'object') {
current[key] = {};
}
current = current[key] as Record<string, unknown>;
}
const lastKey = keys[keys.length - 1];
current[lastKey] = value;
}
/**
* Delete a nested value from an object using dot notation.
*
* @param obj - The object to modify (mutated in place)
* @param path - Dot-separated path (e.g., "featureFlags.someFlag")
* @returns true if the key existed and was deleted, false otherwise
*/
export function deleteNestedValue(obj: Record<string, unknown>, path: string): boolean {
const keys = path.split('.');
let current: Record<string, unknown> = obj;
for (let i = 0; i < keys.length - 1; i++) {
const key = keys[i];
if (current[key] === undefined || current[key] === null || typeof current[key] !== 'object') {
return false;
}
current = current[key] as Record<string, unknown>;
}
const lastKey = keys[keys.length - 1];
if (lastKey in current) {
delete current[lastKey];
return true;
}
return false;
}
/**
* Coerce a string value to its appropriate type.
* - "true" / "false" -> boolean
* - Numeric strings -> number
* - Everything else -> string
*
* @param value - The string value to coerce
* @param forceString - If true, always return the value as a string
* @returns The coerced value
*/
export function coerceValue(value: string, forceString: boolean = false): string | number | boolean {
if (forceString) {
return value;
}
// Boolean coercion
if (value === 'true') {
return true;
}
if (value === 'false') {
return false;
}
// Number coercion - must be a valid finite number
const num = Number(value);
if (!isNaN(num) && isFinite(num) && value.trim() !== '') {
return num;
}
return value;
}
/**
* Format a value for YAML-like display.
*
* @param value - The value to format
* @param indent - Current indentation level
* @returns Formatted string
*/
export function formatValueYaml(value: unknown, indent: number = 0): string {
const indentStr = ' '.repeat(indent);
if (value === null || value === undefined) {
return 'null';
}
if (typeof value === 'boolean' || typeof value === 'number') {
return String(value);
}
if (typeof value === 'string') {
return value;
}
if (Array.isArray(value)) {
if (value.length === 0) {
return '[]';
}
return value.map((item) => `${indentStr}- ${formatValueYaml(item, indent + 1)}`).join('\n');
}
if (typeof value === 'object') {
const entries = Object.entries(value as Record<string, unknown>);
if (entries.length === 0) {
return '{}';
}
return entries
.map(([key, val]) => {
const formattedVal = formatValueYaml(val, indent + 1);
if (typeof val === 'object' && val !== null && Object.keys(val).length > 0) {
return `${indentStr}${key}:\n${formattedVal}`;
}
return `${indentStr}${key}: ${formattedVal}`;
})
.join('\n');
}
return String(value);
}
/**
* Validate a configuration object against the schema.
*
* @param config - The configuration to validate
* @returns Validation result with success status and optional error message
*/
export function validateConfig(config: unknown): { success: boolean; error?: string } {
try {
GlobalConfigSchema.parse(config);
return { success: true };
} catch (error) {
if (error instanceof z.ZodError) {
const zodError = error as z.ZodError;
const messages = zodError.issues.map((e) => `${e.path.join('.')}: ${e.message}`);
return { success: false, error: messages.join('; ') };
}
return { success: false, error: 'Unknown validation error' };
}
}
+18 -3
View File
@@ -1,7 +1,22 @@
export function isInteractive(noInteractiveFlag?: boolean): boolean {
if (noInteractiveFlag) return false;
type InteractiveOptions = {
/**
* Explicit "disable prompts" flag passed by internal callers.
*/
noInteractive?: boolean;
/**
* Commander-style negated option: `--no-interactive` sets this to false.
*/
interactive?: boolean;
};
function resolveNoInteractive(value?: boolean | InteractiveOptions): boolean {
if (typeof value === 'boolean') return value;
return value?.noInteractive === true || value?.interactive === false;
}
export function isInteractive(value?: boolean | InteractiveOptions): boolean {
if (resolveNoInteractive(value)) return false;
if (process.env.OPEN_SPEC_INTERACTIVE === '0') return false;
return !!process.stdin.isTTY;
}
+175
View File
@@ -0,0 +1,175 @@
import { describe, it, expect, beforeEach, afterEach, vi } from 'vitest';
import * as fs from 'node:fs';
import * as path from 'node:path';
import * as os from 'node:os';
describe('config command integration', () => {
// These tests use real file system operations with XDG_CONFIG_HOME override
let tempDir: string;
let originalEnv: NodeJS.ProcessEnv;
let consoleErrorSpy: ReturnType<typeof vi.spyOn>;
beforeEach(() => {
// Create unique temp directory for each test
tempDir = path.join(os.tmpdir(), `openspec-config-test-${Date.now()}-${Math.random().toString(36).slice(2)}`);
fs.mkdirSync(tempDir, { recursive: true });
// Save original env and set XDG_CONFIG_HOME
originalEnv = { ...process.env };
process.env.XDG_CONFIG_HOME = tempDir;
// Spy on console.error
consoleErrorSpy = vi.spyOn(console, 'error').mockImplementation(() => {});
});
afterEach(() => {
// Restore original env
process.env = originalEnv;
// Clean up temp directory
fs.rmSync(tempDir, { recursive: true, force: true });
// Restore spies
consoleErrorSpy.mockRestore();
// Reset module cache to pick up new XDG_CONFIG_HOME
vi.resetModules();
});
it('should use XDG_CONFIG_HOME for config path', async () => {
const { getGlobalConfigPath } = await import('../../src/core/global-config.js');
const configPath = getGlobalConfigPath();
expect(configPath).toBe(path.join(tempDir, 'openspec', 'config.json'));
});
it('should save and load config correctly', async () => {
const { getGlobalConfig, saveGlobalConfig } = await import('../../src/core/global-config.js');
saveGlobalConfig({ featureFlags: { test: true } });
const config = getGlobalConfig();
expect(config.featureFlags).toEqual({ test: true });
});
it('should return defaults when config file does not exist', async () => {
const { getGlobalConfig, getGlobalConfigPath } = await import('../../src/core/global-config.js');
const configPath = getGlobalConfigPath();
// Make sure config doesn't exist
if (fs.existsSync(configPath)) {
fs.unlinkSync(configPath);
}
const config = getGlobalConfig();
expect(config.featureFlags).toEqual({});
});
it('should preserve unknown fields', async () => {
const { getGlobalConfig, getGlobalConfigDir } = await import('../../src/core/global-config.js');
const configDir = getGlobalConfigDir();
fs.mkdirSync(configDir, { recursive: true });
fs.writeFileSync(path.join(configDir, 'config.json'), JSON.stringify({
featureFlags: {},
customField: 'preserved',
}));
const config = getGlobalConfig();
expect((config as Record<string, unknown>).customField).toBe('preserved');
});
it('should handle invalid JSON gracefully', async () => {
const { getGlobalConfig, getGlobalConfigDir } = await import('../../src/core/global-config.js');
const configDir = getGlobalConfigDir();
fs.mkdirSync(configDir, { recursive: true });
fs.writeFileSync(path.join(configDir, 'config.json'), '{ invalid json }');
const config = getGlobalConfig();
// Should return defaults
expect(config.featureFlags).toEqual({});
expect(consoleErrorSpy).toHaveBeenCalledWith(expect.stringContaining('Invalid JSON'));
});
});
describe('config command shell completion registry', () => {
it('should have config command in registry', async () => {
const { COMMAND_REGISTRY } = await import('../../src/core/completions/command-registry.js');
const configCmd = COMMAND_REGISTRY.find((cmd) => cmd.name === 'config');
expect(configCmd).toBeDefined();
expect(configCmd?.description).toBe('View and modify global OpenSpec configuration');
});
it('should have all config subcommands in registry', async () => {
const { COMMAND_REGISTRY } = await import('../../src/core/completions/command-registry.js');
const configCmd = COMMAND_REGISTRY.find((cmd) => cmd.name === 'config');
const subcommandNames = configCmd?.subcommands?.map((s) => s.name) ?? [];
expect(subcommandNames).toContain('path');
expect(subcommandNames).toContain('list');
expect(subcommandNames).toContain('get');
expect(subcommandNames).toContain('set');
expect(subcommandNames).toContain('unset');
expect(subcommandNames).toContain('reset');
expect(subcommandNames).toContain('edit');
});
it('should have --json flag on list subcommand', async () => {
const { COMMAND_REGISTRY } = await import('../../src/core/completions/command-registry.js');
const configCmd = COMMAND_REGISTRY.find((cmd) => cmd.name === 'config');
const listCmd = configCmd?.subcommands?.find((s) => s.name === 'list');
const flagNames = listCmd?.flags?.map((f) => f.name) ?? [];
expect(flagNames).toContain('json');
});
it('should have --string flag on set subcommand', async () => {
const { COMMAND_REGISTRY } = await import('../../src/core/completions/command-registry.js');
const configCmd = COMMAND_REGISTRY.find((cmd) => cmd.name === 'config');
const setCmd = configCmd?.subcommands?.find((s) => s.name === 'set');
const flagNames = setCmd?.flags?.map((f) => f.name) ?? [];
expect(flagNames).toContain('string');
expect(flagNames).toContain('allow-unknown');
});
it('should have --all and -y flags on reset subcommand', async () => {
const { COMMAND_REGISTRY } = await import('../../src/core/completions/command-registry.js');
const configCmd = COMMAND_REGISTRY.find((cmd) => cmd.name === 'config');
const resetCmd = configCmd?.subcommands?.find((s) => s.name === 'reset');
const flagNames = resetCmd?.flags?.map((f) => f.name) ?? [];
expect(flagNames).toContain('all');
expect(flagNames).toContain('yes');
});
it('should have --scope flag on config command', async () => {
const { COMMAND_REGISTRY } = await import('../../src/core/completions/command-registry.js');
const configCmd = COMMAND_REGISTRY.find((cmd) => cmd.name === 'config');
const flagNames = configCmd?.flags?.map((f) => f.name) ?? [];
expect(flagNames).toContain('scope');
});
});
describe('config key validation', () => {
it('rejects unknown top-level keys', async () => {
const { validateConfigKeyPath } = await import('../../src/core/config-schema.js');
expect(validateConfigKeyPath('unknownKey').valid).toBe(false);
});
it('allows feature flag keys', async () => {
const { validateConfigKeyPath } = await import('../../src/core/config-schema.js');
expect(validateConfigKeyPath('featureFlags.someFlag').valid).toBe(true);
});
it('rejects deeply nested feature flag keys', async () => {
const { validateConfigKeyPath } = await import('../../src/core/config-schema.js');
expect(validateConfigKeyPath('featureFlags.someFlag.extra').valid).toBe(false);
});
});
+340
View File
@@ -0,0 +1,340 @@
import { describe, it, expect } from 'vitest';
import {
getNestedValue,
setNestedValue,
deleteNestedValue,
coerceValue,
formatValueYaml,
validateConfig,
GlobalConfigSchema,
DEFAULT_CONFIG,
} from '../../src/core/config-schema.js';
describe('config-schema', () => {
describe('getNestedValue', () => {
it('should get a top-level value', () => {
const obj = { foo: 'bar' };
expect(getNestedValue(obj, 'foo')).toBe('bar');
});
it('should get a nested value with dot notation', () => {
const obj = { a: { b: { c: 'deep' } } };
expect(getNestedValue(obj, 'a.b.c')).toBe('deep');
});
it('should return undefined for non-existent path', () => {
const obj = { foo: 'bar' };
expect(getNestedValue(obj, 'baz')).toBeUndefined();
});
it('should return undefined for non-existent nested path', () => {
const obj = { a: { b: 'value' } };
expect(getNestedValue(obj, 'a.b.c')).toBeUndefined();
});
it('should return undefined when traversing through null', () => {
const obj = { a: null };
expect(getNestedValue(obj as Record<string, unknown>, 'a.b')).toBeUndefined();
});
it('should return undefined when traversing through primitive', () => {
const obj = { a: 'string' };
expect(getNestedValue(obj, 'a.b')).toBeUndefined();
});
it('should get object values', () => {
const obj = { a: { b: 'value' } };
expect(getNestedValue(obj, 'a')).toEqual({ b: 'value' });
});
it('should handle array values', () => {
const obj = { arr: [1, 2, 3] };
expect(getNestedValue(obj, 'arr')).toEqual([1, 2, 3]);
});
});
describe('setNestedValue', () => {
it('should set a top-level value', () => {
const obj: Record<string, unknown> = {};
setNestedValue(obj, 'foo', 'bar');
expect(obj.foo).toBe('bar');
});
it('should set a nested value', () => {
const obj: Record<string, unknown> = {};
setNestedValue(obj, 'a.b.c', 'deep');
expect((obj.a as Record<string, unknown>).b).toEqual({ c: 'deep' });
});
it('should create intermediate objects', () => {
const obj: Record<string, unknown> = {};
setNestedValue(obj, 'x.y.z', 'value');
expect(obj).toEqual({ x: { y: { z: 'value' } } });
});
it('should overwrite existing values', () => {
const obj: Record<string, unknown> = { a: 'old' };
setNestedValue(obj, 'a', 'new');
expect(obj.a).toBe('new');
});
it('should overwrite primitive with object when setting nested path', () => {
const obj: Record<string, unknown> = { a: 'string' };
setNestedValue(obj, 'a.b', 'value');
expect(obj.a).toEqual({ b: 'value' });
});
it('should preserve other keys when setting nested value', () => {
const obj: Record<string, unknown> = { a: { existing: 'keep' } };
setNestedValue(obj, 'a.new', 'added');
expect(obj.a).toEqual({ existing: 'keep', new: 'added' });
});
});
describe('deleteNestedValue', () => {
it('should delete a top-level key', () => {
const obj: Record<string, unknown> = { foo: 'bar', baz: 'qux' };
const result = deleteNestedValue(obj, 'foo');
expect(result).toBe(true);
expect(obj).toEqual({ baz: 'qux' });
});
it('should delete a nested key', () => {
const obj: Record<string, unknown> = { a: { b: 'value', c: 'keep' } };
const result = deleteNestedValue(obj, 'a.b');
expect(result).toBe(true);
expect(obj.a).toEqual({ c: 'keep' });
});
it('should return false for non-existent key', () => {
const obj: Record<string, unknown> = { foo: 'bar' };
const result = deleteNestedValue(obj, 'baz');
expect(result).toBe(false);
});
it('should return false for non-existent nested path', () => {
const obj: Record<string, unknown> = { a: { b: 'value' } };
const result = deleteNestedValue(obj, 'a.c');
expect(result).toBe(false);
});
it('should return false when intermediate path does not exist', () => {
const obj: Record<string, unknown> = { a: 'string' };
const result = deleteNestedValue(obj, 'a.b.c');
expect(result).toBe(false);
});
});
describe('coerceValue', () => {
it('should coerce "true" to boolean true', () => {
expect(coerceValue('true')).toBe(true);
});
it('should coerce "false" to boolean false', () => {
expect(coerceValue('false')).toBe(false);
});
it('should coerce integer string to number', () => {
expect(coerceValue('42')).toBe(42);
});
it('should coerce float string to number', () => {
expect(coerceValue('3.14')).toBe(3.14);
});
it('should coerce negative number string to number', () => {
expect(coerceValue('-10')).toBe(-10);
});
it('should keep regular strings as strings', () => {
expect(coerceValue('hello')).toBe('hello');
});
it('should keep strings that start with numbers but are not numbers', () => {
expect(coerceValue('123abc')).toBe('123abc');
});
it('should keep empty string as string', () => {
expect(coerceValue('')).toBe('');
});
it('should keep whitespace-only string as string', () => {
expect(coerceValue(' ')).toBe(' ');
});
it('should force string when forceString is true', () => {
expect(coerceValue('true', true)).toBe('true');
expect(coerceValue('42', true)).toBe('42');
expect(coerceValue('hello', true)).toBe('hello');
});
it('should not coerce Infinity to number (not finite)', () => {
// Infinity is not a useful config value, so we keep it as string
expect(coerceValue('Infinity')).toBe('Infinity');
});
it('should handle scientific notation', () => {
expect(coerceValue('1e10')).toBe(1e10);
});
});
describe('formatValueYaml', () => {
it('should format null as "null"', () => {
expect(formatValueYaml(null)).toBe('null');
});
it('should format undefined as "null"', () => {
expect(formatValueYaml(undefined)).toBe('null');
});
it('should format boolean as string', () => {
expect(formatValueYaml(true)).toBe('true');
expect(formatValueYaml(false)).toBe('false');
});
it('should format number as string', () => {
expect(formatValueYaml(42)).toBe('42');
expect(formatValueYaml(3.14)).toBe('3.14');
});
it('should format string as-is', () => {
expect(formatValueYaml('hello')).toBe('hello');
});
it('should format empty array as "[]"', () => {
expect(formatValueYaml([])).toBe('[]');
});
it('should format empty object as "{}"', () => {
expect(formatValueYaml({})).toBe('{}');
});
it('should format object with key-value pairs', () => {
const result = formatValueYaml({ foo: 'bar' });
expect(result).toBe('foo: bar');
});
it('should format nested objects with indentation', () => {
const result = formatValueYaml({ a: { b: 'value' } });
expect(result).toContain('a:');
expect(result).toContain('b: value');
});
});
describe('validateConfig', () => {
it('should accept valid config with featureFlags', () => {
const result = validateConfig({ featureFlags: { test: true } });
expect(result.success).toBe(true);
});
it('should accept empty featureFlags', () => {
const result = validateConfig({ featureFlags: {} });
expect(result.success).toBe(true);
});
it('should accept config without featureFlags (uses default)', () => {
const result = validateConfig({});
expect(result.success).toBe(true);
});
it('should accept unknown fields (passthrough)', () => {
const result = validateConfig({ featureFlags: {}, unknownField: 'value' });
expect(result.success).toBe(true);
});
it('should accept unknown fields with various types', () => {
const result = validateConfig({
featureFlags: {},
futureStringField: 'value',
futureNumberField: 123,
futureObjectField: { nested: 'data' },
});
expect(result.success).toBe(true);
});
it('should reject non-boolean values in featureFlags', () => {
const result = validateConfig({ featureFlags: { test: 'string' } });
expect(result.success).toBe(false);
expect(result.error).toBeDefined();
});
it('should include path in error message for invalid featureFlags', () => {
const result = validateConfig({ featureFlags: { someFlag: 'notABoolean' } });
expect(result.success).toBe(false);
expect(result.error).toContain('featureFlags');
});
it('should reject non-object featureFlags', () => {
const result = validateConfig({ featureFlags: 'string' });
expect(result.success).toBe(false);
});
it('should reject number values in featureFlags', () => {
const result = validateConfig({ featureFlags: { flag: 123 } });
expect(result.success).toBe(false);
});
});
describe('config set simulation', () => {
// These tests simulate the full config set flow: coerce value → set nested → validate
it('should accept setting unknown top-level key (forward compatibility)', () => {
const config: Record<string, unknown> = { featureFlags: {} };
const value = coerceValue('123');
setNestedValue(config, 'someFutureKey', value);
const result = validateConfig(config);
expect(result.success).toBe(true);
expect(config.someFutureKey).toBe(123);
});
it('should reject setting non-boolean to featureFlags', () => {
const config: Record<string, unknown> = { featureFlags: {} };
const value = coerceValue('notABoolean'); // stays as string
setNestedValue(config, 'featureFlags.someFlag', value);
const result = validateConfig(config);
expect(result.success).toBe(false);
expect(result.error).toContain('featureFlags');
});
it('should accept setting boolean to featureFlags', () => {
const config: Record<string, unknown> = { featureFlags: {} };
const value = coerceValue('true'); // coerces to boolean
setNestedValue(config, 'featureFlags.newFlag', value);
const result = validateConfig(config);
expect(result.success).toBe(true);
expect((config.featureFlags as Record<string, unknown>).newFlag).toBe(true);
});
it('should create featureFlags object when setting nested flag', () => {
const config: Record<string, unknown> = {};
const value = coerceValue('false');
setNestedValue(config, 'featureFlags.experimental', value);
const result = validateConfig(config);
expect(result.success).toBe(true);
expect((config.featureFlags as Record<string, unknown>).experimental).toBe(false);
});
});
describe('GlobalConfigSchema', () => {
it('should parse valid config', () => {
const result = GlobalConfigSchema.safeParse({ featureFlags: { test: true } });
expect(result.success).toBe(true);
});
it('should provide defaults for missing featureFlags', () => {
const result = GlobalConfigSchema.parse({});
expect(result.featureFlags).toEqual({});
});
});
describe('DEFAULT_CONFIG', () => {
it('should have empty featureFlags', () => {
expect(DEFAULT_CONFIG.featureFlags).toEqual({});
});
});
});