@hyperfrontend/versioning 0.1.0 → 0.2.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.
Files changed (158) hide show
  1. package/ARCHITECTURE.md +50 -1
  2. package/CHANGELOG.md +23 -23
  3. package/README.md +12 -9
  4. package/changelog/index.cjs.js +23 -2
  5. package/changelog/index.cjs.js.map +1 -1
  6. package/changelog/index.esm.js +23 -2
  7. package/changelog/index.esm.js.map +1 -1
  8. package/changelog/models/entry.d.ts +5 -0
  9. package/changelog/models/entry.d.ts.map +1 -1
  10. package/changelog/models/index.cjs.js +2 -0
  11. package/changelog/models/index.cjs.js.map +1 -1
  12. package/changelog/models/index.esm.js +2 -0
  13. package/changelog/models/index.esm.js.map +1 -1
  14. package/changelog/operations/index.cjs.js.map +1 -1
  15. package/changelog/operations/index.esm.js.map +1 -1
  16. package/changelog/parse/index.cjs.js +23 -2
  17. package/changelog/parse/index.cjs.js.map +1 -1
  18. package/changelog/parse/index.esm.js +23 -2
  19. package/changelog/parse/index.esm.js.map +1 -1
  20. package/changelog/parse/line.d.ts.map +1 -1
  21. package/commits/classify/classifier.d.ts +73 -0
  22. package/commits/classify/classifier.d.ts.map +1 -0
  23. package/commits/classify/index.cjs.js +705 -0
  24. package/commits/classify/index.cjs.js.map +1 -0
  25. package/commits/classify/index.d.ts +8 -0
  26. package/commits/classify/index.d.ts.map +1 -0
  27. package/commits/classify/index.esm.js +678 -0
  28. package/commits/classify/index.esm.js.map +1 -0
  29. package/commits/classify/infrastructure.d.ts +205 -0
  30. package/commits/classify/infrastructure.d.ts.map +1 -0
  31. package/commits/classify/models.d.ts +108 -0
  32. package/commits/classify/models.d.ts.map +1 -0
  33. package/commits/classify/project-scopes.d.ts +59 -0
  34. package/commits/classify/project-scopes.d.ts.map +1 -0
  35. package/commits/index.cjs.js +702 -0
  36. package/commits/index.cjs.js.map +1 -1
  37. package/commits/index.d.ts +1 -0
  38. package/commits/index.d.ts.map +1 -1
  39. package/commits/index.esm.js +677 -1
  40. package/commits/index.esm.js.map +1 -1
  41. package/flow/executor/execute.d.ts +6 -0
  42. package/flow/executor/execute.d.ts.map +1 -1
  43. package/flow/executor/index.cjs.js +1604 -42
  44. package/flow/executor/index.cjs.js.map +1 -1
  45. package/flow/executor/index.esm.js +1610 -48
  46. package/flow/executor/index.esm.js.map +1 -1
  47. package/flow/index.cjs.js +6651 -2893
  48. package/flow/index.cjs.js.map +1 -1
  49. package/flow/index.esm.js +6655 -2899
  50. package/flow/index.esm.js.map +1 -1
  51. package/flow/models/index.cjs.js +125 -0
  52. package/flow/models/index.cjs.js.map +1 -1
  53. package/flow/models/index.esm.js +125 -0
  54. package/flow/models/index.esm.js.map +1 -1
  55. package/flow/models/types.d.ts +148 -3
  56. package/flow/models/types.d.ts.map +1 -1
  57. package/flow/presets/conventional.d.ts +9 -8
  58. package/flow/presets/conventional.d.ts.map +1 -1
  59. package/flow/presets/independent.d.ts.map +1 -1
  60. package/flow/presets/index.cjs.js +3588 -298
  61. package/flow/presets/index.cjs.js.map +1 -1
  62. package/flow/presets/index.esm.js +3588 -298
  63. package/flow/presets/index.esm.js.map +1 -1
  64. package/flow/presets/synced.d.ts.map +1 -1
  65. package/flow/steps/analyze-commits.d.ts +9 -6
  66. package/flow/steps/analyze-commits.d.ts.map +1 -1
  67. package/flow/steps/calculate-bump.d.ts.map +1 -1
  68. package/flow/steps/fetch-registry.d.ts.map +1 -1
  69. package/flow/steps/generate-changelog.d.ts.map +1 -1
  70. package/flow/steps/index.cjs.js +3604 -318
  71. package/flow/steps/index.cjs.js.map +1 -1
  72. package/flow/steps/index.d.ts +1 -0
  73. package/flow/steps/index.d.ts.map +1 -1
  74. package/flow/steps/index.esm.js +3603 -319
  75. package/flow/steps/index.esm.js.map +1 -1
  76. package/flow/steps/resolve-repository.d.ts +36 -0
  77. package/flow/steps/resolve-repository.d.ts.map +1 -0
  78. package/flow/steps/update-packages.d.ts.map +1 -1
  79. package/git/factory.d.ts +14 -0
  80. package/git/factory.d.ts.map +1 -1
  81. package/git/index.cjs.js +65 -0
  82. package/git/index.cjs.js.map +1 -1
  83. package/git/index.esm.js +66 -2
  84. package/git/index.esm.js.map +1 -1
  85. package/git/operations/index.cjs.js +40 -0
  86. package/git/operations/index.cjs.js.map +1 -1
  87. package/git/operations/index.d.ts +1 -1
  88. package/git/operations/index.d.ts.map +1 -1
  89. package/git/operations/index.esm.js +41 -2
  90. package/git/operations/index.esm.js.map +1 -1
  91. package/git/operations/log.d.ts +23 -0
  92. package/git/operations/log.d.ts.map +1 -1
  93. package/index.cjs.js +6962 -4413
  94. package/index.cjs.js.map +1 -1
  95. package/index.esm.js +6964 -4415
  96. package/index.esm.js.map +1 -1
  97. package/package.json +26 -1
  98. package/registry/index.cjs.js +3 -3
  99. package/registry/index.cjs.js.map +1 -1
  100. package/registry/index.esm.js +3 -3
  101. package/registry/index.esm.js.map +1 -1
  102. package/registry/models/index.cjs.js +2 -0
  103. package/registry/models/index.cjs.js.map +1 -1
  104. package/registry/models/index.esm.js +2 -0
  105. package/registry/models/index.esm.js.map +1 -1
  106. package/registry/models/version-info.d.ts +10 -0
  107. package/registry/models/version-info.d.ts.map +1 -1
  108. package/registry/npm/client.d.ts.map +1 -1
  109. package/registry/npm/index.cjs.js +1 -3
  110. package/registry/npm/index.cjs.js.map +1 -1
  111. package/registry/npm/index.esm.js +1 -3
  112. package/registry/npm/index.esm.js.map +1 -1
  113. package/repository/index.cjs.js +998 -0
  114. package/repository/index.cjs.js.map +1 -0
  115. package/repository/index.d.ts +4 -0
  116. package/repository/index.d.ts.map +1 -0
  117. package/repository/index.esm.js +981 -0
  118. package/repository/index.esm.js.map +1 -0
  119. package/repository/models/index.cjs.js +301 -0
  120. package/repository/models/index.cjs.js.map +1 -0
  121. package/repository/models/index.d.ts +7 -0
  122. package/repository/models/index.d.ts.map +1 -0
  123. package/repository/models/index.esm.js +290 -0
  124. package/repository/models/index.esm.js.map +1 -0
  125. package/repository/models/platform.d.ts +58 -0
  126. package/repository/models/platform.d.ts.map +1 -0
  127. package/repository/models/repository-config.d.ts +132 -0
  128. package/repository/models/repository-config.d.ts.map +1 -0
  129. package/repository/models/resolution.d.ts +121 -0
  130. package/repository/models/resolution.d.ts.map +1 -0
  131. package/repository/parse/index.cjs.js +755 -0
  132. package/repository/parse/index.cjs.js.map +1 -0
  133. package/repository/parse/index.d.ts +5 -0
  134. package/repository/parse/index.d.ts.map +1 -0
  135. package/repository/parse/index.esm.js +749 -0
  136. package/repository/parse/index.esm.js.map +1 -0
  137. package/repository/parse/package-json.d.ts +100 -0
  138. package/repository/parse/package-json.d.ts.map +1 -0
  139. package/repository/parse/url.d.ts +81 -0
  140. package/repository/parse/url.d.ts.map +1 -0
  141. package/repository/url/compare.d.ts +84 -0
  142. package/repository/url/compare.d.ts.map +1 -0
  143. package/repository/url/index.cjs.js +178 -0
  144. package/repository/url/index.cjs.js.map +1 -0
  145. package/repository/url/index.d.ts +3 -0
  146. package/repository/url/index.d.ts.map +1 -0
  147. package/repository/url/index.esm.js +176 -0
  148. package/repository/url/index.esm.js.map +1 -0
  149. package/workspace/discovery/index.cjs.js +324 -330
  150. package/workspace/discovery/index.cjs.js.map +1 -1
  151. package/workspace/discovery/index.esm.js +324 -330
  152. package/workspace/discovery/index.esm.js.map +1 -1
  153. package/workspace/discovery/packages.d.ts +0 -6
  154. package/workspace/discovery/packages.d.ts.map +1 -1
  155. package/workspace/index.cjs.js +0 -6
  156. package/workspace/index.cjs.js.map +1 -1
  157. package/workspace/index.esm.js +0 -6
  158. package/workspace/index.esm.js.map +1 -1
@@ -1,5 +1,8 @@
1
1
  'use strict';
2
2
 
