commit-and-tag-version 12.5.2 → 12.6.1

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.
@@ -23,7 +23,7 @@ jobs:
23
23
  OS: ${{ matrix.os }}
24
24
  NODE_VERSION: ${{ matrix.node }}
25
25
  steps:
26
- - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4
26
+ - uses: actions/checkout@1af3b93b6815bc44a9784bd300feb67ff0d1eeb3 # v4
27
27
  - run: git fetch --prune --unshallow
28
28
  - run: git config --global user.name 'Actions'
29
29
  - run: git config --global user.email 'dummy@example.org'
@@ -15,7 +15,7 @@ jobs:
15
15
  release-type: node
16
16
  package-name: commit-and-tag-version
17
17
  # The logic below handles the npm publication:
18
- - uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4
18
+ - uses: actions/checkout@1af3b93b6815bc44a9784bd300feb67ff0d1eeb3 # v4
19
19
  # these if statements ensure that a publication only occurs when
20
20
  # a new release is created:
21
21
  if: ${{ steps.release.outputs.release_created }}
package/CHANGELOG.md CHANGED
@@ -2,6 +2,21 @@
2
2
 
3
3
  All notable changes to this project will be documented in this file. See [commit-and-tag-version](https://github.com/absolute-version/commit-and-tag-version) for commit guidelines.
4
4
 
5
+ ## [12.6.1](https://github.com/absolute-version/commit-and-tag-version/compare/v12.6.0...v12.6.1) (2025-12-01)
6
+
7
+
8
+ ### Bug Fixes
9
+
10
+ * **bump:** auto-increment prerelease when tag already exists ([#258](https://github.com/absolute-version/commit-and-tag-version/issues/258)) ([75d69a6](https://github.com/absolute-version/commit-and-tag-version/commit/75d69a6a63c545062525c5566b60d2903a4666f4))
11
+ * use `fast-xml-parser` to parse and build XML. This substantially reduces the bundle size. ([#234](https://github.com/absolute-version/commit-and-tag-version/issues/234)) ([6075f4b](https://github.com/absolute-version/commit-and-tag-version/commit/6075f4bb1e34d55fc403a3b5b49390af0e760684))
12
+
13
+ ## [12.6.0](https://github.com/absolute-version/commit-and-tag-version/compare/v12.5.2...v12.6.0) (2025-08-27)
14
+
15
+
16
+ ### Features
17
+
18
+ * add --config option to allow custom config file path ([#237](https://github.com/absolute-version/commit-and-tag-version/issues/237)) ([3958e68](https://github.com/absolute-version/commit-and-tag-version/commit/3958e688a60df4f1ba46137d1f4147a65817c8d7))
19
+
5
20
  ## [12.5.2](https://github.com/absolute-version/commit-and-tag-version/compare/v12.5.1...v12.5.2) (2025-07-30)
6
21
 
7
22
 
package/README.md CHANGED
@@ -38,6 +38,7 @@ _Having problems? Want to contribute? Join us on the [node-tooling community Sla
38
38
  - [Release as a Pre-Release](#release-as-a-pre-release)
39
39
  - [Release as a Target Type Imperatively (`npm version`-like)](#release-as-a-target-type-imperatively-npm-version-like)
40
40
  - [Prevent Git Hooks](#prevent-git-hooks)
41
+ - [Custom Config Path](#custom-config-path)
41
42
  - [Signing Commits and Tags](#signing-commits-and-tags)
42
43
  - [Lifecycle Scripts](#lifecycle-scripts)
43
44
  - [Skipping Lifecycle Steps](#skipping-lifecycle-steps)
@@ -283,6 +284,20 @@ npm run release -- --prerelease alpha
283
284
 
284
285
  This will tag the version as: `1.0.1-alpha.0`
285
286
 
287
+ #### Prerelease Tag Collision Avoidance
288
+
289
+ When cutting a prerelease with `--prerelease`, `commit-and-tag-version` automatically checks existing git tags (respecting your `tagPrefix` configuration) for the same base version and prerelease channel. If a tag already exists, the numeric suffix is automatically incremented to avoid conflicts.
290
+
291
+ For example, if you're working with multiple prerelease channels simultaneously:
292
+
293
+ ```bash
294
+ commit-and-tag-version --prerelease xyz # Creates v1.4.3-xyz.0
295
+ commit-and-tag-version --prerelease abc # Creates v1.4.3-abc.0
296
+ commit-and-tag-version --prerelease xyz # Creates v1.4.3-xyz.1 (auto-incremented)
297
+ ```
298
+
299
+ This behavior applies to both named prereleases (e.g., `-alpha.0`, `-beta.1`) and unnamed prereleases (e.g., `-0`, `-1`), ensuring that you can safely cut multiple prerelease versions without encountering git tag conflicts.
300
+
286
301
  ### Release as a Target Type Imperatively (`npm version`-like)
287
302
 
288
303
  To forgo the automated version bump use `--release-as` with the argument `major`, `minor` or `patch`.
@@ -312,6 +327,23 @@ npm run release -- --no-verify
312
327
  commit-and-tag-version --no-verify
313
328
  ```
314
329
 
330
+ ### Custom Config Path
331
+
332
+ Specify a custom path to the configuration file using the `--config` option
333
+
334
+ ```sh
335
+ commit-and-tag-version --config ./path/to/.versionrc.js
336
+
337
+ # or using alias
338
+ commit-and-tag-version -c ./path/to/.versionrc.js
339
+ ```
340
+
341
+ All config file formats can be used:
342
+
343
+ ```sh
344
+ commit-and-tag-version -c ./path/to/.versionrc[.js|.cjs|.json]
345
+ ```
346
+
315
347
  ### Signing Commits and Tags
316
348
 
317
349
  If you have your GPG key set up, add the `--sign` or `-s` flag to your `commit-and-tag-version` command.
package/command.js CHANGED
@@ -132,6 +132,12 @@ const yargs = require('yargs')
132
132
  default: defaults.npmPublishHint,
133
133
  describe: 'Customized publishing hint',
134
134
  })
135
+ .option('config', {
136
+ type: 'string',
137
+ default: defaults.config,
138
+ alias: 'c',
139
+ describe: 'Path to a custom configuration file',
140
+ })
135
141
  .check((argv) => {
136
142
  if (typeof argv.scripts !== 'object' || Array.isArray(argv.scripts)) {
137
143
  throw Error('scripts must be an object');
@@ -150,9 +156,10 @@ const yargs = require('yargs')
150
156
  )
151
157
  .pkgConf('standard-version')
152
158
  .pkgConf('commit-and-tag-version')
153
- .config(getConfiguration())
154
159
  .wrap(97);
155
160
 
161
+ yargs.config(getConfiguration(yargs.argv.config));
162
+
156
163
  Object.keys(spec.properties).forEach((propertyKey) => {
157
164
  const property = spec.properties[propertyKey];
158
165
  yargs.option(propertyKey, {
package/defaults.js CHANGED
@@ -17,6 +17,7 @@ const defaults = {
17
17
  gitTagFallback: true,
18
18
  preset: require.resolve('conventional-changelog-conventionalcommits'),
19
19
  npmPublishHint: undefined,
20
+ config: undefined,
20
21
  };
21
22
 
22
23
  /**
@@ -9,9 +9,13 @@ const CONFIGURATION_FILES = [
9
9
  '.versionrc.js',
10
10
  ];
11
11
 
12
- module.exports.getConfiguration = function () {
12
+ module.exports.getConfiguration = function (configFile) {
13
13
  let config = {};
14
- const configPath = findUp.sync(CONFIGURATION_FILES);
14
+
15
+ // If the user has provided a configuration file via the `--config` argument, we use that.
16
+ const configurationFiles = configFile ?? CONFIGURATION_FILES;
17
+
18
+ const configPath = findUp.sync(configurationFiles);
15
19
  if (!configPath) {
16
20
  return config;
17
21
  }
@@ -12,6 +12,7 @@ const runLifecycleScript = require('../run-lifecycle-script');
12
12
  const semver = require('semver');
13
13
  const writeFile = require('../write-file');
14
14
  const { resolveUpdaterObjectFromArgument } = require('../updaters');
15
+ const gitSemverTags = require('git-semver-tags');
15
16
  let configsToUpdate = {};
16
17
  const sanitizeQuotesRegex = /['"]+/g;
17
18
 
@@ -83,6 +84,15 @@ async function Bump(args, version) {
83
84
 
84
85
  newVersion = semver.inc(version, releaseType, args.prerelease);
85
86
  }
87
+
88
+ // If creating a prerelease, ensure the computed version is unique among existing git tags
89
+ if (isString(args.prerelease) && newVersion) {
90
+ newVersion = await resolveUniquePrereleaseVersion(
91
+ newVersion,
92
+ args.tagPrefix,
93
+ args.prerelease,
94
+ );
95
+ }
86
96
  updateConfigs(args, newVersion);
87
97
  } else {
88
98
  checkpoint(
@@ -261,3 +271,96 @@ function updateConfigs(args, newVersion) {
261
271
  }
262
272
 
263
273
  module.exports = Bump;
274
+
275
+ /**
276
+ * Ensure prerelease version uniqueness by checking existing git tags.
277
+ * If a tag for the same base version and prerelease identifier exists, bump the numeric suffix.
278
+ * @param {string} proposedVersion The version computed by bump logic, may include build metadata.
279
+ * @param {string} tagPrefix The tag prefix to respect when reading tags (e.g., 'v').
280
+ * @param {string} prereleaseId The prerelease identifier (e.g., 'alpha', 'beta', 'rc').
281
+ * @returns {Promise<string>} The adjusted version that does not collide with existing tags.
282
+ */
283
+ async function resolveUniquePrereleaseVersion(
284
+ proposedVersion,
285
+ tagPrefix,
286
+ prereleaseId,
287
+ ) {
288
+ try {
289
+ const parsed = new semver.SemVer(proposedVersion);
290
+ const base = `${parsed.major}.${parsed.minor}.${parsed.patch}`;
291
+ const build = parsed.build; // preserve build metadata if present
292
+
293
+ // Determine current numeric index depending on named vs unnamed prerelease
294
+ let currentNum = 0;
295
+ if (Array.isArray(parsed.prerelease) && parsed.prerelease.length) {
296
+ if (prereleaseId === '' && typeof parsed.prerelease[0] === 'number') {
297
+ // unnamed prerelease like 1.2.3-0
298
+ currentNum = parsed.prerelease[0];
299
+ } else if (typeof parsed.prerelease[1] === 'number') {
300
+ // named prerelease like 1.2.3-alpha.0
301
+ currentNum = parsed.prerelease[1];
302
+ }
303
+ }
304
+
305
+ const tags = await new Promise((resolve, reject) => {
306
+ gitSemverTags({ tagPrefix }, (err, t) =>
307
+ err ? reject(err) : resolve(t || []),
308
+ );
309
+ });
310
+
311
+ // strip prefix and clean
312
+ const cleaned = tags
313
+ .map((t) => t.replace(new RegExp('^' + tagPrefix), ''))
314
+ .map((t) => (semver.valid(t) ? semver.clean(t) : null))
315
+ .filter(Boolean);
316
+
317
+ // collect numeric suffix for same base and prerelease id (or unnamed prerelease)
318
+ const nums = cleaned
319
+ .filter((t) => {
320
+ const v = new semver.SemVer(t);
321
+ if (!Array.isArray(v.prerelease) || v.prerelease.length === 0)
322
+ return false;
323
+ const sameBase =
324
+ v.major === parsed.major &&
325
+ v.minor === parsed.minor &&
326
+ v.patch === parsed.patch;
327
+ if (!sameBase) return false;
328
+ if (prereleaseId === '') {
329
+ // unnamed prerelease: include tags where first prerelease token is numeric
330
+ return typeof v.prerelease[0] === 'number';
331
+ }
332
+ // named prerelease: match by identifier
333
+ return String(v.prerelease[0]) === String(prereleaseId);
334
+ })
335
+ .map((t) => {
336
+ const v = new semver.SemVer(t);
337
+ if (prereleaseId === '') {
338
+ return typeof v.prerelease[0] === 'number' ? v.prerelease[0] : 0;
339
+ }
340
+ return typeof v.prerelease[1] === 'number' ? v.prerelease[1] : 0;
341
+ });
342
+
343
+ if (nums.length === 0) {
344
+ // no collisions possible
345
+ return proposedVersion;
346
+ }
347
+
348
+ const maxExisting = Math.max(...nums);
349
+ // If our proposed numeric index is already used or below max, bump to max + 1
350
+ if (currentNum <= maxExisting) {
351
+ let candidate =
352
+ prereleaseId === ''
353
+ ? `${base}-${maxExisting + 1}`
354
+ : `${base}-${prereleaseId}.${maxExisting + 1}`;
355
+ // re-append build metadata if any
356
+ if (build && build.length) {
357
+ candidate = semvarToVersionStr(candidate, build);
358
+ }
359
+ return candidate;
360
+ }
361
+ return proposedVersion;
362
+ } catch {
363
+ // If anything goes wrong, fall back to proposedVersion
364
+ return proposedVersion;
365
+ }
366
+ }
@@ -1,40 +1,39 @@
1
- const jsdom = require('jsdom');
2
- const serialize = require('w3c-xmlserializer');
3
1
  const detectNewline = require('detect-newline');
2
+ const { XMLParser, XMLBuilder } = require('fast-xml-parser');
3
+
4
4
  const CRLF = '\r\n';
5
5
  const LF = '\n';
6
6
 
7
7
  function pomDocument(contents) {
8
- const dom = new jsdom.JSDOM('');
9
- const parser = new dom.window.DOMParser();
10
- return parser.parseFromString(contents, 'application/xml');
8
+ const parser = new XMLParser();
9
+ return parser.parse(contents);
11
10
  }
12
11
 
13
- function pomVersionElement(document) {
14
- const versionElement = document.querySelector('project > version');
12
+ function pomVersion(document) {
13
+ const version = document?.project?.version;
15
14
 
16
- if (!versionElement) {
15
+ if (!version) {
17
16
  throw new Error(
18
17
  'Failed to read the version field in your pom file - is it present?',
19
18
  );
20
19
  }
21
20
 
22
- return versionElement;
21
+ return version;
23
22
  }
24
23
 
25
24
  module.exports.readVersion = function (contents) {
26
25
  const document = pomDocument(contents);
27
- return pomVersionElement(document).textContent;
26
+ return pomVersion(document);
28
27
  };
29
28
 
30
29
  module.exports.writeVersion = function (contents, version) {
31
30
  const newline = detectNewline(contents);
32
31
  const document = pomDocument(contents);
33
- const versionElement = pomVersionElement(document);
34
32
 
35
- versionElement.textContent = version;
33
+ document.project.version = version;
36
34
 
37
- const xml = serialize(document);
35
+ const builder = new XMLBuilder({ format: true });
36
+ const xml = builder.build(document);
38
37
 
39
38
  if (newline === CRLF) {
40
39
  return xml.replace(/\n/g, CRLF) + CRLF;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "commit-and-tag-version",
3
- "version": "12.5.2",
3
+ "version": "12.6.1",
4
4
  "description": "replacement for `npm version` with automatic CHANGELOG generation",
5
5
  "bin": "bin/cli.js",
6
6
  "scripts": {
@@ -47,27 +47,26 @@
47
47
  "detect-indent": "^6.1.0",
48
48
  "detect-newline": "^3.1.0",
49
49
  "dotgitignore": "^2.1.0",
50
+ "fast-xml-parser": "^5.2.5",
50
51
  "figures": "^3.2.0",
51
52
  "find-up": "^5.0.0",
52
53
  "git-semver-tags": "^5.0.1",
53
- "jsdom": "^25.0.1",
54
54
  "semver": "^7.7.2",
55
- "w3c-xmlserializer": "^5.0.0",
56
55
  "yaml": "^2.6.0",
57
56
  "yargs": "^17.7.2"
58
57
  },
59
58
  "devDependencies": {
60
- "@eslint/js": "^9.13.0",
59
+ "@eslint/js": "^9.36.0",
61
60
  "eslint": "^9.13.0",
62
- "eslint-config-prettier": "^9.1.0",
61
+ "eslint-config-prettier": "^10.1.8",
63
62
  "eslint-plugin-import": "^2.31.0",
64
63
  "eslint-plugin-jest": "^28.8.3",
65
- "eslint-plugin-n": "^17.11.1",
64
+ "eslint-plugin-n": "^17.21.3",
66
65
  "eslint-plugin-promise": "^7.1.0",
67
66
  "jest": "^29.7.0",
68
67
  "jest-serial-runner": "^1.2.1",
69
68
  "prettier": "^3.3.3",
70
- "shelljs": "^0.8.5",
69
+ "shelljs": "^0.10.0",
71
70
  "strip-ansi": "^6.0.1"
72
71
  }
73
72
  }
@@ -86,6 +86,30 @@ describe('config files', function () {
86
86
  expect(content).toContain(issueUrlFormat);
87
87
  });
88
88
 
89
+ it('reads config from custom path', async function () {
90
+ const issueUrlFormat = 'http://www.foo.com/{{id}}';
91
+ const changelog = ({ preset }) => preset.issueUrlFormat;
92
+ mock({ bump: 'minor', changelog });
93
+ fs.mkdirSync('custom-folder');
94
+ fs.writeFileSync(
95
+ 'custom-folder/.versionrc.json',
96
+ JSON.stringify({ issueUrlFormat }),
97
+ 'utf-8',
98
+ );
99
+
100
+ // Override process.argv to simulate CLI arguments before `exec`.
101
+ // This ensures yargs parses the custom config argument.
102
+ const originalArgv = process.argv;
103
+ process.argv = ['node', 'script.js', '-c', 'custom-folder/.versionrc.json'];
104
+
105
+ await exec(['-c', 'custom-folder/.versionrc.json']);
106
+ const content = fs.readFileSync('CHANGELOG.md', 'utf-8');
107
+ expect(content).toContain(issueUrlFormat);
108
+
109
+ // Restore original process.argv
110
+ process.argv = originalArgv;
111
+ });
112
+
89
113
  it('evaluates a config-function from .versionrc.js', async function () {
90
114
  const issueUrlFormat = 'http://www.foo.com/{{id}}';
91
115
  const src = `module.exports = function() { return ${JSON.stringify({
@@ -248,6 +248,45 @@ describe('git', function () {
248
248
  expect(getPackageVersion()).toEqual('1.1.0-0');
249
249
  });
250
250
 
251
+ it('increments unnamed prerelease number when unnamed prerelease tag already exists', async function () {
252
+ writePackageJson('1.2.3');
253
+ // Existing unnamed prerelease tag 1.2.3-0 exists
254
+ mock({ bump: 'patch', tags: ['v1.2.3-0'] });
255
+ await exec('--prerelease');
256
+ expect(getPackageVersion()).toEqual('1.2.4-0');
257
+ // Now start from a prerelease of same base to trigger unnamed collision
258
+ writePackageJson('1.2.3-0');
259
+ mock({ bump: 'patch', tags: ['v1.2.3-0'] });
260
+ await exec('--prerelease');
261
+ expect(getPackageVersion()).toEqual('1.2.3-1');
262
+ });
263
+
264
+ it('increments unnamed prerelease number with gitTagFallback when unnamed prerelease tag already exists', async function () {
265
+ shell.rm('package.json');
266
+ mock({ bump: 'patch', tags: ['v1.2.3-0'] });
267
+ await exec({ packageFiles: [], gitTagFallback: true, prerelease: '' });
268
+ const output = shell.exec('git tag').stdout;
269
+ expect(output).toMatch(/v1\.2\.3-1/);
270
+ });
271
+
272
+ it('increments prerelease number when same prerelease tag already exists', async function () {
273
+ writePackageJson('1.4.3-abc.0');
274
+ // Simulate existing tags where v1.4.3-xyz.0 already exists from git history
275
+ mock({ bump: 'patch', tags: ['v1.4.3-xyz.0'] });
276
+ await exec('--prerelease xyz');
277
+ // Base remains 1.4.3 when switching prerelease channel mid-cycle; must bump numeric suffix to avoid tag collision
278
+ expect(getPackageVersion()).toEqual('1.4.3-xyz.1');
279
+ });
280
+
281
+ it('increments prerelease number with gitTagFallback when same prerelease tag already exists', async function () {
282
+ // Setup without package.json and with existing tags only
283
+ shell.rm('package.json');
284
+ mock({ bump: 'patch', tags: ['v1.4.3-xyz.0'] });
285
+ await exec({ packageFiles: [], gitTagFallback: true, prerelease: 'xyz' });
286
+ const output = shell.exec('git tag').stdout;
287
+ expect(output).toMatch(/v1\.4\.3-xyz\.1/);
288
+ });
289
+
251
290
  describe('gitTagFallback', function () {
252
291
  beforeEach(function () {
253
292
  setup();
@@ -1,20 +1,16 @@
1
1
  <project>
2
2
  <modelVersion>4.0.0</modelVersion>
3
-
4
3
  <parent>
5
4
  <groupId>com.mycompany.app</groupId>
6
5
  <artifactId>my-app</artifactId>
7
6
  <version>3.0.0</version>
8
7
  </parent>
9
-
10
8
  <groupId>com.mycompany.app</groupId>
11
9
  <artifactId>my-module</artifactId>
12
10
  <version>6.4.0</version>
13
-
14
11
  <properties>
15
12
  <some.version>2.0.0</some.version>
16
13
  </properties>
17
-
18
14
  <dependencies>
19
15
  <dependency>
20
16
  <groupId>org.some.dependency</groupId>
@@ -28,3 +24,4 @@
28
24
  </dependency>
29
25
  </dependencies>
30
26
  </project>
27
+
@@ -1,20 +1,16 @@
1
1
  <project>
2
2
  <modelVersion>4.0.0</modelVersion>
3
-
4
3
  <parent>
5
4
  <groupId>com.mycompany.app</groupId>
6
5
  <artifactId>my-app</artifactId>
7
6
  <version>3.0.0</version>
8
7
  </parent>
9
-
10
8
  <groupId>com.mycompany.app</groupId>
11
9
  <artifactId>my-module</artifactId>
12
10
  <version>6.4.0</version>
13
-
14
11
  <properties>
15
12
  <some.version>2.0.0</some.version>
16
13
  </properties>
17
-
18
14
  <dependencies>
19
15
  <dependency>
20
16
  <groupId>org.some.dependency</groupId>
@@ -28,3 +24,4 @@
28
24
  </dependency>
29
25
  </dependencies>
30
26
  </project>
27
+