semver-ratchet 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/README.md ADDED
@@ -0,0 +1,284 @@
1
+ # semver-ratchet (Node.js)
2
+
3
+ Node.js implementation of the semver-ratchet versioning system.
4
+
5
+ ## Prerequisites
6
+
7
+ ### Node.js Version Management (nvm)
8
+
9
+ This project uses **nvm** (Node Version Manager) for managing Node.js versions.
10
+
11
+ **Install nvm** (if not already installed):
12
+
13
+ ```bash
14
+ # macOS / Linux
15
+ curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash
16
+
17
+ # Then restart your terminal or run:
18
+ source ~/.bashrc # or ~/.zshrc for zsh
19
+ ```
20
+
21
+ **Install and use Node.js**:
22
+
23
+ ```bash
24
+ # Install latest LTS version
25
+ nvm install --lts
26
+
27
+ # Set as default
28
+ nvm use --lts
29
+
30
+ # Verify installation
31
+ node --version
32
+ npm --version
33
+ ```
34
+
35
+ **Project-specific Node.js version**:
36
+
37
+ ```bash
38
+ # Create .nvmrc file with desired Node.js version
39
+ echo "lts/*" > .nvmrc
40
+
41
+ # Automatically use the correct version when entering project directory
42
+ nvm use
43
+ ```
44
+
45
+ ## Installation
46
+
47
+ ### Development Setup
48
+
49
+ ```bash
50
+ cd node
51
+
52
+ # Install dependencies
53
+ npm install
54
+
55
+ # Run tests
56
+ npm test
57
+ ```
58
+
59
+ ### As a Package
60
+
61
+ ```bash
62
+ npm install semver-ratchet
63
+ ```
64
+
65
+ ## Usage
66
+
67
+ ### As a Module
68
+
69
+ ```javascript
70
+ import semverRatchet from 'semver-ratchet';
71
+
72
+ // Get current version
73
+ const version = semverRatchet.getVersion();
74
+ console.log(version); // e.g., "1.0.0" or "0.12345678.5"
75
+
76
+ // Get compatible versions for dependency pinning
77
+ const compatible = semverRatchet.getCompatibleVersions(version);
78
+ console.log(compatible); // e.g., ["1.0.0", "1.0", "1"]
79
+
80
+ // Get current branch
81
+ const branch = semverRatchet.getBranch();
82
+ console.log(branch);
83
+
84
+ // Check if on main branch
85
+ const isMain = semverRatchet.isMainBranch();
86
+ console.log(isMain);
87
+
88
+ // Calculate CRC32 of a string
89
+ const crc = semverRatchet.calculateCrc32Unsigned('feature/my-branch');
90
+ console.log(crc);
91
+
92
+ // Create a git tag (main branch only)
93
+ semverRatchet.createGitTag(version);
94
+
95
+ // Push tag to remote
96
+ semverRatchet.pushGitTag(version);
97
+ ```
98
+
99
+ ### CommonJS
100
+
101
+ ```javascript
102
+ const semverRatchet = require('semver-ratchet');
103
+
104
+ const version = semverRatchet.getVersion();
105
+ console.log(version);
106
+ ```
107
+
108
+ ### CLI
109
+
110
+ ```bash
111
+ # Get current version
112
+ npx semver-ratchet version
113
+
114
+ # Get current branch
115
+ npx semver-ratchet branch
116
+
117
+ # Calculate CRC32 hash
118
+ npx semver-ratchet crc32 feature/my-branch
119
+
120
+ # Validate commit message
121
+ npx semver-ratchet validate "feat: add login [MINOR]"
122
+
123
+ # Check version monotonicity
124
+ npx semver-ratchet check-monotonic
125
+
126
+ # Display versioning information
127
+ npx semver-ratchet info
128
+
129
+ # Generate compatible versions in CSV format
130
+ npx semver-ratchet versions
131
+
132
+ # Verify git history
133
+ npx semver-ratchet verify
134
+
135
+ # Create and push git tag
136
+ npx semver-ratchet tag --push
137
+
138
+ # With custom options
139
+ npx semver-ratchet version --default-bump minor
140
+ npx semver-ratchet tag --force --push --remote upstream
141
+ ```
142
+
143
+ ## API Reference
144
+
145
+ ### Functions
146
+
147
+ #### `getVersion(defaultBump?, verifyHistory?)`
148
+ Get the appropriate version based on current branch.
149
+
150
+ - **Parameters:**
151
+ - `defaultBump` (string, optional): Default bump type - "major", "minor", or "patch" (default: "patch")
152
+ - `verifyHistory` (boolean, optional): Verify sufficient git history exists (default: true)
153
+ - **Returns:** string - Version string
154
+
155
+ #### `calculateFeatureVersion(verifyHistory?)`
156
+ Calculate version for a feature branch.
157
+
158
+ - **Returns:** string - Version in format "0.{CRC32}.{Distance}"
159
+
160
+ #### `calculateMainVersion(defaultBump?, verifyHistory?)`
161
+ Calculate version for main branch using SemVer.
162
+
163
+ - **Returns:** string - Version in format "Major.Minor.Patch"
164
+
165
+ #### `getCompatibleVersions(version)`
166
+ Generate compatible version strings for dependency pinning.
167
+
168
+ - **Parameters:**
169
+ - `version` (string): Full version string
170
+ - **Returns:** string[] - Array of compatible versions
171
+
172
+ #### `getCurrentBranch()`
173
+ Get the current Git branch name.
174
+
175
+ - **Returns:** string
176
+
177
+ #### `isMainBranch()`
178
+ Check if current branch is main or master.
179
+
180
+ - **Returns:** boolean
181
+
182
+ #### `calculateCrc32Unsigned(str)`
183
+ Calculate CRC32 checksum as unsigned 32-bit integer.
184
+
185
+ - **Parameters:**
186
+ - `str` (string): String to hash
187
+ - **Returns:** number
188
+
189
+ #### `parseSemver(version)`
190
+ Parse a SemVer string into components.
191
+
192
+ - **Parameters:**
193
+ - `version` (string): Version string
194
+ - **Returns:** object - { major, minor, patch }
195
+
196
+ #### `compareSemver(v1, v2)`
197
+ Compare two SemVer versions.
198
+
199
+ - **Parameters:**
200
+ - `v1` (string): First version
201
+ - `v2` (string): Second version
202
+ - **Returns:** number - -1 if v1 < v2, 0 if equal, 1 if v1 > v2
203
+
204
+ #### `determineVersionBump(commitMessages, defaultBump?)`
205
+ Determine version bump based on commit messages.
206
+
207
+ - **Parameters:**
208
+ - `commitMessages` (string[]): List of commit messages
209
+ - `defaultBump` (string, optional): Default bump type
210
+ - **Returns:** string - "major", "minor", or "patch"
211
+
212
+ #### `createGitTag(version, force?)`
213
+ Create a Git tag for the given version.
214
+
215
+ - **Parameters:**
216
+ - `version` (string): Version string
217
+ - `force` (boolean, optional): Overwrite existing tag
218
+ - **Returns:** boolean
219
+
220
+ #### `pushGitTag(version, remote?)`
221
+ Push a Git tag to remote repository.
222
+
223
+ - **Parameters:**
224
+ - `version` (string): Version string
225
+ - `remote` (string, optional): Remote name (default: "origin")
226
+ - **Returns:** boolean
227
+
228
+ #### `verifyGitHistory(fetchDepth?)`
229
+ Verify sufficient git history is available.
230
+
231
+ - **Parameters:**
232
+ - `fetchDepth` (number, optional): Minimum commits required (default: 50)
233
+ - **Returns:** boolean
234
+
235
+ ## Package.json Configuration
236
+
237
+ Add to your `package.json`:
238
+
239
+ ```json
240
+ {
241
+ "dependencies": {
242
+ "semver-ratchet": "^0.2.0"
243
+ },
244
+ "scripts": {
245
+ "version": "semver-ratchet version",
246
+ "prepublish": "semver-ratchet tag --push"
247
+ }
248
+ }
249
+ ```
250
+
251
+ ## Environment Variables
252
+
253
+ - `RATCHET_OVERRIDE`: Override version completely (bypasses git). Useful for dev environments.
254
+
255
+ ```bash
256
+ RATCHET_OVERRIDE=dev-local npx semver-ratchet version
257
+ # Output: dev-local
258
+ ```
259
+
260
+ ## Testing
261
+
262
+ ### Run All Tests
263
+
264
+ ```bash
265
+ npm test
266
+ ```
267
+
268
+ ### Run Tests in Watch Mode
269
+
270
+ ```bash
271
+ npm run test:watch
272
+ ```
273
+
274
+ ### Run Specific Test File
275
+
276
+ ```bash
277
+ node --test test/version.test.js
278
+ ```
279
+
280
+ **Note**: All tests use MockGitAdapter to avoid system git operations during testing, ensuring deterministic and isolated test results.
281
+
282
+ ## License
283
+
284
+ MIT
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env node
2
+ import { main } from '../src/cli.js';
3
+
4
+ main();
package/index.js ADDED
@@ -0,0 +1,29 @@
1
+ /**
2
+ * semver-ratchet: Forward-only versioning for Trunk-Based Development.
3
+ *
4
+ * This module provides monotonic versioning based on Git history.
5
+ * Feature branches use ephemeral versions (0.{CRC32}.{Distance}).
6
+ * Main branch uses standard Semantic Versioning (Major.Minor.Patch).
7
+ */
8
+
9
+ export {
10
+ getVersion,
11
+ calculateFeatureVersion,
12
+ calculateMainVersion,
13
+ getCurrentBranch,
14
+ isMainBranch,
15
+ calculateCrc32Unsigned,
16
+ verifyGitHistory,
17
+ parseSemver,
18
+ compareSemver,
19
+ getCompatibleVersions,
20
+ determineVersionBump,
21
+ createGitTag,
22
+ pushGitTag,
23
+ getBranch
24
+ } from './src/version.js';
25
+
26
+ export { main } from './src/cli.js';
27
+
28
+ // Version of this package
29
+ export const VERSION = '0.2.0';
package/package.json ADDED
@@ -0,0 +1,50 @@
1
+ {
2
+ "name": "semver-ratchet",
3
+ "version": "1.0.0",
4
+ "description": "Forward-only versioning for Trunk-Based Development",
5
+ "main": "index.js",
6
+ "type": "module",
7
+ "bin": {
8
+ "semver-ratchet": "./bin/semver-ratchet"
9
+ },
10
+ "scripts": {
11
+ "test": "node --test test/*.js",
12
+ "test:watch": "node --test --watch test/*.js",
13
+ "lint": "eslint src/ test/ bin/ --max-warnings 0",
14
+ "lint:fix": "eslint src/ test/ bin/ --fix",
15
+ "prepublishOnly": "npm test"
16
+ },
17
+ "keywords": [
18
+ "semver",
19
+ "versioning",
20
+ "git",
21
+ "trunk-based",
22
+ "ci/cd",
23
+ "ratchet"
24
+ ],
25
+ "author": "a-collet (https://github.com/a-collet)",
26
+ "contributors": [
27
+ "semver-ratchet contributors"
28
+ ],
29
+ "license": "MIT",
30
+ "repository": {
31
+ "type": "git",
32
+ "url": "https://github.com/a-collet/semver-ratchet.git"
33
+ },
34
+ "bugs": {
35
+ "url": "https://github.com/a-collet/semver-ratchet/issues"
36
+ },
37
+ "homepage": "https://github.com/a-collet/semver-ratchet#readme",
38
+ "engines": {
39
+ "node": ">=16.0.0"
40
+ },
41
+ "files": [
42
+ "src/",
43
+ "bin/",
44
+ "index.js"
45
+ ],
46
+ "devDependencies": {
47
+ "eslint": "^10.1.0",
48
+ "eslint-config-google": "^0.14.0"
49
+ }
50
+ }