3
+ var node_path = require('node:path');
4
+ var node_fs = require('node:fs');
5
+
3
6
  /**
4
7
  * Creates a version flow.
5
8
  *
@@ -85,98 +88,2714 @@ function createStep(id, name, execute, options = {}) {
85
88
  };
86
89
  }
87
90
  /**
88
- * Creates a skipped step result.
91
+ * Creates a skipped step result.
92
+ *
93
+ * @param message - Explanation for why the step was skipped
94
+ * @returns A FlowStepResult with 'skipped' status
95
+ */
96
+ function createSkippedResult(message) {
97
+ return {
98
+ status: 'skipped',
99
+ message,
100
+ };
101
+ }
102
+
103
+ const FETCH_REGISTRY_STEP_ID = 'fetch-registry';
104
+ /**
105
+ * Creates the fetch-registry step.
106
+ *
107
+ * This step:
108
+ * 1. Queries the registry for the latest published version
109
+ * 2. Reads the current version from package.json
110
+ * 3. Determines if this is a first release
111
+ *
112
+ * State updates:
113
+ * - publishedVersion: Latest version on registry (null if not published)
114
+ * - currentVersion: Version from local package.json
115
+ * - isFirstRelease: True if never published
116
+ *
117
+ * @returns A FlowStep that fetches registry information
118
+ */
119
+ function createFetchRegistryStep() {
120
+ return createStep(FETCH_REGISTRY_STEP_ID, 'Fetch Registry Version', async (ctx) => {
121
+ const { registry, tree, projectRoot, packageName, logger } = ctx;
122
+ // Read local package.json for current version
123
+ const packageJsonPath = `${projectRoot}/package.json`;
124
+ let currentVersion = '0.0.0';
125
+ try {
126
+ const content = tree.read(packageJsonPath, 'utf-8');
127
+ if (content) {
128
+ const pkg = parse(content);
129
+ currentVersion = pkg.version ?? '0.0.0';
130
+ }
131
+ }
132
+ catch (error) {
133
+ logger.warn(`Could not read package.json: ${error}`);
134
+ }
135
+ // Query registry for published version
136
+ let publishedVersion = null;
137
+ let publishedCommit = null;
138
+ let isFirstRelease = true;
139
+ try {
140
+ publishedVersion = await registry.getLatestVersion(packageName);
141
+ isFirstRelease = publishedVersion === null;
142
+ // When published version exists, get its commit hash from gitHead
143
+ if (publishedVersion) {
144
+ try {
145
+ const versionInfo = await registry.getVersionInfo(packageName, publishedVersion);
146
+ publishedCommit = versionInfo?.gitHead ?? null;
147
+ if (publishedCommit) {
148
+ logger.debug(`Published ${publishedVersion} at commit ${publishedCommit.slice(0, 7)}`);
149
+ }
150
+ else {
151
+ logger.debug(`Published ${publishedVersion} has no gitHead (older package or published without git)`);
152
+ }
153
+ }
154
+ catch (error) {
155
+ // Version info fetch failed, but we still have the version
156
+ logger.debug(`Could not fetch version info for ${publishedVersion}: ${error}`);
157
+ }
158
+ }
159
+ }
160
+ catch (error) {
161
+ // Package might not exist yet, which is fine
162
+ logger.debug(`Registry query failed (package may not exist): ${error}`);
163
+ isFirstRelease = true;
164
+ }
165
+ const message = isFirstRelease
166
+ ? `First release (local: ${currentVersion})`
167
+ : `Published: ${publishedVersion}${publishedCommit ? ` @ ${publishedCommit.slice(0, 7)}` : ''}, Local: ${currentVersion}`;
168
+ return {
169
+ status: 'success',
170
+ stateUpdates: {
171
+ publishedVersion,
172
+ publishedCommit,
173
+ currentVersion,
174
+ isFirstRelease,
175
+ },
176
+ message,
177
+ };
178
+ });
179
+ }
180
+
181
+ /**
182
+ * Safe copies of Error built-ins via factory functions.
183
+ *
184
+ * Since constructors cannot be safely captured via Object.assign, this module
185
+ * provides factory functions that use Reflect.construct internally.
186
+ *
187
+ * These references are captured at module initialization time to protect against
188
+ * prototype pollution attacks. Import only what you need for tree-shaking.
189
+ *
190
+ * @module @hyperfrontend/immutable-api-utils/built-in-copy/error
191
+ */
192
+ // Capture references at module initialization time
193
+ const _Error = globalThis.Error;
194
+ const _Reflect$4 = globalThis.Reflect;
195
+ /**
196
+ * (Safe copy) Creates a new Error using the captured Error constructor.
197
+ * Use this instead of `new Error()`.
198
+ *
199
+ * @param message - Optional error message.
200
+ * @param options - Optional error options.
201
+ * @returns A new Error instance.
202
+ */
203
+ const createError = (message, options) => _Reflect$4.construct(_Error, [message, options]);
204
+
205
+ /**
206
+ * Creates a new RepositoryConfig.
207
+ *
208
+ * Normalizes the base URL by stripping trailing slashes and validating
209
+ * that custom platforms have a formatter function.
210
+ *
211
+ * @param options - Repository configuration options
212
+ * @returns A new RepositoryConfig object
213
+ * @throws {Error} if platform is 'custom' but no formatCompareUrl is provided
214
+ *
215
+ * @example
216
+ * ```typescript
217
+ * // GitHub repository
218
+ * const config = createRepositoryConfig({
219
+ * platform: 'github',
220
+ * baseUrl: 'https://github.com/owner/repo'
221
+ * })
222
+ *
223
+ * // Custom platform
224
+ * const customConfig = createRepositoryConfig({
225
+ * platform: 'custom',
226
+ * baseUrl: 'https://my-git.internal/repo',
227
+ * formatCompareUrl: (from, to) => `https://my-git.internal/diff/${from}/${to}`
228
+ * })
229
+ * ```
230
+ */
231
+ function createRepositoryConfig(options) {
232
+ const { platform, formatCompareUrl } = options;
233
+ // Validate custom platform has formatter
234
+ if (platform === 'custom' && !formatCompareUrl) {
235
+ throw createError("Repository config with platform 'custom' requires a formatCompareUrl function");
236
+ }
237
+ // Normalize base URL - strip trailing slashes
238
+ const baseUrl = normalizeBaseUrl(options.baseUrl);
239
+ return {
240
+ platform,
241
+ baseUrl,
242
+ formatCompareUrl,
243
+ };
244
+ }
245
+ /**
246
+ * Checks if a value is a RepositoryConfig object.
247
+ *
248
+ * @param value - Value to check
249
+ * @returns True if the value is a RepositoryConfig
250
+ *
251
+ * @example
252
+ * ```typescript
253
+ * const config = { platform: 'github', baseUrl: 'https://...' }
254
+ * if (isRepositoryConfig(config)) {
255
+ * // config is typed as RepositoryConfig
256
+ * }
257
+ * ```
258
+ */
259
+ function isRepositoryConfig(value) {
260
+ if (typeof value !== 'object' || value === null) {
261
+ return false;
262
+ }
263
+ const obj = value;
264
+ return (typeof obj['platform'] === 'string' &&
265
+ typeof obj['baseUrl'] === 'string' &&
266
+ (obj['formatCompareUrl'] === undefined || typeof obj['formatCompareUrl'] === 'function'));
267
+ }
268
+ /**
269
+ * Normalizes a base URL by stripping trailing slashes and .git suffix.
270
+ *
271
+ * @param url - URL to normalize
272
+ * @returns Normalized URL
273
+ *
274
+ * @internal
275
+ */
276
+ function normalizeBaseUrl(url) {
277
+ let normalized = url.trim();
278
+ // Remove trailing slashes
279
+ while (normalized.endsWith('/')) {
280
+ normalized = normalized.slice(0, -1);
281
+ }
282
+ // Remove .git suffix if present
283
+ if (normalized.endsWith('.git')) {
284
+ normalized = normalized.slice(0, -4);
285
+ }
286
+ return normalized;
287
+ }
288
+
289
+ /**
290
+ * Creates a disabled repository resolution configuration.
291
+ *
292
+ * No compare URLs will be generated.
293
+ *
294
+ * @returns A RepositoryResolution with mode 'disabled'
295
+ *
296
+ * @example
297
+ * ```typescript
298
+ * const config = createDisabledResolution()
299
+ * // { mode: 'disabled' }
300
+ * ```
301
+ */
302
+ /**
303
+ * Checks if a value is a RepositoryResolution object.
304
+ *
305
+ * @param value - Value to check
306
+ * @returns True if the value is a RepositoryResolution
307
+ */
308
+ function isRepositoryResolution(value) {
309
+ if (typeof value !== 'object' || value === null) {
310
+ return false;
311
+ }
312
+ const obj = value;
313
+ const mode = obj['mode'];
314
+ return mode === 'explicit' || mode === 'inferred' || mode === 'disabled';
315
+ }
316
+ /**
317
+ * Default inference order when mode is 'inferred'.
318
+ */
319
+ const DEFAULT_INFERENCE_ORDER = ['package-json', 'git-remote'];
320
+
321
+ /**
322
+ * Safe copies of Map built-in via factory function.
323
+ *
324
+ * Since constructors cannot be safely captured via Object.assign, this module
325
+ * provides a factory function that uses Reflect.construct internally.
326
+ *
327
+ * These references are captured at module initialization time to protect against
328
+ * prototype pollution attacks. Import only what you need for tree-shaking.
329
+ *
330
+ * @module @hyperfrontend/immutable-api-utils/built-in-copy/map
331
+ */
332
+ // Capture references at module initialization time
333
+ const _Map = globalThis.Map;
334
+ const _Reflect$3 = globalThis.Reflect;
335
+ /**
336
+ * (Safe copy) Creates a new Map using the captured Map constructor.
337
+ * Use this instead of `new Map()`.
338
+ *
339
+ * @param iterable - Optional iterable of key-value pairs.
340
+ * @returns A new Map instance.
341
+ */
342
+ const createMap = (iterable) => _Reflect$3.construct(_Map, iterable ? [iterable] : []);
343
+
344
+ /**
345
+ * Safe copies of Math built-in methods.
346
+ *
347
+ * These references are captured at module initialization time to protect against
348
+ * prototype pollution attacks. Import only what you need for tree-shaking.
349
+ *
350
+ * @module @hyperfrontend/immutable-api-utils/built-in-copy/math
351
+ */
352
+ // Capture references at module initialization time
353
+ const _Math = globalThis.Math;
354
+ // ============================================================================
355
+ // Min/Max
356
+ // ============================================================================
357
+ /**
358
+ * (Safe copy) Returns the larger of zero or more numbers.
359
+ */
360
+ const max = _Math.max;
361
+ /**
362
+ * (Safe copy) Returns the smaller of zero or more numbers.
363
+ */
364
+ const min = _Math.min;
365
+
366
+ /**
367
+ * Safe copies of URL built-ins via factory functions.
368
+ *
369
+ * Provides safe references to URL and URLSearchParams.
370
+ * These references are captured at module initialization time to protect against
371
+ * prototype pollution attacks. Import only what you need for tree-shaking.
372
+ *
373
+ * @module @hyperfrontend/immutable-api-utils/built-in-copy/url
374
+ */
375
+ // Capture references at module initialization time
376
+ const _URL = globalThis.URL;
377
+ const _Reflect$2 = globalThis.Reflect;
378
+ // ============================================================================
379
+ // URL
380
+ // ============================================================================
381
+ /**
382
+ * (Safe copy) Creates a new URL using the captured URL constructor.
383
+ * Use this instead of `new URL()`.
384
+ *
385
+ * @param url - The URL string to parse.
386
+ * @param base - Optional base URL for relative URLs.
387
+ * @returns A new URL instance.
388
+ */
389
+ const createURL = (url, base) => _Reflect$2.construct(_URL, [url, base]);
390
+ /**
391
+ * (Safe copy) Creates an object URL for the given object.
392
+ * Use this instead of `URL.createObjectURL()`.
393
+ *
394
+ * Note: This is a browser-only API. In Node.js environments, this will throw.
395
+ */
396
+ typeof _URL.createObjectURL === 'function'
397
+ ? _URL.createObjectURL.bind(_URL)
398
+ : () => {
399
+ throw new Error('URL.createObjectURL is not available in this environment');
400
+ };
401
+ /**
402
+ * (Safe copy) Revokes an object URL previously created with createObjectURL.
403
+ * Use this instead of `URL.revokeObjectURL()`.
404
+ *
405
+ * Note: This is a browser-only API. In Node.js environments, this will throw.
406
+ */
407
+ typeof _URL.revokeObjectURL === 'function'
408
+ ? _URL.revokeObjectURL.bind(_URL)
409
+ : () => {
410
+ throw new Error('URL.revokeObjectURL is not available in this environment');
411
+ };
412
+
413
+ /**
414
+ * Checks if a platform identifier is a known platform with built-in support.
415
+ *
416
+ * @param platform - Platform identifier to check
417
+ * @returns True if the platform is a known platform
418
+ *
419
+ * @example
420
+ * ```typescript
421
+ * isKnownPlatform('github') // true
422
+ * isKnownPlatform('gitlab') // true
423
+ * isKnownPlatform('custom') // false
424
+ * isKnownPlatform('unknown') // false
425
+ * ```
426
+ */
427
+ function isKnownPlatform(platform) {
428
+ return platform === 'github' || platform === 'gitlab' || platform === 'bitbucket' || platform === 'azure-devops';
429
+ }
430
+ /**
431
+ * Known platform hostnames mapped to their platform type.
432
+ * Used for automatic platform detection from repository URLs.
433
+ *
434
+ * Includes both standard SaaS domains and common patterns for self-hosted instances.
435
+ */
436
+ const PLATFORM_HOSTNAMES = createMap([
437
+ // GitHub
438
+ ['github.com', 'github'],
439
+ // GitLab
440
+ ['gitlab.com', 'gitlab'],
441
+ // Bitbucket
442
+ ['bitbucket.org', 'bitbucket'],
443
+ // Azure DevOps
444
+ ['dev.azure.com', 'azure-devops'],
445
+ ['visualstudio.com', 'azure-devops'],
446
+ ]);
447
+ /**
448
+ * Detects platform from a hostname.
449
+ *
450
+ * First checks for exact match in known platforms, then applies heuristics
451
+ * for self-hosted instances (e.g., `github.company.com` → `github`).
452
+ *
453
+ * @param hostname - Hostname to detect platform from (e.g., "github.com")
454
+ * @returns Detected platform or 'unknown' if not recognized
455
+ *
456
+ * @example
457
+ * ```typescript
458
+ * detectPlatformFromHostname('github.com') // 'github'
459
+ * detectPlatformFromHostname('gitlab.mycompany.com') // 'gitlab'
460
+ * detectPlatformFromHostname('custom-git.internal') // 'unknown'
461
+ * ```
462
+ */
463
+ function detectPlatformFromHostname(hostname) {
464
+ const normalized = hostname.toLowerCase();
465
+ // Check exact matches first
466
+ const exactMatch = PLATFORM_HOSTNAMES.get(normalized);
467
+ if (exactMatch) {
468
+ return exactMatch;
469
+ }
470
+ // Check for Azure DevOps legacy domain pattern
471
+ if (normalized.endsWith('.visualstudio.com')) {
472
+ return 'azure-devops';
473
+ }
474
+ // Check for Azure DevOps modern domain pattern (includes ssh.dev.azure.com)
475
+ if (normalized.endsWith('.azure.com')) {
476
+ return 'azure-devops';
477
+ }
478
+ // Heuristics for self-hosted instances
479
+ // GitHub Enterprise typically uses "github" in the hostname
480
+ if (normalized.includes('github')) {
481
+ return 'github';
482
+ }
483
+ // GitLab self-hosted typically uses "gitlab" in the hostname
484
+ if (normalized.includes('gitlab')) {
485
+ return 'gitlab';
486
+ }
487
+ // Bitbucket Data Center/Server might use "bitbucket" in hostname
488
+ if (normalized.includes('bitbucket')) {
489
+ return 'bitbucket';
490
+ }
491
+ return 'unknown';
492
+ }
493
+
494
+ /**
495
+ * Parses a git URL and extracts platform and base URL.
496
+ *
497
+ * Supports multiple URL formats:
498
+ * - `https://github.com/owner/repo`
499
+ * - `https://github.com/owner/repo.git`
500
+ * - `git+https://github.com/owner/repo.git`
501
+ * - `git://github.com/owner/repo.git`
502
+ * - `git@github.com:owner/repo.git` (SSH format)
503
+ *
504
+ * Handles self-hosted instances by detecting platform from hostname:
505
+ * - `github.mycompany.com` → `github`
506
+ * - `gitlab.internal.com` → `gitlab`
507
+ *
508
+ * Handles Azure DevOps URL formats:
509
+ * - `https://dev.azure.com/org/project/_git/repo`
510
+ * - `https://org.visualstudio.com/project/_git/repo`
511
+ *
512
+ * @param gitUrl - Git repository URL in any supported format
513
+ * @returns Parsed repository info with platform and base URL, or null if parsing fails
514
+ *
515
+ * @example
516
+ * ```typescript
517
+ * // GitHub HTTPS
518
+ * parseRepositoryUrl('https://github.com/owner/repo')
519
+ * // → { platform: 'github', baseUrl: 'https://github.com/owner/repo' }
520
+ *
521
+ * // SSH format
522
+ * parseRepositoryUrl('git@github.com:owner/repo.git')
523
+ * // → { platform: 'github', baseUrl: 'https://github.com/owner/repo' }
524
+ *
525
+ * // Azure DevOps
526
+ * parseRepositoryUrl('https://dev.azure.com/org/proj/_git/repo')
527
+ * // → { platform: 'azure-devops', baseUrl: 'https://dev.azure.com/org/proj/_git/repo' }
528
+ *
529
+ * // Self-hosted GitLab
530
+ * parseRepositoryUrl('https://gitlab.mycompany.com/team/project')
531
+ * // → { platform: 'gitlab', baseUrl: 'https://gitlab.mycompany.com/team/project' }
532
+ * ```
533
+ */
534
+ function parseRepositoryUrl(gitUrl) {
535
+ if (!gitUrl || typeof gitUrl !== 'string') {
536
+ return null;
537
+ }
538
+ const trimmed = gitUrl.trim();
539
+ if (!trimmed) {
540
+ return null;
541
+ }
542
+ // Try SSH format first: git@hostname:path
543
+ const sshParsed = parseSshUrl(trimmed);
544
+ if (sshParsed) {
545
+ return sshParsed;
546
+ }
547
+ // Try HTTP(S) formats
548
+ const httpParsed = parseHttpUrl(trimmed);
549
+ if (httpParsed) {
550
+ return httpParsed;
551
+ }
552
+ return null;
553
+ }
554
+ /**
555
+ * Parses an SSH-style git URL.
556
+ *
557
+ * @param url - URL to parse (e.g., "git@github.com:owner/repo.git")
558
+ * @returns Parsed repository or null
559
+ *
560
+ * @internal
561
+ */
562
+ function parseSshUrl(url) {
563
+ // Handle optional ssh:// prefix
564
+ let remaining = url;
565
+ if (remaining.startsWith('ssh://')) {
566
+ remaining = remaining.slice(6);
567
+ }
568
+ // Must start with git@
569
+ if (!remaining.startsWith('git@')) {
570
+ return null;
571
+ }
572
+ // Remove git@ prefix
573
+ remaining = remaining.slice(4);
574
+ // Find the separator (: or /)
575
+ const colonIndex = remaining.indexOf(':');
576
+ const slashIndex = remaining.indexOf('/');
577
+ let separatorIndex;
578
+ if (colonIndex === -1 && slashIndex === -1) {
579
+ return null;
580
+ }
581
+ else if (colonIndex === -1) {
582
+ separatorIndex = slashIndex;
583
+ }
584
+ else if (slashIndex === -1) {
585
+ separatorIndex = colonIndex;
586
+ }
587
+ else {
588
+ separatorIndex = min(colonIndex, slashIndex);
589
+ }
590
+ const hostname = remaining.slice(0, separatorIndex);
591
+ const pathPart = normalizePathPart(remaining.slice(separatorIndex + 1));
592
+ if (!hostname || !pathPart) {
593
+ return null;
594
+ }
595
+ const platform = detectPlatformFromHostname(hostname);
596
+ // For Azure DevOps, construct proper base URL
597
+ if (platform === 'azure-devops') {
598
+ const baseUrl = constructAzureDevOpsBaseUrl(hostname, pathPart);
599
+ if (baseUrl) {
600
+ return { platform, baseUrl };
601
+ }
602
+ return null;
603
+ }
604
+ // Standard platforms: https://hostname/path
605
+ const baseUrl = `https://${hostname}/${pathPart}`;
606
+ return { platform, baseUrl };
607
+ }
608
+ /**
609
+ * Parses an HTTP(S)-style git URL.
610
+ *
611
+ * @param url - URL to parse
612
+ * @returns Parsed repository or null
613
+ *
614
+ * @internal
615
+ */
616
+ function parseHttpUrl(url) {
617
+ // Normalize various git URL prefixes to https://
618
+ const normalized = url
619
+ .replace(/^git\+/, '') // git+https:// → https://
620
+ .replace(/^git:\/\//, 'https://'); // git:// → https://
621
+ let parsed;
622
+ try {
623
+ parsed = createURL(normalized);
624
+ }
625
+ catch {
626
+ return null;
627
+ }
628
+ // Only support http and https protocols
629
+ if (parsed.protocol !== 'http:' && parsed.protocol !== 'https:') {
630
+ return null;
631
+ }
632
+ const hostname = parsed.hostname.toLowerCase();
633
+ const platform = detectPlatformFromHostname(hostname);
634
+ const pathPart = normalizePathPart(parsed.pathname);
635
+ if (!pathPart) {
636
+ return null;
637
+ }
638
+ // Handle Azure DevOps special URL structure
639
+ if (platform === 'azure-devops') {
640
+ const baseUrl = constructAzureDevOpsBaseUrl(hostname, pathPart);
641
+ if (baseUrl) {
642
+ return { platform, baseUrl };
643
+ }
644
+ // If Azure DevOps URL cannot be parsed properly, return null
645
+ return null;
646
+ }
647
+ // Standard platforms
648
+ const baseUrl = `${parsed.protocol}//${hostname}/${pathPart}`;
649
+ return { platform, baseUrl };
650
+ }
651
+ /**
652
+ * Normalizes a path part by removing leading slashes and .git suffix.
653
+ *
654
+ * @param path - Path to normalize
655
+ * @returns Normalized path or null if empty
656
+ *
657
+ * @internal
658
+ */
659
+ function normalizePathPart(path) {
660
+ let normalized = path.trim();
661
+ // Remove leading slashes
662
+ while (normalized.startsWith('/')) {
663
+ normalized = normalized.slice(1);
664
+ }
665
+ // Remove trailing slashes
666
+ while (normalized.endsWith('/')) {
667
+ normalized = normalized.slice(0, -1);
668
+ }
669
+ // Remove .git suffix
670
+ if (normalized.endsWith('.git')) {
671
+ normalized = normalized.slice(0, -4);
672
+ }
673
+ // Validate we have something
674
+ if (!normalized) {
675
+ return null;
676
+ }
677
+ return normalized;
678
+ }
679
+ /**
680
+ * Constructs the base URL for Azure DevOps repositories.
681
+ *
682
+ * Azure DevOps has special URL structures:
683
+ * - Modern: `https://dev.azure.com/{org}/{project}/_git/{repo}`
684
+ * - Legacy: `https://{org}.visualstudio.com/{project}/_git/{repo}`
685
+ * - SSH: `git@ssh.dev.azure.com:v3/{org}/{project}/{repo}`
686
+ *
687
+ * @param hostname - Hostname from the URL
688
+ * @param pathPart - Path portion after hostname
689
+ * @returns Constructed base URL or null if invalid
690
+ *
691
+ * @internal
692
+ */
693
+ function constructAzureDevOpsBaseUrl(hostname, pathPart) {
694
+ const pathParts = pathPart.split('/');
695
+ // dev.azure.com format: org/project/_git/repo
696
+ if (hostname === 'dev.azure.com' || hostname.endsWith('.azure.com')) {
697
+ // Need at least: org/project/_git/repo (4 parts)
698
+ // Or for SSH v3: v3/org/project/repo (4 parts)
699
+ if (pathParts.length >= 4) {
700
+ // Check for v3 SSH format
701
+ if (pathParts[0] === 'v3') {
702
+ // v3/org/project/repo → https://dev.azure.com/org/project/_git/repo
703
+ const org = pathParts[1];
704
+ const project = pathParts[2];
705
+ const repo = pathParts[3];
706
+ if (org && project && repo) {
707
+ return `https://dev.azure.com/${org}/${project}/_git/${repo}`;
708
+ }
709
+ }
710
+ // Standard format: org/project/_git/repo
711
+ const gitIndex = pathParts.indexOf('_git');
712
+ if (gitIndex >= 2 && pathParts[gitIndex + 1]) {
713
+ const org = pathParts.slice(0, gitIndex - 1).join('/');
714
+ const project = pathParts[gitIndex - 1];
715
+ const repo = pathParts[gitIndex + 1];
716
+ if (org && project && repo) {
717
+ return `https://dev.azure.com/${org}/${project}/_git/${repo}`;
718
+ }
719
+ }
720
+ }
721
+ return null;
722
+ }
723
+ // visualstudio.com format: {org}.visualstudio.com/project/_git/repo
724
+ if (hostname.endsWith('.visualstudio.com')) {
725
+ const org = hostname.replace('.visualstudio.com', '');
726
+ const gitIndex = pathParts.indexOf('_git');
727
+ if (gitIndex >= 1 && pathParts[gitIndex + 1]) {
728
+ const project = pathParts.slice(0, gitIndex).join('/');
729
+ const repo = pathParts[gitIndex + 1];
730
+ if (project && repo) {
731
+ // Normalize to dev.azure.com format
732
+ return `https://dev.azure.com/${org}/${project}/_git/${repo}`;
733
+ }
734
+ }
735
+ return null;
736
+ }
737
+ return null;
738
+ }
739
+ /**
740
+ * Creates a RepositoryConfig from a git URL.
741
+ *
742
+ * This is a convenience function that combines `parseRepositoryUrl` with
743
+ * `createRepositoryConfig` to produce a ready-to-use configuration.
744
+ *
745
+ * @param gitUrl - Git repository URL in any supported format
746
+ * @returns RepositoryConfig or null if URL cannot be parsed
747
+ *
748
+ * @example
749
+ * ```typescript
750
+ * const config = createRepositoryConfigFromUrl('https://github.com/owner/repo')
751
+ * // → { platform: 'github', baseUrl: 'https://github.com/owner/repo' }
752
+ *
753
+ * const config = createRepositoryConfigFromUrl('git@gitlab.com:group/project.git')
754
+ * // → { platform: 'gitlab', baseUrl: 'https://gitlab.com/group/project' }
755
+ * ```
756
+ */
757
+ function createRepositoryConfigFromUrl(gitUrl) {
758
+ const parsed = parseRepositoryUrl(gitUrl);
759
+ if (!parsed) {
760
+ return null;
761
+ }
762
+ // Don't create configs for unknown platforms as they can't generate URLs
763
+ if (parsed.platform === 'unknown') {
764
+ return null;
765
+ }
766
+ return createRepositoryConfig({
767
+ platform: parsed.platform,
768
+ baseUrl: parsed.baseUrl,
769
+ });
770
+ }
771
+
772
+ /**
773
+ * Shorthand platform prefixes supported in package.json repository field.
774
+ *
775
+ * Format: `"platform:owner/repo"` or `"owner/repo"` (defaults to GitHub)
776
+ *
777
+ * @see https://docs.npmjs.com/cli/v9/configuring-npm/package-json#repository
778
+ */
779
+ const SHORTHAND_PLATFORMS = createMap([
780
+ ['github', 'https://github.com'],
781
+ ['gitlab', 'https://gitlab.com'],
782
+ ['bitbucket', 'https://bitbucket.org'],
783
+ ['gist', 'https://gist.github.com'],
784
+ ]);
785
+ /**
786
+ * Infers repository configuration from package.json content.
787
+ *
788
+ * Handles multiple formats:
789
+ * - Shorthand: `"github:owner/repo"`, `"gitlab:group/project"`, `"bitbucket:team/repo"`
790
+ * - Bare shorthand: `"owner/repo"` (defaults to GitHub)
791
+ * - URL string: `"https://github.com/owner/repo"`
792
+ * - Object with URL: `{ "type": "git", "url": "https://..." }`
793
+ *
794
+ * @param packageJsonContent - Raw JSON string content of package.json
795
+ * @returns RepositoryConfig or null if repository cannot be inferred
796
+ *
797
+ * @example
798
+ * ```typescript
799
+ * // Shorthand format
800
+ * inferRepositoryFromPackageJson('{"repository": "github:owner/repo"}')
801
+ * // → { platform: 'github', baseUrl: 'https://github.com/owner/repo' }
802
+ *
803
+ * // URL string
804
+ * inferRepositoryFromPackageJson('{"repository": "https://github.com/owner/repo"}')
805
+ * // → { platform: 'github', baseUrl: 'https://github.com/owner/repo' }
806
+ *
807
+ * // Object format
808
+ * inferRepositoryFromPackageJson('{"repository": {"type": "git", "url": "https://github.com/owner/repo"}}')
809
+ * // → { platform: 'github', baseUrl: 'https://github.com/owner/repo' }
810
+ *
811
+ * // Bare shorthand (defaults to GitHub)
812
+ * inferRepositoryFromPackageJson('{"repository": "owner/repo"}')
813
+ * // → { platform: 'github', baseUrl: 'https://github.com/owner/repo' }
814
+ * ```
815
+ */
816
+ function inferRepositoryFromPackageJson(packageJsonContent) {
817
+ if (!packageJsonContent || typeof packageJsonContent !== 'string') {
818
+ return null;
819
+ }
820
+ let packageJson;
821
+ try {
822
+ packageJson = parse(packageJsonContent);
823
+ }
824
+ catch {
825
+ return null;
826
+ }
827
+ return inferRepositoryFromPackageJsonObject(packageJson);
828
+ }
829
+ /**
830
+ * Infers repository configuration from a parsed package.json object.
831
+ *
832
+ * This is useful when you already have the parsed object.
833
+ *
834
+ * @param packageJson - Parsed package.json object
835
+ * @returns RepositoryConfig or null if repository cannot be inferred
836
+ *
837
+ * @example
838
+ * ```typescript
839
+ * const pkg = { repository: 'github:owner/repo' }
840
+ * inferRepositoryFromPackageJsonObject(pkg)
841
+ * // → { platform: 'github', baseUrl: 'https://github.com/owner/repo' }
842
+ * ```
843
+ */
844
+ function inferRepositoryFromPackageJsonObject(packageJson) {
845
+ const { repository } = packageJson;
846
+ if (!repository) {
847
+ return null;
848
+ }
849
+ // Handle string format
850
+ if (typeof repository === 'string') {
851
+ return parseRepositoryString(repository);
852
+ }
853
+ // Handle object format
854
+ if (typeof repository === 'object' && repository.url) {
855
+ return createRepositoryConfigFromUrl(repository.url);
856
+ }
857
+ return null;
858
+ }
859
+ /**
860
+ * Parses a repository string (shorthand or URL).
861
+ *
862
+ * @param repoString - Repository string from package.json
863
+ * @returns RepositoryConfig or null
864
+ *
865
+ * @internal
866
+ */
867
+ function parseRepositoryString(repoString) {
868
+ const trimmed = repoString.trim();
869
+ if (!trimmed) {
870
+ return null;
871
+ }
872
+ // Check for shorthand format: platform:owner/repo
873
+ const colonIndex = trimmed.indexOf(':');
874
+ if (colonIndex > 0) {
875
+ const potentialPlatform = trimmed.slice(0, colonIndex);
876
+ // Platform must be only letters (a-z, case insensitive)
877
+ if (isOnlyLetters(potentialPlatform)) {
878
+ const platform = potentialPlatform.toLowerCase();
879
+ const path = trimmed.slice(colonIndex + 1);
880
+ if (path) {
881
+ const baseUrl = SHORTHAND_PLATFORMS.get(platform);
882
+ if (baseUrl) {
883
+ // Construct full URL and parse it
884
+ const fullUrl = `${baseUrl}/${path}`;
885
+ return createRepositoryConfigFromUrl(fullUrl);
886
+ }
887
+ // Unknown shorthand platform - try as URL
888
+ return createRepositoryConfigFromUrl(trimmed);
889
+ }
890
+ }
891
+ }
892
+ // Check for bare shorthand: owner/repo (no protocol, no platform prefix)
893
+ // Must match pattern like "owner/repo" but not "https://..." or "git@..."
894
+ if (!trimmed.includes('://') && !trimmed.startsWith('git@')) {
895
+ if (isBareShorthand(trimmed)) {
896
+ // Bare shorthand defaults to GitHub
897
+ const fullUrl = `https://github.com/${trimmed}`;
898
+ return createRepositoryConfigFromUrl(fullUrl);
899
+ }
900
+ }
901
+ // Try as a full URL
902
+ return createRepositoryConfigFromUrl(trimmed);
903
+ }
904
+ /**
905
+ * Checks if a string contains only ASCII letters (a-z, A-Z).
906
+ *
907
+ * @param str - String to check
908
+ * @returns True if string contains only letters
909
+ *
910
+ * @internal
911
+ */
912
+ function isOnlyLetters(str) {
913
+ for (let i = 0; i < str.length; i++) {
914
+ const char = str.charCodeAt(i);
915
+ const isLowercase = char >= 97 && char <= 122; // a-z
916
+ const isUppercase = char >= 65 && char <= 90; // A-Z
917
+ if (!isLowercase && !isUppercase) {
918
+ return false;
919
+ }
920
+ }
921
+ return str.length > 0;
922
+ }
923
+ /**
924
+ * Checks if a string is a bare shorthand format (owner/repo).
925
+ * Must have exactly one forward slash with content on both sides.
926
+ *
927
+ * @param str - String to check
928
+ * @returns True if string matches owner/repo format
929
+ *
930
+ * @internal
931
+ */
932
+ function isBareShorthand(str) {
933
+ const slashIndex = str.indexOf('/');
934
+ if (slashIndex <= 0 || slashIndex === str.length - 1) {
935
+ return false;
936
+ }
937
+ // Must not have another slash
938
+ return str.indexOf('/', slashIndex + 1) === -1;
939
+ }
940
+
941
+ const RESOLVE_REPOSITORY_STEP_ID = 'resolve-repository';
942
+ /**
943
+ * Creates the resolve-repository step.
944
+ *
945
+ * This step resolves repository configuration for compare URL generation.
946
+ * It supports multiple resolution modes:
947
+ *
948
+ * - `undefined` or `'disabled'`: No-op, backward compatible default
949
+ * - `'inferred'`: Auto-detect from package.json or git remote
950
+ * - `RepositoryConfig`: Direct repository configuration provided
951
+ * - `RepositoryResolution`: Fine-grained control with mode and options
952
+ *
953
+ * State updates:
954
+ * - repositoryConfig: Resolved repository configuration (if successful)
955
+ *
956
+ * @returns A FlowStep that resolves repository configuration
957
+ *
958
+ * @example
959
+ * ```typescript
960
+ * // Auto-detect repository
961
+ * const flow = createFlow({
962
+ * repository: 'inferred'
963
+ * })
964
+ *
965
+ * // Explicit repository
966
+ * const flow = createFlow({
967
+ * repository: {
968
+ * platform: 'github',
969
+ * baseUrl: 'https://github.com/owner/repo'
970
+ * }
971
+ * })
972
+ * ```
973
+ */
974
+ function createResolveRepositoryStep() {
975
+ return createStep(RESOLVE_REPOSITORY_STEP_ID, 'Resolve Repository', async (ctx) => {
976
+ const { config, logger, tree, git, projectRoot } = ctx;
977
+ const repoConfig = config.repository;
978
+ // Disabled or undefined - no-op for backward compatibility
979
+ if (repoConfig === undefined || repoConfig === 'disabled') {
980
+ logger.debug('Repository resolution disabled');
981
+ return {
982
+ status: 'skipped',
983
+ message: 'Repository resolution disabled',
984
+ };
985
+ }
986
+ // Direct RepositoryConfig provided
987
+ if (isRepositoryConfig(repoConfig)) {
988
+ logger.debug(`Using explicit repository config: ${repoConfig.platform}`);
989
+ return {
990
+ status: 'success',
991
+ stateUpdates: {
992
+ repositoryConfig: repoConfig,
993
+ },
994
+ message: `Using explicit ${repoConfig.platform} repository`,
995
+ };
996
+ }
997
+ // Shorthand 'inferred' mode
998
+ if (repoConfig === 'inferred') {
999
+ const resolved = await inferRepository(tree, git, projectRoot, DEFAULT_INFERENCE_ORDER, logger);
1000
+ if (resolved) {
1001
+ return {
1002
+ status: 'success',
1003
+ stateUpdates: {
1004
+ repositoryConfig: resolved,
1005
+ },
1006
+ message: `Inferred ${resolved.platform} repository from ${resolved.baseUrl}`,
1007
+ };
1008
+ }
1009
+ // Graceful degradation - no error, just no URLs
1010
+ logger.debug('Could not infer repository from package.json or git remote');
1011
+ return {
1012
+ status: 'skipped',
1013
+ message: 'Could not infer repository configuration',
1014
+ };
1015
+ }
1016
+ // Full RepositoryResolution object
1017
+ if (isRepositoryResolution(repoConfig)) {
1018
+ return handleRepositoryResolution(repoConfig, tree, git, projectRoot, logger);
1019
+ }
1020
+ // Unknown configuration - should not happen with TypeScript
1021
+ logger.warn('Unknown repository configuration format');
1022
+ return {
1023
+ status: 'skipped',
1024
+ message: 'Unknown repository configuration format',
1025
+ };
1026
+ }, {
1027
+ description: 'Resolves repository configuration for compare URL generation',
1028
+ });
1029
+ }
1030
+ /**
1031
+ * Handles a full RepositoryResolution configuration.
1032
+ *
1033
+ * @param resolution - Repository resolution configuration
1034
+ * @param tree - Virtual file system tree
1035
+ * @param git - Git client instance
1036
+ * @param projectRoot - Path to the project root
1037
+ * @param logger - Logger instance
1038
+ * @returns Flow step result with repository config or skip/error status
1039
+ * @internal
1040
+ */
1041
+ async function handleRepositoryResolution(resolution, tree, git, projectRoot, logger) {
1042
+ const { mode, repository, inferenceOrder } = resolution;
1043
+ // Disabled mode
1044
+ if (mode === 'disabled') {
1045
+ logger.debug('Repository resolution explicitly disabled');
1046
+ return {
1047
+ status: 'skipped',
1048
+ message: 'Repository resolution disabled',
1049
+ };
1050
+ }
1051
+ // Explicit mode - must have repository
1052
+ if (mode === 'explicit') {
1053
+ if (!repository) {
1054
+ return {
1055
+ status: 'failed',
1056
+ message: 'Repository config required when mode is "explicit"',
1057
+ error: createError('Repository config required when mode is "explicit"'),
1058
+ };
1059
+ }
1060
+ logger.debug(`Using explicit repository config: ${repository.platform}`);
1061
+ return {
1062
+ status: 'success',
1063
+ stateUpdates: {
1064
+ repositoryConfig: repository,
1065
+ },
1066
+ message: `Using explicit ${repository.platform} repository`,
1067
+ };
1068
+ }
1069
+ // Inferred mode
1070
+ const order = inferenceOrder ?? DEFAULT_INFERENCE_ORDER;
1071
+ const resolved = await inferRepository(tree, git, projectRoot, order, logger);
1072
+ if (resolved) {
1073
+ return {
1074
+ status: 'success',
1075
+ stateUpdates: {
1076
+ repositoryConfig: resolved,
1077
+ },
1078
+ message: `Inferred ${resolved.platform} repository`,
1079
+ };
1080
+ }
1081
+ // Graceful degradation
1082
+ logger.debug('Could not infer repository configuration');
1083
+ return {
1084
+ status: 'skipped',
1085
+ message: 'Could not infer repository configuration',
1086
+ };
1087
+ }
1088
+ /**
1089
+ * Infers repository configuration from available sources.
1090
+ *
1091
+ * @param tree - Virtual file system tree
1092
+ * @param git - Git client instance
1093
+ * @param projectRoot - Path to the project root
1094
+ * @param order - Inference source order
1095
+ * @param logger - Logger instance
1096
+ * @returns Repository config or null if none found
1097
+ * @internal
1098
+ */
1099
+ async function inferRepository(tree, git, projectRoot, order, logger) {
1100
+ for (const source of order) {
1101
+ const config = await inferFromSource(tree, git, projectRoot, source, logger);
1102
+ if (config) {
1103
+ logger.debug(`Inferred repository from ${source}: ${config.platform}`);
1104
+ return config;
1105
+ }
1106
+ }
1107
+ return null;
1108
+ }
1109
+ /**
1110
+ * Infers repository from a single source.
1111
+ *
1112
+ * @param tree - Virtual file system tree
1113
+ * @param git - Git client instance
1114
+ * @param projectRoot - Path to the project root
1115
+ * @param source - Inference source type
1116
+ * @param logger - Logger instance
1117
+ * @returns Repository config or null if not found
1118
+ * @internal
1119
+ */
1120
+ async function inferFromSource(tree, git, projectRoot, source, logger) {
1121
+ if (source === 'package-json') {
1122
+ return inferFromPackageJson(tree, projectRoot, logger);
1123
+ }
1124
+ if (source === 'git-remote') {
1125
+ return inferFromGitRemote(git, logger);
1126
+ }
1127
+ logger.warn(`Unknown inference source: ${source}`);
1128
+ return null;
1129
+ }
1130
+ /**
1131
+ * Infers repository from package.json repository field.
1132
+ *
1133
+ * @param tree - Virtual file system tree
1134
+ * @param projectRoot - Path to the project root
1135
+ * @param logger - Logger instance
1136
+ * @returns Repository config or null if not found
1137
+ * @internal
1138
+ */
1139
+ function inferFromPackageJson(tree, projectRoot, logger) {
1140
+ const packageJsonPath = `${projectRoot}/package.json`;
1141
+ if (!tree.exists(packageJsonPath)) {
1142
+ logger.debug(`package.json not found at ${packageJsonPath}`);
1143
+ return null;
1144
+ }
1145
+ const content = tree.read(packageJsonPath, 'utf-8');
1146
+ if (!content) {
1147
+ logger.debug('Could not read package.json');
1148
+ return null;
1149
+ }
1150
+ const config = inferRepositoryFromPackageJson(content);
1151
+ if (config) {
1152
+ logger.debug(`Found repository in package.json: ${config.baseUrl}`);
1153
+ }
1154
+ return config;
1155
+ }
1156
+ /**
1157
+ * Infers repository from git remote URL.
1158
+ *
1159
+ * @param git - Git client instance
1160
+ * @param logger - Logger instance
1161
+ * @returns Repository config or null if not found
1162
+ * @internal
1163
+ */
1164
+ async function inferFromGitRemote(git, logger) {
1165
+ const remoteUrl = await git.getRemoteUrl('origin');
1166
+ if (!remoteUrl) {
1167
+ logger.debug('Could not get git remote URL');
1168
+ return null;
1169
+ }
1170
+ const config = createRepositoryConfigFromUrl(remoteUrl);
1171
+ if (config) {
1172
+ logger.debug(`Inferred repository from git remote: ${config.baseUrl}`);
1173
+ }
1174
+ return config;
1175
+ }
1176
+
1177
+ /**
1178
+ * Safe copies of Set built-in via factory function.
1179
+ *
1180
+ * Since constructors cannot be safely captured via Object.assign, this module
1181
+ * provides a factory function that uses Reflect.construct internally.
1182
+ *
1183
+ * These references are captured at module initialization time to protect against
1184
+ * prototype pollution attacks. Import only what you need for tree-shaking.
1185
+ *
1186
+ * @module @hyperfrontend/immutable-api-utils/built-in-copy/set
1187
+ */
1188
+ // Capture references at module initialization time
1189
+ const _Set = globalThis.Set;
1190
+ const _Reflect$1 = globalThis.Reflect;
1191
+ /**
1192
+ * (Safe copy) Creates a new Set using the captured Set constructor.
1193
+ * Use this instead of `new Set()`.
1194
+ *
1195
+ * @param iterable - Optional iterable of values.
1196
+ * @returns A new Set instance.
1197
+ */
1198
+ const createSet = (iterable) => _Reflect$1.construct(_Set, iterable ? [iterable] : []);
1199
+
1200
+ /**
1201
+ * Safe copies of Object built-in methods.
1202
+ *
1203
+ * These references are captured at module initialization time to protect against
1204
+ * prototype pollution attacks. Import only what you need for tree-shaking.
1205
+ *
1206
+ * @module @hyperfrontend/immutable-api-utils/built-in-copy/object
1207
+ */
1208
+ // Capture references at module initialization time
1209
+ const _Object = globalThis.Object;
1210
+ /**
1211
+ * (Safe copy) Prevents modification of existing property attributes and values,
1212
+ * and prevents the addition of new properties.
1213
+ */
1214
+ const freeze = _Object.freeze;
1215
+ /**
1216
+ * (Safe copy) Returns the names of the enumerable string properties and methods of an object.
1217
+ */
1218
+ const keys = _Object.keys;
1219
+ /**
1220
+ * (Safe copy) Returns an array of key/values of the enumerable own properties of an object.
1221
+ */
1222
+ const entries = _Object.entries;
1223
+ /**
1224
+ * (Safe copy) Returns an array of values of the enumerable own properties of an object.
1225
+ */
1226
+ const values = _Object.values;
1227
+ /**
1228
+ * (Safe copy) Adds one or more properties to an object, and/or modifies attributes of existing properties.
1229
+ */
1230
+ const defineProperties = _Object.defineProperties;
1231
+
1232
+ /**
1233
+ * Safe copies of Array built-in static methods.
1234
+ *
1235
+ * These references are captured at module initialization time to protect against
1236
+ * prototype pollution attacks. Import only what you need for tree-shaking.
1237
+ *
1238
+ * @module @hyperfrontend/immutable-api-utils/built-in-copy/array
1239
+ */
1240
+ // Capture references at module initialization time
1241
+ const _Array = globalThis.Array;
1242
+ /**
1243
+ * (Safe copy) Determines whether the passed value is an Array.
1244
+ */
1245
+ const isArray = _Array.isArray;
1246
+
1247
+ /**
1248
+ * Safe copies of Console built-in methods.
1249
+ *
1250
+ * These references are captured at module initialization time to protect against
1251
+ * prototype pollution attacks. Import only what you need for tree-shaking.
1252
+ *
1253
+ * @module @hyperfrontend/immutable-api-utils/built-in-copy/console
1254
+ */
1255
+ // Capture references at module initialization time
1256
+ const _console = globalThis.console;
1257
+ /**
1258
+ * (Safe copy) Outputs a message to the console.
1259
+ */
1260
+ const log = _console.log.bind(_console);
1261
+ /**
1262
+ * (Safe copy) Outputs a warning message to the console.
1263
+ */
1264
+ const warn = _console.warn.bind(_console);
1265
+ /**
1266
+ * (Safe copy) Outputs an error message to the console.
1267
+ */
1268
+ const error = _console.error.bind(_console);
1269
+ /**
1270
+ * (Safe copy) Outputs an informational message to the console.
1271
+ */
1272
+ const info = _console.info.bind(_console);
1273
+ /**
1274
+ * (Safe copy) Outputs a debug message to the console.
1275
+ */
1276
+ const debug = _console.debug.bind(_console);
1277
+ /**
1278
+ * (Safe copy) Outputs a stack trace to the console.
1279
+ */
1280
+ _console.trace.bind(_console);
1281
+ /**
1282
+ * (Safe copy) Displays an interactive listing of the properties of a specified object.
1283
+ */
1284
+ _console.dir.bind(_console);
1285
+ /**
1286
+ * (Safe copy) Displays tabular data as a table.
1287
+ */
1288
+ _console.table.bind(_console);
1289
+ /**
1290
+ * (Safe copy) Writes an error message to the console if the assertion is false.
1291
+ */
1292
+ _console.assert.bind(_console);
1293
+ /**
1294
+ * (Safe copy) Clears the console.
1295
+ */
1296
+ _console.clear.bind(_console);
1297
+ /**
1298
+ * (Safe copy) Logs the number of times that this particular call to count() has been called.
1299
+ */
1300
+ _console.count.bind(_console);
1301
+ /**
1302
+ * (Safe copy) Resets the counter used with console.count().
1303
+ */
1304
+ _console.countReset.bind(_console);
1305
+ /**
1306
+ * (Safe copy) Creates a new inline group in the console.
1307
+ */
1308
+ _console.group.bind(_console);
1309
+ /**
1310
+ * (Safe copy) Creates a new inline group in the console that is initially collapsed.
1311
+ */
1312
+ _console.groupCollapsed.bind(_console);
1313
+ /**
1314
+ * (Safe copy) Exits the current inline group.
1315
+ */
1316
+ _console.groupEnd.bind(_console);
1317
+ /**
1318
+ * (Safe copy) Starts a timer with a name specified as an input parameter.
1319
+ */
1320
+ _console.time.bind(_console);
1321
+ /**
1322
+ * (Safe copy) Stops a timer that was previously started.
1323
+ */
1324
+ _console.timeEnd.bind(_console);
1325
+ /**
1326
+ * (Safe copy) Logs the current value of a timer that was previously started.
1327
+ */
1328
+ _console.timeLog.bind(_console);
1329
+
1330
+ const registeredClasses = [];
1331
+
1332
+ /**
1333
+ * Returns the data type of the target.
1334
+ * Uses native `typeof` operator, however, makes distinction between `null`, `array`, and `object`.
1335
+ * Also, when classes are registered via `registerClass`, it checks if objects are instance of any known registered class.
1336
+ *
1337
+ * @param target - The target to get the data type of.
1338
+ * @returns The data type of the target.
1339
+ */
1340
+ const getType = (target) => {
1341
+ if (target === null)
1342
+ return 'null';
1343
+ const nativeDataType = typeof target;
1344
+ if (nativeDataType === 'object') {
1345
+ if (isArray(target))
1346
+ return 'array';
1347
+ for (const registeredClass of registeredClasses) {
1348
+ if (target instanceof registeredClass)
1349
+ return registeredClass.name;
1350
+ }
1351
+ }
1352
+ return nativeDataType;
1353
+ };
1354
+
1355
+ /**
1356
+ * Safe copies of Date built-in via factory function and static methods.
1357
+ *
1358
+ * Since constructors cannot be safely captured via Object.assign, this module
1359
+ * provides a factory function that uses Reflect.construct internally.
1360
+ *
1361
+ * These references are captured at module initialization time to protect against
1362
+ * prototype pollution attacks. Import only what you need for tree-shaking.
1363
+ *
1364
+ * @module @hyperfrontend/immutable-api-utils/built-in-copy/date
1365
+ */
1366
+ // Capture references at module initialization time
1367
+ const _Date = globalThis.Date;
1368
+ const _Reflect = globalThis.Reflect;
1369
+ function createDate(...args) {
1370
+ return _Reflect.construct(_Date, args);
1371
+ }
1372
+
1373
+ /* eslint-disable @typescript-eslint/no-explicit-any */
1374
+ /**
1375
+ * Creates a wrapper function that only executes the wrapped function if the condition function returns true.
1376
+ *
1377
+ * @param func - The function to be conditionally executed.
1378
+ * @param conditionFunc - A function that returns a boolean, determining if `func` should be executed.
1379
+ * @returns A wrapped version of `func` that executes conditionally.
1380
+ */
1381
+ function createConditionalExecutionFunction(func, conditionFunc) {
1382
+ return function (...args) {
1383
+ if (conditionFunc()) {
1384
+ return func(...args);
1385
+ }
1386
+ };
1387
+ }
1388
+
1389
+ /* eslint-disable @typescript-eslint/no-explicit-any */
1390
+ /**
1391
+ * Creates a wrapper function that silently ignores any errors thrown by the wrapped void function.
1392
+ * This function is specifically for wrapping functions that do not return a value (void functions).
1393
+ * Exceptions are swallowed without any logging or handling.
1394
+ *
1395
+ * @param func - The void function to be wrapped.
1396
+ * @returns A wrapped version of the input function that ignores errors.
1397
+ */
1398
+ function createErrorIgnoringFunction(func) {
1399
+ return function (...args) {
1400
+ try {
1401
+ func(...args);
1402
+ }
1403
+ catch {
1404
+ // Deliberately swallowing/ignoring the exception
1405
+ }
1406
+ };
1407
+ }
1408
+
1409
+ /* eslint-disable @typescript-eslint/no-unused-vars */
1410
+ /**
1411
+ * A no-operation function (noop) that does nothing regardless of the arguments passed.
1412
+ * It is designed to be as permissive as possible in its typing without using the `Function` keyword.
1413
+ *
1414
+ * @param args - Any arguments passed to the function (ignored)
1415
+ */
1416
+ const noop = (...args) => {
1417
+ // Intentionally does nothing
1418
+ };
1419
+
1420
+ const logLevels = ['none', 'error', 'warn', 'log', 'info', 'debug'];
1421
+ const priority = {
1422
+ error: 4,
1423
+ warn: 3,
1424
+ log: 2,
1425
+ info: 1,
1426
+ debug: 0,
1427
+ };
1428
+ /**
1429
+ * Validates whether a given string is a valid log level.
1430
+ *
1431
+ * @param level - The log level to validate
1432
+ * @returns True if the level is valid, false otherwise
1433
+ */
1434
+ function isValidLogLevel(level) {
1435
+ return logLevels.includes(level);
1436
+ }
1437
+ /**
1438
+ * Creates a log level configuration manager for controlling logging behavior.
1439
+ * Provides methods to get, set, and evaluate log levels based on priority.
1440
+ *
1441
+ * @param level - The initial log level (defaults to 'error')
1442
+ * @returns A configuration object with log level management methods
1443
+ * @throws {Error} When the provided level is not a valid log level
1444
+ */
1445
+ function createLogLevelConfig(level = 'error') {
1446
+ if (!isValidLogLevel(level)) {
1447
+ throw createError('Cannot create log level configuration with a valid default log level');
1448
+ }
1449
+ const state = { level };
1450
+ const getLogLevel = () => state.level;
1451
+ const setLogLevel = (level) => {
1452
+ if (!isValidLogLevel(level)) {
1453
+ throw createError(`Cannot set value '${level}' level. Expected levels are ${logLevels}.`);
1454
+ }
1455
+ state.level = level;
1456
+ };
1457
+ const shouldLog = (level) => {
1458
+ if (state.level === 'none' || level === 'none' || !isValidLogLevel(level)) {
1459
+ return false;
1460
+ }
1461
+ return priority[level] >= priority[state.level];
1462
+ };
1463
+ return freeze({
1464
+ getLogLevel,
1465
+ setLogLevel,
1466
+ shouldLog,
1467
+ });
1468
+ }
1469
+
1470
+ /**
1471
+ * Creates a logger instance with configurable log level filtering.
1472
+ * Each log function is wrapped to respect the current log level setting.
1473
+ *
1474
+ * @param error - Function to handle error-level logs (required)
1475
+ * @param warn - Function to handle warning-level logs (optional, defaults to noop)
1476
+ * @param log - Function to handle standard logs (optional, defaults to noop)
1477
+ * @param info - Function to handle info-level logs (optional, defaults to noop)
1478
+ * @param debug - Function to handle debug-level logs (optional, defaults to noop)
1479
+ * @returns A frozen logger object with log methods and level control
1480
+ * @throws {ErrorLevelFn} When any provided log function is invalid
1481
+ */
1482
+ function createLogger(error, warn = noop, log = noop, info = noop, debug = noop) {
1483
+ if (notValidLogFn(error)) {
1484
+ throw createError(notFnMsg('error'));
1485
+ }
1486
+ if (notValidLogFn(warn)) {
1487
+ throw createError(notFnMsg('warn'));
1488
+ }
1489
+ if (notValidLogFn(log)) {
1490
+ throw createError(notFnMsg('log'));
1491
+ }
1492
+ if (notValidLogFn(info)) {
1493
+ throw createError(notFnMsg('info'));
1494
+ }
1495
+ if (notValidLogFn(debug)) {
1496
+ throw createError(notFnMsg('debug'));
1497
+ }
1498
+ const { setLogLevel, getLogLevel, shouldLog } = createLogLevelConfig();
1499
+ const wrapLogFn = (fn, level) => {
1500
+ if (fn === noop)
1501
+ return fn;
1502
+ const condition = () => shouldLog(level);
1503
+ return createConditionalExecutionFunction(createErrorIgnoringFunction(fn), condition);
1504
+ };
1505
+ return freeze({
1506
+ error: wrapLogFn(error, 'error'),
1507
+ warn: wrapLogFn(warn, 'warn'),
1508
+ log: wrapLogFn(log, 'log'),
1509
+ info: wrapLogFn(info, 'info'),
1510
+ debug: wrapLogFn(debug, 'debug'),
1511
+ setLogLevel,
1512
+ getLogLevel,
1513
+ });
1514
+ }
1515
+ /**
1516
+ * Validates whether a given value is a valid log function.
1517
+ *
1518
+ * @param fn - The value to validate
1519
+ * @returns True if the value is not a function (invalid), false if it is valid
1520
+ */
1521
+ function notValidLogFn(fn) {
1522
+ return getType(fn) !== 'function' && fn !== noop;
1523
+ }
1524
+ /**
1525
+ * Generates an error message for invalid log function parameters.
1526
+ *
1527
+ * @param label - The name of the log function that failed validation
1528
+ * @returns A formatted error message string
1529
+ */
1530
+ function notFnMsg(label) {
1531
+ return `Cannot create a logger when ${label} is not a function`;
1532
+ }
1533
+
1534
+ createLogger(error, warn, log, info, debug);
1535
+
1536
+ /**
1537
+ * Global log level registry.
1538
+ * Tracks all created scoped loggers to allow global log level changes.
1539
+ */
1540
+ const loggerRegistry = createSet();
1541
+ /** Redacted placeholder for sensitive values */
1542
+ const REDACTED = '[REDACTED]';
1543
+ /**
1544
+ * Patterns that indicate a sensitive key name.
1545
+ * Keys containing these patterns will have their values sanitized.
1546
+ */
1547
+ const SENSITIVE_KEY_PATTERNS = [
1548
+ /token/i,
1549
+ /key/i,
1550
+ /password/i,
1551
+ /secret/i,
1552
+ /credential/i,
1553
+ /auth/i,
1554
+ /bearer/i,
1555
+ /api[_-]?key/i,
1556
+ /private/i,
1557
+ /passphrase/i,
1558
+ ];
1559
+ /**
1560
+ * Checks if a key name indicates sensitive data.
1561
+ *
1562
+ * @param key - Key name to check
1563
+ * @returns True if the key indicates sensitive data
1564
+ */
1565
+ function isSensitiveKey(key) {
1566
+ return SENSITIVE_KEY_PATTERNS.some((pattern) => pattern.test(key));
1567
+ }
1568
+ /**
1569
+ * Sanitizes an object by replacing sensitive values with REDACTED.
1570
+ * This function recursively processes nested objects and arrays.
1571
+ *
1572
+ * @param obj - Object to sanitize
1573
+ * @returns New object with sensitive values redacted
1574
+ */
1575
+ function sanitize(obj) {
1576
+ if (obj === null || obj === undefined) {
1577
+ return obj;
1578
+ }
1579
+ if (isArray(obj)) {
1580
+ return obj.map((item) => sanitize(item));
1581
+ }
1582
+ if (typeof obj === 'object') {
1583
+ const result = {};
1584
+ for (const [key, value] of entries(obj)) {
1585
+ if (isSensitiveKey(key)) {
1586
+ result[key] = REDACTED;
1587
+ }
1588
+ else if (typeof value === 'object' && value !== null) {
1589
+ result[key] = sanitize(value);
1590
+ }
1591
+ else {
1592
+ result[key] = value;
1593
+ }
1594
+ }
1595
+ return result;
1596
+ }
1597
+ return obj;
1598
+ }
1599
+ /**
1600
+ * Formats a log message with optional metadata.
1601
+ *
1602
+ * @param namespace - Logger namespace prefix
1603
+ * @param message - Log message
1604
+ * @param meta - Optional metadata object
1605
+ * @returns Formatted log string
1606
+ */
1607
+ function formatMessage(namespace, message, meta) {
1608
+ const prefix = `[${namespace}]`;
1609
+ if (meta && keys(meta).length > 0) {
1610
+ const sanitizedMeta = sanitize(meta);
1611
+ return `${prefix} ${message} ${stringify(sanitizedMeta)}`;
1612
+ }
1613
+ return `${prefix} ${message}`;
1614
+ }
1615
+ /**
1616
+ * Creates a scoped logger with namespace prefix and optional secret sanitization.
1617
+ * All log messages will be prefixed with [namespace] and sensitive metadata
1618
+ * values will be automatically redacted.
1619
+ *
1620
+ * @param namespace - Logger namespace (e.g., 'project-scope', 'analyze')
1621
+ * @param options - Logger configuration options
1622
+ * @returns A configured scoped logger instance
1623
+ *
1624
+ * @example
1625
+ * ```typescript
1626
+ * const logger = createScopedLogger('project-scope')
1627
+ * logger.setLogLevel('debug')
1628
+ *
1629
+ * // Basic logging
1630
+ * logger.info('Starting analysis', { path: './project' })
1631
+ *
1632
+ * // Sensitive data is automatically redacted
1633
+ * logger.debug('Config loaded', { apiKey: 'secret123' })
1634
+ * // Output: [project-scope] Config loaded {"apiKey":"[REDACTED]"}
1635
+ * ```
1636
+ */
1637
+ function createScopedLogger(namespace, options = {}) {
1638
+ const { level = 'error', sanitizeSecrets = true } = options;
1639
+ // Create wrapper functions that add namespace prefix and sanitization
1640
+ const createLogFn = (baseFn) => (message, meta) => {
1641
+ const processedMeta = sanitizeSecrets && meta ? sanitize(meta) : meta;
1642
+ baseFn(formatMessage(namespace, message, processedMeta));
1643
+ };
1644
+ // Create base logger with wrapped functions
1645
+ const baseLogger = createLogger(createLogFn(error), createLogFn(warn), createLogFn(log), createLogFn(info), createLogFn(debug));
1646
+ // Set initial log level (use global override if set)
1647
+ baseLogger.setLogLevel(level);
1648
+ const scopedLogger = freeze({
1649
+ error: (message, meta) => baseLogger.error(message, meta),
1650
+ warn: (message, meta) => baseLogger.warn(message, meta),
1651
+ log: (message, meta) => baseLogger.log(message, meta),
1652
+ info: (message, meta) => baseLogger.info(message, meta),
1653
+ debug: (message, meta) => baseLogger.debug(message, meta),
1654
+ setLogLevel: baseLogger.setLogLevel,
1655
+ getLogLevel: baseLogger.getLogLevel,
1656
+ });
1657
+ // Register logger for global level management
1658
+ loggerRegistry.add(scopedLogger);
1659
+ return scopedLogger;
1660
+ }
1661
+ /**
1662
+ * Default logger instance for the project-scope library.
1663
+ * Use this for general logging within the library.
1664
+ *
1665
+ * @example
1666
+ * ```typescript
1667
+ * import { logger } from '@hyperfrontend/project-scope/core'
1668
+ *
1669
+ * logger.setLogLevel('debug')
1670
+ * logger.debug('Analyzing project', { path: './src' })
1671
+ * ```
1672
+ */
1673
+ createScopedLogger('project-scope');
1674
+
1675
+ createScopedLogger('project-scope:fs');
1676
+ /**
1677
+ * Create a file system error with code and context.
1678
+ *
1679
+ * @param message - The error message describing what went wrong
1680
+ * @param code - The category code for this type of filesystem failure
1681
+ * @param context - Additional context including path, operation, and cause
1682
+ * @returns A configured Error object with code and context properties
1683
+ */
1684
+ function createFileSystemError(message, code, context) {
1685
+ const error = createError(message);
1686
+ defineProperties(error, {
1687
+ code: { value: code, enumerable: true },
1688
+ context: { value: context, enumerable: true },
1689
+ });
1690
+ return error;
1691
+ }
1692
+ /**
1693
+ * Read file if exists, return null otherwise.
1694
+ *
1695
+ * @param filePath - Path to file
1696
+ * @param encoding - File encoding (default: utf-8)
1697
+ * @returns File contents or null if file doesn't exist
1698
+ */
1699
+ function readFileIfExists(filePath, encoding = 'utf-8') {
1700
+ if (!node_fs.existsSync(filePath)) {
1701
+ return null;
1702
+ }
1703
+ try {
1704
+ return node_fs.readFileSync(filePath, { encoding });
1705
+ }
1706
+ catch {
1707
+ return null;
1708
+ }
1709
+ }
1710
+ /**
1711
+ * Read and parse JSON file if exists, return null otherwise.
1712
+ *
1713
+ * @param filePath - Path to JSON file
1714
+ * @returns Parsed JSON object or null if file doesn't exist or is invalid
1715
+ */
1716
+ function readJsonFileIfExists(filePath) {
1717
+ if (!node_fs.existsSync(filePath)) {
1718
+ return null;
1719
+ }
1720
+ try {
1721
+ const content = node_fs.readFileSync(filePath, { encoding: 'utf-8' });
1722
+ return parse(content);
1723
+ }
1724
+ catch {
1725
+ return null;
1726
+ }
1727
+ }
1728
+
1729
+ createScopedLogger('project-scope:fs:write');
1730
+
1731
+ /**
1732
+ * Get file stats with error handling.
1733
+ *
1734
+ * @param filePath - Path to file
1735
+ * @param followSymlinks - Whether to follow symlinks (default: true)
1736
+ * @returns File stats or null if path doesn't exist
1737
+ */
1738
+ function getFileStat(filePath, followSymlinks = true) {
1739
+ if (!node_fs.existsSync(filePath)) {
1740
+ return null;
1741
+ }
1742
+ try {
1743
+ const stat = followSymlinks ? node_fs.statSync(filePath) : node_fs.lstatSync(filePath);
1744
+ return {
1745
+ isFile: stat.isFile(),
1746
+ isDirectory: stat.isDirectory(),
1747
+ isSymlink: stat.isSymbolicLink(),
1748
+ size: stat.size,
1749
+ created: stat.birthtime,
1750
+ modified: stat.mtime,
1751
+ accessed: stat.atime,
1752
+ mode: stat.mode,
1753
+ };
1754
+ }
1755
+ catch {
1756
+ return null;
1757
+ }
1758
+ }
1759
+ /**
1760
+ * Check if path is a directory.
1761
+ *
1762
+ * @param dirPath - Path to check
1763
+ * @returns True if path is a directory
1764
+ */
1765
+ function isDirectory(dirPath) {
1766
+ const stats = getFileStat(dirPath);
1767
+ return stats?.isDirectory ?? false;
1768
+ }
1769
+ /**
1770
+ * Check if path exists.
1771
+ *
1772
+ * @param filePath - Path to check
1773
+ * @returns True if path exists
1774
+ */
1775
+ function exists(filePath) {
1776
+ return node_fs.existsSync(filePath);
1777
+ }
1778
+
1779
+ const fsDirLogger = createScopedLogger('project-scope:fs:dir');
1780
+ /**
1781
+ * List immediate contents of a directory.
1782
+ *
1783
+ * @param dirPath - Absolute or relative path to the directory
1784
+ * @returns Array of entries with metadata for each file/directory
1785
+ * @throws {Error} If directory doesn't exist or isn't a directory
1786
+ *
1787
+ * @example
1788
+ * ```typescript
1789
+ * import { readDirectory } from '@hyperfrontend/project-scope'
1790
+ *
1791
+ * const entries = readDirectory('./src')
1792
+ * for (const entry of entries) {
1793
+ * console.log(entry.name, entry.isFile ? 'file' : 'directory')
1794
+ * }
1795
+ * ```
1796
+ */
1797
+ function readDirectory(dirPath) {
1798
+ fsDirLogger.debug('Reading directory', { path: dirPath });
1799
+ if (!node_fs.existsSync(dirPath)) {
1800
+ fsDirLogger.debug('Directory not found', { path: dirPath });
1801
+ throw createFileSystemError(`Directory not found: ${dirPath}`, 'FS_NOT_FOUND', { path: dirPath, operation: 'readdir' });
1802
+ }
1803
+ if (!isDirectory(dirPath)) {
1804
+ fsDirLogger.debug('Path is not a directory', { path: dirPath });
1805
+ throw createFileSystemError(`Not a directory: ${dirPath}`, 'FS_NOT_A_DIRECTORY', { path: dirPath, operation: 'readdir' });
1806
+ }
1807
+ try {
1808
+ const entries = node_fs.readdirSync(dirPath, { withFileTypes: true });
1809
+ fsDirLogger.debug('Directory read complete', { path: dirPath, entryCount: entries.length });
1810
+ return entries.map((entry) => ({
1811
+ name: entry.name,
1812
+ path: node_path.join(dirPath, entry.name),
1813
+ isFile: entry.isFile(),
1814
+ isDirectory: entry.isDirectory(),
1815
+ isSymlink: entry.isSymbolicLink(),
1816
+ }));
1817
+ }
1818
+ catch (error) {
1819
+ fsDirLogger.warn('Failed to read directory', { path: dirPath, error: error instanceof Error ? error.message : String(error) });
1820
+ throw createFileSystemError(`Failed to read directory: ${dirPath}`, 'FS_READ_ERROR', {
1821
+ path: dirPath,
1822
+ operation: 'readdir',
1823
+ cause: error,
1824
+ });
1825
+ }
1826
+ }
1827
+
1828
+ createScopedLogger('project-scope:fs:traversal');
1829
+
1830
+ const packageLogger = createScopedLogger('project-scope:project:package');
1831
+ /**
1832
+ * Verifies that a value is an object with only string values,
1833
+ * used for validating dependency maps and script definitions.
1834
+ *
1835
+ * @param value - Value to check
1836
+ * @returns True if value is a record of strings
1837
+ */
1838
+ function isStringRecord(value) {
1839
+ if (typeof value !== 'object' || value === null)
1840
+ return false;
1841
+ return values(value).every((v) => typeof v === 'string');
1842
+ }
1843
+ /**
1844
+ * Extracts and normalizes the workspaces field from package.json,
1845
+ * supporting both array format and object with packages array.
1846
+ *
1847
+ * @param value - Raw workspaces value from package.json
1848
+ * @returns Normalized workspace patterns or undefined if invalid
1849
+ */
1850
+ function parseWorkspaces(value) {
1851
+ if (isArray(value) && value.every((v) => typeof v === 'string')) {
1852
+ return value;
1853
+ }
1854
+ if (typeof value === 'object' && value !== null) {
1855
+ const obj = value;
1856
+ if (isArray(obj['packages'])) {
1857
+ return { packages: obj['packages'] };
1858
+ }
1859
+ }
1860
+ return undefined;
1861
+ }
1862
+ /**
1863
+ * Validate and normalize package.json data.
1864
+ *
1865
+ * @param data - Raw parsed data
1866
+ * @returns Validated package.json
1867
+ */
1868
+ function validatePackageJson(data) {
1869
+ if (typeof data !== 'object' || data === null) {
1870
+ throw createError('package.json must be an object');
1871
+ }
1872
+ const pkg = data;
1873
+ return {
1874
+ name: typeof pkg['name'] === 'string' ? pkg['name'] : undefined,
1875
+ version: typeof pkg['version'] === 'string' ? pkg['version'] : undefined,
1876
+ description: typeof pkg['description'] === 'string' ? pkg['description'] : undefined,
1877
+ main: typeof pkg['main'] === 'string' ? pkg['main'] : undefined,
1878
+ module: typeof pkg['module'] === 'string' ? pkg['module'] : undefined,
1879
+ browser: typeof pkg['browser'] === 'string' ? pkg['browser'] : undefined,
1880
+ types: typeof pkg['types'] === 'string' ? pkg['types'] : undefined,
1881
+ bin: typeof pkg['bin'] === 'string' || isStringRecord(pkg['bin']) ? pkg['bin'] : undefined,
1882
+ scripts: isStringRecord(pkg['scripts']) ? pkg['scripts'] : undefined,
1883
+ dependencies: isStringRecord(pkg['dependencies']) ? pkg['dependencies'] : undefined,
1884
+ devDependencies: isStringRecord(pkg['devDependencies']) ? pkg['devDependencies'] : undefined,
1885
+ peerDependencies: isStringRecord(pkg['peerDependencies']) ? pkg['peerDependencies'] : undefined,
1886
+ optionalDependencies: isStringRecord(pkg['optionalDependencies']) ? pkg['optionalDependencies'] : undefined,
1887
+ workspaces: parseWorkspaces(pkg['workspaces']),
1888
+ exports: typeof pkg['exports'] === 'object' ? pkg['exports'] : undefined,
1889
+ engines: isStringRecord(pkg['engines']) ? pkg['engines'] : undefined,
1890
+ ...pkg,
1891
+ };
1892
+ }
1893
+ /**
1894
+ * Attempts to read and parse package.json if it exists,
1895
+ * returning null on missing file or parse failure.
1896
+ *
1897
+ * @param projectPath - Project directory path or path to package.json
1898
+ * @returns Parsed package.json or null if not found
1899
+ */
1900
+ function readPackageJsonIfExists(projectPath) {
1901
+ const packageJsonPath = projectPath.endsWith('package.json') ? projectPath : node_path.join(projectPath, 'package.json');
1902
+ const content = readFileIfExists(packageJsonPath);
1903
+ if (!content) {
1904
+ packageLogger.debug('Package.json not found', { path: packageJsonPath });
1905
+ return null;
1906
+ }
1907
+ try {
1908
+ const validated = validatePackageJson(parse(content));
1909
+ packageLogger.debug('Package.json loaded', { path: packageJsonPath, name: validated.name });
1910
+ return validated;
1911
+ }
1912
+ catch {
1913
+ packageLogger.debug('Failed to parse package.json, returning null', { path: packageJsonPath });
1914
+ return null;
1915
+ }
1916
+ }
1917
+
1918
+ createScopedLogger('project-scope:root');
1919
+
1920
+ const nxLogger = createScopedLogger('project-scope:nx');
1921
+ /**
1922
+ * Files indicating NX workspace root.
1923
+ */
1924
+ const NX_CONFIG_FILES = ['nx.json', 'workspace.json'];
1925
+ /**
1926
+ * NX-specific project file.
1927
+ */
1928
+ const NX_PROJECT_FILE = 'project.json';
1929
+ /**
1930
+ * Check if directory is an NX workspace root.
1931
+ *
1932
+ * @param path - Directory path to check
1933
+ * @returns True if the directory contains nx.json or workspace.json
1934
+ *
1935
+ * @example
1936
+ * ```typescript
1937
+ * import { isNxWorkspace } from '@hyperfrontend/project-scope'
1938
+ *
1939
+ * if (isNxWorkspace('./my-project')) {
1940
+ * console.log('This is an NX monorepo')
1941
+ * }
1942
+ * ```
1943
+ */
1944
+ function isNxWorkspace(path) {
1945
+ for (const configFile of NX_CONFIG_FILES) {
1946
+ if (exists(node_path.join(path, configFile))) {
1947
+ nxLogger.debug('NX workspace detected', { path, configFile });
1948
+ return true;
1949
+ }
1950
+ }
1951
+ nxLogger.debug('Not an NX workspace', { path });
1952
+ return false;
1953
+ }
1954
+ /**
1955
+ * Check if directory is an NX project.
1956
+ *
1957
+ * @param path - Directory path to check
1958
+ * @returns True if the directory contains project.json
1959
+ */
1960
+ function isNxProject(path) {
1961
+ const isProject = exists(node_path.join(path, NX_PROJECT_FILE));
1962
+ nxLogger.debug('NX project check', { path, isProject });
1963
+ return isProject;
1964
+ }
1965
+ /**
1966
+ * Detect NX version from package.json dependencies.
1967
+ *
1968
+ * @param workspacePath - Workspace root path
1969
+ * @returns NX version string (without semver range) or null
1970
+ */
1971
+ function detectNxVersion(workspacePath) {
1972
+ const packageJson = readPackageJsonIfExists(workspacePath);
1973
+ if (packageJson) {
1974
+ const nxVersion = packageJson.devDependencies?.['nx'] ?? packageJson.dependencies?.['nx'];
1975
+ if (nxVersion) {
1976
+ // Strip semver range characters (^, ~, >=, etc.)
1977
+ return nxVersion.replace(/^[\^~>=<]+/, '');
1978
+ }
1979
+ }
1980
+ return null;
1981
+ }
1982
+ /**
1983
+ * Check if workspace is integrated (not standalone).
1984
+ * Integrated repos typically have workspaceLayout, namedInputs, or targetDefaults.
1985
+ *
1986
+ * @param nxJson - Parsed nx.json configuration
1987
+ * @returns True if the workspace is integrated
1988
+ */
1989
+ function isIntegratedRepo(nxJson) {
1990
+ return nxJson.workspaceLayout !== undefined || nxJson.namedInputs !== undefined || nxJson.targetDefaults !== undefined;
1991
+ }
1992
+ /**
1993
+ * Get comprehensive NX workspace information.
1994
+ *
1995
+ * @param workspacePath - Workspace root path
1996
+ * @returns Workspace info or null if not an NX workspace
1997
+ */
1998
+ function getNxWorkspaceInfo(workspacePath) {
1999
+ nxLogger.debug('Getting NX workspace info', { workspacePath });
2000
+ if (!isNxWorkspace(workspacePath)) {
2001
+ return null;
2002
+ }
2003
+ const nxJson = readJsonFileIfExists(node_path.join(workspacePath, 'nx.json'));
2004
+ if (!nxJson) {
2005
+ // Check for workspace.json as fallback (older NX)
2006
+ const workspaceJson = readJsonFileIfExists(node_path.join(workspacePath, 'workspace.json'));
2007
+ if (!workspaceJson) {
2008
+ nxLogger.debug('No nx.json or workspace.json found', { workspacePath });
2009
+ return null;
2010
+ }
2011
+ nxLogger.debug('Using legacy workspace.json', { workspacePath });
2012
+ // Create minimal nx.json from workspace.json
2013
+ return {
2014
+ root: workspacePath,
2015
+ version: detectNxVersion(workspacePath),
2016
+ nxJson: {},
2017
+ isIntegrated: true,
2018
+ workspaceLayout: {
2019
+ appsDir: 'apps',
2020
+ libsDir: 'libs',
2021
+ },
2022
+ };
2023
+ }
2024
+ const info = {
2025
+ root: workspacePath,
2026
+ version: detectNxVersion(workspacePath),
2027
+ nxJson,
2028
+ isIntegrated: isIntegratedRepo(nxJson),
2029
+ defaultProject: nxJson.defaultProject,
2030
+ workspaceLayout: {
2031
+ appsDir: nxJson.workspaceLayout?.appsDir ?? 'apps',
2032
+ libsDir: nxJson.workspaceLayout?.libsDir ?? 'libs',
2033
+ },
2034
+ };
2035
+ nxLogger.debug('NX workspace info retrieved', {
2036
+ workspacePath,
2037
+ version: info.version,
2038
+ isIntegrated: info.isIntegrated,
2039
+ defaultProject: info.defaultProject,
2040
+ });
2041
+ return info;
2042
+ }
2043
+
2044
+ createScopedLogger('project-scope:nx:devkit');
2045
+
2046
+ const nxConfigLogger = createScopedLogger('project-scope:nx:config');
2047
+ /**
2048
+ * Read project.json for an NX project.
2049
+ *
2050
+ * @param projectPath - Project directory path
2051
+ * @returns Parsed project.json or null if not found
2052
+ */
2053
+ function readProjectJson(projectPath) {
2054
+ const projectJsonPath = node_path.join(projectPath, NX_PROJECT_FILE);
2055
+ nxConfigLogger.debug('Reading project.json', { path: projectJsonPath });
2056
+ const result = readJsonFileIfExists(projectJsonPath);
2057
+ if (result) {
2058
+ nxConfigLogger.debug('Project.json loaded', { path: projectJsonPath, name: result.name });
2059
+ }
2060
+ else {
2061
+ nxConfigLogger.debug('Project.json not found', { path: projectJsonPath });
2062
+ }
2063
+ return result;
2064
+ }
2065
+ /**
2066
+ * Get project configuration from project.json or package.json nx field.
2067
+ *
2068
+ * @param projectPath - Project directory path
2069
+ * @param workspacePath - Workspace root path (for relative path calculation)
2070
+ * @returns Project configuration or null if not found
2071
+ */
2072
+ function getProjectConfig(projectPath, workspacePath) {
2073
+ nxConfigLogger.debug('Getting project config', { projectPath, workspacePath });
2074
+ // Try project.json first
2075
+ const projectJson = readProjectJson(projectPath);
2076
+ if (projectJson) {
2077
+ nxConfigLogger.debug('Using project.json config', { projectPath, name: projectJson.name });
2078
+ return {
2079
+ ...projectJson,
2080
+ root: projectJson.root ?? node_path.relative(workspacePath, projectPath),
2081
+ };
2082
+ }
2083
+ // Try to infer from package.json nx field
2084
+ const packageJson = readPackageJsonIfExists(projectPath);
2085
+ if (packageJson && typeof packageJson['nx'] === 'object') {
2086
+ nxConfigLogger.debug('Using package.json nx field', { projectPath, name: packageJson.name });
2087
+ const nxConfig = packageJson['nx'];
2088
+ return {
2089
+ name: packageJson.name,
2090
+ root: node_path.relative(workspacePath, projectPath),
2091
+ ...nxConfig,
2092
+ };
2093
+ }
2094
+ nxConfigLogger.debug('No project config found', { projectPath });
2095
+ return null;
2096
+ }
2097
+ /**
2098
+ * Recursively scan directory for project.json files.
2099
+ *
2100
+ * @param dirPath - Directory to scan
2101
+ * @param workspacePath - Workspace root path
2102
+ * @param projects - Map to add discovered projects to
2103
+ * @param maxDepth - Maximum recursion depth
2104
+ * @param currentDepth - Current recursion depth
2105
+ */
2106
+ function scanForProjects(dirPath, workspacePath, projects, maxDepth, currentDepth = 0) {
2107
+ if (currentDepth > maxDepth)
2108
+ return;
2109
+ try {
2110
+ const entries = readDirectory(dirPath);
2111
+ for (const entry of entries) {
2112
+ // Skip node_modules and hidden directories
2113
+ if (entry.name.startsWith('.') || entry.name === 'node_modules' || entry.name === 'dist') {
2114
+ continue;
2115
+ }
2116
+ const fullPath = node_path.join(dirPath, entry.name);
2117
+ if (entry.isDirectory) {
2118
+ // Check if this directory is an NX project
2119
+ if (isNxProject(fullPath)) {
2120
+ const config = getProjectConfig(fullPath, workspacePath);
2121
+ if (config) {
2122
+ const name = config.name || node_path.relative(workspacePath, fullPath).replace(/[\\/]/g, '-');
2123
+ projects.set(name, {
2124
+ ...config,
2125
+ name,
2126
+ root: node_path.relative(workspacePath, fullPath),
2127
+ });
2128
+ }
2129
+ }
2130
+ // Recursively scan subdirectories
2131
+ scanForProjects(fullPath, workspacePath, projects, maxDepth, currentDepth + 1);
2132
+ }
2133
+ }
2134
+ }
2135
+ catch {
2136
+ // Directory not readable, skip
2137
+ }
2138
+ }
2139
+ /**
2140
+ * Discover all NX projects in workspace.
2141
+ * Supports both workspace.json (older format) and project.json (newer format).
2142
+ *
2143
+ * @param workspacePath - Workspace root path
2144
+ * @returns Map of project name to configuration
2145
+ */
2146
+ function discoverNxProjects(workspacePath) {
2147
+ const projects = createMap();
2148
+ // Check for workspace.json (older NX format)
2149
+ const workspaceJson = readJsonFileIfExists(node_path.join(workspacePath, 'workspace.json'));
2150
+ if (workspaceJson?.projects) {
2151
+ for (const [name, config] of entries(workspaceJson.projects)) {
2152
+ if (typeof config === 'string') {
2153
+ // Path reference to project directory
2154
+ const projectPath = node_path.join(workspacePath, config);
2155
+ const projectConfig = getProjectConfig(projectPath, workspacePath);
2156
+ if (projectConfig) {
2157
+ projects.set(name, { ...projectConfig, name });
2158
+ }
2159
+ }
2160
+ else if (typeof config === 'object' && config !== null) {
2161
+ // Inline config
2162
+ projects.set(name, { name, ...config });
2163
+ }
2164
+ }
2165
+ return projects;
2166
+ }
2167
+ // Scan for project.json files (newer NX format)
2168
+ const workspaceInfo = getNxWorkspaceInfo(workspacePath);
2169
+ const appsDir = workspaceInfo?.workspaceLayout.appsDir ?? 'apps';
2170
+ const libsDir = workspaceInfo?.workspaceLayout.libsDir ?? 'libs';
2171
+ const searchDirs = [appsDir, libsDir];
2172
+ // Also check packages directory (common in some setups)
2173
+ if (exists(node_path.join(workspacePath, 'packages'))) {
2174
+ searchDirs.push('packages');
2175
+ }
2176
+ for (const dir of searchDirs) {
2177
+ const dirPath = node_path.join(workspacePath, dir);
2178
+ if (exists(dirPath) && isDirectory(dirPath)) {
2179
+ try {
2180
+ scanForProjects(dirPath, workspacePath, projects, 3);
2181
+ }
2182
+ catch {
2183
+ // Directory not accessible
2184
+ }
2185
+ }
2186
+ }
2187
+ // Also check root-level projects (standalone projects in monorepo root)
2188
+ if (isNxProject(workspacePath)) {
2189
+ const config = readProjectJson(workspacePath);
2190
+ if (config) {
2191
+ const name = config.name || node_path.basename(workspacePath);
2192
+ projects.set(name, {
2193
+ ...config,
2194
+ name,
2195
+ root: '.',
2196
+ });
2197
+ }
2198
+ }
2199
+ return projects;
2200
+ }
2201
+ /**
2202
+ * Build a simple project graph from discovered projects.
2203
+ * For full graph capabilities, use `@nx/devkit`.
2204
+ *
2205
+ * @param workspacePath - Workspace root path
2206
+ * @param projects - Existing configuration map to skip auto-discovery
2207
+ * @returns NxProjectGraph with nodes and dependencies
2208
+ */
2209
+ function buildSimpleProjectGraph(workspacePath, projects) {
2210
+ const projectMap = projects ?? discoverNxProjects(workspacePath);
2211
+ const nodes = {};
2212
+ const dependencies = {};
2213
+ for (const [name, config] of projectMap) {
2214
+ nodes[name] = {
2215
+ name,
2216
+ type: config.projectType ?? 'library',
2217
+ data: config,
2218
+ };
2219
+ dependencies[name] = [];
2220
+ // Add implicit dependencies
2221
+ if (config.implicitDependencies) {
2222
+ for (const dep of config.implicitDependencies) {
2223
+ // Skip negative dependencies (those starting with !)
2224
+ if (!dep.startsWith('!')) {
2225
+ dependencies[name].push({
2226
+ target: dep,
2227
+ type: 'implicit',
2228
+ });
2229
+ }
2230
+ }
2231
+ }
2232
+ }
2233
+ return { nodes, dependencies };
2234
+ }
2235
+
2236
+ /**
2237
+ * Creates an empty classification summary.
2238
+ *
2239
+ * @returns A new ClassificationSummary with all counts at zero
2240
+ */
2241
+ function createEmptyClassificationSummary() {
2242
+ return {
2243
+ total: 0,
2244
+ included: 0,
2245
+ excluded: 0,
2246
+ bySource: {
2247
+ 'direct-scope': 0,
2248
+ 'direct-file': 0,
2249
+ 'unscoped-file': 0,
2250
+ 'indirect-dependency': 0,
2251
+ 'indirect-infra': 0,
2252
+ 'unscoped-global': 0,
2253
+ excluded: 0,
2254
+ },
2255
+ };
2256
+ }
2257
+ /**
2258
+ * Creates a classified commit.
2259
+ *
2260
+ * @param commit - The parsed conventional commit
2261
+ * @param raw - The raw git commit
2262
+ * @param source - How the commit relates to the project
2263
+ * @param options - Additional classification options
2264
+ * @param options.touchedFiles - Files in the project modified by this commit
2265
+ * @param options.dependencyPath - Chain of dependencies leading to indirect inclusion
2266
+ * @returns A new ClassifiedCommit object
2267
+ */
2268
+ function createClassifiedCommit(commit, raw, source, options) {
2269
+ const include = isIncludedSource(source);
2270
+ const preserveScope = shouldPreserveScope(source);
2271
+ return {
2272
+ commit,
2273
+ raw,
2274
+ source,
2275
+ include,
2276
+ preserveScope,
2277
+ touchedFiles: options?.touchedFiles,
2278
+ dependencyPath: options?.dependencyPath,
2279
+ };
2280
+ }
2281
+ /**
2282
+ * Determines if a source type should be included in changelog.
2283
+ *
2284
+ * @param source - The commit source type
2285
+ * @returns True if commits with this source should be included
2286
+ */
2287
+ function isIncludedSource(source) {
2288
+ switch (source) {
2289
+ case 'direct-scope':
2290
+ case 'direct-file':
2291
+ case 'unscoped-file':
2292
+ case 'indirect-dependency':
2293
+ case 'indirect-infra':
2294
+ return true;
2295
+ case 'unscoped-global':
2296
+ case 'excluded':
2297
+ return false;
2298
+ }
2299
+ }
2300
+ /**
2301
+ * Determines if scope should be preserved for a source type.
2302
+ *
2303
+ * Direct commits omit scope (redundant in project changelog).
2304
+ * Indirect commits preserve scope for context.
2305
+ *
2306
+ * @param source - The commit source type
2307
+ * @returns True if scope should be preserved in changelog
2308
+ */
2309
+ function shouldPreserveScope(source) {
2310
+ switch (source) {
2311
+ case 'direct-scope':
2312
+ case 'unscoped-file':
2313
+ return false; // Scope would be redundant
2314
+ case 'direct-file':
2315
+ case 'indirect-dependency':
2316
+ case 'indirect-infra':
2317
+ return true; // Scope provides context
2318
+ case 'unscoped-global':
2319
+ case 'excluded':
2320
+ return false; // Won't be shown
2321
+ }
2322
+ }
2323
+
2324
+ /**
2325
+ * Derives all scope variations that should match a project.
2326
+ *
2327
+ * Given a project named 'lib-versioning' with package '@hyperfrontend/versioning',
2328
+ * this generates variations like:
2329
+ * - 'lib-versioning' (full project name)
2330
+ * - 'versioning' (without lib- prefix)
2331
+ *
2332
+ * @param options - Project identification options
2333
+ * @returns Array of scope strings that match this project
2334
+ *
2335
+ * @example
2336
+ * deriveProjectScopes({ projectName: 'lib-versioning', packageName: '@hyperfrontend/versioning' })
2337
+ * // Returns: ['lib-versioning', 'versioning']
2338
+ *
2339
+ * @example
2340
+ * deriveProjectScopes({ projectName: 'app-demo', packageName: 'demo-app' })
2341
+ * // Returns: ['app-demo', 'demo']
2342
+ */
2343
+ function deriveProjectScopes(options) {
2344
+ const { projectName, packageName, additionalScopes = [] } = options;
2345
+ const scopes = createSet();
2346
+ // Always include the full project name
2347
+ scopes.add(projectName);
2348
+ // Add variations based on common prefixes
2349
+ const prefixVariations = extractPrefixVariations(projectName);
2350
+ for (const variation of prefixVariations) {
2351
+ scopes.add(variation);
2352
+ }
2353
+ // Add package name variations if provided
2354
+ if (packageName) {
2355
+ const packageVariations = extractPackageNameVariations(packageName);
2356
+ for (const variation of packageVariations) {
2357
+ scopes.add(variation);
2358
+ }
2359
+ }
2360
+ // Add any additional scopes
2361
+ for (const scope of additionalScopes) {
2362
+ if (scope) {
2363
+ scopes.add(scope);
2364
+ }
2365
+ }
2366
+ return [...scopes];
2367
+ }
2368
+ /**
2369
+ * Recognized project name prefixes that can be stripped for scope matching.
2370
+ */
2371
+ const PROJECT_PREFIXES = ['lib-', 'app-', 'e2e-', 'tool-', 'plugin-', 'feature-', 'package-'];
2372
+ /**
2373
+ * Generates scope variations by stripping recognized project prefixes.
2374
+ *
2375
+ * @param projectName - The project name to extract variations from
2376
+ * @returns Array of scope name variations
2377
+ */
2378
+ function extractPrefixVariations(projectName) {
2379
+ const variations = [];
2380
+ for (const prefix of PROJECT_PREFIXES) {
2381
+ if (projectName.startsWith(prefix)) {
2382
+ const withoutPrefix = projectName.slice(prefix.length);
2383
+ if (withoutPrefix) {
2384
+ variations.push(withoutPrefix);
2385
+ }
2386
+ break; // Only remove one prefix
2387
+ }
2388
+ }
2389
+ return variations;
2390
+ }
2391
+ /**
2392
+ * Extracts scope variations from an npm package name.
2393
+ *
2394
+ * @param packageName - The npm package name (e.g., '@scope/name')
2395
+ * @returns Array of name variations
2396
+ */
2397
+ function extractPackageNameVariations(packageName) {
2398
+ const variations = [];
2399
+ // Handle scoped packages: @scope/name -> name
2400
+ if (packageName.startsWith('@')) {
2401
+ const slashIndex = packageName.indexOf('/');
2402
+ if (slashIndex !== -1) {
2403
+ const unscoped = packageName.slice(slashIndex + 1);
2404
+ if (unscoped) {
2405
+ variations.push(unscoped);
2406
+ }
2407
+ }
2408
+ }
2409
+ else {
2410
+ // Non-scoped package: just use the name
2411
+ variations.push(packageName);
2412
+ }
2413
+ return variations;
2414
+ }
2415
+ /**
2416
+ * Checks if a commit scope matches any of the project scopes.
2417
+ *
2418
+ * @param commitScope - The scope from a conventional commit
2419
+ * @param projectScopes - Array of scopes that match the project
2420
+ * @returns True if the commit scope matches the project
2421
+ *
2422
+ * @example
2423
+ * scopeMatchesProject('versioning', ['lib-versioning', 'versioning']) // true
2424
+ * scopeMatchesProject('logging', ['lib-versioning', 'versioning']) // false
2425
+ */
2426
+ function scopeMatchesProject(commitScope, projectScopes) {
2427
+ if (!commitScope) {
2428
+ return false;
2429
+ }
2430
+ // Case-insensitive comparison
2431
+ const normalizedScope = commitScope.toLowerCase();
2432
+ return projectScopes.some((scope) => scope.toLowerCase() === normalizedScope);
2433
+ }
2434
+ /**
2435
+ * Checks if a commit scope should be explicitly excluded.
2436
+ *
2437
+ * @param commitScope - The scope from a conventional commit
2438
+ * @param excludeScopes - Array of scopes to exclude
2439
+ * @returns True if the scope should be excluded
2440
+ */
2441
+ function scopeIsExcluded(commitScope, excludeScopes) {
2442
+ if (!commitScope) {
2443
+ return false;
2444
+ }
2445
+ const normalizedScope = commitScope.toLowerCase();
2446
+ return excludeScopes.some((scope) => scope.toLowerCase() === normalizedScope);
2447
+ }
2448
+ /**
2449
+ * Default scopes to exclude from changelogs.
2450
+ *
2451
+ * These represent repository-level or infrastructure changes
2452
+ * that typically don't belong in individual project changelogs.
2453
+ */
2454
+ const DEFAULT_EXCLUDE_SCOPES = ['release', 'deps', 'workspace', 'root', 'repo', 'ci', 'build'];
2455
+
2456
+ /**
2457
+ * Classifies a single commit against a project.
2458
+ *
2459
+ * Implements the hybrid classification strategy:
2460
+ * 1. Check scope match (fast path)
2461
+ * 2. Check file touch (validation/catch-all)
2462
+ * 3. Check dependency touch (indirect)
2463
+ * 4. Fallback to excluded
2464
+ *
2465
+ * @param input - The commit to classify
2466
+ * @param context - Classification context with project info
2467
+ * @returns Classified commit with source attribution
2468
+ *
2469
+ * @example
2470
+ * const classified = classifyCommit(
2471
+ * { commit: parsedCommit, raw: gitCommit },
2472
+ * { projectScopes: ['versioning'], fileCommitHashes: new Set(['abc123']) }
2473
+ * )
2474
+ */
2475
+ function classifyCommit(input, context) {
2476
+ const { commit, raw } = input;
2477
+ const { projectScopes, fileCommitHashes, dependencyCommitMap, infrastructureCommitHashes, excludeScopes = DEFAULT_EXCLUDE_SCOPES, includeScopes = [], } = context;
2478
+ const scope = commit.scope;
2479
+ const hasScope = !!scope;
2480
+ const allProjectScopes = [...projectScopes, ...includeScopes];
2481
+ // First check: Is this scope explicitly excluded?
2482
+ if (hasScope && scopeIsExcluded(scope, excludeScopes)) {
2483
+ return createClassifiedCommit(commit, raw, 'excluded');
2484
+ }
2485
+ // Priority 1: Scope-based direct match (fast path)
2486
+ if (hasScope && scopeMatchesProject(scope, allProjectScopes)) {
2487
+ return createClassifiedCommit(commit, raw, 'direct-scope');
2488
+ }
2489
+ // Priority 2: File-based direct match (validation/catch-all)
2490
+ if (fileCommitHashes.has(raw.hash)) {
2491
+ // Commit touched project files
2492
+ if (hasScope) {
2493
+ // Has a scope but it's different - likely a typo or cross-cutting change
2494
+ return createClassifiedCommit(commit, raw, 'direct-file');
2495
+ }
2496
+ // No scope but touched project files
2497
+ return createClassifiedCommit(commit, raw, 'unscoped-file');
2498
+ }
2499
+ // Priority 3: Indirect dependency match
2500
+ if (hasScope && dependencyCommitMap) {
2501
+ const dependencyPath = findDependencyPath(scope, raw.hash, dependencyCommitMap);
2502
+ if (dependencyPath) {
2503
+ return createClassifiedCommit(commit, raw, 'indirect-dependency', { dependencyPath });
2504
+ }
2505
+ }
2506
+ // File-based infrastructure match
2507
+ if (infrastructureCommitHashes?.has(raw.hash)) {
2508
+ return createClassifiedCommit(commit, raw, 'indirect-infra');
2509
+ }
2510
+ // Fallback: No match found
2511
+ if (!hasScope) {
2512
+ // Unscoped commit that didn't touch any project files
2513
+ return createClassifiedCommit(commit, raw, 'unscoped-global');
2514
+ }
2515
+ // Scoped commit that doesn't match anything
2516
+ return createClassifiedCommit(commit, raw, 'excluded');
2517
+ }
2518
+ /**
2519
+ * Classifies multiple commits against a project.
2520
+ *
2521
+ * @param commits - Array of commits to classify
2522
+ * @param context - Classification context with project info
2523
+ * @returns Classification result with all commits and summary
2524
+ */
2525
+ function classifyCommits(commits, context) {
2526
+ const classified = [];
2527
+ const included = [];
2528
+ const excluded = [];
2529
+ const summary = createEmptyClassificationSummary();
2530
+ const bySource = { ...summary.bySource };
2531
+ for (const input of commits) {
2532
+ const result = classifyCommit(input, context);
2533
+ classified.push(result);
2534
+ // Update summary
2535
+ bySource[result.source]++;
2536
+ if (result.include) {
2537
+ included.push(result);
2538
+ }
2539
+ else {
2540
+ excluded.push(result);
2541
+ }
2542
+ }
2543
+ return {
2544
+ commits: classified,
2545
+ included,
2546
+ excluded,
2547
+ summary: {
2548
+ total: classified.length,
2549
+ included: included.length,
2550
+ excluded: excluded.length,
2551
+ bySource,
2552
+ },
2553
+ };
2554
+ }
2555
+ /**
2556
+ * Finds a dependency path for a given scope and commit hash.
2557
+ *
2558
+ * Verifies both:
2559
+ * 1. The scope matches a dependency name (or variation)
2560
+ * 2. The commit hash is in that dependency's commit set
2561
+ *
2562
+ * This prevents false positives from mislabeled commits.
2563
+ *
2564
+ * @param scope - The commit scope
2565
+ * @param hash - The commit hash to verify
2566
+ * @param dependencyCommitMap - Map of dependencies to their commit hashes
2567
+ * @returns Dependency path if found and hash verified, undefined otherwise
2568
+ */
2569
+ function findDependencyPath(scope, hash, dependencyCommitMap) {
2570
+ const normalizedScope = scope.toLowerCase();
2571
+ for (const [depName, depHashes] of dependencyCommitMap) {
2572
+ // Check if scope matches dependency name or variations
2573
+ const depVariations = getDependencyVariations(depName);
2574
+ if (depVariations.some((v) => v.toLowerCase() === normalizedScope)) {
2575
+ // CRITICAL: Verify the commit actually touched this dependency's files
2576
+ // This prevents false positives from mislabeled commits
2577
+ if (depHashes.has(hash)) {
2578
+ return [depName];
2579
+ }
2580
+ }
2581
+ }
2582
+ return undefined;
2583
+ }
2584
+ /**
2585
+ * Generates name variations for a dependency to enable flexible scope matching.
2586
+ *
2587
+ * @param depName - The dependency project or package name
2588
+ * @returns Array of name variations including stripped prefixes
2589
+ */
2590
+ function getDependencyVariations(depName) {
2591
+ const variations = [depName];
2592
+ // Handle lib- prefix
2593
+ if (depName.startsWith('lib-')) {
2594
+ variations.push(depName.slice(4));
2595
+ }
2596
+ // Handle @scope/name
2597
+ if (depName.startsWith('@')) {
2598
+ const slashIndex = depName.indexOf('/');
2599
+ if (slashIndex !== -1) {
2600
+ variations.push(depName.slice(slashIndex + 1));
2601
+ }
2602
+ }
2603
+ return variations;
2604
+ }
2605
+ /**
2606
+ * Creates a classification context from common inputs.
89
2607
  *
90
- * @param message - Explanation for why the step was skipped
91
- * @returns A FlowStepResult with 'skipped' status
2608
+ * @param projectScopes - Scopes that match the project
2609
+ * @param fileCommitHashes - Set of commit hashes that touched project files
2610
+ * @param options - Additional context options
2611
+ * @param options.dependencyCommitMap - Map of dependency names to commit hashes touching them
2612
+ * @param options.infrastructureCommitHashes - Set of commit hashes touching infrastructure paths
2613
+ * @param options.excludeScopes - Scopes to explicitly exclude from classification
2614
+ * @param options.includeScopes - Additional scopes to include as direct matches
2615
+ * @returns A ClassificationContext object
92
2616
  */
93
- function createSkippedResult(message) {
2617
+ function createClassificationContext(projectScopes, fileCommitHashes, options) {
94
2618
  return {
95
- status: 'skipped',
96
- message,
2619
+ projectScopes,
2620
+ fileCommitHashes,
2621
+ dependencyCommitMap: options?.dependencyCommitMap,
2622
+ infrastructureCommitHashes: options?.infrastructureCommitHashes,
2623
+ excludeScopes: options?.excludeScopes ?? DEFAULT_EXCLUDE_SCOPES,
2624
+ includeScopes: options?.includeScopes,
97
2625
  };
98
2626
  }
99
-
100
- const FETCH_REGISTRY_STEP_ID = 'fetch-registry';
101
2627
  /**
102
- * Creates the fetch-registry step.
103
- *
104
- * This step:
105
- * 1. Queries the registry for the latest published version
106
- * 2. Reads the current version from package.json
107
- * 3. Determines if this is a first release
2628
+ * Creates a modified conventional commit with scope handling based on classification.
108
2629
  *
109
- * State updates:
110
- * - publishedVersion: Latest version on registry (null if not published)
111
- * - currentVersion: Version from local package.json
112
- * - isFirstRelease: True if never published
2630
+ * For direct commits, the scope is removed (redundant in project changelog).
2631
+ * For indirect commits, the scope is preserved (provides context).
113
2632
  *
114
- * @returns A FlowStep that fetches registry information
2633
+ * @param classified - Commit with classification metadata determining scope display
2634
+ * @returns A conventional commit with appropriate scope handling
115
2635
  */
116
- function createFetchRegistryStep() {
117
- return createStep(FETCH_REGISTRY_STEP_ID, 'Fetch Registry Version', async (ctx) => {
118
- const { registry, tree, projectRoot, packageName, logger } = ctx;
119
- // Read local package.json for current version
120
- const packageJsonPath = `${projectRoot}/package.json`;
121
- let currentVersion = '0.0.0';
122
- try {
123
- const content = tree.read(packageJsonPath, 'utf-8');
124
- if (content) {
125
- const pkg = parse(content);
126
- currentVersion = pkg.version ?? '0.0.0';
127
- }
128
- }
129
- catch (error) {
130
- logger.warn(`Could not read package.json: ${error}`);
131
- }
132
- // Query registry for published version
133
- let publishedVersion = null;
134
- let isFirstRelease = true;
135
- try {
136
- publishedVersion = await registry.getLatestVersion(packageName);
137
- isFirstRelease = publishedVersion === null;
138
- }
139
- catch (error) {
140
- // Package might not exist yet, which is fine
141
- logger.debug(`Registry query failed (package may not exist): ${error}`);
142
- isFirstRelease = true;
143
- }
144
- const message = isFirstRelease ? `First release (local: ${currentVersion})` : `Published: ${publishedVersion}, Local: ${currentVersion}`;
2636
+ function toChangelogCommit(classified) {
2637
+ const { commit, preserveScope } = classified;
2638
+ if (!preserveScope && commit.scope) {
2639
+ // Remove the scope for direct commits
145
2640
  return {
146
- status: 'success',
147
- stateUpdates: {
148
- publishedVersion,
149
- currentVersion,
150
- isFirstRelease,
151
- },
152
- message,
2641
+ ...commit,
2642
+ scope: undefined,
2643
+ // Rebuild raw to reflect removed scope
2644
+ raw: rebuildRawWithoutScope(commit),
153
2645
  };
154
- });
2646
+ }
2647
+ return commit;
2648
+ }
2649
+ /**
2650
+ * Reconstructs a conventional commit message string without the scope portion.
2651
+ *
2652
+ * @param commit - The conventional commit to rebuild
2653
+ * @returns Reconstructed raw message with scope removed
2654
+ */
2655
+ function rebuildRawWithoutScope(commit) {
2656
+ const breaking = commit.breaking && !commit.breakingDescription ? '!' : '';
2657
+ const header = `${commit.type}${breaking}: ${commit.subject}`;
2658
+ if (!commit.body && commit.footers.length === 0) {
2659
+ return header;
2660
+ }
2661
+ let raw = header;
2662
+ if (commit.body) {
2663
+ raw += `\n\n${commit.body}`;
2664
+ }
2665
+ for (const footer of commit.footers) {
2666
+ raw += `\n${footer.key}${footer.separator}${footer.value}`;
2667
+ }
2668
+ return raw;
155
2669
  }
156
2670
 
157
2671
  /**
158
- * Safe copies of Error built-ins via factory functions.
2672
+ * Creates a matcher that checks if commit scope matches any of the given scopes.
159
2673
  *
160
- * Since constructors cannot be safely captured via Object.assign, this module
161
- * provides factory functions that use Reflect.construct internally.
2674
+ * @param scopes - Scopes to match against (case-insensitive)
2675
+ * @returns Matcher that returns true if scope matches
162
2676
  *
163
- * These references are captured at module initialization time to protect against
164
- * prototype pollution attacks. Import only what you need for tree-shaking.
2677
+ * @example
2678
+ * const matcher = scopeMatcher(['ci', 'build', 'tooling'])
2679
+ * matcher({ scope: 'CI', ... }) // true
2680
+ * matcher({ scope: 'feat', ... }) // false
2681
+ */
2682
+ function scopeMatcher(scopes) {
2683
+ const normalizedScopes = createSet(scopes.map((s) => s.toLowerCase()));
2684
+ return (ctx) => {
2685
+ if (!ctx.scope)
2686
+ return false;
2687
+ return normalizedScopes.has(ctx.scope.toLowerCase());
2688
+ };
2689
+ }
2690
+ /**
2691
+ * Creates a matcher that checks if commit scope starts with any of the given prefixes.
165
2692
  *
166
- * @module @hyperfrontend/immutable-api-utils/built-in-copy/error
2693
+ * @param prefixes - Scope prefixes to match (case-insensitive)
2694
+ * @returns Matcher that returns true if scope starts with any prefix
2695
+ *
2696
+ * @example
2697
+ * const matcher = scopePrefixMatcher(['tool-', 'infra-'])
2698
+ * matcher({ scope: 'tool-package', ... }) // true
2699
+ * matcher({ scope: 'lib-utils', ... }) // false
2700
+ */
2701
+ function scopePrefixMatcher(prefixes) {
2702
+ const normalizedPrefixes = prefixes.map((p) => p.toLowerCase());
2703
+ return (ctx) => {
2704
+ if (!ctx.scope)
2705
+ return false;
2706
+ const normalizedScope = ctx.scope.toLowerCase();
2707
+ return normalizedPrefixes.some((prefix) => normalizedScope.startsWith(prefix));
2708
+ };
2709
+ }
2710
+ /**
2711
+ * Combines matchers with OR logic - returns true if ANY matcher matches.
2712
+ *
2713
+ * @param matchers - Matchers to combine
2714
+ * @returns Combined matcher
2715
+ *
2716
+ * @example
2717
+ * const combined = anyOf(
2718
+ * scopeMatcher(['ci', 'build']),
2719
+ * messageMatcher(['[infra]']),
2720
+ * custom((ctx) => ctx.scope?.startsWith('tool-'))
2721
+ * )
167
2722
  */
168
- // Capture references at module initialization time
169
- const _Error = globalThis.Error;
170
- const _Reflect$2 = globalThis.Reflect;
2723
+ function anyOf(...matchers) {
2724
+ return (ctx) => matchers.some((matcher) => matcher(ctx));
2725
+ }
171
2726
  /**
172
- * (Safe copy) Creates a new Error using the captured Error constructor.
173
- * Use this instead of `new Error()`.
2727
+ * Matches common CI/CD scopes.
174
2728
  *
175
- * @param message - Optional error message.
176
- * @param options - Optional error options.
177
- * @returns A new Error instance.
2729
+ * Matches: ci, cd, build, pipeline, workflow, actions
2730
+ */
2731
+ const CI_SCOPE_MATCHER = scopeMatcher(['ci', 'cd', 'build', 'pipeline', 'workflow', 'actions']);
2732
+ /**
2733
+ * Matches common tooling/workspace scopes.
2734
+ *
2735
+ * Matches: tooling, workspace, monorepo, nx, root
2736
+ */
2737
+ const TOOLING_SCOPE_MATCHER = scopeMatcher(['tooling', 'workspace', 'monorepo', 'nx', 'root']);
2738
+ /**
2739
+ * Matches tool-prefixed scopes (e.g., tool-package, tool-scripts).
2740
+ */
2741
+ const TOOL_PREFIX_MATCHER = scopePrefixMatcher(['tool-']);
2742
+ /**
2743
+ * Combined matcher for common infrastructure patterns.
2744
+ *
2745
+ * Combines CI, tooling, and tool-prefix matchers.
2746
+ */
2747
+ anyOf(CI_SCOPE_MATCHER, TOOLING_SCOPE_MATCHER, TOOL_PREFIX_MATCHER);
2748
+ /**
2749
+ * Builds a combined matcher from infrastructure configuration.
2750
+ *
2751
+ * Combines scope-based matching with any custom matcher using OR logic.
2752
+ * Path-based matching is handled separately via git queries.
2753
+ *
2754
+ * @param config - Infrastructure configuration
2755
+ * @returns Combined matcher, or null if no matchers configured
2756
+ *
2757
+ * @example
2758
+ * const matcher = buildInfrastructureMatcher({
2759
+ * scopes: ['ci', 'build'],
2760
+ * matcher: (ctx) => ctx.scope?.startsWith('tool-')
2761
+ * })
178
2762
  */
179
- const createError = (message, options) => _Reflect$2.construct(_Error, [message, options]);
2763
+ function buildInfrastructureMatcher(config) {
2764
+ const matchers = [];
2765
+ // Add scope matcher if scopes configured
2766
+ if (config.scopes && config.scopes.length > 0) {
2767
+ matchers.push(scopeMatcher(config.scopes));
2768
+ }
2769
+ // Add custom matcher if provided
2770
+ if (config.matcher) {
2771
+ matchers.push(config.matcher);
2772
+ }
2773
+ // Return combined or null
2774
+ if (matchers.length === 0) {
2775
+ return null;
2776
+ }
2777
+ if (matchers.length === 1) {
2778
+ return matchers[0];
2779
+ }
2780
+ return anyOf(...matchers);
2781
+ }
2782
+ /**
2783
+ * Creates match context from a git commit.
2784
+ *
2785
+ * Extracts scope from conventional commit message if present.
2786
+ *
2787
+ * @param commit - Git commit to create context for
2788
+ * @param scope - Pre-parsed scope (optional, saves re-parsing)
2789
+ * @returns Match context for use with matchers
2790
+ */
2791
+ function createMatchContext(commit, scope) {
2792
+ return {
2793
+ commit,
2794
+ scope,
2795
+ subject: commit.subject,
2796
+ message: commit.message,
2797
+ };
2798
+ }
180
2799
 
181
2800
  /**
182
2801
  * Replaces all occurrences of a character in a string.
@@ -602,78 +3221,525 @@ function splitLines(message) {
602
3221
  return lines;
603
3222
  }
604
3223
 
3224
+ /**
3225
+ * Default scope filtering configuration.
3226
+ *
3227
+ * Uses DEFAULT_EXCLUDE_SCOPES from commits/classify to ensure consistency
3228
+ * between flow-level filtering and commit classification.
3229
+ */
3230
+ const DEFAULT_SCOPE_FILTERING_CONFIG = {
3231
+ strategy: 'hybrid',
3232
+ includeScopes: [],
3233
+ excludeScopes: DEFAULT_EXCLUDE_SCOPES,
3234
+ trackDependencyChanges: false,
3235
+ infrastructure: undefined,
3236
+ infrastructureMatcher: undefined,
3237
+ };
3238
+
605
3239
  const ANALYZE_COMMITS_STEP_ID = 'analyze-commits';
606
3240
  /**
607
3241
  * Creates the analyze-commits step.
608
3242
  *
609
3243
  * This step:
610
- * 1. Finds the last release tag for this package
611
- * 2. Gets all commits since that tag (or all commits if first release)
612
- * 3. Parses each commit using conventional commit format
613
- * 4. Filters to only release-worthy commits
3244
+ * 1. Uses publishedCommit from npm registry (set by fetch-registry step)
3245
+ * 2. Verifies the commit is reachable from current HEAD
3246
+ * 3. Gets all commits since that commit (or recent commits if first release/fallback)
3247
+ * 4. Parses each commit using conventional commit format
3248
+ * 5. Classifies commits based on scope filtering strategy
3249
+ * 6. Filters to only release-worthy commits that belong to this project
614
3250
  *
615
3251
  * State updates:
616
- * - lastReleaseTag: Tag name of last release (null if first release)
617
- * - commits: Array of parsed conventional commits
3252
+ * - effectiveBaseCommit: The verified base commit (null if fallback was used)
3253
+ * - commits: Array of parsed conventional commits (for backward compatibility)
3254
+ * - classificationResult: Full classification result with source attribution
618
3255
  *
619
3256
  * @returns A FlowStep that analyzes commits
620
3257
  */
621
3258
  function createAnalyzeCommitsStep() {
622
3259
  return createStep(ANALYZE_COMMITS_STEP_ID, 'Analyze Commits', async (ctx) => {
623
- const { git, projectName, packageName, config, logger, state } = ctx;
624
- // Find the last release tag for this package
625
- let lastReleaseTag = null;
626
- if (!state.isFirstRelease) {
627
- // Try to find a tag matching the package name pattern
628
- const tags = git.getTagsForPackage(packageName);
629
- if (tags.length > 0) {
630
- // Tags are returned in reverse chronological order
631
- lastReleaseTag = tags[0].name;
632
- logger.debug(`Found last release tag: ${lastReleaseTag}`);
3260
+ const { git, projectName, projectRoot, packageName, workspaceRoot, config, logger, state } = ctx;
3261
+ // Use publishedCommit from registry (set by fetch-registry step)
3262
+ const { publishedCommit, isFirstRelease } = state;
3263
+ let rawCommits;
3264
+ let effectiveBaseCommit = null;
3265
+ if (publishedCommit && !isFirstRelease) {
3266
+ // CRITICAL: Verify the commit exists and is reachable from HEAD
3267
+ if (git.commitReachableFromHead(publishedCommit)) {
3268
+ rawCommits = git.getCommitsSince(publishedCommit);
3269
+ effectiveBaseCommit = publishedCommit;
3270
+ logger.debug(`Found ${rawCommits.length} commits since ${publishedCommit.slice(0, 7)}`);
633
3271
  }
634
3272
  else {
635
- // Try with project name format
636
- const projectTags = git.getTagsForPackage(projectName);
637
- if (projectTags.length > 0) {
638
- lastReleaseTag = projectTags[0].name;
639
- logger.debug(`Found last release tag (project format): ${lastReleaseTag}`);
640
- }
3273
+ // GRACEFUL DEGRADATION: Commit not in history (rebase/force push occurred)
3274
+ logger.warn(`Published commit ${publishedCommit.slice(0, 7)} not found in history. ` +
3275
+ `This may indicate a rebase or force push occurred after publishing v${state.publishedVersion}. ` +
3276
+ `Falling back to recent commit analysis.`);
3277
+ rawCommits = git.getCommitLog({ maxCount: 100 });
3278
+ // effectiveBaseCommit stays null - no compare URL will be generated
641
3279
  }
642
3280
  }
643
- // Get commits
644
- let rawCommits;
645
- if (lastReleaseTag) {
646
- rawCommits = git.getCommitsSince(lastReleaseTag);
647
- logger.debug(`Found ${rawCommits.length} commits since ${lastReleaseTag}`);
648
- }
649
3281
  else {
650
- // First release - get all commits (limit to recent for performance)
3282
+ // First release or no published version
651
3283
  rawCommits = git.getCommitLog({ maxCount: 100 });
652
3284
  logger.debug(`First release - analyzing up to ${rawCommits.length} commits`);
653
3285
  }
654
- // Parse commits using conventional commit format
655
- const commits = [];
3286
+ // Get scope filtering configuration
3287
+ const scopeFilteringConfig = {
3288
+ ...DEFAULT_SCOPE_FILTERING_CONFIG,
3289
+ ...config.scopeFiltering,
3290
+ };
3291
+ const strategy = resolveStrategy(scopeFilteringConfig.strategy ?? 'hybrid', rawCommits);
3292
+ // Parse commits with conventional commit format
656
3293
  const releaseTypes = config.releaseTypes ?? ['feat', 'fix', 'perf', 'revert'];
3294
+ const parsedCommits = [];
657
3295
  for (const rawCommit of rawCommits) {
658
3296
  const parsed = parseConventionalCommit(rawCommit.message);
659
3297
  if (parsed.type && releaseTypes.includes(parsed.type)) {
660
- commits.push(parsed);
3298
+ parsedCommits.push({
3299
+ commit: parsed,
3300
+ raw: {
3301
+ hash: rawCommit.hash,
3302
+ shortHash: rawCommit.hash.slice(0, 7),
3303
+ message: rawCommit.message,
3304
+ subject: parsed.subject ?? rawCommit.message.split('\n')[0],
3305
+ body: parsed.body ?? '',
3306
+ authorName: '',
3307
+ authorEmail: '',
3308
+ authorDate: '',
3309
+ committerName: '',
3310
+ committerEmail: '',
3311
+ commitDate: '',
3312
+ parents: [],
3313
+ refs: [],
3314
+ },
3315
+ });
3316
+ }
3317
+ }
3318
+ // Build file commit hashes for hybrid/file-only strategies
3319
+ let fileCommitHashes = createSet();
3320
+ if (strategy === 'hybrid' || strategy === 'file-only') {
3321
+ // Get commits that touched project files using path filter
3322
+ const relativePath = getRelativePath(workspaceRoot, projectRoot);
3323
+ const pathFilteredCommits = effectiveBaseCommit
3324
+ ? git.getCommitsSince(effectiveBaseCommit, { path: relativePath })
3325
+ : git.getCommitLog({ maxCount: 100, path: relativePath });
3326
+ fileCommitHashes = createSet(pathFilteredCommits.map((c) => c.hash));
3327
+ logger.debug(`Found ${fileCommitHashes.size} commits touching ${relativePath}`);
3328
+ }
3329
+ // Derive project scopes
3330
+ const projectScopes = deriveProjectScopes({
3331
+ projectName,
3332
+ packageName,
3333
+ additionalScopes: scopeFilteringConfig.includeScopes,
3334
+ });
3335
+ logger.debug(`Project scopes: ${projectScopes.join(', ')}`);
3336
+ // Build infrastructure commit hashes for file-based infrastructure detection
3337
+ const infrastructureCommitHashes = buildInfrastructureCommitHashes(git, effectiveBaseCommit, rawCommits, parsedCommits, scopeFilteringConfig, logger);
3338
+ // Build dependency commit map if tracking is enabled (Phase 4)
3339
+ let dependencyCommitMap;
3340
+ if (scopeFilteringConfig.trackDependencyChanges) {
3341
+ dependencyCommitMap = buildDependencyCommitMap(git, workspaceRoot, projectName, effectiveBaseCommit, logger);
3342
+ }
3343
+ // Create classification context
3344
+ const classificationContext = createClassificationContext(projectScopes, fileCommitHashes, {
3345
+ excludeScopes: scopeFilteringConfig.excludeScopes,
3346
+ includeScopes: scopeFilteringConfig.includeScopes,
3347
+ infrastructureCommitHashes,
3348
+ dependencyCommitMap,
3349
+ });
3350
+ // Classify commits
3351
+ const classificationResult = classifyCommits(parsedCommits, classificationContext);
3352
+ // Apply strategy-specific filtering
3353
+ const includedCommits = applyStrategyFilter(classificationResult.included, strategy);
3354
+ // Extract conventional commits for backward compatibility
3355
+ // Use toChangelogCommit to properly handle scope based on classification
3356
+ const commits = includedCommits.map((c) => toChangelogCommit(c));
3357
+ // Build message with classification summary
3358
+ const { summary } = classificationResult;
3359
+ const message = buildSummaryMessage(commits.length, rawCommits.length, summary, strategy);
3360
+ logger.debug(`Classification breakdown: direct-scope=${summary.bySource['direct-scope']}, ` +
3361
+ `direct-file=${summary.bySource['direct-file']}, unscoped-file=${summary.bySource['unscoped-file']}, ` +
3362
+ `excluded=${summary.bySource['excluded']}`);
3363
+ return {
3364
+ status: 'success',
3365
+ stateUpdates: {
3366
+ effectiveBaseCommit,
3367
+ commits,
3368
+ classificationResult,
3369
+ },
3370
+ message,
3371
+ };
3372
+ }, {
3373
+ dependsOn: ['fetch-registry'],
3374
+ });
3375
+ }
3376
+ /**
3377
+ * Resolves the filtering strategy, handling 'inferred' by analyzing commits.
3378
+ *
3379
+ * @param strategy - The configured scope filtering strategy
3380
+ * @param commits - The commits to analyze for strategy inference
3381
+ * @returns The resolved strategy (never 'inferred')
3382
+ */
3383
+ function resolveStrategy(strategy, commits) {
3384
+ if (strategy !== 'inferred') {
3385
+ return strategy;
3386
+ }
3387
+ // Infer strategy from commit history
3388
+ // Count commits with conventional scopes
3389
+ let scopedCount = 0;
3390
+ for (const commit of commits) {
3391
+ const parsed = parseConventionalCommit(commit.message);
3392
+ if (parsed.scope) {
3393
+ scopedCount++;
3394
+ }
3395
+ }
3396
+ const scopeRatio = commits.length > 0 ? scopedCount / commits.length : 0;
3397
+ // If >70% of commits have scopes, scope-only is viable
3398
+ // If <30% have scopes, file-only is better
3399
+ // Otherwise, use hybrid
3400
+ if (scopeRatio > 0.7) {
3401
+ return 'scope-only';
3402
+ }
3403
+ else if (scopeRatio < 0.3) {
3404
+ return 'file-only';
3405
+ }
3406
+ return 'hybrid';
3407
+ }
3408
+ /**
3409
+ * Applies strategy-specific filtering to classified commits.
3410
+ *
3411
+ * @param commits - The classified commits to filter
3412
+ * @param strategy - The resolved filtering strategy to apply
3413
+ * @returns Filtered commits based on the strategy
3414
+ */
3415
+ function applyStrategyFilter(commits, strategy) {
3416
+ switch (strategy) {
3417
+ case 'scope-only':
3418
+ // Only include direct-scope commits
3419
+ return commits.filter((c) => c.source === 'direct-scope');
3420
+ case 'file-only':
3421
+ // Only include file-based commits (direct-file, unscoped-file)
3422
+ return commits.filter((c) => c.source === 'direct-file' || c.source === 'unscoped-file');
3423
+ case 'hybrid':
3424
+ default:
3425
+ // Include all non-excluded commits (already filtered in classifyCommits)
3426
+ return commits;
3427
+ }
3428
+ }
3429
+ /**
3430
+ * Gets the relative path from workspace root to project root.
3431
+ *
3432
+ * @param workspaceRoot - The absolute path to the workspace root
3433
+ * @param projectRoot - The absolute path to the project root
3434
+ * @returns The relative path from workspace to project
3435
+ */
3436
+ function getRelativePath(workspaceRoot, projectRoot) {
3437
+ if (projectRoot.startsWith(workspaceRoot)) {
3438
+ return projectRoot.slice(workspaceRoot.length).replace(/^\//, '');
3439
+ }
3440
+ return projectRoot;
3441
+ }
3442
+ /**
3443
+ * Builds a summary message for the step result.
3444
+ *
3445
+ * @param includedCount - Number of commits included in the release
3446
+ * @param totalCount - Total number of commits analyzed
3447
+ * @param summary - Classification summary object
3448
+ * @param summary.bySource - Count of commits by source type
3449
+ * @param strategy - The filtering strategy used
3450
+ * @returns A human-readable summary message
3451
+ */
3452
+ function buildSummaryMessage(includedCount, totalCount, summary, strategy) {
3453
+ if (includedCount === 0) {
3454
+ return `No releasable commits found for this project (${totalCount} total, strategy: ${strategy})`;
3455
+ }
3456
+ const parts = [`Found ${includedCount} releasable commits`, `(${totalCount} total`, `strategy: ${strategy})`];
3457
+ return parts.join(' ');
3458
+ }
3459
+ /**
3460
+ * Builds a set of commit hashes that touched infrastructure paths or match infrastructure criteria.
3461
+ *
3462
+ * Supports multiple detection methods combined with OR logic:
3463
+ * 1. Path-based: Commits touching configured infrastructure paths (via git)
3464
+ * 2. Scope-based: Commits with scopes matching infrastructure.scopes
3465
+ * 3. Custom matcher: User-provided matching logic
3466
+ *
3467
+ * @param git - Git client for querying commits by path
3468
+ * @param baseCommit - Base commit hash for commit range (null for first release/fallback)
3469
+ * @param rawCommits - All raw commits being analyzed
3470
+ * @param parsedCommits - Parsed commits with conventional commit data
3471
+ * @param config - Scope filtering configuration
3472
+ * @param logger - Logger with debug method for output
3473
+ * @param logger.debug - Debug logging function
3474
+ * @returns Set of commit hashes classified as infrastructure
3475
+ */
3476
+ function buildInfrastructureCommitHashes(git, baseCommit, rawCommits, parsedCommits, config, logger) {
3477
+ // Collect all infrastructure commit hashes
3478
+ let infraHashes = createSet();
3479
+ // Method 1: Path-based detection (query git for commits touching infra paths)
3480
+ const infraPaths = config.infrastructure?.paths ?? [];
3481
+ if (infraPaths.length > 0) {
3482
+ for (const infraPath of infraPaths) {
3483
+ const pathCommits = baseCommit
3484
+ ? git.getCommitsSince(baseCommit, { path: infraPath })
3485
+ : git.getCommitLog({ maxCount: 100, path: infraPath });
3486
+ for (const commit of pathCommits) {
3487
+ infraHashes = infraHashes.add(commit.hash);
3488
+ }
3489
+ }
3490
+ logger.debug(`Found ${infraHashes.size} commits touching infrastructure paths: ${infraPaths.join(', ')}`);
3491
+ }
3492
+ // Method 2 & 3: Scope-based and custom matcher detection
3493
+ // Build a combined matcher from infrastructure config and/or custom matcher
3494
+ const configMatcher = config.infrastructure ? buildInfrastructureMatcher(config.infrastructure) : null;
3495
+ const customMatcher = config.infrastructureMatcher;
3496
+ const combinedMatcher = combineMatcher(configMatcher, customMatcher);
3497
+ if (combinedMatcher) {
3498
+ // Build a lookup for parsed commits by hash
3499
+ let parsedByHash = createMap();
3500
+ for (const parsed of parsedCommits) {
3501
+ parsedByHash = parsedByHash.set(parsed.raw.hash, parsed);
3502
+ }
3503
+ // Evaluate each raw commit against the matcher
3504
+ for (const rawCommit of rawCommits) {
3505
+ // Skip if already matched by path
3506
+ if (infraHashes.has(rawCommit.hash))
3507
+ continue;
3508
+ // Get parsed scope if available
3509
+ const parsed = parsedByHash.get(rawCommit.hash);
3510
+ const scope = parsed?.commit.scope;
3511
+ // Create match context and evaluate
3512
+ const context = createMatchContext(rawCommit, scope);
3513
+ if (combinedMatcher(context)) {
3514
+ infraHashes = infraHashes.add(rawCommit.hash);
3515
+ }
3516
+ }
3517
+ logger.debug(`Infrastructure matcher found ${infraHashes.size} total commits`);
3518
+ }
3519
+ // Return undefined if no infrastructure detection configured
3520
+ if (infraHashes.size === 0 && infraPaths.length === 0 && !combinedMatcher) {
3521
+ return undefined;
3522
+ }
3523
+ return infraHashes;
3524
+ }
3525
+ /**
3526
+ * Combines two optional matchers into one using OR logic.
3527
+ *
3528
+ * @param a - First matcher (may be null)
3529
+ * @param b - Second matcher (may be undefined)
3530
+ * @returns Combined matcher or null if neither provided
3531
+ */
3532
+ function combineMatcher(a, b) {
3533
+ if (a && b) {
3534
+ return (ctx) => a(ctx) || b(ctx);
3535
+ }
3536
+ return a ?? b ?? null;
3537
+ }
3538
+ /**
3539
+ * Builds a map of dependency project names to the commit hashes that touched them.
3540
+ *
3541
+ * This enables accurate indirect-dependency classification by verifying that:
3542
+ * 1. A commit's scope matches a dependency name
3543
+ * 2. The commit actually touched that dependency's files (hash in set)
3544
+ *
3545
+ * Uses lib-project-scope for dependency discovery, avoiding hard NX dependency.
3546
+ *
3547
+ * @param git - Git client for querying commits by path
3548
+ * @param workspaceRoot - Absolute path to workspace root
3549
+ * @param projectName - Name of the project being versioned
3550
+ * @param baseCommit - Base commit hash for commit range (null for first release/fallback)
3551
+ * @param logger - Logger with debug method for output
3552
+ * @param logger.debug - Debug logging function
3553
+ * @returns Map of dependency names to commit hashes touching that dependency
3554
+ */
3555
+ function buildDependencyCommitMap(git, workspaceRoot, projectName, baseCommit, logger) {
3556
+ let dependencyMap = createMap();
3557
+ try {
3558
+ // Discover all projects in workspace using lib-project-scope
3559
+ // This gracefully handles NX and non-NX workspaces
3560
+ const projects = discoverNxProjects(workspaceRoot);
3561
+ const projectGraph = buildSimpleProjectGraph(workspaceRoot, projects);
3562
+ // Get dependencies for the current project
3563
+ const projectDeps = projectGraph.dependencies[projectName] ?? [];
3564
+ if (projectDeps.length === 0) {
3565
+ logger.debug(`No dependencies found for project: ${projectName}`);
3566
+ return dependencyMap;
3567
+ }
3568
+ logger.debug(`Found ${projectDeps.length} dependencies for ${projectName}: ${projectDeps.map((d) => d.target).join(', ')}`);
3569
+ // For each dependency, find commits that touched its files
3570
+ for (const dep of projectDeps) {
3571
+ const depNode = projectGraph.nodes[dep.target];
3572
+ if (!depNode?.data?.root) {
3573
+ logger.debug(`Skipping dependency ${dep.target}: no root path found`);
3574
+ continue;
661
3575
  }
3576
+ const depRoot = depNode.data.root;
3577
+ // Query git for commits touching this dependency's path
3578
+ const depCommits = baseCommit
3579
+ ? git.getCommitsSince(baseCommit, { path: depRoot })
3580
+ : git.getCommitLog({ maxCount: 100, path: depRoot });
3581
+ if (depCommits.length > 0) {
3582
+ const hashSet = createSet(depCommits.map((c) => c.hash));
3583
+ dependencyMap = dependencyMap.set(dep.target, hashSet);
3584
+ logger.debug(`Dependency ${dep.target}: ${depCommits.length} commits at ${depRoot}`);
3585
+ }
3586
+ }
3587
+ }
3588
+ catch (error) {
3589
+ // Graceful degradation: if project discovery fails, return empty map
3590
+ // This allows versioning to proceed without dependency tracking
3591
+ const message = error instanceof Error ? error.message : String(error);
3592
+ logger.debug(`Failed to build dependency map: ${message}`);
3593
+ }
3594
+ return dependencyMap;
3595
+ }
3596
+
3597
+ /**
3598
+ * Safe copies of Number built-in methods and constants.
3599
+ *
3600
+ * These references are captured at module initialization time to protect against
3601
+ * prototype pollution attacks. Import only what you need for tree-shaking.
3602
+ *
3603
+ * @module @hyperfrontend/immutable-api-utils/built-in-copy/number
3604
+ */
3605
+ // Capture references at module initialization time
3606
+ const _parseInt = globalThis.parseInt;
3607
+ const _isNaN = globalThis.isNaN;
3608
+ // ============================================================================
3609
+ // Parsing
3610
+ // ============================================================================
3611
+ /**
3612
+ * (Safe copy) Parses a string and returns an integer.
3613
+ */
3614
+ const parseInt = _parseInt;
3615
+ // ============================================================================
3616
+ // Global Type Checking (legacy, less strict)
3617
+ // ============================================================================
3618
+ /**
3619
+ * (Safe copy) Global isNaN function (coerces to number first, less strict than Number.isNaN).
3620
+ */
3621
+ const globalIsNaN = _isNaN;
3622
+
3623
+ /**
3624
+ * Compares two semantic versions.
3625
+ *
3626
+ * @param a - First version
3627
+ * @param b - Second version
3628
+ * @returns -1 if a < b, 0 if a == b, 1 if a > b
3629
+ *
3630
+ * @example
3631
+ * compare(parseVersion('1.0.0'), parseVersion('2.0.0')) // -1
3632
+ * compare(parseVersion('1.0.0'), parseVersion('1.0.0')) // 0
3633
+ * compare(parseVersion('2.0.0'), parseVersion('1.0.0')) // 1
3634
+ */
3635
+ function compare(a, b) {
3636
+ // Compare major, minor, patch
3637
+ if (a.major !== b.major) {
3638
+ return a.major < b.major ? -1 : 1;
3639
+ }
3640
+ if (a.minor !== b.minor) {
3641
+ return a.minor < b.minor ? -1 : 1;
3642
+ }
3643
+ if (a.patch !== b.patch) {
3644
+ return a.patch < b.patch ? -1 : 1;
3645
+ }
3646
+ // Compare prerelease
3647
+ // Version with prerelease has lower precedence than release
3648
+ if (a.prerelease.length === 0 && b.prerelease.length > 0) {
3649
+ return 1; // a is release, b is prerelease -> a > b
3650
+ }
3651
+ if (a.prerelease.length > 0 && b.prerelease.length === 0) {
3652
+ return -1; // a is prerelease, b is release -> a < b
3653
+ }
3654
+ // Both have prerelease - compare identifiers
3655
+ const maxLen = max(a.prerelease.length, b.prerelease.length);
3656
+ for (let i = 0; i < maxLen; i++) {
3657
+ const aId = a.prerelease[i];
3658
+ const bId = b.prerelease[i];
3659
+ // Shorter prerelease array has lower precedence
3660
+ if (aId === undefined && bId !== undefined) {
3661
+ return -1;
3662
+ }
3663
+ if (aId !== undefined && bId === undefined) {
3664
+ return 1;
3665
+ }
3666
+ if (aId === undefined || bId === undefined) {
3667
+ continue;
3668
+ }
3669
+ // Compare identifiers
3670
+ const cmp = compareIdentifiers(aId, bId);
3671
+ if (cmp !== 0) {
3672
+ return cmp;
3673
+ }
3674
+ }
3675
+ return 0;
3676
+ }
3677
+ /**
3678
+ * Checks if a > b.
3679
+ *
3680
+ * @param a - First version to compare
3681
+ * @param b - Second version to compare
3682
+ * @returns True if a is greater than b
3683
+ */
3684
+ function gt(a, b) {
3685
+ return compare(a, b) === 1;
3686
+ }
3687
+ // ============================================================================
3688
+ // Internal helpers
3689
+ // ============================================================================
3690
+ /**
3691
+ * Compares two prerelease identifiers.
3692
+ * Numeric identifiers have lower precedence than alphanumeric.
3693
+ * Numeric identifiers are compared numerically.
3694
+ * Alphanumeric identifiers are compared lexically.
3695
+ *
3696
+ * @param a - First prerelease identifier
3697
+ * @param b - Second prerelease identifier
3698
+ * @returns -1 if a < b, 0 if equal, 1 if a > b
3699
+ */
3700
+ function compareIdentifiers(a, b) {
3701
+ const aIsNumeric = isNumeric(a);
3702
+ const bIsNumeric = isNumeric(b);
3703
+ // Numeric identifiers have lower precedence
3704
+ if (aIsNumeric && !bIsNumeric) {
3705
+ return -1;
3706
+ }
3707
+ if (!aIsNumeric && bIsNumeric) {
3708
+ return 1;
3709
+ }
3710
+ // Both numeric - compare as numbers
3711
+ if (aIsNumeric && bIsNumeric) {
3712
+ const aNum = parseInt(a, 10);
3713
+ const bNum = parseInt(b, 10);
3714
+ if (aNum < bNum)
3715
+ return -1;
3716
+ if (aNum > bNum)
3717
+ return 1;
3718
+ return 0;
3719
+ }
3720
+ // Both alphanumeric - compare lexically
3721
+ if (a < b)
3722
+ return -1;
3723
+ if (a > b)
3724
+ return 1;
3725
+ return 0;
3726
+ }
3727
+ /**
3728
+ * Checks if a string consists only of digits.
3729
+ *
3730
+ * @param str - String to check for numeric content
3731
+ * @returns True if string contains only digits
3732
+ */
3733
+ function isNumeric(str) {
3734
+ if (str.length === 0)
3735
+ return false;
3736
+ for (let i = 0; i < str.length; i++) {
3737
+ const code = str.charCodeAt(i);
3738
+ if (code < 48 || code > 57) {
3739
+ return false;
662
3740
  }
663
- const message = commits.length > 0
664
- ? `Found ${commits.length} releasable commits (${rawCommits.length} total)`
665
- : `No releasable commits found (${rawCommits.length} total)`;
666
- return {
667
- status: 'success',
668
- stateUpdates: {
669
- lastReleaseTag,
670
- commits,
671
- },
672
- message,
673
- };
674
- }, {
675
- dependsOn: ['fetch-registry'],
676
- });
3741
+ }
3742
+ return true;
677
3743
  }
678
3744
 
679
3745
  /**
@@ -693,32 +3759,6 @@ function format(version) {
693
3759
  return result;
694
3760
  }
695
3761
 
696
- /**
697
- * Safe copies of Number built-in methods and constants.
698
- *
699
- * These references are captured at module initialization time to protect against
700
- * prototype pollution attacks. Import only what you need for tree-shaking.
701
- *
702
- * @module @hyperfrontend/immutable-api-utils/built-in-copy/number
703
- */
704
- // Capture references at module initialization time
705
- const _parseInt = globalThis.parseInt;
706
- const _isNaN = globalThis.isNaN;
707
- // ============================================================================
708
- // Parsing
709
- // ============================================================================
710
- /**
711
- * (Safe copy) Parses a string and returns an integer.
712
- */
713
- const parseInt = _parseInt;
714
- // ============================================================================
715
- // Global Type Checking (legacy, less strict)
716
- // ============================================================================
717
- /**
718
- * (Safe copy) Global isNaN function (coerces to number first, less strict than Number.isNaN).
719
- */
720
- const globalIsNaN = _isNaN;
721
-
722
3762
  /**
723
3763
  * Creates a new SemVer object.
724
3764
  *
@@ -1196,7 +4236,7 @@ function createCalculateBumpStep() {
1196
4236
  message: 'No version bump needed',
1197
4237
  };
1198
4238
  }
1199
- // Calculate next version
4239
+ // Parse versions for comparison
1200
4240
  const current = parseVersion(currentVersion ?? '0.0.0');
1201
4241
  if (!current.success || !current.version) {
1202
4242
  return {
@@ -1205,6 +4245,27 @@ function createCalculateBumpStep() {
1205
4245
  message: `Could not parse current version: ${currentVersion}`,
1206
4246
  };
1207
4247
  }
4248
+ const { publishedVersion } = state;
4249
+ const published = parseVersion(publishedVersion ?? '0.0.0');
4250
+ // Detect pending publication state: currentVersion > publishedVersion
4251
+ // This means a previous bump happened but was never published
4252
+ const isPendingPublication = published.success && published.version && publishedVersion != null && gt(current.version, published.version);
4253
+ if (isPendingPublication && published.version) {
4254
+ // ALWAYS calculate from publishedVersion - commits may have changed
4255
+ const next = increment(published.version, bumpType);
4256
+ const nextVersion = format(next);
4257
+ logger.info(`Pending publication detected: recalculating from ${publishedVersion} → ${nextVersion}`);
4258
+ return {
4259
+ status: 'success',
4260
+ stateUpdates: {
4261
+ bumpType,
4262
+ nextVersion,
4263
+ isPendingPublication: true,
4264
+ },
4265
+ message: `${bumpType} bump (pending): ${publishedVersion} → ${nextVersion}`,
4266
+ };
4267
+ }
4268
+ // Normal path: increment from currentVersion
1208
4269
  const next = increment(current.version, bumpType);
1209
4270
  const nextVersion = format(next);
1210
4271
  return {
@@ -1259,24 +4320,6 @@ function createCheckIdempotencyStep() {
1259
4320
  });
1260
4321
  }
1261
4322
 
1262
- /**
1263
- * Safe copies of Date built-in via factory function and static methods.
1264
- *
1265
- * Since constructors cannot be safely captured via Object.assign, this module
1266
- * provides a factory function that uses Reflect.construct internally.
1267
- *
1268
- * These references are captured at module initialization time to protect against
1269
- * prototype pollution attacks. Import only what you need for tree-shaking.
1270
- *
1271
- * @module @hyperfrontend/immutable-api-utils/built-in-copy/date
1272
- */
1273
- // Capture references at module initialization time
1274
- const _Date = globalThis.Date;
1275
- const _Reflect$1 = globalThis.Reflect;
1276
- function createDate(...args) {
1277
- return _Reflect$1.construct(_Date, args);
1278
- }
1279
-
1280
4323
  /**
1281
4324
  * Creates a new changelog item.
1282
4325
  *
@@ -1291,6 +4334,8 @@ function createChangelogItem(description, options) {
1291
4334
  commits: options?.commits ?? [],
1292
4335
  references: options?.references ?? [],
1293
4336
  breaking: options?.breaking ?? false,
4337
+ source: options?.source,
4338
+ indirect: options?.indirect,
1294
4339
  };
1295
4340
  }
1296
4341
  /**
@@ -1414,96 +4459,6 @@ function getSectionType(heading) {
1414
4459
  return SECTION_TYPE_MAP[normalized] ?? 'other';
1415
4460
  }
1416
4461
 
1417
- /**
1418
- * Safe copies of Map built-in via factory function.
1419
- *
1420
- * Since constructors cannot be safely captured via Object.assign, this module
1421
- * provides a factory function that uses Reflect.construct internally.
1422
- *
1423
- * These references are captured at module initialization time to protect against
1424
- * prototype pollution attacks. Import only what you need for tree-shaking.
1425
- *
1426
- * @module @hyperfrontend/immutable-api-utils/built-in-copy/map
1427
- */
1428
- // Capture references at module initialization time
1429
- const _Map = globalThis.Map;
1430
- const _Reflect = globalThis.Reflect;
1431
- /**
1432
- * (Safe copy) Creates a new Map using the captured Map constructor.
1433
- * Use this instead of `new Map()`.
1434
- *
1435
- * @param iterable - Optional iterable of key-value pairs.
1436
- * @returns A new Map instance.
1437
- */
1438
- const createMap = (iterable) => _Reflect.construct(_Map, iterable ? [iterable] : []);
1439
-
1440
- /**
1441
- * Safe copies of Object built-in methods.
1442
- *
1443
- * These references are captured at module initialization time to protect against
1444
- * prototype pollution attacks. Import only what you need for tree-shaking.
1445
- *
1446
- * @module @hyperfrontend/immutable-api-utils/built-in-copy/object
1447
- */
1448
- // Capture references at module initialization time
1449
- const _Object = globalThis.Object;
1450
- /**
1451
- * (Safe copy) Returns an array of key/values of the enumerable own properties of an object.
1452
- */
1453
- const entries = _Object.entries;
1454
-
1455
- /**
1456
- * Safe copies of URL built-ins via factory functions.
1457
- *
1458
- * Provides safe references to URL and URLSearchParams.
1459
- * These references are captured at module initialization time to protect against
1460
- * prototype pollution attacks. Import only what you need for tree-shaking.
1461
- *
1462
- * @module @hyperfrontend/immutable-api-utils/built-in-copy/url
1463
- */
1464
- // Capture references at module initialization time
1465
- const _URL = globalThis.URL;
1466
- /**
1467
- * (Safe copy) Creates an object URL for the given object.
1468
- * Use this instead of `URL.createObjectURL()`.
1469
- *
1470
- * Note: This is a browser-only API. In Node.js environments, this will throw.
1471
- */
1472
- typeof _URL.createObjectURL === 'function'
1473
- ? _URL.createObjectURL.bind(_URL)
1474
- : () => {
1475
- throw new Error('URL.createObjectURL is not available in this environment');
1476
- };
1477
- /**
1478
- * (Safe copy) Revokes an object URL previously created with createObjectURL.
1479
- * Use this instead of `URL.revokeObjectURL()`.
1480
- *
1481
- * Note: This is a browser-only API. In Node.js environments, this will throw.
1482
- */
1483
- typeof _URL.revokeObjectURL === 'function'
1484
- ? _URL.revokeObjectURL.bind(_URL)
1485
- : () => {
1486
- throw new Error('URL.revokeObjectURL is not available in this environment');
1487
- };
1488
-
1489
- /**
1490
- * Safe copies of Math built-in methods.
1491
- *
1492
- * These references are captured at module initialization time to protect against
1493
- * prototype pollution attacks. Import only what you need for tree-shaking.
1494
- *
1495
- * @module @hyperfrontend/immutable-api-utils/built-in-copy/math
1496
- */
1497
- // Capture references at module initialization time
1498
- const _Math = globalThis.Math;
1499
- // ============================================================================
1500
- // Min/Max
1501
- // ============================================================================
1502
- /**
1503
- * (Safe copy) Returns the larger of zero or more numbers.
1504
- */
1505
- const max = _Math.max;
1506
-
1507
4462
  /**
1508
4463
  * Line Parser
1509
4464
  *
@@ -1559,6 +4514,25 @@ function parseVersionFromHeading(heading) {
1559
4514
  if (trimmed[pos] === ']') {
1560
4515
  pos++;
1561
4516
  }
4517
+ // Handle markdown link format [version](url) - jscutlery/semver style
4518
+ // This extracts the compare URL from patterns like [0.0.4](https://github.com/.../compare/...)
4519
+ if (trimmed[pos] === '(') {
4520
+ const urlStart = pos + 1;
4521
+ let depth = 1;
4522
+ pos++;
4523
+ // Find matching closing parenthesis (handles nested parens in URLs)
4524
+ while (pos < trimmed.length && depth > 0) {
4525
+ if (trimmed[pos] === '(')
4526
+ depth++;
4527
+ else if (trimmed[pos] === ')')
4528
+ depth--;
4529
+ pos++;
4530
+ }
4531
+ // Extract URL if we found the closing paren
4532
+ if (depth === 0) {
4533
+ compareUrl = trimmed.slice(urlStart, pos - 1);
4534
+ }
4535
+ }
1562
4536
  // Skip whitespace and separator
1563
4537
  while (pos < trimmed.length && (trimmed[pos] === ' ' || trimmed[pos] === '-' || trimmed[pos] === '–')) {
1564
4538
  pos++;
@@ -1575,8 +4549,8 @@ function parseVersionFromHeading(heading) {
1575
4549
  while (pos < trimmed.length && trimmed[pos] === ' ') {
1576
4550
  pos++;
1577
4551
  }
1578
- // Check for link at end: [compare](url)
1579
- if (pos < trimmed.length) {
4552
+ // Check for link at end: [compare](url) - only if no URL was already extracted
4553
+ if (pos < trimmed.length && !compareUrl) {
1580
4554
  const linkMatch = extractLink(trimmed.slice(pos));
1581
4555
  if (linkMatch?.url) {
1582
4556
  compareUrl = linkMatch.url;
@@ -2973,20 +5947,28 @@ function serializeIssueRef(ref) {
2973
5947
  * ```
2974
5948
  */
2975
5949
  function addEntry(changelog, entry, options) {
5950
+ const position = options?.position ?? 'start';
5951
+ const replaceExisting = options?.replaceExisting ?? false;
5952
+ const updateMetadata = options?.updateMetadata ?? false;
2976
5953
  // Check for existing entry
2977
5954
  const existingIndex = changelog.entries.findIndex((e) => e.version === entry.version);
2978
- if (existingIndex !== -1 && true) {
5955
+ if (existingIndex !== -1 && !replaceExisting) {
2979
5956
  throw createError(`Entry with version "${entry.version}" already exists. Use replaceExisting: true to replace.`);
2980
5957
  }
2981
5958
  let newEntries;
2982
- {
5959
+ if (existingIndex !== -1 && replaceExisting) {
5960
+ // Replace existing entry
5961
+ newEntries = [...changelog.entries];
5962
+ newEntries[existingIndex] = entry;
5963
+ }
5964
+ else {
2983
5965
  // Add new entry
2984
- const insertIndex = 0 ;
5966
+ const insertIndex = position === 'start' ? 0 : position === 'end' ? changelog.entries.length : position;
2985
5967
  newEntries = [...changelog.entries];
2986
5968
  newEntries.splice(insertIndex, 0, entry);
2987
5969
  }
2988
5970
  // Build new metadata if requested
2989
- const metadata = changelog.metadata;
5971
+ const metadata = updateMetadata ? { ...changelog.metadata, warnings: [] } : changelog.metadata;
2990
5972
  return {
2991
5973
  ...changelog,
2992
5974
  entries: newEntries,
@@ -2994,6 +5976,144 @@ function addEntry(changelog, entry, options) {
2994
5976
  };
2995
5977
  }
2996
5978
 
5979
+ /**
5980
+ * Changelog Entry Removal
5981
+ *
5982
+ * Functions for removing entries from a changelog.
5983
+ */
5984
+ /**
5985
+ * Removes multiple entries from a changelog.
5986
+ *
5987
+ * @param changelog - The changelog to remove from
5988
+ * @param versions - The versions to remove
5989
+ * @param options - Optional removal options
5990
+ * @returns A new changelog without the specified entries
5991
+ */
5992
+ function removeEntries(changelog, versions, options) {
5993
+ const versionsSet = createSet(versions);
5994
+ const newEntries = changelog.entries.filter((e) => !versionsSet.has(e.version));
5995
+ return {
5996
+ ...changelog,
5997
+ entries: newEntries,
5998
+ };
5999
+ }
6000
+
6001
+ /**
6002
+ * Creates a platform-specific compare URL for viewing changes between two commits.
6003
+ *
6004
+ * Each platform has a different URL format:
6005
+ * - **GitHub**: `{baseUrl}/compare/{fromCommit}...{toCommit}` (three dots)
6006
+ * - **GitLab**: `{baseUrl}/-/compare/{fromCommit}...{toCommit}` (three dots, `/-/` prefix)
6007
+ * - **Bitbucket**: `{baseUrl}/compare/{toCommit}..{fromCommit}` (two dots, reversed order)
6008
+ * - **Azure DevOps**: `{baseUrl}/compare?version=GT{toCommit}&compareVersion=GT{fromCommit}` (query params)
6009
+ *
6010
+ * For `custom` platforms, a `formatCompareUrl` function must be provided in the repository config.
6011
+ * For `unknown` platforms, returns `null`.
6012
+ *
6013
+ * @param options - Compare URL options including repository, fromCommit, and toCommit
6014
+ * @returns The compare URL string, or null if URL cannot be generated
6015
+ *
6016
+ * @example
6017
+ * ```typescript
6018
+ * // GitHub
6019
+ * createCompareUrl({
6020
+ * repository: { platform: 'github', baseUrl: 'https://github.com/owner/repo' },
6021
+ * fromCommit: 'abc1234',
6022
+ * toCommit: 'def5678'
6023
+ * })
6024
+ * // → 'https://github.com/owner/repo/compare/abc1234...def5678'
6025
+ *
6026
+ * // GitLab
6027
+ * createCompareUrl({
6028
+ * repository: { platform: 'gitlab', baseUrl: 'https://gitlab.com/group/project' },
6029
+ * fromCommit: 'abc1234',
6030
+ * toCommit: 'def5678'
6031
+ * })
6032
+ * // → 'https://gitlab.com/group/project/-/compare/abc1234...def5678'
6033
+ *
6034
+ * // Bitbucket (reversed order)
6035
+ * createCompareUrl({
6036
+ * repository: { platform: 'bitbucket', baseUrl: 'https://bitbucket.org/owner/repo' },
6037
+ * fromCommit: 'abc1234',
6038
+ * toCommit: 'def5678'
6039
+ * })
6040
+ * // → 'https://bitbucket.org/owner/repo/compare/def5678..abc1234'
6041
+ *
6042
+ * // Azure DevOps
6043
+ * createCompareUrl({
6044
+ * repository: { platform: 'azure-devops', baseUrl: 'https://dev.azure.com/org/proj/_git/repo' },
6045
+ * fromCommit: 'abc1234',
6046
+ * toCommit: 'def5678'
6047
+ * })
6048
+ * // → 'https://dev.azure.com/org/proj/_git/repo/compare?version=GTdef5678&compareVersion=GTabc1234'
6049
+ *
6050
+ * // Custom formatter
6051
+ * createCompareUrl({
6052
+ * repository: {
6053
+ * platform: 'custom',
6054
+ * baseUrl: 'https://my-git.internal/repo',
6055
+ * formatCompareUrl: (from, to) => `https://my-git.internal/diff/${from}/${to}`
6056
+ * },
6057
+ * fromCommit: 'abc1234',
6058
+ * toCommit: 'def5678'
6059
+ * })
6060
+ * // → 'https://my-git.internal/diff/abc1234/def5678'
6061
+ * ```
6062
+ */
6063
+ function createCompareUrl(options) {
6064
+ const { repository, fromCommit, toCommit } = options;
6065
+ // Validate inputs
6066
+ if (!repository || !fromCommit || !toCommit) {
6067
+ return null;
6068
+ }
6069
+ // If custom formatter is provided, use it (works for any platform including overrides)
6070
+ if (repository.formatCompareUrl) {
6071
+ return repository.formatCompareUrl(fromCommit, toCommit);
6072
+ }
6073
+ const { platform, baseUrl } = repository;
6074
+ // Cannot generate URL for unknown platforms without a formatter
6075
+ if (platform === 'unknown') {
6076
+ return null;
6077
+ }
6078
+ // Custom platform requires a formatter
6079
+ if (platform === 'custom') {
6080
+ return null;
6081
+ }
6082
+ // Generate URL for known platforms
6083
+ if (isKnownPlatform(platform)) {
6084
+ return formatKnownPlatformCompareUrl(platform, baseUrl, fromCommit, toCommit);
6085
+ }
6086
+ return null;
6087
+ }
6088
+ /**
6089
+ * Formats a compare URL for known platforms.
6090
+ *
6091
+ * @param platform - Known platform type
6092
+ * @param baseUrl - Repository base URL
6093
+ * @param fromCommit - Source commit hash (older version)
6094
+ * @param toCommit - Target commit hash (newer version)
6095
+ * @returns Formatted compare URL
6096
+ *
6097
+ * @internal
6098
+ */
6099
+ function formatKnownPlatformCompareUrl(platform, baseUrl, fromCommit, toCommit) {
6100
+ switch (platform) {
6101
+ case 'github':
6102
+ // GitHub: {baseUrl}/compare/{fromCommit}...{toCommit}
6103
+ return `${baseUrl}/compare/${fromCommit}...${toCommit}`;
6104
+ case 'gitlab':
6105
+ // GitLab: {baseUrl}/-/compare/{fromCommit}...{toCommit}
6106
+ return `${baseUrl}/-/compare/${fromCommit}...${toCommit}`;
6107
+ case 'bitbucket':
6108
+ // Bitbucket: {baseUrl}/compare/{toCommit}..{fromCommit} (reversed order, two dots)
6109
+ return `${baseUrl}/compare/${toCommit}..${fromCommit}`;
6110
+ case 'azure-devops':
6111
+ // Azure DevOps: {baseUrl}/compare?version=GT{toCommit}&compareVersion=GT{fromCommit}
6112
+ // Use encodeURIComponent for query parameter values
6113
+ return `${baseUrl}/compare?version=GT${encodeURIComponent(toCommit)}&compareVersion=GT${encodeURIComponent(fromCommit)}`;
6114
+ }
6115
+ }
6116
+
2997
6117
  const GENERATE_CHANGELOG_STEP_ID = 'generate-changelog';
2998
6118
  /**
2999
6119
  * Maps conventional commit types to changelog section types.
@@ -3011,6 +6131,32 @@ const COMMIT_TYPE_TO_SECTION = {
3011
6131
  chore: 'chores',
3012
6132
  style: 'other',
3013
6133
  };
6134
+ /**
6135
+ * Checks if a commit source represents an indirect change.
6136
+ *
6137
+ * @param source - The commit source type
6138
+ * @returns True if the commit is indirect (dependency or infrastructure)
6139
+ */
6140
+ function isIndirectSource(source) {
6141
+ return source === 'indirect-dependency' || source === 'indirect-infra';
6142
+ }
6143
+ /**
6144
+ * Groups classified commits by their section type.
6145
+ *
6146
+ * @param commits - Array of classified commits
6147
+ * @returns Record of section type to classified commits
6148
+ */
6149
+ function groupClassifiedCommitsBySection(commits) {
6150
+ const groups = {};
6151
+ for (const classified of commits) {
6152
+ const sectionType = COMMIT_TYPE_TO_SECTION[classified.commit.type ?? 'chore'] ?? 'chores';
6153
+ if (!groups[sectionType]) {
6154
+ groups[sectionType] = [];
6155
+ }
6156
+ groups[sectionType].push(classified);
6157
+ }
6158
+ return groups;
6159
+ }
3014
6160
  /**
3015
6161
  * Groups commits by their section type.
3016
6162
  *
@@ -3028,6 +6174,35 @@ function groupCommitsBySection(commits) {
3028
6174
  }
3029
6175
  return groups;
3030
6176
  }
6177
+ /**
6178
+ * Creates a changelog item from a classified commit.
6179
+ *
6180
+ * Applies scope display rules:
6181
+ * - Direct commits: scope omitted (redundant in project changelog)
6182
+ * - Indirect commits: scope preserved (provides context)
6183
+ *
6184
+ * @param classified - The classified commit with source metadata
6185
+ * @returns A changelog item with proper scope handling
6186
+ */
6187
+ function classifiedCommitToItem(classified) {
6188
+ // Apply scope transformation based on classification
6189
+ const commit = toChangelogCommit(classified);
6190
+ const indirect = isIndirectSource(classified.source);
6191
+ let text = commit.subject;
6192
+ // Add scope prefix if preserved (indirect commits)
6193
+ if (commit.scope) {
6194
+ text = `**${commit.scope}:** ${text}`;
6195
+ }
6196
+ // Add breaking change indicator
6197
+ if (commit.breaking) {
6198
+ text = `⚠️ BREAKING: ${text}`;
6199
+ }
6200
+ return createChangelogItem(text, {
6201
+ source: classified.source,
6202
+ indirect,
6203
+ breaking: commit.breaking,
6204
+ });
6205
+ }
3031
6206
  /**
3032
6207
  * Creates a changelog item from a conventional commit.
3033
6208
  *
@@ -3073,9 +6248,26 @@ function createGenerateChangelogStep() {
3073
6248
  }
3074
6249
  // Handle case with no commits (e.g., first release)
3075
6250
  if (!commits || commits.length === 0) {
6251
+ // Generate compare URL using commit hashes ONLY
6252
+ // Only generate if we have a valid base commit (effectiveBaseCommit will be null if fallback was used)
6253
+ let compareUrl;
6254
+ if (state.repositoryConfig && state.effectiveBaseCommit) {
6255
+ const currentCommit = ctx.git.getHeadHash();
6256
+ compareUrl =
6257
+ createCompareUrl({
6258
+ repository: state.repositoryConfig,
6259
+ fromCommit: state.effectiveBaseCommit,
6260
+ toCommit: currentCommit,
6261
+ }) ?? undefined;
6262
+ }
6263
+ else if (state.publishedCommit && !state.effectiveBaseCommit) {
6264
+ // Log why we're not generating a compare URL
6265
+ ctx.logger.info('Compare URL omitted: published commit not in current history');
6266
+ }
3076
6267
  const entry = createChangelogEntry(nextVersion, {
3077
6268
  date: createDate().toISOString().split('T')[0],
3078
6269
  sections: [createChangelogSection('features', 'Features', [createChangelogItem('Initial release')])],
6270
+ compareUrl,
3079
6271
  });
3080
6272
  return {
3081
6273
  status: 'success',
@@ -3083,41 +6275,109 @@ function createGenerateChangelogStep() {
3083
6275
  message: 'Generated initial release changelog entry',
3084
6276
  };
3085
6277
  }
3086
- // Group commits by section
3087
- const grouped = groupCommitsBySection(commits);
3088
- // Create sections
6278
+ // Use classification result when available for proper scope handling
6279
+ const { classificationResult } = state;
3089
6280
  const sections = [];
3090
- // Add breaking changes section first if any
3091
- const breakingCommits = commits.filter((c) => c.breaking);
3092
- if (breakingCommits.length > 0) {
3093
- sections.push(createChangelogSection('breaking', 'Breaking Changes', breakingCommits.map((c) => {
3094
- const text = c.breakingDescription ?? c.subject;
3095
- return createChangelogItem(c.scope ? `**${c.scope}:** ${text}` : text);
3096
- })));
3097
- }
3098
- // Add other sections in conventional order
3099
- const sectionOrder = [
3100
- { type: 'features', heading: 'Features' },
3101
- { type: 'fixes', heading: 'Bug Fixes' },
3102
- { type: 'performance', heading: 'Performance' },
3103
- { type: 'documentation', heading: 'Documentation' },
3104
- { type: 'refactoring', heading: 'Code Refactoring' },
3105
- { type: 'build', heading: 'Build' },
3106
- { type: 'ci', heading: 'Continuous Integration' },
3107
- { type: 'tests', heading: 'Tests' },
3108
- { type: 'chores', heading: 'Chores' },
3109
- { type: 'other', heading: 'Other' },
3110
- ];
3111
- for (const { type: sectionType, heading } of sectionOrder) {
3112
- const sectionCommits = grouped[sectionType];
3113
- if (sectionCommits && sectionCommits.length > 0) {
3114
- sections.push(createChangelogSection(sectionType, heading, sectionCommits.map(commitToItem)));
6281
+ if (classificationResult && classificationResult.included.length > 0) {
6282
+ // Use classified commits for proper scope display rules
6283
+ const classifiedCommits = classificationResult.included;
6284
+ // Separate direct and indirect commits
6285
+ const directCommits = classifiedCommits.filter((c) => !isIndirectSource(c.source));
6286
+ const indirectCommits = classifiedCommits.filter((c) => isIndirectSource(c.source));
6287
+ // Add breaking changes section first if any
6288
+ const breakingCommits = classifiedCommits.filter((c) => c.commit.breaking);
6289
+ if (breakingCommits.length > 0) {
6290
+ sections.push(createChangelogSection('breaking', 'Breaking Changes', breakingCommits.map((c) => {
6291
+ const commit = toChangelogCommit(c);
6292
+ const text = commit.breakingDescription ?? commit.subject;
6293
+ const indirect = isIndirectSource(c.source);
6294
+ return createChangelogItem(commit.scope ? `**${commit.scope}:** ${text}` : text, {
6295
+ source: c.source,
6296
+ indirect,
6297
+ breaking: true,
6298
+ });
6299
+ })));
6300
+ }
6301
+ // Group direct commits by section
6302
+ const groupedDirect = groupClassifiedCommitsBySection(directCommits);
6303
+ // Add other sections in conventional order (direct commits only)
6304
+ const sectionOrder = [
6305
+ { type: 'features', heading: 'Features' },
6306
+ { type: 'fixes', heading: 'Bug Fixes' },
6307
+ { type: 'performance', heading: 'Performance' },
6308
+ { type: 'documentation', heading: 'Documentation' },
6309
+ { type: 'refactoring', heading: 'Code Refactoring' },
6310
+ { type: 'build', heading: 'Build' },
6311
+ { type: 'ci', heading: 'Continuous Integration' },
6312
+ { type: 'tests', heading: 'Tests' },
6313
+ { type: 'chores', heading: 'Chores' },
6314
+ { type: 'other', heading: 'Other' },
6315
+ ];
6316
+ for (const { type: sectionType, heading } of sectionOrder) {
6317
+ const sectionCommits = groupedDirect[sectionType];
6318
+ if (sectionCommits && sectionCommits.length > 0) {
6319
+ sections.push(createChangelogSection(sectionType, heading, sectionCommits.map(classifiedCommitToItem)));
6320
+ }
6321
+ }
6322
+ // Add Dependency Updates section for indirect commits if any
6323
+ if (indirectCommits.length > 0) {
6324
+ sections.push(createChangelogSection('other', // Use 'other' as section type for dependency updates
6325
+ 'Dependency Updates', indirectCommits.map((c) => classifiedCommitToItem(c))));
6326
+ }
6327
+ }
6328
+ else {
6329
+ // Fallback: use commits without classification (backward compatibility)
6330
+ const grouped = groupCommitsBySection(commits);
6331
+ // Add breaking changes section first if any
6332
+ const breakingCommits = commits.filter((c) => c.breaking);
6333
+ if (breakingCommits.length > 0) {
6334
+ sections.push(createChangelogSection('breaking', 'Breaking Changes', breakingCommits.map((c) => {
6335
+ const text = c.breakingDescription ?? c.subject;
6336
+ return createChangelogItem(c.scope ? `**${c.scope}:** ${text}` : text);
6337
+ })));
6338
+ }
6339
+ // Add other sections in conventional order
6340
+ const sectionOrder = [
6341
+ { type: 'features', heading: 'Features' },
6342
+ { type: 'fixes', heading: 'Bug Fixes' },
6343
+ { type: 'performance', heading: 'Performance' },
6344
+ { type: 'documentation', heading: 'Documentation' },
6345
+ { type: 'refactoring', heading: 'Code Refactoring' },
6346
+ { type: 'build', heading: 'Build' },
6347
+ { type: 'ci', heading: 'Continuous Integration' },
6348
+ { type: 'tests', heading: 'Tests' },
6349
+ { type: 'chores', heading: 'Chores' },
6350
+ { type: 'other', heading: 'Other' },
6351
+ ];
6352
+ for (const { type: sectionType, heading } of sectionOrder) {
6353
+ const sectionCommits = grouped[sectionType];
6354
+ if (sectionCommits && sectionCommits.length > 0) {
6355
+ sections.push(createChangelogSection(sectionType, heading, sectionCommits.map(commitToItem)));
6356
+ }
3115
6357
  }
3116
6358
  }
6359
+ // Generate compare URL using commit hashes ONLY
6360
+ // Only generate if we have a valid base commit (effectiveBaseCommit will be null if fallback was used)
6361
+ let compareUrl;
6362
+ if (state.repositoryConfig && state.effectiveBaseCommit) {
6363
+ const currentCommit = ctx.git.getHeadHash();
6364
+ compareUrl =
6365
+ createCompareUrl({
6366
+ repository: state.repositoryConfig,
6367
+ fromCommit: state.effectiveBaseCommit,
6368
+ toCommit: currentCommit,
6369
+ }) ?? undefined;
6370
+ ctx.logger.debug(`Compare URL: ${state.effectiveBaseCommit.slice(0, 7)}...${currentCommit.slice(0, 7)}`);
6371
+ }
6372
+ else if (state.publishedCommit && !state.effectiveBaseCommit) {
6373
+ // Log why we're not generating a compare URL
6374
+ ctx.logger.info('Compare URL omitted: published commit not in current history');
6375
+ }
3117
6376
  // Create the entry
3118
6377
  const entry = createChangelogEntry(nextVersion, {
3119
6378
  date: createDate().toISOString().split('T')[0],
3120
6379
  sections,
6380
+ compareUrl,
3121
6381
  });
3122
6382
  return {
3123
6383
  status: 'success',
@@ -3173,7 +6433,28 @@ function createWriteChangelogStep() {
3173
6433
  }
3174
6434
  // Parse existing and add entry
3175
6435
  const existing = parseChangelog(existingContent);
3176
- const updated = addEntry(existing, changelogEntry);
6436
+ const isPendingPublication = state.isPendingPublication === true;
6437
+ let changelog = existing;
6438
+ // Clean up stacked entries when in pending publication state
6439
+ if (isPendingPublication && state.publishedVersion) {
6440
+ const publishedVer = parseVersion(state.publishedVersion);
6441
+ if (publishedVer.success && publishedVer.version) {
6442
+ const pubVer = publishedVer.version;
6443
+ const toRemove = changelog.entries
6444
+ .filter((e) => !e.unreleased)
6445
+ .filter((e) => {
6446
+ const ver = parseVersion(e.version);
6447
+ return ver.success && ver.version && gt(ver.version, pubVer);
6448
+ })
6449
+ .map((e) => e.version);
6450
+ if (toRemove.length > 0) {
6451
+ logger.info(`Removing stacked entries: ${toRemove.join(', ')}`);
6452
+ changelog = removeEntries(changelog, toRemove);
6453
+ }
6454
+ }
6455
+ }
6456
+ // Add entry (replaceExisting handles case where nextVersion entry already exists)
6457
+ const updated = addEntry(changelog, changelogEntry, { replaceExisting: isPendingPublication });
3177
6458
  const serialized = serializeChangelog(updated);
3178
6459
  tree.write(changelogPath, serialized);
3179
6460
  return {
@@ -3210,23 +6491,26 @@ function createUpdatePackageStep() {
3210
6491
  return createSkippedResult('No version bump needed');
3211
6492
  }
3212
6493
  const packageJsonPath = `${projectRoot}/package.json`;
6494
+ logger.debug(`Reading package.json from: ${packageJsonPath}`);
3213
6495
  // Read package.json
3214
6496
  let content;
3215
6497
  try {
3216
6498
  content = tree.read(packageJsonPath, 'utf-8') ?? '';
3217
6499
  if (!content) {
6500
+ logger.error(`package.json not found at ${packageJsonPath}`);
3218
6501
  return {
3219
6502
  status: 'failed',
3220
- error: createError('package.json not found'),
3221
- message: 'Could not read package.json',
6503
+ error: createError(`package.json not found at ${packageJsonPath}`),
6504
+ message: `Could not read package.json at ${packageJsonPath}`,
3222
6505
  };
3223
6506
  }
3224
6507
  }
3225
6508
  catch (error) {
6509
+ logger.error(`Failed to read package.json at ${packageJsonPath}: ${error}`);
3226
6510
  return {
3227
6511
  status: 'failed',
3228
6512
  error: error instanceof Error ? error : createError(String(error)),
3229
- message: 'Failed to read package.json',
6513
+ message: `Failed to read package.json at ${packageJsonPath}`,
3230
6514
  };
3231
6515
  }
3232
6516
  // Parse and update version
@@ -3509,14 +6793,15 @@ const CONVENTIONAL_FLOW_CONFIG = {
3509
6793
  *
3510
6794
  * This flow follows the standard conventional commits workflow:
3511
6795
  * 1. Fetch published version from registry
3512
- * 2. Analyze commits since last release
3513
- * 3. Calculate version bump based on commit types
3514
- * 4. Check if version already published (idempotency)
3515
- * 5. Generate changelog entry
3516
- * 6. Update package.json version
3517
- * 7. Write changelog to file
3518
- * 8. Create git commit (optional)
3519
- * 9. Create git tag (optional, typically after publish)
6796
+ * 2. Resolve repository configuration (for compare URLs)
6797
+ * 3. Analyze commits since last release
6798
+ * 4. Calculate version bump based on commit types
6799
+ * 5. Check if version already published (idempotency)
6800
+ * 6. Generate changelog entry (with compare URL if repository resolved)
6801
+ * 7. Update package.json version
6802
+ * 8. Write changelog to file
6803
+ * 9. Create git commit (optional)
6804
+ * 10. Create git tag (optional, typically after publish)
3520
6805
  *
3521
6806
  * @param config - Optional configuration overrides
3522
6807
  * @returns A VersionFlow configured for conventional commits
@@ -3541,6 +6826,7 @@ function createConventionalFlow(config) {
3541
6826
  const mergedConfig = { ...CONVENTIONAL_FLOW_CONFIG, ...config };
3542
6827
  return createFlow('conventional', 'Conventional Commits Flow', [
3543
6828
  createFetchRegistryStep(),
6829
+ createResolveRepositoryStep(),
3544
6830
  createAnalyzeCommitsStep(),
3545
6831
  createCalculateBumpStep(),
3546
6832
  createCheckIdempotencyStep(),
@@ -3672,6 +6958,7 @@ function createIndependentFlow(config) {
3672
6958
  const mergedConfig = { ...INDEPENDENT_FLOW_CONFIG, ...config };
3673
6959
  return createFlow('independent', 'Independent Versioning Flow', [
3674
6960
  createFetchRegistryStep(),
6961
+ createResolveRepositoryStep(),
3675
6962
  createAnalyzeCommitsStep(),
3676
6963
  createCalculateBumpStep(),
3677
6964
  createCheckDependentBumpsStep(),
@@ -3700,6 +6987,7 @@ function createIndependentFlow(config) {
3700
6987
  function createBatchReleaseFlow(config) {
3701
6988
  return createFlow('batch-release', 'Batch Release Flow', [
3702
6989
  createFetchRegistryStep(),
6990
+ createResolveRepositoryStep(),
3703
6991
  createAnalyzeCommitsStep(),
3704
6992
  createCalculateBumpStep(),
3705
6993
  createCheckIdempotencyStep(),
@@ -3833,6 +7121,7 @@ function createSyncedFlow(config) {
3833
7121
  const mergedConfig = { ...SYNCED_FLOW_CONFIG, ...config };
3834
7122
  return createFlow('synced', 'Synced Versioning Flow', [
3835
7123
  createFetchRegistryStep(),
7124
+ createResolveRepositoryStep(),
3836
7125
  createAnalyzeCommitsStep(),
3837
7126
  createCalculateBumpStep(),
3838
7127
  createCheckIdempotencyStep(),
@@ -3872,6 +7161,7 @@ function createFixedVersionFlow(version, config) {
3872
7161
  });
3873
7162
  return createFlow('fixed', 'Fixed Version Flow', [
3874
7163
  createFetchRegistryStep(),
7164
+ createResolveRepositoryStep(),
3875
7165
  createAnalyzeCommitsStep(),
3876
7166
  fixedBumpStep,
3877
7167
  createCheckIdempotencyStep(),