@vsokolov/semantix 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/LICENSE +21 -0
- package/README.md +303 -0
- package/dist/index.js +355 -0
- package/package.json +68 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Viktor Sokolov
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,303 @@
|
|
|
1
|
+
# Semantix
|
|
2
|
+
|
|
3
|
+
A TypeScript CLI tool to automatically configure conventional commits, semantic-release, and automated releases for your projects.
|
|
4
|
+
|
|
5
|
+
## Quick Start
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
# Install globally
|
|
9
|
+
bun install -g @vsokolov/semantix
|
|
10
|
+
|
|
11
|
+
# Run in your project
|
|
12
|
+
cd your-project
|
|
13
|
+
semantix
|
|
14
|
+
|
|
15
|
+
# Make a commit
|
|
16
|
+
git commit -m "feat: add new feature"
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
See [QUICKSTART.md](./QUICKSTART.md) for a detailed getting started guide.
|
|
20
|
+
|
|
21
|
+
## Features
|
|
22
|
+
|
|
23
|
+
- ✅ **Commitlint** - Enforces conventional commit format
|
|
24
|
+
- ✅ **Lefthook** - Git hooks to validate commits
|
|
25
|
+
- ✅ **Semantic Release** - Automated versioning and releases
|
|
26
|
+
- ✅ **Changelog Generation** - Auto-generates CHANGELOG.md
|
|
27
|
+
- ✅ **GitHub Releases** - Creates releases automatically
|
|
28
|
+
- ✅ **GitHub Actions** - CI/CD workflow included
|
|
29
|
+
|
|
30
|
+
## Installation
|
|
31
|
+
|
|
32
|
+
### Global Installation (Recommended)
|
|
33
|
+
|
|
34
|
+
```bash
|
|
35
|
+
# Using Bun
|
|
36
|
+
bun install -g semantix
|
|
37
|
+
|
|
38
|
+
# Using npm
|
|
39
|
+
npm install -g @vsokolov/semantix
|
|
40
|
+
|
|
41
|
+
# Using pnpm
|
|
42
|
+
pnpm add -g @vsokolov/semantix
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
### Local Installation
|
|
46
|
+
|
|
47
|
+
```bash
|
|
48
|
+
# Using Bun
|
|
49
|
+
bun add -D @vsokolov/semantix
|
|
50
|
+
|
|
51
|
+
# Using npm
|
|
52
|
+
npm install --save-dev @vsokolov/semantix
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
## Usage
|
|
56
|
+
|
|
57
|
+
Navigate to your project directory and run:
|
|
58
|
+
|
|
59
|
+
```bash
|
|
60
|
+
# If installed globally
|
|
61
|
+
semantix
|
|
62
|
+
|
|
63
|
+
# If installed locally
|
|
64
|
+
bunx @vsokolov/semantix
|
|
65
|
+
# or
|
|
66
|
+
npx @vsokolov/semantix
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
That's it! Semantix will automatically configure everything for you.
|
|
70
|
+
|
|
71
|
+
## Project Structure
|
|
72
|
+
|
|
73
|
+
After running Semantix, your project will have:
|
|
74
|
+
|
|
75
|
+
```
|
|
76
|
+
your-project/
|
|
77
|
+
├── .github/
|
|
78
|
+
│ └── workflows/
|
|
79
|
+
│ └── release.yml # GitHub Actions workflow
|
|
80
|
+
├── lefthook.yml # Git hooks configuration
|
|
81
|
+
├── commitlint.config.js # Commit message rules
|
|
82
|
+
├── .releaserc.mjs # Semantic-release config
|
|
83
|
+
├── COMMIT_CONVENTION.md # Team documentation
|
|
84
|
+
└── package.json # Updated with scripts
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
## What Gets Configured
|
|
88
|
+
|
|
89
|
+
### 1. Dependencies Installed
|
|
90
|
+
|
|
91
|
+
- `@commitlint/cli` & `@commitlint/config-conventional`
|
|
92
|
+
- `@semantic-release/changelog`
|
|
93
|
+
- `@semantic-release/git`
|
|
94
|
+
- `@semantic-release/github`
|
|
95
|
+
- `lefthook`
|
|
96
|
+
- `semantic-release`
|
|
97
|
+
|
|
98
|
+
### 2. Configuration Files Created
|
|
99
|
+
|
|
100
|
+
- **commitlint.config.js** - Commit message validation rules
|
|
101
|
+
- **.releaserc.mjs** - Semantic-release configuration
|
|
102
|
+
- **lefthook.yml** - Git hooks configuration
|
|
103
|
+
- **.github/workflows/release.yml** - GitHub Actions workflow
|
|
104
|
+
- **COMMIT_CONVENTION.md** - Documentation for your team
|
|
105
|
+
|
|
106
|
+
### 3. Package.json Scripts Added
|
|
107
|
+
|
|
108
|
+
```json
|
|
109
|
+
{
|
|
110
|
+
"scripts": {
|
|
111
|
+
"release": "semantic-release",
|
|
112
|
+
"release:dry": "semantic-release --dry-run",
|
|
113
|
+
"prepare": "lefthook install"
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
## Commit Message Format
|
|
119
|
+
|
|
120
|
+
```
|
|
121
|
+
<type>[optional scope]: <description>
|
|
122
|
+
|
|
123
|
+
[optional body]
|
|
124
|
+
|
|
125
|
+
[optional footer]
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
### Types
|
|
129
|
+
|
|
130
|
+
- `feat`: New feature
|
|
131
|
+
- `fix`: Bug fix
|
|
132
|
+
- `docs`: Documentation changes
|
|
133
|
+
- `style`: Code style changes (formatting)
|
|
134
|
+
- `refactor`: Code refactoring
|
|
135
|
+
- `perf`: Performance improvements
|
|
136
|
+
- `test`: Adding or updating tests
|
|
137
|
+
- `build`: Build system changes
|
|
138
|
+
- `ci`: CI configuration changes
|
|
139
|
+
- `chore`: Other changes
|
|
140
|
+
- `revert`: Revert previous commit
|
|
141
|
+
|
|
142
|
+
### Examples
|
|
143
|
+
|
|
144
|
+
```bash
|
|
145
|
+
git commit -m "feat: add user authentication"
|
|
146
|
+
git commit -m "fix: resolve memory leak in data processing"
|
|
147
|
+
git commit -m "docs: update installation instructions"
|
|
148
|
+
git commit -m "feat(api)!: redesign authentication flow
|
|
149
|
+
|
|
150
|
+
BREAKING CHANGE: The authentication API has been completely redesigned"
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
## How Releases Work
|
|
154
|
+
|
|
155
|
+
1. **Commit** with conventional commit format
|
|
156
|
+
2. **Push** to `main` or `master` branch
|
|
157
|
+
3. **GitHub Actions** runs automatically
|
|
158
|
+
4. **Semantic-release** analyzes commits and:
|
|
159
|
+
- Determines version bump (major/minor/patch)
|
|
160
|
+
- Generates CHANGELOG.md
|
|
161
|
+
- Creates git tag
|
|
162
|
+
- Publishes GitHub release
|
|
163
|
+
- Updates package.json version
|
|
164
|
+
|
|
165
|
+
### Version Bumping
|
|
166
|
+
|
|
167
|
+
- `fix:` → Patch release (1.0.0 → 1.0.1)
|
|
168
|
+
- `feat:` → Minor release (1.0.0 → 1.1.0)
|
|
169
|
+
- `BREAKING CHANGE:` → Major release (1.0.0 → 2.0.0)
|
|
170
|
+
|
|
171
|
+
## Testing Releases
|
|
172
|
+
|
|
173
|
+
Before pushing, test the release process locally:
|
|
174
|
+
|
|
175
|
+
```bash
|
|
176
|
+
bun run release:dry
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
This shows what would happen without actually creating a release.
|
|
180
|
+
|
|
181
|
+
## GitHub Configuration
|
|
182
|
+
|
|
183
|
+
### Required: GitHub Token
|
|
184
|
+
|
|
185
|
+
The GitHub Actions workflow needs a `GITHUB_TOKEN` to create releases. This is automatically provided by GitHub Actions, but ensure your repository has:
|
|
186
|
+
|
|
187
|
+
1. **Settings** → **Actions** → **General**
|
|
188
|
+
2. **Workflow permissions** → Select "Read and write permissions"
|
|
189
|
+
3. Check "Allow GitHub Actions to create and approve pull requests"
|
|
190
|
+
|
|
191
|
+
## Customization
|
|
192
|
+
|
|
193
|
+
### Change Release Branches
|
|
194
|
+
|
|
195
|
+
Edit `.releaserc.mjs`:
|
|
196
|
+
|
|
197
|
+
```javascript
|
|
198
|
+
const config = {
|
|
199
|
+
branches: ['main', 'develop', { name: 'beta', prerelease: true }],
|
|
200
|
+
// ... rest of config
|
|
201
|
+
};
|
|
202
|
+
|
|
203
|
+
export default config;
|
|
204
|
+
```
|
|
205
|
+
|
|
206
|
+
### Customize Commit Types
|
|
207
|
+
|
|
208
|
+
Edit `commitlint.config.js`:
|
|
209
|
+
|
|
210
|
+
```javascript
|
|
211
|
+
module.exports = {
|
|
212
|
+
extends: ['@commitlint/config-conventional'],
|
|
213
|
+
rules: {
|
|
214
|
+
'type-enum': [2, 'always', ['feat', 'fix', 'docs', 'custom-type']],
|
|
215
|
+
},
|
|
216
|
+
};
|
|
217
|
+
```
|
|
218
|
+
|
|
219
|
+
## Development
|
|
220
|
+
|
|
221
|
+
### Setup
|
|
222
|
+
|
|
223
|
+
```bash
|
|
224
|
+
# Clone the repository
|
|
225
|
+
git clone https://github.com/yourusername/semantix.git
|
|
226
|
+
cd semantix
|
|
227
|
+
|
|
228
|
+
# Install dependencies
|
|
229
|
+
bun install
|
|
230
|
+
```
|
|
231
|
+
|
|
232
|
+
### Local Development
|
|
233
|
+
|
|
234
|
+
```bash
|
|
235
|
+
# Run in watch mode
|
|
236
|
+
bun run dev
|
|
237
|
+
|
|
238
|
+
# Build the project
|
|
239
|
+
bun run build
|
|
240
|
+
|
|
241
|
+
# Test locally in another project
|
|
242
|
+
bun link
|
|
243
|
+
cd /path/to/test-project
|
|
244
|
+
semantix
|
|
245
|
+
```
|
|
246
|
+
|
|
247
|
+
### Testing
|
|
248
|
+
|
|
249
|
+
```bash
|
|
250
|
+
# Run tests
|
|
251
|
+
bun run test
|
|
252
|
+
|
|
253
|
+
# Run tests with coverage
|
|
254
|
+
bun run test:coverage
|
|
255
|
+
|
|
256
|
+
# Type checking
|
|
257
|
+
bun run typecheck
|
|
258
|
+
|
|
259
|
+
# Linting
|
|
260
|
+
bun run lint
|
|
261
|
+
|
|
262
|
+
# Fix linting issues
|
|
263
|
+
bun run lint:fix
|
|
264
|
+
|
|
265
|
+
# Code formatting
|
|
266
|
+
bun run format
|
|
267
|
+
|
|
268
|
+
# Check formatting
|
|
269
|
+
bun run format:check
|
|
270
|
+
```
|
|
271
|
+
|
|
272
|
+
### Publishing
|
|
273
|
+
|
|
274
|
+
```bash
|
|
275
|
+
# Build and publish to npm
|
|
276
|
+
bun run build
|
|
277
|
+
npm publish
|
|
278
|
+
|
|
279
|
+
# Or use Bun to publish
|
|
280
|
+
bun publish
|
|
281
|
+
```
|
|
282
|
+
|
|
283
|
+
## Troubleshooting
|
|
284
|
+
|
|
285
|
+
### Commits are not being validated
|
|
286
|
+
|
|
287
|
+
```bash
|
|
288
|
+
bunx lefthook install
|
|
289
|
+
```
|
|
290
|
+
|
|
291
|
+
### Release not triggering
|
|
292
|
+
|
|
293
|
+
- Check GitHub Actions tab for errors
|
|
294
|
+
- Verify workflow permissions are set correctly
|
|
295
|
+
- Ensure you're pushing to `main` or `master` branch
|
|
296
|
+
|
|
297
|
+
### "No commits since last release"
|
|
298
|
+
|
|
299
|
+
This means there are no conventional commits that would trigger a release (feat, fix, etc.)
|
|
300
|
+
|
|
301
|
+
## License
|
|
302
|
+
|
|
303
|
+
MIT
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,355 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { execSync } from "child_process";
|
|
3
|
+
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "fs";
|
|
4
|
+
import { join } from "path";
|
|
5
|
+
import { createInterface } from "readline";
|
|
6
|
+
import { resolve } from "node:path";
|
|
7
|
+
|
|
8
|
+
//#region src/constants.ts
|
|
9
|
+
const PM_LOCK_FILES = {
|
|
10
|
+
npm: "package-lock.json",
|
|
11
|
+
yarn: "yarn.lock",
|
|
12
|
+
pnpm: "pnpm-lock.yaml",
|
|
13
|
+
bun: ["bun.lockb", "bun.lock"]
|
|
14
|
+
};
|
|
15
|
+
const DEPENDENCIES = [
|
|
16
|
+
"@semantic-release/changelog",
|
|
17
|
+
"@semantic-release/git",
|
|
18
|
+
"@semantic-release/github",
|
|
19
|
+
"@commitlint/cli",
|
|
20
|
+
"@commitlint/config-conventional",
|
|
21
|
+
"lefthook",
|
|
22
|
+
"semantic-release"
|
|
23
|
+
];
|
|
24
|
+
const COMMIT_TYPES = [
|
|
25
|
+
"feat",
|
|
26
|
+
"fix",
|
|
27
|
+
"docs",
|
|
28
|
+
"style",
|
|
29
|
+
"refactor",
|
|
30
|
+
"perf",
|
|
31
|
+
"test",
|
|
32
|
+
"build",
|
|
33
|
+
"ci",
|
|
34
|
+
"chore",
|
|
35
|
+
"revert"
|
|
36
|
+
];
|
|
37
|
+
const COMMITLINT_CONFIG = {
|
|
38
|
+
extends: ["@commitlint/config-conventional"],
|
|
39
|
+
rules: {
|
|
40
|
+
"type-enum": [
|
|
41
|
+
2,
|
|
42
|
+
"always",
|
|
43
|
+
COMMIT_TYPES
|
|
44
|
+
],
|
|
45
|
+
"subject-case": [
|
|
46
|
+
2,
|
|
47
|
+
"never",
|
|
48
|
+
["upper-case"]
|
|
49
|
+
]
|
|
50
|
+
}
|
|
51
|
+
};
|
|
52
|
+
const SEMANTIC_RELEASE_CONFIG = {
|
|
53
|
+
branches: ["main", "master"],
|
|
54
|
+
plugins: [
|
|
55
|
+
"@semantic-release/commit-analyzer",
|
|
56
|
+
"@semantic-release/release-notes-generator",
|
|
57
|
+
["@semantic-release/changelog", { changelogFile: "CHANGELOG.md" }],
|
|
58
|
+
"@semantic-release/npm",
|
|
59
|
+
["@semantic-release/github", { assets: ["CHANGELOG.md"] }],
|
|
60
|
+
["@semantic-release/git", {
|
|
61
|
+
assets: ["CHANGELOG.md", "package.json"],
|
|
62
|
+
message: "chore(release): ${nextRelease.version} [skip ci]\n\n${nextRelease.notes}"
|
|
63
|
+
}]
|
|
64
|
+
]
|
|
65
|
+
};
|
|
66
|
+
const getGithubWorkflow = (pm = "bun") => {
|
|
67
|
+
return `name: Release
|
|
68
|
+
|
|
69
|
+
on:
|
|
70
|
+
push:
|
|
71
|
+
tags:
|
|
72
|
+
- 'v*'
|
|
73
|
+
workflow_dispatch:
|
|
74
|
+
|
|
75
|
+
permissions:
|
|
76
|
+
contents: write
|
|
77
|
+
issues: write
|
|
78
|
+
pull-requests: write
|
|
79
|
+
id-token: write
|
|
80
|
+
|
|
81
|
+
jobs:
|
|
82
|
+
release:
|
|
83
|
+
runs-on: ubuntu-latest
|
|
84
|
+
steps:
|
|
85
|
+
- name: Checkout
|
|
86
|
+
uses: actions/checkout@v6
|
|
87
|
+
with:
|
|
88
|
+
fetch-depth: 0
|
|
89
|
+
${pm === "bun" ? `
|
|
90
|
+
- name: Setup Bun
|
|
91
|
+
uses: oven-sh/setup-bun@v2
|
|
92
|
+
with:
|
|
93
|
+
bun-version: latest` : `
|
|
94
|
+
- name: Setup Node
|
|
95
|
+
uses: actions/setup-node@v6
|
|
96
|
+
with:
|
|
97
|
+
node-version: 22
|
|
98
|
+
cache: '${pm}'`}
|
|
99
|
+
|
|
100
|
+
- name: Install dependencies
|
|
101
|
+
run: ${pm === "npm" ? "npm ci" : `${pm} install`}
|
|
102
|
+
|
|
103
|
+
- name: Release
|
|
104
|
+
env:
|
|
105
|
+
GH_TOKEN: \${{ secrets.GH_TOKEN }}
|
|
106
|
+
run: ${pm === "bun" ? "bun run release" : `${pm} run release`}
|
|
107
|
+
`;
|
|
108
|
+
};
|
|
109
|
+
const ASCII_ART = `
|
|
110
|
+
╔══════════════════════════════════════════════════════════════════════╗
|
|
111
|
+
║ ║
|
|
112
|
+
║ ███████╗███████╗███╗ ███╗ █████╗ ███╗ ██╗████████╗██╗██╗ ██╗ ║
|
|
113
|
+
║ ██╔════╝██╔════╝████╗ ████║██╔══██╗████╗ ██║╚══██╔══╝██║╚██╗██╔╝ ║
|
|
114
|
+
║ ███████╗█████╗ ██╔████╔██║███████║██╔██╗ ██║ ██║ ██║ ╚███╔╝ ║
|
|
115
|
+
║ ╚════██║██╔══╝ ██║╚██╔╝██║██╔══██║██║╚██╗██║ ██║ ██║ ██╔██╗ ║
|
|
116
|
+
║ ███████║███████╗██║ ╚═╝ ██║██║ ██║██║ ╚████║ ██║ ██║██╔╝ ██╗ ║
|
|
117
|
+
║ ╚══════╝╚══════╝╚═╝ ╚═╝╚═╝ ╚═╝╚═╝ ╚═══╝ ╚═╝ ╚═╝╚═╝ ╚═╝ ║
|
|
118
|
+
║ ║
|
|
119
|
+
║ Automated Conventional Commits & Releases ║
|
|
120
|
+
║ ║
|
|
121
|
+
╚══════════════════════════════════════════════════════════════════════╝
|
|
122
|
+
`;
|
|
123
|
+
const getLefthookConfig = (pm = "bun") => {
|
|
124
|
+
const runPrefix = pm === "bun" ? "bun run" : `${pm} run`;
|
|
125
|
+
return `pre-commit:
|
|
126
|
+
commands:
|
|
127
|
+
format-check:
|
|
128
|
+
run: ${runPrefix} format:check
|
|
129
|
+
lint:
|
|
130
|
+
run: ${runPrefix} lint
|
|
131
|
+
typecheck:
|
|
132
|
+
run: ${runPrefix} typecheck
|
|
133
|
+
|
|
134
|
+
commit-msg:
|
|
135
|
+
commands:
|
|
136
|
+
commitlint:
|
|
137
|
+
run: ${pm === "bun" ? "bunx --no -- commitlint --edit {1}" : `${pm === "bun" ? "bunx" : pm === "npm" ? "npx" : pm === "yarn" ? "yarn dlx" : "pnpm dlx"} commitlint --edit {1}`}
|
|
138
|
+
`;
|
|
139
|
+
};
|
|
140
|
+
|
|
141
|
+
//#endregion
|
|
142
|
+
//#region src/utils.ts
|
|
143
|
+
function log(message, type = "info") {
|
|
144
|
+
console.log(`${{
|
|
145
|
+
info: "\x1B[36m",
|
|
146
|
+
success: "\x1B[32m",
|
|
147
|
+
error: "\x1B[31m",
|
|
148
|
+
warning: "\x1B[33m"
|
|
149
|
+
}[type]}${message}[0m`);
|
|
150
|
+
}
|
|
151
|
+
function execCommand(command, cwd) {
|
|
152
|
+
try {
|
|
153
|
+
execSync(command, {
|
|
154
|
+
cwd,
|
|
155
|
+
stdio: "inherit"
|
|
156
|
+
});
|
|
157
|
+
} catch (error) {
|
|
158
|
+
log(`Failed to execute: ${command}`, "error");
|
|
159
|
+
throw error;
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
function ensureDirectoryExists(dirPath) {
|
|
163
|
+
if (!existsSync(dirPath)) mkdirSync(dirPath, { recursive: true });
|
|
164
|
+
}
|
|
165
|
+
function writeJsonFile(filePath, data) {
|
|
166
|
+
writeFileSync(filePath, JSON.stringify(data, null, 4));
|
|
167
|
+
}
|
|
168
|
+
function writeTextFile(filePath, content) {
|
|
169
|
+
writeFileSync(filePath, content);
|
|
170
|
+
}
|
|
171
|
+
function readJsonFile(filePath) {
|
|
172
|
+
return JSON.parse(readFileSync(filePath, "utf-8"));
|
|
173
|
+
}
|
|
174
|
+
function detectPackageManager(cwd) {
|
|
175
|
+
for (const [pm, lockFiles] of Object.entries(PM_LOCK_FILES)) if ((Array.isArray(lockFiles) ? lockFiles : [lockFiles]).some((file) => existsSync(join(cwd, file)))) return pm;
|
|
176
|
+
return "bun";
|
|
177
|
+
}
|
|
178
|
+
function getInstallCommand(pm, dependencies) {
|
|
179
|
+
const deps = dependencies.join(" ");
|
|
180
|
+
switch (pm) {
|
|
181
|
+
case "npm": return `npm install -D ${deps}`;
|
|
182
|
+
case "yarn": return `yarn add -D ${deps}`;
|
|
183
|
+
case "pnpm": return `pnpm add -D ${deps}`;
|
|
184
|
+
case "bun": return `bun add -D ${deps}`;
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
function promptConfirmation(question) {
|
|
188
|
+
const rl = createInterface({
|
|
189
|
+
input: process.stdin,
|
|
190
|
+
output: process.stdout
|
|
191
|
+
});
|
|
192
|
+
return new Promise((resolve) => {
|
|
193
|
+
rl.question(`${question} (Y/n): `, (answer) => {
|
|
194
|
+
rl.close();
|
|
195
|
+
const normalized = answer.trim().toLowerCase();
|
|
196
|
+
resolve(normalized === "" || normalized === "y" || normalized === "yes");
|
|
197
|
+
});
|
|
198
|
+
});
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
//#endregion
|
|
202
|
+
//#region src/configs.ts
|
|
203
|
+
function createCommitlintConfig(cwd) {
|
|
204
|
+
log("📝 Creating commitlint configuration...", "info");
|
|
205
|
+
writeTextFile(join(cwd, "commitlint.config.js"), `export default ${JSON.stringify(COMMITLINT_CONFIG, null, 4)};`);
|
|
206
|
+
log("✓ commitlint.config.js created", "success");
|
|
207
|
+
}
|
|
208
|
+
function createSemanticReleaseConfig(cwd) {
|
|
209
|
+
log("📝 Creating semantic-release configuration...", "info");
|
|
210
|
+
const configContent = `const config = ${JSON.stringify(SEMANTIC_RELEASE_CONFIG, null, 4)};
|
|
211
|
+
|
|
212
|
+
export default config;
|
|
213
|
+
`;
|
|
214
|
+
writeTextFile(join(cwd, ".releaserc.mjs"), configContent);
|
|
215
|
+
log("✓ .releaserc.mjs created", "success");
|
|
216
|
+
}
|
|
217
|
+
function setupLefthook(cwd, pm) {
|
|
218
|
+
log("🥊 Setting up Lefthook...", "info");
|
|
219
|
+
writeTextFile(join(cwd, "lefthook.yml"), getLefthookConfig(pm));
|
|
220
|
+
try {
|
|
221
|
+
let execCmd = "npx lefthook install";
|
|
222
|
+
if (pm === "bun") execCmd = "bunx lefthook install";
|
|
223
|
+
if (pm === "yarn") execCmd = "yarn dlx lefthook install";
|
|
224
|
+
if (pm === "pnpm") execCmd = "pnpm dlx lefthook install";
|
|
225
|
+
execCommand(execCmd, cwd);
|
|
226
|
+
log("✓ Lefthook installed and configured", "success");
|
|
227
|
+
} catch (error) {
|
|
228
|
+
log(`⚠️ Failed to run 'lefthook install': ${error}`, "warning");
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
function updatePackageJson(cwd) {
|
|
232
|
+
log("📦 Updating package.json scripts...", "info");
|
|
233
|
+
const packageJsonPath = join(cwd, "package.json");
|
|
234
|
+
const scripts = {
|
|
235
|
+
release: "semantic-release",
|
|
236
|
+
"release:dry": "semantic-release --dry-run",
|
|
237
|
+
prepare: "lefthook install"
|
|
238
|
+
};
|
|
239
|
+
let packageJson;
|
|
240
|
+
try {
|
|
241
|
+
packageJson = readJsonFile(packageJsonPath);
|
|
242
|
+
} catch (error) {
|
|
243
|
+
throw new Error(`Failed to read package.json: ${error}`);
|
|
244
|
+
}
|
|
245
|
+
if (!packageJson.scripts || typeof packageJson.scripts !== "object") packageJson.scripts = {};
|
|
246
|
+
const existingScripts = packageJson.scripts;
|
|
247
|
+
const conflicts = [];
|
|
248
|
+
const mergedScripts = { ...existingScripts };
|
|
249
|
+
for (const [key, value] of Object.entries(scripts)) if (existingScripts[key]) {
|
|
250
|
+
if (existingScripts[key] === value) continue;
|
|
251
|
+
conflicts.push(key);
|
|
252
|
+
if (key === "prepare") {
|
|
253
|
+
const existing = existingScripts[key];
|
|
254
|
+
if (existing.includes("&&") || existing.includes("lefthook")) {
|
|
255
|
+
mergedScripts[key] = `${existing} && ${value}`;
|
|
256
|
+
log(`⚠️ Merged prepare script: "${existing}" + "${value}"`, "warning");
|
|
257
|
+
} else {
|
|
258
|
+
mergedScripts[`${key}:lefthook`] = value;
|
|
259
|
+
log(`⚠️ Conflict detected for "${key}". Preserving existing and adding "prepare:lefthook"`, "warning");
|
|
260
|
+
}
|
|
261
|
+
} else {
|
|
262
|
+
mergedScripts[`${key}:semantic`] = value;
|
|
263
|
+
log(`⚠️ Conflict detected for "${key}". Preserving existing and adding "${key}:semantic"`, "warning");
|
|
264
|
+
}
|
|
265
|
+
} else mergedScripts[key] = value;
|
|
266
|
+
packageJson.scripts = mergedScripts;
|
|
267
|
+
try {
|
|
268
|
+
writeJsonFile(packageJsonPath, packageJson);
|
|
269
|
+
log("✓ package.json updated", "success");
|
|
270
|
+
if (conflicts.length > 0) log(`ℹ️ Conflicts resolved for scripts: ${conflicts.join(", ")}`, "info");
|
|
271
|
+
} catch (error) {
|
|
272
|
+
throw new Error(`Failed to write package.json: ${error}`);
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
function createGitHubWorkflow(cwd, pm) {
|
|
276
|
+
log("🔄 Creating GitHub Actions workflow...", "info");
|
|
277
|
+
const workflowDir = join(cwd, ".github", "workflows");
|
|
278
|
+
ensureDirectoryExists(workflowDir);
|
|
279
|
+
writeTextFile(join(workflowDir, "release.yml"), getGithubWorkflow(pm));
|
|
280
|
+
log("✓ GitHub Actions workflow created", "success");
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
//#endregion
|
|
284
|
+
//#region src/setup.ts
|
|
285
|
+
var ConventionalCommitSetup = class {
|
|
286
|
+
cwd;
|
|
287
|
+
packageManager;
|
|
288
|
+
skipConfirmation;
|
|
289
|
+
constructor(cwd = process.cwd(), skipConfirmation = false) {
|
|
290
|
+
this.cwd = cwd;
|
|
291
|
+
this.packageManager = detectPackageManager(cwd);
|
|
292
|
+
this.skipConfirmation = skipConfirmation || process.env.CI === "true" || process.env.NODE_ENV === "test";
|
|
293
|
+
}
|
|
294
|
+
installDependencies() {
|
|
295
|
+
log("📦 Installing dependencies...", "info");
|
|
296
|
+
execCommand(getInstallCommand(this.packageManager, DEPENDENCIES), this.cwd);
|
|
297
|
+
log("✓ Dependencies installed", "success");
|
|
298
|
+
}
|
|
299
|
+
showPreview() {
|
|
300
|
+
const installCmd = getInstallCommand(this.packageManager, DEPENDENCIES);
|
|
301
|
+
log("\n📋 The following will be installed and configured:", "info");
|
|
302
|
+
log("\n📦 Packages to install:", "info");
|
|
303
|
+
for (const dep of DEPENDENCIES) log(` • ${dep}`, "info");
|
|
304
|
+
log(`\n Install command: ${installCmd}`, "info");
|
|
305
|
+
log("\n📝 Configuration files to create:", "info");
|
|
306
|
+
log(" • commitlint.config.js", "info");
|
|
307
|
+
log(" • .releaserc.mjs", "info");
|
|
308
|
+
log(" • lefthook.yml", "info");
|
|
309
|
+
log(" • .github/workflows/release.yml", "info");
|
|
310
|
+
log("\n📦 package.json scripts to add:", "info");
|
|
311
|
+
log(" • release", "info");
|
|
312
|
+
log(" • release:dry", "info");
|
|
313
|
+
log(" • prepare (lefthook install)", "info");
|
|
314
|
+
}
|
|
315
|
+
async setup() {
|
|
316
|
+
console.log(ASCII_ART);
|
|
317
|
+
log("\n🚀 Setting up Conventional Commits...\n", "info");
|
|
318
|
+
log(`ℹ️ Detected package manager: ${this.packageManager}`, "info");
|
|
319
|
+
this.showPreview();
|
|
320
|
+
if (!this.skipConfirmation) {
|
|
321
|
+
if (!await promptConfirmation("\nDo you want to proceed with the installation")) {
|
|
322
|
+
log("\n❌ Setup cancelled by user", "error");
|
|
323
|
+
process.exit(0);
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
log("\n⏳ Starting installation...\n", "info");
|
|
327
|
+
try {
|
|
328
|
+
this.installDependencies();
|
|
329
|
+
createCommitlintConfig(this.cwd);
|
|
330
|
+
createSemanticReleaseConfig(this.cwd);
|
|
331
|
+
setupLefthook(this.cwd, this.packageManager);
|
|
332
|
+
updatePackageJson(this.cwd);
|
|
333
|
+
createGitHubWorkflow(this.cwd, this.packageManager);
|
|
334
|
+
log("\n✨ Setup completed successfully!\n", "success");
|
|
335
|
+
const runCmd = this.packageManager === "bun" ? "bun run" : `${this.packageManager} run`;
|
|
336
|
+
log("Next steps:", "info");
|
|
337
|
+
log("1. Commit your changes with a conventional commit message", "info");
|
|
338
|
+
log("2. Push to main/master branch to trigger automatic release", "info");
|
|
339
|
+
log(`3. Run '${runCmd} release:dry' to test the release process\n`, "info");
|
|
340
|
+
} catch (error) {
|
|
341
|
+
log("\n❌ Setup failed", "error");
|
|
342
|
+
throw error;
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
};
|
|
346
|
+
|
|
347
|
+
//#endregion
|
|
348
|
+
//#region src/index.ts
|
|
349
|
+
new ConventionalCommitSetup(process.argv[2] ? resolve(process.argv[2]) : void 0).setup().catch((error) => {
|
|
350
|
+
console.error(error);
|
|
351
|
+
process.exit(1);
|
|
352
|
+
});
|
|
353
|
+
|
|
354
|
+
//#endregion
|
|
355
|
+
export { };
|
package/package.json
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@vsokolov/semantix",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "CLI tool to setup conventional commits, semantic-release, and automated releases",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"cli",
|
|
7
|
+
"commitlint",
|
|
8
|
+
"conventional-commits",
|
|
9
|
+
"semantic-release",
|
|
10
|
+
"typescript"
|
|
11
|
+
],
|
|
12
|
+
"publishConfig": {
|
|
13
|
+
"access": "public"
|
|
14
|
+
},
|
|
15
|
+
"license": "MIT",
|
|
16
|
+
"author": "Viktor Sokolov",
|
|
17
|
+
"bin": {
|
|
18
|
+
"setup-commits": "./dist/index.js"
|
|
19
|
+
},
|
|
20
|
+
"files": [
|
|
21
|
+
"dist"
|
|
22
|
+
],
|
|
23
|
+
"type": "module",
|
|
24
|
+
"scripts": {
|
|
25
|
+
"build": "rolldown src/index.ts -o dist/index.js -f esm -p node --clean-dir",
|
|
26
|
+
"start": "bun run ./src/index.ts",
|
|
27
|
+
"dev": "bun --watch ./src/index.ts",
|
|
28
|
+
"lint": "oxlint",
|
|
29
|
+
"lint:fix": "oxlint --fix",
|
|
30
|
+
"format": "oxfmt src tests --write",
|
|
31
|
+
"format:check": "oxfmt --check src tests",
|
|
32
|
+
"typecheck": "tsc --noEmit",
|
|
33
|
+
"test": "bun test",
|
|
34
|
+
"test:coverage": "bun test --coverage",
|
|
35
|
+
"prepublishOnly": "bun run build",
|
|
36
|
+
"commit": "cz",
|
|
37
|
+
"jscpd": "jscpd src/ tests",
|
|
38
|
+
"up-latest": "bun update --latest",
|
|
39
|
+
"release": "semantic-release",
|
|
40
|
+
"release:dry": "semantic-release --dry-run",
|
|
41
|
+
"prepare": "bunx lefthook install"
|
|
42
|
+
},
|
|
43
|
+
"devDependencies": {
|
|
44
|
+
"@commitlint/cli": "^20.4.0",
|
|
45
|
+
"@commitlint/config-conventional": "^20.4.0",
|
|
46
|
+
"@semantic-release/changelog": "^6.0.3",
|
|
47
|
+
"@semantic-release/git": "^10.0.1",
|
|
48
|
+
"@semantic-release/github": "12.0.3",
|
|
49
|
+
"@types/bun": "1.3.8",
|
|
50
|
+
"@types/node": "25.2.0",
|
|
51
|
+
"find-up": "^8.0.0",
|
|
52
|
+
"jscpd": "4.0.8",
|
|
53
|
+
"lefthook": "^2.0.16",
|
|
54
|
+
"oxfmt": "0.27.0",
|
|
55
|
+
"oxlint": "1.42.0",
|
|
56
|
+
"rolldown": "1.0.0-rc.2",
|
|
57
|
+
"semantic-release": "^25.0.3",
|
|
58
|
+
"typescript": "^5.9.3"
|
|
59
|
+
},
|
|
60
|
+
"peerDependencies": {
|
|
61
|
+
"bun": "^1.3.8"
|
|
62
|
+
},
|
|
63
|
+
"config": {
|
|
64
|
+
"commitizen": {
|
|
65
|
+
"path": "cz-conventional-changelog"
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
}
|