@cbashik/commit 1.0.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.
- package/.claude/CLAUDE.md +123 -0
- package/.github/workflows/publish-npm.yml +26 -0
- package/.husky/pre-commit +68 -0
- package/.vscode/settings.json +53 -0
- package/.zed/settings.json +50 -0
- package/AGENTS.md +239 -0
- package/biome.jsonc +4 -0
- package/dist/index.js +271 -0
- package/dist/index.js.map +1 -0
- package/package.json +45 -0
- package/src/index.ts +330 -0
- package/tsconfig.json +16 -0
- package/tsup.config.ts +13 -0
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
# Ultracite Code Standards
|
|
2
|
+
|
|
3
|
+
This project uses **Ultracite**, a zero-config preset that enforces strict code quality standards through automated formatting and linting.
|
|
4
|
+
|
|
5
|
+
## Quick Reference
|
|
6
|
+
|
|
7
|
+
- **Format code**: `npm exec -- ultracite fix`
|
|
8
|
+
- **Check for issues**: `npm exec -- ultracite check`
|
|
9
|
+
- **Diagnose setup**: `npm exec -- ultracite doctor`
|
|
10
|
+
|
|
11
|
+
Biome (the underlying engine) provides robust linting and formatting. Most issues are automatically fixable.
|
|
12
|
+
|
|
13
|
+
---
|
|
14
|
+
|
|
15
|
+
## Core Principles
|
|
16
|
+
|
|
17
|
+
Write code that is **accessible, performant, type-safe, and maintainable**. Focus on clarity and explicit intent over brevity.
|
|
18
|
+
|
|
19
|
+
### Type Safety & Explicitness
|
|
20
|
+
|
|
21
|
+
- Use explicit types for function parameters and return values when they enhance clarity
|
|
22
|
+
- Prefer `unknown` over `any` when the type is genuinely unknown
|
|
23
|
+
- Use const assertions (`as const`) for immutable values and literal types
|
|
24
|
+
- Leverage TypeScript's type narrowing instead of type assertions
|
|
25
|
+
- Use meaningful variable names instead of magic numbers - extract constants with descriptive names
|
|
26
|
+
|
|
27
|
+
### Modern JavaScript/TypeScript
|
|
28
|
+
|
|
29
|
+
- Use arrow functions for callbacks and short functions
|
|
30
|
+
- Prefer `for...of` loops over `.forEach()` and indexed `for` loops
|
|
31
|
+
- Use optional chaining (`?.`) and nullish coalescing (`??`) for safer property access
|
|
32
|
+
- Prefer template literals over string concatenation
|
|
33
|
+
- Use destructuring for object and array assignments
|
|
34
|
+
- Use `const` by default, `let` only when reassignment is needed, never `var`
|
|
35
|
+
|
|
36
|
+
### Async & Promises
|
|
37
|
+
|
|
38
|
+
- Always `await` promises in async functions - don't forget to use the return value
|
|
39
|
+
- Use `async/await` syntax instead of promise chains for better readability
|
|
40
|
+
- Handle errors appropriately in async code with try-catch blocks
|
|
41
|
+
- Don't use async functions as Promise executors
|
|
42
|
+
|
|
43
|
+
### React & JSX
|
|
44
|
+
|
|
45
|
+
- Use function components over class components
|
|
46
|
+
- Call hooks at the top level only, never conditionally
|
|
47
|
+
- Specify all dependencies in hook dependency arrays correctly
|
|
48
|
+
- Use the `key` prop for elements in iterables (prefer unique IDs over array indices)
|
|
49
|
+
- Nest children between opening and closing tags instead of passing as props
|
|
50
|
+
- Don't define components inside other components
|
|
51
|
+
- Use semantic HTML and ARIA attributes for accessibility:
|
|
52
|
+
- Provide meaningful alt text for images
|
|
53
|
+
- Use proper heading hierarchy
|
|
54
|
+
- Add labels for form inputs
|
|
55
|
+
- Include keyboard event handlers alongside mouse events
|
|
56
|
+
- Use semantic elements (`<button>`, `<nav>`, etc.) instead of divs with roles
|
|
57
|
+
|
|
58
|
+
### Error Handling & Debugging
|
|
59
|
+
|
|
60
|
+
- Remove `console.log`, `debugger`, and `alert` statements from production code
|
|
61
|
+
- Throw `Error` objects with descriptive messages, not strings or other values
|
|
62
|
+
- Use `try-catch` blocks meaningfully - don't catch errors just to rethrow them
|
|
63
|
+
- Prefer early returns over nested conditionals for error cases
|
|
64
|
+
|
|
65
|
+
### Code Organization
|
|
66
|
+
|
|
67
|
+
- Keep functions focused and under reasonable cognitive complexity limits
|
|
68
|
+
- Extract complex conditions into well-named boolean variables
|
|
69
|
+
- Use early returns to reduce nesting
|
|
70
|
+
- Prefer simple conditionals over nested ternary operators
|
|
71
|
+
- Group related code together and separate concerns
|
|
72
|
+
|
|
73
|
+
### Security
|
|
74
|
+
|
|
75
|
+
- Add `rel="noopener"` when using `target="_blank"` on links
|
|
76
|
+
- Avoid `dangerouslySetInnerHTML` unless absolutely necessary
|
|
77
|
+
- Don't use `eval()` or assign directly to `document.cookie`
|
|
78
|
+
- Validate and sanitize user input
|
|
79
|
+
|
|
80
|
+
### Performance
|
|
81
|
+
|
|
82
|
+
- Avoid spread syntax in accumulators within loops
|
|
83
|
+
- Use top-level regex literals instead of creating them in loops
|
|
84
|
+
- Prefer specific imports over namespace imports
|
|
85
|
+
- Avoid barrel files (index files that re-export everything)
|
|
86
|
+
- Use proper image components (e.g., Next.js `<Image>`) over `<img>` tags
|
|
87
|
+
|
|
88
|
+
### Framework-Specific Guidance
|
|
89
|
+
|
|
90
|
+
**Next.js:**
|
|
91
|
+
- Use Next.js `<Image>` component for images
|
|
92
|
+
- Use `next/head` or App Router metadata API for head elements
|
|
93
|
+
- Use Server Components for async data fetching instead of async Client Components
|
|
94
|
+
|
|
95
|
+
**React 19+:**
|
|
96
|
+
- Use ref as a prop instead of `React.forwardRef`
|
|
97
|
+
|
|
98
|
+
**Solid/Svelte/Vue/Qwik:**
|
|
99
|
+
- Use `class` and `for` attributes (not `className` or `htmlFor`)
|
|
100
|
+
|
|
101
|
+
---
|
|
102
|
+
|
|
103
|
+
## Testing
|
|
104
|
+
|
|
105
|
+
- Write assertions inside `it()` or `test()` blocks
|
|
106
|
+
- Avoid done callbacks in async tests - use async/await instead
|
|
107
|
+
- Don't use `.only` or `.skip` in committed code
|
|
108
|
+
- Keep test suites reasonably flat - avoid excessive `describe` nesting
|
|
109
|
+
|
|
110
|
+
## When Biome Can't Help
|
|
111
|
+
|
|
112
|
+
Biome's linter will catch most issues automatically. Focus your attention on:
|
|
113
|
+
|
|
114
|
+
1. **Business logic correctness** - Biome can't validate your algorithms
|
|
115
|
+
2. **Meaningful naming** - Use descriptive names for functions, variables, and types
|
|
116
|
+
3. **Architecture decisions** - Component structure, data flow, and API design
|
|
117
|
+
4. **Edge cases** - Handle boundary conditions and error states
|
|
118
|
+
5. **User experience** - Accessibility, performance, and usability considerations
|
|
119
|
+
6. **Documentation** - Add comments for complex logic, but prefer self-documenting code
|
|
120
|
+
|
|
121
|
+
---
|
|
122
|
+
|
|
123
|
+
Most formatting and common issues are automatically fixed by Biome. Run `npm exec -- ultracite fix` before committing to ensure compliance.
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
name: Publish to npm
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
release:
|
|
5
|
+
types: [published]
|
|
6
|
+
|
|
7
|
+
jobs:
|
|
8
|
+
publish:
|
|
9
|
+
runs-on: ubuntu-latest
|
|
10
|
+
permissions:
|
|
11
|
+
contents: read
|
|
12
|
+
steps:
|
|
13
|
+
- name: Checkout
|
|
14
|
+
uses: actions/checkout@v4
|
|
15
|
+
- name: Setup Node
|
|
16
|
+
uses: actions/setup-node@v4
|
|
17
|
+
with:
|
|
18
|
+
node-version: "20.x"
|
|
19
|
+
registry-url: "https://registry.npmjs.org"
|
|
20
|
+
cache: "npm"
|
|
21
|
+
- name: Install
|
|
22
|
+
run: npm ci
|
|
23
|
+
- name: Publish
|
|
24
|
+
run: npm publish --access public
|
|
25
|
+
env:
|
|
26
|
+
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
#!/bin/sh
|
|
2
|
+
# Exit on any error
|
|
3
|
+
set -e
|
|
4
|
+
|
|
5
|
+
# Check if there are any staged files
|
|
6
|
+
if [ -z "$(git diff --cached --name-only)" ]; then
|
|
7
|
+
echo "No staged files to format"
|
|
8
|
+
exit 0
|
|
9
|
+
fi
|
|
10
|
+
|
|
11
|
+
# Store the hash of staged changes to detect modifications
|
|
12
|
+
STAGED_HASH=$(git diff --cached | sha256sum | cut -d' ' -f1)
|
|
13
|
+
|
|
14
|
+
# Save list of staged files (handling all file states)
|
|
15
|
+
STAGED_FILES=$(git diff --cached --name-only --diff-filter=ACMR)
|
|
16
|
+
PARTIALLY_STAGED=$(git diff --name-only)
|
|
17
|
+
|
|
18
|
+
# Stash unstaged changes to preserve working directory
|
|
19
|
+
# --keep-index keeps staged changes in working tree
|
|
20
|
+
git stash push --quiet --keep-index --message "pre-commit-stash" || true
|
|
21
|
+
STASHED=$?
|
|
22
|
+
|
|
23
|
+
# Run formatter on the staged files
|
|
24
|
+
npx ultracite fix
|
|
25
|
+
FORMAT_EXIT_CODE=$?
|
|
26
|
+
|
|
27
|
+
# Restore working directory state
|
|
28
|
+
if [ $STASHED -eq 0 ]; then
|
|
29
|
+
# Re-stage the formatted files
|
|
30
|
+
if [ -n "$STAGED_FILES" ]; then
|
|
31
|
+
echo "$STAGED_FILES" | while IFS= read -r file; do
|
|
32
|
+
if [ -f "$file" ]; then
|
|
33
|
+
git add "$file"
|
|
34
|
+
fi
|
|
35
|
+
done
|
|
36
|
+
fi
|
|
37
|
+
|
|
38
|
+
# Restore unstaged changes
|
|
39
|
+
git stash pop --quiet || true
|
|
40
|
+
|
|
41
|
+
# Restore partial staging if files were partially staged
|
|
42
|
+
if [ -n "$PARTIALLY_STAGED" ]; then
|
|
43
|
+
for file in $PARTIALLY_STAGED; do
|
|
44
|
+
if [ -f "$file" ] && echo "$STAGED_FILES" | grep -q "^$file$"; then
|
|
45
|
+
# File was partially staged - need to unstage the unstaged parts
|
|
46
|
+
git restore --staged "$file" 2>/dev/null || true
|
|
47
|
+
git add -p "$file" < /dev/null 2>/dev/null || git add "$file"
|
|
48
|
+
fi
|
|
49
|
+
done
|
|
50
|
+
fi
|
|
51
|
+
else
|
|
52
|
+
# No stash was created, just re-add the formatted files
|
|
53
|
+
if [ -n "$STAGED_FILES" ]; then
|
|
54
|
+
echo "$STAGED_FILES" | while IFS= read -r file; do
|
|
55
|
+
if [ -f "$file" ]; then
|
|
56
|
+
git add "$file"
|
|
57
|
+
fi
|
|
58
|
+
done
|
|
59
|
+
fi
|
|
60
|
+
fi
|
|
61
|
+
|
|
62
|
+
# Check if staged files actually changed
|
|
63
|
+
NEW_STAGED_HASH=$(git diff --cached | sha256sum | cut -d' ' -f1)
|
|
64
|
+
if [ "$STAGED_HASH" != "$NEW_STAGED_HASH" ]; then
|
|
65
|
+
echo "✨ Files formatted by Ultracite"
|
|
66
|
+
fi
|
|
67
|
+
|
|
68
|
+
exit $FORMAT_EXIT_CODE
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
{
|
|
2
|
+
"editor.defaultFormatter": "esbenp.prettier-vscode",
|
|
3
|
+
"typescript.tsdk": "node_modules/typescript/lib",
|
|
4
|
+
"editor.formatOnSave": true,
|
|
5
|
+
"editor.formatOnPaste": true,
|
|
6
|
+
"emmet.showExpandedAbbreviation": "never",
|
|
7
|
+
"[javascript]": {
|
|
8
|
+
"editor.defaultFormatter": "biomejs.biome"
|
|
9
|
+
},
|
|
10
|
+
"[typescript]": {
|
|
11
|
+
"editor.defaultFormatter": "biomejs.biome"
|
|
12
|
+
},
|
|
13
|
+
"[javascriptreact]": {
|
|
14
|
+
"editor.defaultFormatter": "biomejs.biome"
|
|
15
|
+
},
|
|
16
|
+
"[typescriptreact]": {
|
|
17
|
+
"editor.defaultFormatter": "biomejs.biome"
|
|
18
|
+
},
|
|
19
|
+
"[json]": {
|
|
20
|
+
"editor.defaultFormatter": "biomejs.biome"
|
|
21
|
+
},
|
|
22
|
+
"[jsonc]": {
|
|
23
|
+
"editor.defaultFormatter": "biomejs.biome"
|
|
24
|
+
},
|
|
25
|
+
"[html]": {
|
|
26
|
+
"editor.defaultFormatter": "biomejs.biome"
|
|
27
|
+
},
|
|
28
|
+
"[vue]": {
|
|
29
|
+
"editor.defaultFormatter": "biomejs.biome"
|
|
30
|
+
},
|
|
31
|
+
"[svelte]": {
|
|
32
|
+
"editor.defaultFormatter": "biomejs.biome"
|
|
33
|
+
},
|
|
34
|
+
"[css]": {
|
|
35
|
+
"editor.defaultFormatter": "biomejs.biome"
|
|
36
|
+
},
|
|
37
|
+
"[yaml]": {
|
|
38
|
+
"editor.defaultFormatter": "biomejs.biome"
|
|
39
|
+
},
|
|
40
|
+
"[graphql]": {
|
|
41
|
+
"editor.defaultFormatter": "biomejs.biome"
|
|
42
|
+
},
|
|
43
|
+
"[markdown]": {
|
|
44
|
+
"editor.defaultFormatter": "biomejs.biome"
|
|
45
|
+
},
|
|
46
|
+
"[mdx]": {
|
|
47
|
+
"editor.defaultFormatter": "biomejs.biome"
|
|
48
|
+
},
|
|
49
|
+
"editor.codeActionsOnSave": {
|
|
50
|
+
"source.fixAll.biome": "explicit",
|
|
51
|
+
"source.organizeImports.biome": "explicit"
|
|
52
|
+
}
|
|
53
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
{
|
|
2
|
+
"formatter": "language_server",
|
|
3
|
+
"format_on_save": "on",
|
|
4
|
+
"lsp": {
|
|
5
|
+
"typescript-language-server": {
|
|
6
|
+
"settings": {
|
|
7
|
+
"typescript": {
|
|
8
|
+
"preferences": {
|
|
9
|
+
"includePackageJsonAutoImports": "on"
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
},
|
|
15
|
+
"languages": {
|
|
16
|
+
"JavaScript": {
|
|
17
|
+
"formatter": {
|
|
18
|
+
"language_server": {
|
|
19
|
+
"name": "biome"
|
|
20
|
+
}
|
|
21
|
+
},
|
|
22
|
+
"code_actions_on_format": {
|
|
23
|
+
"source.fixAll.biome": true,
|
|
24
|
+
"source.organizeImports.biome": true
|
|
25
|
+
}
|
|
26
|
+
},
|
|
27
|
+
"TypeScript": {
|
|
28
|
+
"formatter": {
|
|
29
|
+
"language_server": {
|
|
30
|
+
"name": "biome"
|
|
31
|
+
}
|
|
32
|
+
},
|
|
33
|
+
"code_actions_on_format": {
|
|
34
|
+
"source.fixAll.biome": true,
|
|
35
|
+
"source.organizeImports.biome": true
|
|
36
|
+
}
|
|
37
|
+
},
|
|
38
|
+
"TSX": {
|
|
39
|
+
"formatter": {
|
|
40
|
+
"language_server": {
|
|
41
|
+
"name": "biome"
|
|
42
|
+
}
|
|
43
|
+
},
|
|
44
|
+
"code_actions_on_format": {
|
|
45
|
+
"source.fixAll.biome": true,
|
|
46
|
+
"source.organizeImports.biome": true
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
}
|
package/AGENTS.md
ADDED
|
@@ -0,0 +1,239 @@
|
|
|
1
|
+
# AGENTS.md
|
|
2
|
+
# Guidance for coding agents working in this repo.
|
|
3
|
+
|
|
4
|
+
## Scope
|
|
5
|
+
- Project: commit-cli (TypeScript, ESM, Node.js)
|
|
6
|
+
- Purpose: CLI to generate commit messages via Claude
|
|
7
|
+
- Source: `src/`, build output: `dist/`
|
|
8
|
+
- Package manager: npm (package-lock.json present)
|
|
9
|
+
|
|
10
|
+
## Commands
|
|
11
|
+
### Install
|
|
12
|
+
- `npm install`
|
|
13
|
+
|
|
14
|
+
### Build
|
|
15
|
+
- `npm run build` (tsup build to `dist/`)
|
|
16
|
+
- `npm run dev` (tsup watch)
|
|
17
|
+
|
|
18
|
+
### Lint
|
|
19
|
+
- No lint script is configured in `package.json`.
|
|
20
|
+
- If you add a linter, also add `npm run lint` here.
|
|
21
|
+
|
|
22
|
+
### Tests
|
|
23
|
+
- No test runner is configured in `package.json`.
|
|
24
|
+
- There is no known single-test command in this repo.
|
|
25
|
+
- If you add tests, document the exact single-test command here.
|
|
26
|
+
|
|
27
|
+
## Repo Layout
|
|
28
|
+
- `src/index.ts`: CLI entry point
|
|
29
|
+
- `dist/`: build output
|
|
30
|
+
- `tsconfig.json`: TypeScript config (strict)
|
|
31
|
+
- `tsup.config.ts`: build config
|
|
32
|
+
|
|
33
|
+
## Cursor/Copilot Rules
|
|
34
|
+
- No Cursor rules found in `.cursor/rules/` or `.cursorrules`.
|
|
35
|
+
- No Copilot rules found in `.github/copilot-instructions.md`.
|
|
36
|
+
- If you add rules, summarize them in this section.
|
|
37
|
+
|
|
38
|
+
## Language and Runtime
|
|
39
|
+
- TypeScript, ESM (`"type": "module"`)
|
|
40
|
+
- Target: ES2022
|
|
41
|
+
- Module resolution: `bundler`
|
|
42
|
+
- Strict TS enabled (`"strict": true`)
|
|
43
|
+
|
|
44
|
+
## Code Style
|
|
45
|
+
### Formatting
|
|
46
|
+
- Use double quotes for strings.
|
|
47
|
+
- Keep semicolons.
|
|
48
|
+
- Use trailing commas where already used.
|
|
49
|
+
- Keep line lengths reasonable (existing file uses multi-line strings for long text).
|
|
50
|
+
- Prefer compact, readable blocks over overly clever expressions.
|
|
51
|
+
|
|
52
|
+
### Imports
|
|
53
|
+
- Prefer Node built-ins with `node:` specifier (example: `node:child_process`).
|
|
54
|
+
- Keep third-party imports after Node imports.
|
|
55
|
+
- Use named imports when appropriate; default import for modules like `picocolors`.
|
|
56
|
+
|
|
57
|
+
### Types
|
|
58
|
+
- Use explicit return types on exported/async functions when clarity helps.
|
|
59
|
+
- Keep types simple; prefer built-in types over custom types unless reused.
|
|
60
|
+
- `strict` is on: avoid implicit `any` and unsafe narrowing.
|
|
61
|
+
|
|
62
|
+
### Naming
|
|
63
|
+
- Use `camelCase` for functions/variables.
|
|
64
|
+
- Use `UPPER_SNAKE_CASE` for constants.
|
|
65
|
+
- Use descriptive names for CLI actions and state flags.
|
|
66
|
+
|
|
67
|
+
### Error Handling
|
|
68
|
+
- Wrap shell invocations with `try/catch` and fail gracefully.
|
|
69
|
+
- For fatal CLI errors, log a clear message and `process.exit(1)`.
|
|
70
|
+
- For non-fatal scenarios, print user-friendly guidance and `process.exit(0)`.
|
|
71
|
+
|
|
72
|
+
### CLI UX
|
|
73
|
+
- Use `@inquirer/prompts` for interactive choices.
|
|
74
|
+
- Prefer clear, short prompts and visible next steps.
|
|
75
|
+
- Colorized output uses `picocolors`; keep output consistent.
|
|
76
|
+
|
|
77
|
+
### Git Interaction
|
|
78
|
+
- Use `execSync`/`spawn` with safe quoting when interpolating user input.
|
|
79
|
+
- Prefer `git diff --cached` for staged changes, fall back to `git diff`.
|
|
80
|
+
- Keep git commands in one place; do not add hidden side effects.
|
|
81
|
+
|
|
82
|
+
## Build and Output
|
|
83
|
+
- `tsup` builds from `src/` to `dist/`.
|
|
84
|
+
- The CLI entry is `dist/index.js`.
|
|
85
|
+
- Do not hand-edit `dist/`; always rebuild.
|
|
86
|
+
|
|
87
|
+
## Dependency Guidelines
|
|
88
|
+
- Keep dependencies minimal; this is a small CLI.
|
|
89
|
+
- Prefer Node stdlib over new packages.
|
|
90
|
+
- If adding a dependency, justify it in commit/PR summary.
|
|
91
|
+
|
|
92
|
+
## File Editing Tips
|
|
93
|
+
- Primary file is `src/index.ts`.
|
|
94
|
+
- Keep the main flow in `main()` readable.
|
|
95
|
+
- Avoid unnecessary comments; add only for non-obvious logic.
|
|
96
|
+
|
|
97
|
+
## Example Workflows
|
|
98
|
+
- Build and run:
|
|
99
|
+
- `npm run build`
|
|
100
|
+
- `node dist/index.js`
|
|
101
|
+
- Local dev watch:
|
|
102
|
+
- `npm run dev`
|
|
103
|
+
|
|
104
|
+
## When Adding Tests/Linting
|
|
105
|
+
- Add scripts to `package.json`.
|
|
106
|
+
- Update this file with:
|
|
107
|
+
- How to run all tests
|
|
108
|
+
- How to run a single test
|
|
109
|
+
- Any lint/format commands
|
|
110
|
+
|
|
111
|
+
## Notes for Agents
|
|
112
|
+
- Keep changes focused; avoid touching unrelated files.
|
|
113
|
+
- Respect existing behavior of the CLI (staging prompts, commit flow).
|
|
114
|
+
- Maintain consistent output messages and style.
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
# Ultracite Code Standards
|
|
118
|
+
|
|
119
|
+
This project uses **Ultracite**, a zero-config preset that enforces strict code quality standards through automated formatting and linting.
|
|
120
|
+
|
|
121
|
+
## Quick Reference
|
|
122
|
+
|
|
123
|
+
- **Format code**: `npm exec -- ultracite fix`
|
|
124
|
+
- **Check for issues**: `npm exec -- ultracite check`
|
|
125
|
+
- **Diagnose setup**: `npm exec -- ultracite doctor`
|
|
126
|
+
|
|
127
|
+
Biome (the underlying engine) provides robust linting and formatting. Most issues are automatically fixable.
|
|
128
|
+
|
|
129
|
+
---
|
|
130
|
+
|
|
131
|
+
## Core Principles
|
|
132
|
+
|
|
133
|
+
Write code that is **accessible, performant, type-safe, and maintainable**. Focus on clarity and explicit intent over brevity.
|
|
134
|
+
|
|
135
|
+
### Type Safety & Explicitness
|
|
136
|
+
|
|
137
|
+
- Use explicit types for function parameters and return values when they enhance clarity
|
|
138
|
+
- Prefer `unknown` over `any` when the type is genuinely unknown
|
|
139
|
+
- Use const assertions (`as const`) for immutable values and literal types
|
|
140
|
+
- Leverage TypeScript's type narrowing instead of type assertions
|
|
141
|
+
- Use meaningful variable names instead of magic numbers - extract constants with descriptive names
|
|
142
|
+
|
|
143
|
+
### Modern JavaScript/TypeScript
|
|
144
|
+
|
|
145
|
+
- Use arrow functions for callbacks and short functions
|
|
146
|
+
- Prefer `for...of` loops over `.forEach()` and indexed `for` loops
|
|
147
|
+
- Use optional chaining (`?.`) and nullish coalescing (`??`) for safer property access
|
|
148
|
+
- Prefer template literals over string concatenation
|
|
149
|
+
- Use destructuring for object and array assignments
|
|
150
|
+
- Use `const` by default, `let` only when reassignment is needed, never `var`
|
|
151
|
+
|
|
152
|
+
### Async & Promises
|
|
153
|
+
|
|
154
|
+
- Always `await` promises in async functions - don't forget to use the return value
|
|
155
|
+
- Use `async/await` syntax instead of promise chains for better readability
|
|
156
|
+
- Handle errors appropriately in async code with try-catch blocks
|
|
157
|
+
- Don't use async functions as Promise executors
|
|
158
|
+
|
|
159
|
+
### React & JSX
|
|
160
|
+
|
|
161
|
+
- Use function components over class components
|
|
162
|
+
- Call hooks at the top level only, never conditionally
|
|
163
|
+
- Specify all dependencies in hook dependency arrays correctly
|
|
164
|
+
- Use the `key` prop for elements in iterables (prefer unique IDs over array indices)
|
|
165
|
+
- Nest children between opening and closing tags instead of passing as props
|
|
166
|
+
- Don't define components inside other components
|
|
167
|
+
- Use semantic HTML and ARIA attributes for accessibility:
|
|
168
|
+
- Provide meaningful alt text for images
|
|
169
|
+
- Use proper heading hierarchy
|
|
170
|
+
- Add labels for form inputs
|
|
171
|
+
- Include keyboard event handlers alongside mouse events
|
|
172
|
+
- Use semantic elements (`<button>`, `<nav>`, etc.) instead of divs with roles
|
|
173
|
+
|
|
174
|
+
### Error Handling & Debugging
|
|
175
|
+
|
|
176
|
+
- Remove `console.log`, `debugger`, and `alert` statements from production code
|
|
177
|
+
- Throw `Error` objects with descriptive messages, not strings or other values
|
|
178
|
+
- Use `try-catch` blocks meaningfully - don't catch errors just to rethrow them
|
|
179
|
+
- Prefer early returns over nested conditionals for error cases
|
|
180
|
+
|
|
181
|
+
### Code Organization
|
|
182
|
+
|
|
183
|
+
- Keep functions focused and under reasonable cognitive complexity limits
|
|
184
|
+
- Extract complex conditions into well-named boolean variables
|
|
185
|
+
- Use early returns to reduce nesting
|
|
186
|
+
- Prefer simple conditionals over nested ternary operators
|
|
187
|
+
- Group related code together and separate concerns
|
|
188
|
+
|
|
189
|
+
### Security
|
|
190
|
+
|
|
191
|
+
- Add `rel="noopener"` when using `target="_blank"` on links
|
|
192
|
+
- Avoid `dangerouslySetInnerHTML` unless absolutely necessary
|
|
193
|
+
- Don't use `eval()` or assign directly to `document.cookie`
|
|
194
|
+
- Validate and sanitize user input
|
|
195
|
+
|
|
196
|
+
### Performance
|
|
197
|
+
|
|
198
|
+
- Avoid spread syntax in accumulators within loops
|
|
199
|
+
- Use top-level regex literals instead of creating them in loops
|
|
200
|
+
- Prefer specific imports over namespace imports
|
|
201
|
+
- Avoid barrel files (index files that re-export everything)
|
|
202
|
+
- Use proper image components (e.g., Next.js `<Image>`) over `<img>` tags
|
|
203
|
+
|
|
204
|
+
### Framework-Specific Guidance
|
|
205
|
+
|
|
206
|
+
**Next.js:**
|
|
207
|
+
- Use Next.js `<Image>` component for images
|
|
208
|
+
- Use `next/head` or App Router metadata API for head elements
|
|
209
|
+
- Use Server Components for async data fetching instead of async Client Components
|
|
210
|
+
|
|
211
|
+
**React 19+:**
|
|
212
|
+
- Use ref as a prop instead of `React.forwardRef`
|
|
213
|
+
|
|
214
|
+
**Solid/Svelte/Vue/Qwik:**
|
|
215
|
+
- Use `class` and `for` attributes (not `className` or `htmlFor`)
|
|
216
|
+
|
|
217
|
+
---
|
|
218
|
+
|
|
219
|
+
## Testing
|
|
220
|
+
|
|
221
|
+
- Write assertions inside `it()` or `test()` blocks
|
|
222
|
+
- Avoid done callbacks in async tests - use async/await instead
|
|
223
|
+
- Don't use `.only` or `.skip` in committed code
|
|
224
|
+
- Keep test suites reasonably flat - avoid excessive `describe` nesting
|
|
225
|
+
|
|
226
|
+
## When Biome Can't Help
|
|
227
|
+
|
|
228
|
+
Biome's linter will catch most issues automatically. Focus your attention on:
|
|
229
|
+
|
|
230
|
+
1. **Business logic correctness** - Biome can't validate your algorithms
|
|
231
|
+
2. **Meaningful naming** - Use descriptive names for functions, variables, and types
|
|
232
|
+
3. **Architecture decisions** - Component structure, data flow, and API design
|
|
233
|
+
4. **Edge cases** - Handle boundary conditions and error states
|
|
234
|
+
5. **User experience** - Accessibility, performance, and usability considerations
|
|
235
|
+
6. **Documentation** - Add comments for complex logic, but prefer self-documenting code
|
|
236
|
+
|
|
237
|
+
---
|
|
238
|
+
|
|
239
|
+
Most formatting and common issues are automatically fixed by Biome. Run `npm exec -- ultracite fix` before committing to ensure compliance.
|
package/biome.jsonc
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,271 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
// src/index.ts
|
|
4
|
+
import { execSync, spawn } from "child_process";
|
|
5
|
+
import { input, select } from "@inquirer/prompts";
|
|
6
|
+
import pc from "picocolors";
|
|
7
|
+
var MAIN_PROMPT = `You are a commit message generator. Analyze the git diff and status provided and generate a concise, meaningful commit message.
|
|
8
|
+
|
|
9
|
+
Rules:
|
|
10
|
+
- Follow conventional commits format: type(scope): description
|
|
11
|
+
- Types: feat, fix, docs, style, refactor, test, chore, perf, ci, build
|
|
12
|
+
- Keep the first line under 72 characters
|
|
13
|
+
- Be specific about what changed
|
|
14
|
+
- Focus on the "why" not just the "what"
|
|
15
|
+
- If multiple changes, summarize the main change
|
|
16
|
+
- Do not include any explanation, just output the commit message
|
|
17
|
+
|
|
18
|
+
Output ONLY the commit message, nothing else.`;
|
|
19
|
+
function exec(command) {
|
|
20
|
+
try {
|
|
21
|
+
return execSync(command, { encoding: "utf-8" }).trim();
|
|
22
|
+
} catch {
|
|
23
|
+
return "";
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
function getGitStatus() {
|
|
27
|
+
return exec("git status --short");
|
|
28
|
+
}
|
|
29
|
+
function getGitDiff() {
|
|
30
|
+
const staged = exec("git diff --cached");
|
|
31
|
+
if (staged) {
|
|
32
|
+
return staged;
|
|
33
|
+
}
|
|
34
|
+
return exec("git diff");
|
|
35
|
+
}
|
|
36
|
+
function hasStagedChanges() {
|
|
37
|
+
return exec("git diff --cached --name-only").length > 0;
|
|
38
|
+
}
|
|
39
|
+
function hasUnstagedChanges() {
|
|
40
|
+
return exec("git diff --name-only").length > 0;
|
|
41
|
+
}
|
|
42
|
+
function hasUntrackedFiles() {
|
|
43
|
+
return exec("git ls-files --others --exclude-standard").length > 0;
|
|
44
|
+
}
|
|
45
|
+
function getStagedFiles() {
|
|
46
|
+
const output = exec("git diff --cached --name-only");
|
|
47
|
+
return output ? output.split("\n").filter(Boolean) : [];
|
|
48
|
+
}
|
|
49
|
+
function getUnstagedFiles() {
|
|
50
|
+
const output = exec("git diff --name-only");
|
|
51
|
+
return output ? output.split("\n").filter(Boolean) : [];
|
|
52
|
+
}
|
|
53
|
+
function getPartiallyStagedFiles() {
|
|
54
|
+
const staged = new Set(getStagedFiles());
|
|
55
|
+
const unstaged = getUnstagedFiles();
|
|
56
|
+
return unstaged.filter((file) => staged.has(file));
|
|
57
|
+
}
|
|
58
|
+
async function promptForStagingChanges() {
|
|
59
|
+
const staged = hasStagedChanges();
|
|
60
|
+
const unstaged = hasUnstagedChanges();
|
|
61
|
+
const untracked = hasUntrackedFiles();
|
|
62
|
+
if (!staged && (unstaged || untracked)) {
|
|
63
|
+
console.log(pc.yellow("No staged changes. Stage your changes first:"));
|
|
64
|
+
console.log(pc.dim(" git add <files>"));
|
|
65
|
+
console.log(pc.dim(" git add -A (to stage all)\n"));
|
|
66
|
+
const shouldStageAll = await select({
|
|
67
|
+
message: "Would you like to stage all changes?",
|
|
68
|
+
choices: [
|
|
69
|
+
{ name: "Yes, stage all changes", value: "yes" },
|
|
70
|
+
{ name: "No, exit", value: "no" }
|
|
71
|
+
]
|
|
72
|
+
});
|
|
73
|
+
if (shouldStageAll === "yes") {
|
|
74
|
+
exec("git add -A");
|
|
75
|
+
console.log(pc.green("All changes staged\n"));
|
|
76
|
+
} else {
|
|
77
|
+
process.exit(0);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
if (staged && (unstaged || untracked)) {
|
|
81
|
+
const partiallyStaged = getPartiallyStagedFiles();
|
|
82
|
+
if (partiallyStaged.length > 0) {
|
|
83
|
+
console.log(pc.yellow("Some files are partially staged:"));
|
|
84
|
+
console.log(pc.dim(` ${partiallyStaged.join("\n ")}
|
|
85
|
+
`));
|
|
86
|
+
}
|
|
87
|
+
const stageMore = await select({
|
|
88
|
+
message: "Would you like to stage other changes too?",
|
|
89
|
+
choices: [
|
|
90
|
+
{ name: "Keep staged changes only", value: "keep" },
|
|
91
|
+
{ name: "Stage tracked changes", value: "tracked" },
|
|
92
|
+
{ name: "Stage all changes", value: "all" },
|
|
93
|
+
{ name: "Cancel", value: "cancel" }
|
|
94
|
+
]
|
|
95
|
+
});
|
|
96
|
+
if (stageMore === "tracked") {
|
|
97
|
+
exec("git add -u");
|
|
98
|
+
console.log(pc.green("Tracked changes staged\n"));
|
|
99
|
+
} else if (stageMore === "all") {
|
|
100
|
+
exec("git add -A");
|
|
101
|
+
console.log(pc.green("All changes staged\n"));
|
|
102
|
+
} else if (stageMore === "cancel") {
|
|
103
|
+
process.exit(0);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
var DEFAULT_PROVIDER = "codex";
|
|
108
|
+
var DEFAULT_MODELS = {
|
|
109
|
+
codex: "gpt-5.1-codex-mini",
|
|
110
|
+
claude: "claude-sonnet-4-20250514"
|
|
111
|
+
};
|
|
112
|
+
function getProviderConfig() {
|
|
113
|
+
const args = process.argv.slice(2);
|
|
114
|
+
const providerArgIndex = args.findIndex(
|
|
115
|
+
(arg) => arg === "-p" || arg === "--provider"
|
|
116
|
+
);
|
|
117
|
+
const modelArgIndex = args.findIndex(
|
|
118
|
+
(arg) => arg === "-m" || arg === "--model"
|
|
119
|
+
);
|
|
120
|
+
const providerFromArgs = providerArgIndex >= 0 ? args[providerArgIndex + 1] : void 0;
|
|
121
|
+
const modelFromArgs = modelArgIndex >= 0 ? args[modelArgIndex + 1] : void 0;
|
|
122
|
+
const provider = (providerFromArgs || process.env.COMMIT_CLI_PROVIDER || DEFAULT_PROVIDER).toLowerCase();
|
|
123
|
+
const model = modelFromArgs || process.env.COMMIT_CLI_MODEL || DEFAULT_MODELS[provider];
|
|
124
|
+
return { provider, model };
|
|
125
|
+
}
|
|
126
|
+
function getProviderCommand(provider, model) {
|
|
127
|
+
switch (provider) {
|
|
128
|
+
case "codex": {
|
|
129
|
+
const args = ["exec"];
|
|
130
|
+
if (model) {
|
|
131
|
+
args.push("--model", model);
|
|
132
|
+
}
|
|
133
|
+
args.push("-");
|
|
134
|
+
return { command: "codex", args, usesStdin: true };
|
|
135
|
+
}
|
|
136
|
+
case "claude": {
|
|
137
|
+
const args = ["-p"];
|
|
138
|
+
if (model) {
|
|
139
|
+
args.push("--model", model);
|
|
140
|
+
}
|
|
141
|
+
return { command: "claude", args, usesStdin: true };
|
|
142
|
+
}
|
|
143
|
+
default: {
|
|
144
|
+
const args = model ? ["--model", model] : [];
|
|
145
|
+
return { command: provider, args, usesStdin: true };
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
function generateCommitMessage(status, diff) {
|
|
150
|
+
const prompt = `${MAIN_PROMPT}
|
|
151
|
+
|
|
152
|
+
Git Status:
|
|
153
|
+
${status}
|
|
154
|
+
|
|
155
|
+
Git Diff:
|
|
156
|
+
${diff}`;
|
|
157
|
+
return new Promise((resolve, reject) => {
|
|
158
|
+
const { provider, model } = getProviderConfig();
|
|
159
|
+
const { command, args, usesStdin } = getProviderCommand(provider, model);
|
|
160
|
+
const child = spawn(command, args, {
|
|
161
|
+
stdio: ["pipe", "pipe", "pipe"]
|
|
162
|
+
});
|
|
163
|
+
let stdout = "";
|
|
164
|
+
let stderr = "";
|
|
165
|
+
child.stdout.on("data", (data) => {
|
|
166
|
+
stdout += data.toString();
|
|
167
|
+
});
|
|
168
|
+
child.stderr.on("data", (data) => {
|
|
169
|
+
stderr += data.toString();
|
|
170
|
+
});
|
|
171
|
+
child.on("close", (code) => {
|
|
172
|
+
if (code !== 0) {
|
|
173
|
+
const details = stderr || stdout;
|
|
174
|
+
const message = details ? `${provider} failed (exit ${code}): ${details.trim()}` : `${provider} failed (exit ${code})`;
|
|
175
|
+
reject(new Error(message));
|
|
176
|
+
} else {
|
|
177
|
+
resolve(stdout.trim());
|
|
178
|
+
}
|
|
179
|
+
});
|
|
180
|
+
child.on("error", (err) => {
|
|
181
|
+
reject(err);
|
|
182
|
+
});
|
|
183
|
+
if (usesStdin) {
|
|
184
|
+
child.stdin.write(prompt);
|
|
185
|
+
child.stdin.end();
|
|
186
|
+
}
|
|
187
|
+
});
|
|
188
|
+
}
|
|
189
|
+
function commitWithMessage(message) {
|
|
190
|
+
try {
|
|
191
|
+
execSync(`git commit -m "${message.replace(/"/g, '\\"')}"`, {
|
|
192
|
+
stdio: "inherit"
|
|
193
|
+
});
|
|
194
|
+
} catch {
|
|
195
|
+
console.error(pc.red("Failed to create commit"));
|
|
196
|
+
process.exit(1);
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
async function main() {
|
|
200
|
+
console.log(pc.cyan("\n Commit Message Generator\n"));
|
|
201
|
+
try {
|
|
202
|
+
exec("git rev-parse --is-inside-work-tree");
|
|
203
|
+
} catch {
|
|
204
|
+
console.error(pc.red("Error: Not a git repository"));
|
|
205
|
+
process.exit(1);
|
|
206
|
+
}
|
|
207
|
+
const status = getGitStatus();
|
|
208
|
+
if (!status) {
|
|
209
|
+
console.log(pc.yellow("No changes to commit"));
|
|
210
|
+
process.exit(0);
|
|
211
|
+
}
|
|
212
|
+
await promptForStagingChanges();
|
|
213
|
+
const diff = getGitDiff();
|
|
214
|
+
if (!diff) {
|
|
215
|
+
console.log(pc.yellow("No diff available to analyze"));
|
|
216
|
+
process.exit(0);
|
|
217
|
+
}
|
|
218
|
+
console.log(pc.dim("Analyzing changes...\n"));
|
|
219
|
+
let commitMessage;
|
|
220
|
+
try {
|
|
221
|
+
commitMessage = await generateCommitMessage(status, diff);
|
|
222
|
+
} catch (error) {
|
|
223
|
+
console.error(
|
|
224
|
+
pc.red("Failed to generate commit message:"),
|
|
225
|
+
error instanceof Error ? error.message : error
|
|
226
|
+
);
|
|
227
|
+
process.exit(1);
|
|
228
|
+
}
|
|
229
|
+
console.log(pc.green("Generated commit message:"));
|
|
230
|
+
console.log(pc.white(pc.bold(`
|
|
231
|
+
${commitMessage}
|
|
232
|
+
`)));
|
|
233
|
+
const action = await select({
|
|
234
|
+
message: "What would you like to do?",
|
|
235
|
+
choices: [
|
|
236
|
+
{ name: "Commit with this message", value: "commit" },
|
|
237
|
+
{ name: "Edit the message", value: "edit" },
|
|
238
|
+
{ name: "Cancel", value: "cancel" }
|
|
239
|
+
]
|
|
240
|
+
});
|
|
241
|
+
switch (action) {
|
|
242
|
+
case "commit":
|
|
243
|
+
commitWithMessage(commitMessage);
|
|
244
|
+
console.log(pc.green("\nCommit created successfully!"));
|
|
245
|
+
break;
|
|
246
|
+
case "edit": {
|
|
247
|
+
const editedMessage = await input({
|
|
248
|
+
message: "Enter your commit message:",
|
|
249
|
+
default: commitMessage
|
|
250
|
+
});
|
|
251
|
+
if (editedMessage.trim()) {
|
|
252
|
+
commitWithMessage(editedMessage.trim());
|
|
253
|
+
console.log(pc.green("\nCommit created successfully!"));
|
|
254
|
+
} else {
|
|
255
|
+
console.log(pc.yellow("Empty message, commit cancelled"));
|
|
256
|
+
}
|
|
257
|
+
break;
|
|
258
|
+
}
|
|
259
|
+
case "cancel":
|
|
260
|
+
console.log(pc.yellow("Commit cancelled"));
|
|
261
|
+
break;
|
|
262
|
+
default:
|
|
263
|
+
console.log(pc.yellow("No action selected"));
|
|
264
|
+
break;
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
main().catch((error) => {
|
|
268
|
+
console.error(pc.red("Error:"), error);
|
|
269
|
+
process.exit(1);
|
|
270
|
+
});
|
|
271
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import { execSync, spawn } from \"node:child_process\";\nimport { input, select } from \"@inquirer/prompts\";\nimport pc from \"picocolors\";\n\nconst MAIN_PROMPT = `You are a commit message generator. Analyze the git diff and status provided and generate a concise, meaningful commit message.\n\nRules:\n- Follow conventional commits format: type(scope): description\n- Types: feat, fix, docs, style, refactor, test, chore, perf, ci, build\n- Keep the first line under 72 characters\n- Be specific about what changed\n- Focus on the \"why\" not just the \"what\"\n- If multiple changes, summarize the main change\n- Do not include any explanation, just output the commit message\n\nOutput ONLY the commit message, nothing else.`;\n\nfunction exec(command: string): string {\n try {\n return execSync(command, { encoding: \"utf-8\" }).trim();\n } catch {\n return \"\";\n }\n}\n\nfunction getGitStatus(): string {\n return exec(\"git status --short\");\n}\n\nfunction getGitDiff(): string {\n // Get staged changes first, if none, get unstaged changes\n const staged = exec(\"git diff --cached\");\n if (staged) {\n return staged;\n }\n return exec(\"git diff\");\n}\n\nfunction hasStagedChanges(): boolean {\n return exec(\"git diff --cached --name-only\").length > 0;\n}\n\nfunction hasUnstagedChanges(): boolean {\n return exec(\"git diff --name-only\").length > 0;\n}\n\nfunction hasUntrackedFiles(): boolean {\n return exec(\"git ls-files --others --exclude-standard\").length > 0;\n}\n\nfunction getStagedFiles(): string[] {\n const output = exec(\"git diff --cached --name-only\");\n return output ? output.split(\"\\n\").filter(Boolean) : [];\n}\n\nfunction getUnstagedFiles(): string[] {\n const output = exec(\"git diff --name-only\");\n return output ? output.split(\"\\n\").filter(Boolean) : [];\n}\n\nfunction getPartiallyStagedFiles(): string[] {\n const staged = new Set(getStagedFiles());\n const unstaged = getUnstagedFiles();\n\n return unstaged.filter((file) => staged.has(file));\n}\n\nasync function promptForStagingChanges(): Promise<void> {\n const staged = hasStagedChanges();\n const unstaged = hasUnstagedChanges();\n const untracked = hasUntrackedFiles();\n\n if (!staged && (unstaged || untracked)) {\n console.log(pc.yellow(\"No staged changes. Stage your changes first:\"));\n console.log(pc.dim(\" git add <files>\"));\n console.log(pc.dim(\" git add -A (to stage all)\\n\"));\n\n const shouldStageAll = await select({\n message: \"Would you like to stage all changes?\",\n choices: [\n { name: \"Yes, stage all changes\", value: \"yes\" },\n { name: \"No, exit\", value: \"no\" },\n ],\n });\n\n if (shouldStageAll === \"yes\") {\n exec(\"git add -A\");\n console.log(pc.green(\"All changes staged\\n\"));\n } else {\n process.exit(0);\n }\n }\n\n if (staged && (unstaged || untracked)) {\n const partiallyStaged = getPartiallyStagedFiles();\n\n if (partiallyStaged.length > 0) {\n console.log(pc.yellow(\"Some files are partially staged:\"));\n console.log(pc.dim(` ${partiallyStaged.join(\"\\n \")}\\n`));\n }\n\n const stageMore = await select({\n message: \"Would you like to stage other changes too?\",\n choices: [\n { name: \"Keep staged changes only\", value: \"keep\" },\n { name: \"Stage tracked changes\", value: \"tracked\" },\n { name: \"Stage all changes\", value: \"all\" },\n { name: \"Cancel\", value: \"cancel\" },\n ],\n });\n\n if (stageMore === \"tracked\") {\n exec(\"git add -u\");\n console.log(pc.green(\"Tracked changes staged\\n\"));\n } else if (stageMore === \"all\") {\n exec(\"git add -A\");\n console.log(pc.green(\"All changes staged\\n\"));\n } else if (stageMore === \"cancel\") {\n process.exit(0);\n }\n }\n}\n\nconst DEFAULT_PROVIDER = \"codex\";\nconst DEFAULT_MODELS: Record<string, string> = {\n codex: \"gpt-5.1-codex-mini\",\n claude: \"claude-sonnet-4-20250514\",\n};\n\nfunction getProviderConfig(): { provider: string; model?: string } {\n const args = process.argv.slice(2);\n const providerArgIndex = args.findIndex(\n (arg) => arg === \"-p\" || arg === \"--provider\"\n );\n const modelArgIndex = args.findIndex(\n (arg) => arg === \"-m\" || arg === \"--model\"\n );\n\n const providerFromArgs =\n providerArgIndex >= 0 ? args[providerArgIndex + 1] : undefined;\n const modelFromArgs =\n modelArgIndex >= 0 ? args[modelArgIndex + 1] : undefined;\n\n const provider = (\n providerFromArgs ||\n process.env.COMMIT_CLI_PROVIDER ||\n DEFAULT_PROVIDER\n ).toLowerCase();\n const model =\n modelFromArgs || process.env.COMMIT_CLI_MODEL || DEFAULT_MODELS[provider];\n\n return { provider, model };\n}\n\nfunction getProviderCommand(\n provider: string,\n model?: string\n): { command: string; args: string[]; usesStdin: boolean } {\n switch (provider) {\n case \"codex\": {\n const args = [\"exec\"];\n if (model) {\n args.push(\"--model\", model);\n }\n args.push(\"-\");\n return { command: \"codex\", args, usesStdin: true };\n }\n case \"claude\": {\n const args = [\"-p\"];\n if (model) {\n args.push(\"--model\", model);\n }\n return { command: \"claude\", args, usesStdin: true };\n }\n default: {\n const args = model ? [\"--model\", model] : [];\n return { command: provider, args, usesStdin: true };\n }\n }\n}\n\nfunction generateCommitMessage(status: string, diff: string): Promise<string> {\n const prompt = `${MAIN_PROMPT}\n\nGit Status:\n${status}\n\nGit Diff:\n${diff}`;\n\n return new Promise((resolve, reject) => {\n const { provider, model } = getProviderConfig();\n const { command, args, usesStdin } = getProviderCommand(provider, model);\n\n const child = spawn(command, args, {\n stdio: [\"pipe\", \"pipe\", \"pipe\"],\n });\n\n let stdout = \"\";\n let stderr = \"\";\n\n child.stdout.on(\"data\", (data) => {\n stdout += data.toString();\n });\n\n child.stderr.on(\"data\", (data) => {\n stderr += data.toString();\n });\n\n child.on(\"close\", (code) => {\n if (code !== 0) {\n const details = stderr || stdout;\n const message = details\n ? `${provider} failed (exit ${code}): ${details.trim()}`\n : `${provider} failed (exit ${code})`;\n reject(new Error(message));\n } else {\n resolve(stdout.trim());\n }\n });\n\n child.on(\"error\", (err) => {\n reject(err);\n });\n\n if (usesStdin) {\n child.stdin.write(prompt);\n child.stdin.end();\n }\n });\n}\n\nfunction commitWithMessage(message: string): void {\n try {\n execSync(`git commit -m \"${message.replace(/\"/g, '\\\\\"')}\"`, {\n stdio: \"inherit\",\n });\n } catch {\n console.error(pc.red(\"Failed to create commit\"));\n process.exit(1);\n }\n}\n\nasync function main(): Promise<void> {\n console.log(pc.cyan(\"\\n Commit Message Generator\\n\"));\n\n // Check if we're in a git repository\n try {\n exec(\"git rev-parse --is-inside-work-tree\");\n } catch {\n console.error(pc.red(\"Error: Not a git repository\"));\n process.exit(1);\n }\n\n const status = getGitStatus();\n\n if (!status) {\n console.log(pc.yellow(\"No changes to commit\"));\n process.exit(0);\n }\n\n await promptForStagingChanges();\n\n const diff = getGitDiff();\n\n if (!diff) {\n console.log(pc.yellow(\"No diff available to analyze\"));\n process.exit(0);\n }\n\n console.log(pc.dim(\"Analyzing changes...\\n\"));\n\n let commitMessage: string;\n try {\n commitMessage = await generateCommitMessage(status, diff);\n } catch (error) {\n console.error(\n pc.red(\"Failed to generate commit message:\"),\n error instanceof Error ? error.message : error\n );\n process.exit(1);\n }\n\n console.log(pc.green(\"Generated commit message:\"));\n console.log(pc.white(pc.bold(`\\n ${commitMessage}\\n`)));\n\n const action = await select({\n message: \"What would you like to do?\",\n choices: [\n { name: \"Commit with this message\", value: \"commit\" },\n { name: \"Edit the message\", value: \"edit\" },\n { name: \"Cancel\", value: \"cancel\" },\n ],\n });\n\n switch (action) {\n case \"commit\":\n commitWithMessage(commitMessage);\n console.log(pc.green(\"\\nCommit created successfully!\"));\n break;\n\n case \"edit\": {\n const editedMessage = await input({\n message: \"Enter your commit message:\",\n default: commitMessage,\n });\n\n if (editedMessage.trim()) {\n commitWithMessage(editedMessage.trim());\n console.log(pc.green(\"\\nCommit created successfully!\"));\n } else {\n console.log(pc.yellow(\"Empty message, commit cancelled\"));\n }\n break;\n }\n\n case \"cancel\":\n console.log(pc.yellow(\"Commit cancelled\"));\n break;\n\n default:\n console.log(pc.yellow(\"No action selected\"));\n break;\n }\n}\n\nmain().catch((error) => {\n console.error(pc.red(\"Error:\"), error);\n process.exit(1);\n});\n"],"mappings":";;;AAAA,SAAS,UAAU,aAAa;AAChC,SAAS,OAAO,cAAc;AAC9B,OAAO,QAAQ;AAEf,IAAM,cAAc;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAapB,SAAS,KAAK,SAAyB;AACrC,MAAI;AACF,WAAO,SAAS,SAAS,EAAE,UAAU,QAAQ,CAAC,EAAE,KAAK;AAAA,EACvD,QAAQ;AACN,WAAO;AAAA,EACT;AACF;AAEA,SAAS,eAAuB;AAC9B,SAAO,KAAK,oBAAoB;AAClC;AAEA,SAAS,aAAqB;AAE5B,QAAM,SAAS,KAAK,mBAAmB;AACvC,MAAI,QAAQ;AACV,WAAO;AAAA,EACT;AACA,SAAO,KAAK,UAAU;AACxB;AAEA,SAAS,mBAA4B;AACnC,SAAO,KAAK,+BAA+B,EAAE,SAAS;AACxD;AAEA,SAAS,qBAA8B;AACrC,SAAO,KAAK,sBAAsB,EAAE,SAAS;AAC/C;AAEA,SAAS,oBAA6B;AACpC,SAAO,KAAK,0CAA0C,EAAE,SAAS;AACnE;AAEA,SAAS,iBAA2B;AAClC,QAAM,SAAS,KAAK,+BAA+B;AACnD,SAAO,SAAS,OAAO,MAA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C;AAAA,EAChB;AAEA,UAAQ,IAAI,GAAG,MAAM,2BAA2B,CAAC;AACjD,UAAQ,IAAI,GAAG,MAAM,GAAG,KAAK;AAAA,IAAO,aAAa;AAAA,CAAI,CAAC,CAAC;AAEvD,QAAM,SAAS,MAAM,OAAO;AAAA,IAC1B,SAAS;AAAA,IACT,SAAS;AAAA,MACP,EAAE,MAAM,4BAA4B,OAAO,SAAS;AAAA,MACpD,EAAE,MAAM,oBAAoB,OAAO,OAAO;AAAA,MAC1C,EAAE,MAAM,UAAU,OAAO,SAAS;AAAA,IACpC;AAAA,EACF,CAAC;AAED,UAAQ,QAAQ;AAAA,IACd,KAAK;AACH,wBAAkB,aAAa;AAC/B,cAAQ,IAAI,GAAG,MAAM,gCAAgC,CAAC;AACtD;AAAA,IAEF,KAAK,QAAQ;AACX,YAAM,gBAAgB,MAAM,MAAM;AAAA,QAChC,SAAS;AAAA,QACT,SAAS;AAAA,MACX,CAAC;AAED,UAAI,cAAc,KAAK,GAAG;AACxB,0BAAkB,cAAc,KAAK,CAAC;AACtC,gBAAQ,IAAI,GAAG,MAAM,gCAAgC,CAAC;AAAA,MACxD,OAAO;AACL,gBAAQ,IAAI,GAAG,OAAO,iCAAiC,CAAC;AAAA,MAC1D;AACA;AAAA,IACF;AAAA,IAEA,KAAK;AACH,cAAQ,IAAI,GAAG,OAAO,kBAAkB,CAAC;AACzC;AAAA,IAEF;AACE,cAAQ,IAAI,GAAG,OAAO,oBAAoB,CAAC;AAC3C;AAAA,EACJ;AACF;AAEA,KAAK,EAAE,MAAM,CAAC,UAAU;AACtB,UAAQ,MAAM,GAAG,IAAI,QAAQ,GAAG,KAAK;AACrC,UAAQ,KAAK,CAAC;AAChB,CAAC;","names":[]}
|
package/package.json
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@cbashik/commit",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "AI-powered commit message generator using Claude",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "dist/index.js",
|
|
7
|
+
"bin": {
|
|
8
|
+
"commit": "dist/index.js"
|
|
9
|
+
},
|
|
10
|
+
"scripts": {
|
|
11
|
+
"build": "tsup",
|
|
12
|
+
"dev": "tsup --watch",
|
|
13
|
+
"prepublishOnly": "npm run build",
|
|
14
|
+
"check": "ultracite check",
|
|
15
|
+
"fix": "ultracite fix",
|
|
16
|
+
"prepare": "husky"
|
|
17
|
+
},
|
|
18
|
+
"keywords": [
|
|
19
|
+
"cli",
|
|
20
|
+
"git",
|
|
21
|
+
"commit",
|
|
22
|
+
"ai",
|
|
23
|
+
"claude"
|
|
24
|
+
],
|
|
25
|
+
"author": "",
|
|
26
|
+
"license": "ISC",
|
|
27
|
+
"devDependencies": {
|
|
28
|
+
"@biomejs/biome": "^2.3.12",
|
|
29
|
+
"@types/node": "^25.0.10",
|
|
30
|
+
"husky": "^9.1.7",
|
|
31
|
+
"lint-staged": "^16.2.7",
|
|
32
|
+
"tsup": "^8.5.1",
|
|
33
|
+
"typescript": "^5.9.3",
|
|
34
|
+
"ultracite": "^7.1.1"
|
|
35
|
+
},
|
|
36
|
+
"dependencies": {
|
|
37
|
+
"@inquirer/prompts": "^8.2.0",
|
|
38
|
+
"picocolors": "^1.1.1"
|
|
39
|
+
},
|
|
40
|
+
"lint-staged": {
|
|
41
|
+
"*.{js,jsx,ts,tsx,json,jsonc,css,scss,md,mdx}": [
|
|
42
|
+
"npx ultracite fix"
|
|
43
|
+
]
|
|
44
|
+
}
|
|
45
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,330 @@
|
|
|
1
|
+
import { execSync, spawn } from "node:child_process";
|
|
2
|
+
import { input, select } from "@inquirer/prompts";
|
|
3
|
+
import pc from "picocolors";
|
|
4
|
+
|
|
5
|
+
const MAIN_PROMPT = `You are a commit message generator. Analyze the git diff and status provided and generate a concise, meaningful commit message.
|
|
6
|
+
|
|
7
|
+
Rules:
|
|
8
|
+
- Follow conventional commits format: type(scope): description
|
|
9
|
+
- Types: feat, fix, docs, style, refactor, test, chore, perf, ci, build
|
|
10
|
+
- Keep the first line under 72 characters
|
|
11
|
+
- Be specific about what changed
|
|
12
|
+
- Focus on the "why" not just the "what"
|
|
13
|
+
- If multiple changes, summarize the main change
|
|
14
|
+
- Do not include any explanation, just output the commit message
|
|
15
|
+
|
|
16
|
+
Output ONLY the commit message, nothing else.`;
|
|
17
|
+
|
|
18
|
+
function exec(command: string): string {
|
|
19
|
+
try {
|
|
20
|
+
return execSync(command, { encoding: "utf-8" }).trim();
|
|
21
|
+
} catch {
|
|
22
|
+
return "";
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function getGitStatus(): string {
|
|
27
|
+
return exec("git status --short");
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function getGitDiff(): string {
|
|
31
|
+
// Get staged changes first, if none, get unstaged changes
|
|
32
|
+
const staged = exec("git diff --cached");
|
|
33
|
+
if (staged) {
|
|
34
|
+
return staged;
|
|
35
|
+
}
|
|
36
|
+
return exec("git diff");
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function hasStagedChanges(): boolean {
|
|
40
|
+
return exec("git diff --cached --name-only").length > 0;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function hasUnstagedChanges(): boolean {
|
|
44
|
+
return exec("git diff --name-only").length > 0;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function hasUntrackedFiles(): boolean {
|
|
48
|
+
return exec("git ls-files --others --exclude-standard").length > 0;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function getStagedFiles(): string[] {
|
|
52
|
+
const output = exec("git diff --cached --name-only");
|
|
53
|
+
return output ? output.split("\n").filter(Boolean) : [];
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function getUnstagedFiles(): string[] {
|
|
57
|
+
const output = exec("git diff --name-only");
|
|
58
|
+
return output ? output.split("\n").filter(Boolean) : [];
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function getPartiallyStagedFiles(): string[] {
|
|
62
|
+
const staged = new Set(getStagedFiles());
|
|
63
|
+
const unstaged = getUnstagedFiles();
|
|
64
|
+
|
|
65
|
+
return unstaged.filter((file) => staged.has(file));
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
async function promptForStagingChanges(): Promise<void> {
|
|
69
|
+
const staged = hasStagedChanges();
|
|
70
|
+
const unstaged = hasUnstagedChanges();
|
|
71
|
+
const untracked = hasUntrackedFiles();
|
|
72
|
+
|
|
73
|
+
if (!staged && (unstaged || untracked)) {
|
|
74
|
+
console.log(pc.yellow("No staged changes. Stage your changes first:"));
|
|
75
|
+
console.log(pc.dim(" git add <files>"));
|
|
76
|
+
console.log(pc.dim(" git add -A (to stage all)\n"));
|
|
77
|
+
|
|
78
|
+
const shouldStageAll = await select({
|
|
79
|
+
message: "Would you like to stage all changes?",
|
|
80
|
+
choices: [
|
|
81
|
+
{ name: "Yes, stage all changes", value: "yes" },
|
|
82
|
+
{ name: "No, exit", value: "no" },
|
|
83
|
+
],
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
if (shouldStageAll === "yes") {
|
|
87
|
+
exec("git add -A");
|
|
88
|
+
console.log(pc.green("All changes staged\n"));
|
|
89
|
+
} else {
|
|
90
|
+
process.exit(0);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
if (staged && (unstaged || untracked)) {
|
|
95
|
+
const partiallyStaged = getPartiallyStagedFiles();
|
|
96
|
+
|
|
97
|
+
if (partiallyStaged.length > 0) {
|
|
98
|
+
console.log(pc.yellow("Some files are partially staged:"));
|
|
99
|
+
console.log(pc.dim(` ${partiallyStaged.join("\n ")}\n`));
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
const stageMore = await select({
|
|
103
|
+
message: "Would you like to stage other changes too?",
|
|
104
|
+
choices: [
|
|
105
|
+
{ name: "Keep staged changes only", value: "keep" },
|
|
106
|
+
{ name: "Stage tracked changes", value: "tracked" },
|
|
107
|
+
{ name: "Stage all changes", value: "all" },
|
|
108
|
+
{ name: "Cancel", value: "cancel" },
|
|
109
|
+
],
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
if (stageMore === "tracked") {
|
|
113
|
+
exec("git add -u");
|
|
114
|
+
console.log(pc.green("Tracked changes staged\n"));
|
|
115
|
+
} else if (stageMore === "all") {
|
|
116
|
+
exec("git add -A");
|
|
117
|
+
console.log(pc.green("All changes staged\n"));
|
|
118
|
+
} else if (stageMore === "cancel") {
|
|
119
|
+
process.exit(0);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
const DEFAULT_PROVIDER = "codex";
|
|
125
|
+
const DEFAULT_MODELS: Record<string, string> = {
|
|
126
|
+
codex: "gpt-5.1-codex-mini",
|
|
127
|
+
claude: "claude-sonnet-4-20250514",
|
|
128
|
+
};
|
|
129
|
+
|
|
130
|
+
function getProviderConfig(): { provider: string; model?: string } {
|
|
131
|
+
const args = process.argv.slice(2);
|
|
132
|
+
const providerArgIndex = args.findIndex(
|
|
133
|
+
(arg) => arg === "-p" || arg === "--provider"
|
|
134
|
+
);
|
|
135
|
+
const modelArgIndex = args.findIndex(
|
|
136
|
+
(arg) => arg === "-m" || arg === "--model"
|
|
137
|
+
);
|
|
138
|
+
|
|
139
|
+
const providerFromArgs =
|
|
140
|
+
providerArgIndex >= 0 ? args[providerArgIndex + 1] : undefined;
|
|
141
|
+
const modelFromArgs =
|
|
142
|
+
modelArgIndex >= 0 ? args[modelArgIndex + 1] : undefined;
|
|
143
|
+
|
|
144
|
+
const provider = (
|
|
145
|
+
providerFromArgs ||
|
|
146
|
+
process.env.COMMIT_CLI_PROVIDER ||
|
|
147
|
+
DEFAULT_PROVIDER
|
|
148
|
+
).toLowerCase();
|
|
149
|
+
const model =
|
|
150
|
+
modelFromArgs || process.env.COMMIT_CLI_MODEL || DEFAULT_MODELS[provider];
|
|
151
|
+
|
|
152
|
+
return { provider, model };
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
function getProviderCommand(
|
|
156
|
+
provider: string,
|
|
157
|
+
model?: string
|
|
158
|
+
): { command: string; args: string[]; usesStdin: boolean } {
|
|
159
|
+
switch (provider) {
|
|
160
|
+
case "codex": {
|
|
161
|
+
const args = ["exec"];
|
|
162
|
+
if (model) {
|
|
163
|
+
args.push("--model", model);
|
|
164
|
+
}
|
|
165
|
+
args.push("-");
|
|
166
|
+
return { command: "codex", args, usesStdin: true };
|
|
167
|
+
}
|
|
168
|
+
case "claude": {
|
|
169
|
+
const args = ["-p"];
|
|
170
|
+
if (model) {
|
|
171
|
+
args.push("--model", model);
|
|
172
|
+
}
|
|
173
|
+
return { command: "claude", args, usesStdin: true };
|
|
174
|
+
}
|
|
175
|
+
default: {
|
|
176
|
+
const args = model ? ["--model", model] : [];
|
|
177
|
+
return { command: provider, args, usesStdin: true };
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
function generateCommitMessage(status: string, diff: string): Promise<string> {
|
|
183
|
+
const prompt = `${MAIN_PROMPT}
|
|
184
|
+
|
|
185
|
+
Git Status:
|
|
186
|
+
${status}
|
|
187
|
+
|
|
188
|
+
Git Diff:
|
|
189
|
+
${diff}`;
|
|
190
|
+
|
|
191
|
+
return new Promise((resolve, reject) => {
|
|
192
|
+
const { provider, model } = getProviderConfig();
|
|
193
|
+
const { command, args, usesStdin } = getProviderCommand(provider, model);
|
|
194
|
+
|
|
195
|
+
const child = spawn(command, args, {
|
|
196
|
+
stdio: ["pipe", "pipe", "pipe"],
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
let stdout = "";
|
|
200
|
+
let stderr = "";
|
|
201
|
+
|
|
202
|
+
child.stdout.on("data", (data) => {
|
|
203
|
+
stdout += data.toString();
|
|
204
|
+
});
|
|
205
|
+
|
|
206
|
+
child.stderr.on("data", (data) => {
|
|
207
|
+
stderr += data.toString();
|
|
208
|
+
});
|
|
209
|
+
|
|
210
|
+
child.on("close", (code) => {
|
|
211
|
+
if (code !== 0) {
|
|
212
|
+
const details = stderr || stdout;
|
|
213
|
+
const message = details
|
|
214
|
+
? `${provider} failed (exit ${code}): ${details.trim()}`
|
|
215
|
+
: `${provider} failed (exit ${code})`;
|
|
216
|
+
reject(new Error(message));
|
|
217
|
+
} else {
|
|
218
|
+
resolve(stdout.trim());
|
|
219
|
+
}
|
|
220
|
+
});
|
|
221
|
+
|
|
222
|
+
child.on("error", (err) => {
|
|
223
|
+
reject(err);
|
|
224
|
+
});
|
|
225
|
+
|
|
226
|
+
if (usesStdin) {
|
|
227
|
+
child.stdin.write(prompt);
|
|
228
|
+
child.stdin.end();
|
|
229
|
+
}
|
|
230
|
+
});
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
function commitWithMessage(message: string): void {
|
|
234
|
+
try {
|
|
235
|
+
execSync(`git commit -m "${message.replace(/"/g, '\\"')}"`, {
|
|
236
|
+
stdio: "inherit",
|
|
237
|
+
});
|
|
238
|
+
} catch {
|
|
239
|
+
console.error(pc.red("Failed to create commit"));
|
|
240
|
+
process.exit(1);
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
async function main(): Promise<void> {
|
|
245
|
+
console.log(pc.cyan("\n Commit Message Generator\n"));
|
|
246
|
+
|
|
247
|
+
// Check if we're in a git repository
|
|
248
|
+
try {
|
|
249
|
+
exec("git rev-parse --is-inside-work-tree");
|
|
250
|
+
} catch {
|
|
251
|
+
console.error(pc.red("Error: Not a git repository"));
|
|
252
|
+
process.exit(1);
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
const status = getGitStatus();
|
|
256
|
+
|
|
257
|
+
if (!status) {
|
|
258
|
+
console.log(pc.yellow("No changes to commit"));
|
|
259
|
+
process.exit(0);
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
await promptForStagingChanges();
|
|
263
|
+
|
|
264
|
+
const diff = getGitDiff();
|
|
265
|
+
|
|
266
|
+
if (!diff) {
|
|
267
|
+
console.log(pc.yellow("No diff available to analyze"));
|
|
268
|
+
process.exit(0);
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
console.log(pc.dim("Analyzing changes...\n"));
|
|
272
|
+
|
|
273
|
+
let commitMessage: string;
|
|
274
|
+
try {
|
|
275
|
+
commitMessage = await generateCommitMessage(status, diff);
|
|
276
|
+
} catch (error) {
|
|
277
|
+
console.error(
|
|
278
|
+
pc.red("Failed to generate commit message:"),
|
|
279
|
+
error instanceof Error ? error.message : error
|
|
280
|
+
);
|
|
281
|
+
process.exit(1);
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
console.log(pc.green("Generated commit message:"));
|
|
285
|
+
console.log(pc.white(pc.bold(`\n ${commitMessage}\n`)));
|
|
286
|
+
|
|
287
|
+
const action = await select({
|
|
288
|
+
message: "What would you like to do?",
|
|
289
|
+
choices: [
|
|
290
|
+
{ name: "Commit with this message", value: "commit" },
|
|
291
|
+
{ name: "Edit the message", value: "edit" },
|
|
292
|
+
{ name: "Cancel", value: "cancel" },
|
|
293
|
+
],
|
|
294
|
+
});
|
|
295
|
+
|
|
296
|
+
switch (action) {
|
|
297
|
+
case "commit":
|
|
298
|
+
commitWithMessage(commitMessage);
|
|
299
|
+
console.log(pc.green("\nCommit created successfully!"));
|
|
300
|
+
break;
|
|
301
|
+
|
|
302
|
+
case "edit": {
|
|
303
|
+
const editedMessage = await input({
|
|
304
|
+
message: "Enter your commit message:",
|
|
305
|
+
default: commitMessage,
|
|
306
|
+
});
|
|
307
|
+
|
|
308
|
+
if (editedMessage.trim()) {
|
|
309
|
+
commitWithMessage(editedMessage.trim());
|
|
310
|
+
console.log(pc.green("\nCommit created successfully!"));
|
|
311
|
+
} else {
|
|
312
|
+
console.log(pc.yellow("Empty message, commit cancelled"));
|
|
313
|
+
}
|
|
314
|
+
break;
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
case "cancel":
|
|
318
|
+
console.log(pc.yellow("Commit cancelled"));
|
|
319
|
+
break;
|
|
320
|
+
|
|
321
|
+
default:
|
|
322
|
+
console.log(pc.yellow("No action selected"));
|
|
323
|
+
break;
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
main().catch((error) => {
|
|
328
|
+
console.error(pc.red("Error:"), error);
|
|
329
|
+
process.exit(1);
|
|
330
|
+
});
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "ES2022",
|
|
4
|
+
"module": "ESNext",
|
|
5
|
+
"moduleResolution": "bundler",
|
|
6
|
+
"esModuleInterop": true,
|
|
7
|
+
"strict": true,
|
|
8
|
+
"skipLibCheck": true,
|
|
9
|
+
"outDir": "dist",
|
|
10
|
+
"rootDir": "src",
|
|
11
|
+
"declaration": true,
|
|
12
|
+
"resolveJsonModule": true
|
|
13
|
+
},
|
|
14
|
+
"include": ["src/**/*"],
|
|
15
|
+
"exclude": ["node_modules", "dist"]
|
|
16
|
+
}
|
package/tsup.config.ts
ADDED