@grunnverk/commands-publish 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js ADDED
@@ -0,0 +1,2848 @@
1
+ import { getDryRunLogger, findDevelopmentBranch, KODRDRIV_DEFAULTS, incrementPatchVersion, incrementMajorVersion, incrementMinorVersion, DEFAULT_TO_COMMIT_ALIAS, Log, DEFAULT_MAX_DIFF_BYTES, Diff, DEFAULT_EXCLUDED_PATTERNS, DEFAULT_OUTPUT_DIRECTORY, toAIConfig, createStorageAdapter, createLoggerAdapter, getOutputPath, getTimestampedResponseFilename, getTimestampedRequestFilename, filterContent, getTimestampedReleaseNotesFilename, improveContentWithLLM, validateReleaseSummary, runGitWithLock, calculateBranchDependentVersion, checkIfTagExists, confirmVersionInteractively, getLogger, ValidationError } from '@grunnverk/core';
2
+ import { getCurrentBranch, run, localBranchExists, safeJsonParse, validatePackageJson, getDefaultFromRef, remoteBranchExists, runSecure, runWithDryRunSupport, validateGitRef, safeSyncBranchWithRemote, isBranchInSyncWithRemote } from '@grunnverk/git-tools';
3
+ import { createStorage, calculateTargetVersion } from '@grunnverk/shared';
4
+ import path from 'path';
5
+ import * as Commit from '@grunnverk/commands-git';
6
+ import { Formatter } from '@riotprompt/riotprompt';
7
+ import 'dotenv/config';
8
+ import { runAgenticRelease, requireTTY, generateReflectionReport, getUserChoice, STANDARD_CHOICES, getLLMFeedbackInEditor, editContentInEditor, createCompletionWithRetry, createReleasePrompt, runAgenticPublish, formatAgenticPublishResult } from '@grunnverk/ai-service';
9
+ import * as GitHub from '@grunnverk/github-tools';
10
+ import fs from 'fs/promises';
11
+
12
+ /**
13
+ * Create retroactive working branch tags for past releases
14
+ * Scans git history for X.X.X-dev.0 commits and tags them
15
+ */ async function createRetroactiveTags(workingBranch, isDryRun, logger, tagPrefix = 'working/') {
16
+ logger.info('');
17
+ logger.info('DEV_TAG_SCAN_STARTING: Scanning git history for past release points | Purpose: Create retroactive tags | Pattern: X.X.X-dev.0 version bumps');
18
+ logger.info('DEV_TAG_SCAN_PATTERN: Looking for development version bump commits | Version Format: X.X.X-dev.0 | Purpose: Identify release points');
19
+ logger.info('');
20
+ try {
21
+ // Get all commits on working branch with oneline format
22
+ const { stdout } = await run(`git log ${workingBranch} --oneline --all`);
23
+ const commits = stdout.trim().split('\n');
24
+ // Find commits that are version bumps to -dev.0 (these mark release points)
25
+ const devCommits = commits.filter((line)=>{
26
+ // Match patterns like: "4.4.52-dev.0" or "chore: bump version to 4.4.52-dev.0"
27
+ return /\b\d+\.\d+\.\d+-dev\.0\b/.test(line);
28
+ });
29
+ logger.info(`DEV_TAG_COMMITS_FOUND: Found potential development version commits | Count: ${devCommits.length} | Status: Analyzing for tag creation`);
30
+ const tagsCreated = [];
31
+ const tagsSkipped = [];
32
+ for (const commitLine of devCommits){
33
+ const [sha, ...messageParts] = commitLine.split(' ');
34
+ const message = messageParts.join(' ');
35
+ // Extract version from message (e.g., "4.4.52-dev.0" → "4.4.52")
36
+ const versionMatch = message.match(/(\d+\.\d+\.\d+)-dev\.0/);
37
+ if (!versionMatch) continue;
38
+ const releaseVersion = versionMatch[1]; // e.g., "4.4.52"
39
+ const workingTagName = `${tagPrefix}v${releaseVersion}`;
40
+ // Check if tag already exists
41
+ const tagExistsResult = await run(`git tag -l "${workingTagName}"`);
42
+ const tagExists = tagExistsResult.stdout.trim() !== '';
43
+ if (tagExists) {
44
+ tagsSkipped.push(workingTagName);
45
+ logger.verbose(` Skip: ${workingTagName} (already exists)`);
46
+ continue;
47
+ }
48
+ if (!isDryRun) {
49
+ // Tag the commit that represents the dev version bump
50
+ // This is the commit AFTER the release, which marks the starting point
51
+ logger.verbose(` Create: ${workingTagName} at ${sha.substring(0, 7)}`);
52
+ await run(`git tag ${workingTagName} ${sha}`);
53
+ tagsCreated.push(workingTagName);
54
+ } else {
55
+ logger.info(`DEV_TAG_DRY_RUN: Would create retroactive tag | Mode: dry-run | Tag: ${workingTagName} | Commit: ${sha.substring(0, 7)}`);
56
+ tagsCreated.push(workingTagName);
57
+ }
58
+ }
59
+ logger.info('');
60
+ if (tagsCreated.length > 0 && !isDryRun) {
61
+ logger.info(`DEV_TAG_PUSHING: Pushing retroactive tags to remote | Count: ${tagsCreated.length} | Remote: origin | Command: git push origin --tags`);
62
+ await run('git push origin --tags');
63
+ logger.info('');
64
+ logger.info(`DEV_TAG_PUSH_SUCCESS: Successfully created and pushed retroactive tags | Count: ${tagsCreated.length} | Remote: origin | Status: completed`);
65
+ tagsCreated.forEach((tag)=>logger.info(`DEV_TAG_CREATED: Retroactive tag created | Tag: ${tag} | Status: pushed`));
66
+ } else if (tagsCreated.length > 0 && isDryRun) {
67
+ logger.info(`DEV_TAG_DRY_RUN_SUMMARY: Would create and push retroactive tags | Mode: dry-run | Count: ${tagsCreated.length}`);
68
+ tagsCreated.forEach((tag)=>logger.info(`DEV_TAG_DRY_RUN_TAG: Would create tag | Tag: ${tag} | Mode: dry-run`));
69
+ }
70
+ if (tagsSkipped.length > 0) {
71
+ logger.verbose('');
72
+ logger.verbose(`Skipped ${tagsSkipped.length} existing tags:`);
73
+ tagsSkipped.forEach((tag)=>logger.verbose(` - ${tag}`));
74
+ }
75
+ if (tagsCreated.length === 0 && tagsSkipped.length === 0) {
76
+ logger.info('DEV_TAG_NO_COMMITS: No development version commits found in history | Pattern: X.X.X-dev.0 | Status: Nothing to tag | Action: No retroactive tags created');
77
+ }
78
+ logger.info('');
79
+ } catch (error) {
80
+ logger.warn(`DEV_TAG_CREATION_FAILED: Unable to create retroactive tags | Error: ${error.message} | Impact: Past releases not tagged | Alternative: Manual tagging available`);
81
+ logger.warn('DEV_TAG_MANUAL_OPTION: Manual tagging option available | Action: Use git tag manually for past releases | Purpose: Tag historical releases');
82
+ // Don't throw - retroactive tagging is optional
83
+ }
84
+ }
85
+ /**
86
+ * Execute the development command
87
+ */ const execute$2 = async (runConfig)=>{
88
+ const isDryRun = runConfig.dryRun || false;
89
+ const logger = getDryRunLogger(isDryRun);
90
+ logger.info('DEV_BRANCH_NAVIGATION: Navigating to working branch for development | Purpose: Start development cycle | Next: Version bump and sync');
91
+ try {
92
+ var _runConfig_development, _runConfig_development1, _runConfig_development2;
93
+ // Get current branch
94
+ const currentBranch = isDryRun ? 'mock-branch' : await getCurrentBranch();
95
+ logger.info(`DEV_CURRENT_BRANCH: Current branch identified | Branch: ${currentBranch} | Action: Determine working branch`);
96
+ // Find the working/development branch from configuration
97
+ let workingBranch = 'working'; // Default fallback
98
+ if (runConfig.branches) {
99
+ const configuredDevBranch = findDevelopmentBranch(runConfig.branches);
100
+ if (configuredDevBranch) {
101
+ workingBranch = configuredDevBranch;
102
+ logger.info(`DEV_WORKING_BRANCH_CONFIGURED: Using configured working branch | Branch: ${workingBranch} | Source: config | Current: ${currentBranch}`);
103
+ } else {
104
+ logger.info(`DEV_WORKING_BRANCH_DEFAULT: No working branch configured | Branch: ${workingBranch} | Source: default | Current: ${currentBranch}`);
105
+ }
106
+ } else {
107
+ logger.info(`DEV_WORKING_BRANCH_NO_CONFIG: No branch configuration found | Branch: ${workingBranch} | Source: default | Current: ${currentBranch}`);
108
+ }
109
+ // Track what actions are taken to determine the appropriate return message
110
+ let branchCreated = false;
111
+ let branchUpdated = false;
112
+ let alreadyOnBranch = false;
113
+ let mergedDevelopmentIntoWorking = false;
114
+ // Determine prerelease tag and increment level from configuration
115
+ const allBranchConfig = runConfig.branches || KODRDRIV_DEFAULTS.branches;
116
+ let prereleaseTag = 'dev'; // Default
117
+ let incrementLevel = 'patch'; // Default
118
+ // Check for development command specific targetVersion override
119
+ if ((_runConfig_development = runConfig.development) === null || _runConfig_development === void 0 ? void 0 : _runConfig_development.targetVersion) {
120
+ const targetVersion = runConfig.development.targetVersion;
121
+ // Validate targetVersion
122
+ if (![
123
+ 'patch',
124
+ 'minor',
125
+ 'major'
126
+ ].includes(targetVersion) && !/^\d+\.\d+\.\d+$/.test(targetVersion.replace(/^v/, ''))) {
127
+ throw new Error(`Invalid target version: ${targetVersion}. Expected "patch", "minor", "major", or a valid version string like "2.1.0"`);
128
+ }
129
+ incrementLevel = targetVersion;
130
+ } else if (allBranchConfig && allBranchConfig[workingBranch]) {
131
+ const workingBranchConfig = allBranchConfig[workingBranch];
132
+ if (workingBranchConfig.version) {
133
+ if (workingBranchConfig.version.tag) {
134
+ prereleaseTag = workingBranchConfig.version.tag;
135
+ }
136
+ if (workingBranchConfig.version.incrementLevel) {
137
+ incrementLevel = workingBranchConfig.version.incrementLevel;
138
+ }
139
+ }
140
+ }
141
+ logger.info(`DEV_VERSION_CONFIG: Development version configuration | Prerelease Tag: ${prereleaseTag} | Increment Level: ${incrementLevel}`);
142
+ logger.info(`DEV_VERSION_STRATEGY: Version increment strategy | Level: ${incrementLevel} | Tag: ${prereleaseTag} | Purpose: Development version management`);
143
+ // Step 1: Fetch latest remote information
144
+ if (!isDryRun) {
145
+ logger.info('DEV_GIT_FETCH: Fetching latest remote information | Remote: origin | Purpose: Ensure sync before branch operations');
146
+ try {
147
+ await run('git fetch origin');
148
+ logger.info('DEV_GIT_FETCH_SUCCESS: Successfully fetched remote information | Remote: origin | Status: up-to-date');
149
+ } catch (error) {
150
+ logger.warn(`DEV_GIT_FETCH_FAILED: Unable to fetch remote | Remote: origin | Error: ${error.message} | Impact: May have stale branch info`);
151
+ }
152
+ } else {
153
+ logger.info('DEV_GIT_FETCH_DRY_RUN: Would fetch latest remote information | Mode: dry-run | Remote: origin');
154
+ }
155
+ // Special case: If currently on development branch, merge development into working
156
+ if (currentBranch === 'development') {
157
+ if (!isDryRun) {
158
+ logger.info('DEV_MERGE_STARTING: Currently on development branch, merging into working | Source: development | Target: working | Purpose: Sync branches before development');
159
+ await run(`git checkout ${workingBranch}`);
160
+ await run(`git merge development --no-ff -m "Merge development into working for continued development"`);
161
+ await run('npm install');
162
+ // Check if npm install created any changes and commit them
163
+ const gitStatus = await run('git status --porcelain');
164
+ if (gitStatus.stdout.trim()) {
165
+ await run('git add -A');
166
+ await run('git commit -m "chore: update dependencies after merge"');
167
+ }
168
+ // Stay on working branch for development (removed checkout development)
169
+ mergedDevelopmentIntoWorking = true;
170
+ } else {
171
+ logger.info('DEV_MERGE_DRY_RUN: Would merge development into working | Mode: dry-run | Source: development | Target: working');
172
+ mergedDevelopmentIntoWorking = true;
173
+ }
174
+ }
175
+ // Step 2: Switch to working branch (create if needed) - skip if we handled development branch case
176
+ if (!isDryRun && !mergedDevelopmentIntoWorking) {
177
+ const workingBranchExists = await localBranchExists(workingBranch);
178
+ if (!workingBranchExists) {
179
+ logger.info(`DEV_BRANCH_CREATING: Working branch does not exist, creating now | Branch: ${workingBranch} | Action: Create and checkout | Source: current HEAD`);
180
+ await run(`git checkout -b ${workingBranch}`);
181
+ logger.info(`DEV_BRANCH_CREATED: Successfully created and switched to branch | Branch: ${workingBranch} | Status: checked-out`);
182
+ branchCreated = true;
183
+ } else if (currentBranch !== workingBranch) {
184
+ logger.info(`DEV_BRANCH_SWITCHING: Switching to working branch | Branch: ${workingBranch} | Action: checkout | Previous: ${currentBranch}`);
185
+ await run(`git checkout ${workingBranch}`);
186
+ logger.info(`DEV_BRANCH_SWITCHED: Successfully switched to branch | Branch: ${workingBranch} | Status: checked-out`);
187
+ branchUpdated = true;
188
+ } else {
189
+ logger.info(`DEV_BRANCH_CURRENT: Already on working branch | Branch: ${workingBranch} | Status: no-switch-needed`);
190
+ alreadyOnBranch = true;
191
+ }
192
+ } else if (!mergedDevelopmentIntoWorking) {
193
+ // For dry run, we need to mock the logic
194
+ const workingBranchExists = await localBranchExists(workingBranch);
195
+ if (!workingBranchExists) {
196
+ branchCreated = true;
197
+ } else if (currentBranch !== workingBranch) {
198
+ branchUpdated = true;
199
+ } else {
200
+ alreadyOnBranch = true;
201
+ }
202
+ logger.info(`DEV_BRANCH_DRY_RUN: Would switch to working branch | Mode: dry-run | Branch: ${workingBranch} | Action: Create if needed`);
203
+ logger.info(`DEV_SYNC_DRY_RUN: Would sync branch with remote | Mode: dry-run | Branch: ${workingBranch} | Purpose: Avoid conflicts`);
204
+ }
205
+ // Step 2.1: Sync with remote working branch to avoid conflicts
206
+ if (!isDryRun) {
207
+ try {
208
+ logger.info(`DEV_BRANCH_SYNCING: Synchronizing working branch with remote | Branch: ${workingBranch} | Remote: origin/${workingBranch} | Purpose: Avoid conflicts`);
209
+ const remoteExists = await run(`git ls-remote --exit-code --heads origin ${workingBranch}`).then(()=>true).catch(()=>false);
210
+ if (remoteExists) {
211
+ // Use explicit fetch+merge instead of pull to avoid git config conflicts
212
+ await run(`git fetch origin ${workingBranch}`);
213
+ await run(`git merge origin/${workingBranch} --no-edit`);
214
+ logger.info(`DEV_BRANCH_SYNCED: Successfully synchronized with remote | Branch: ${workingBranch} | Remote: origin/${workingBranch} | Status: in-sync`);
215
+ } else {
216
+ logger.info(`DEV_REMOTE_BRANCH_NOT_FOUND: No remote branch exists | Branch: ${workingBranch} | Remote: origin | Action: Will be created on first push`);
217
+ }
218
+ } catch (error) {
219
+ if (error.message && error.message.includes('CONFLICT')) {
220
+ logger.error(`DEV_MERGE_CONFLICTS: Merge conflicts detected when syncing with remote | Branch: ${workingBranch} | Remote: origin | Status: conflicts-detected`);
221
+ logger.error(`DEV_CONFLICT_RESOLUTION: Manual conflict resolution required:`);
222
+ logger.error(` Step 1: Resolve conflicts in the files`);
223
+ logger.error(` Step 2: Stage resolved files | Command: git add <resolved-files>`);
224
+ logger.error(` Step 3: Complete merge | Command: git commit`);
225
+ logger.error(` Step 4: Resume development | Command: kodrdriv development`);
226
+ throw new Error(`Merge conflicts detected when syncing ${workingBranch} with remote. Please resolve conflicts manually.`);
227
+ } else {
228
+ logger.warn(`DEV_SYNC_FAILED: Could not sync with remote | Branch: ${workingBranch} | Remote: origin | Error: ${error.message}`);
229
+ }
230
+ }
231
+ }
232
+ // Step 2.5: Sync with target branch (main) if it exists
233
+ // This is a safety net for when publish fails or user ends up on target branch
234
+ if (!isDryRun) {
235
+ var _allBranchConfig_workingBranch;
236
+ // Determine target branch from config
237
+ const targetBranch = allBranchConfig && ((_allBranchConfig_workingBranch = allBranchConfig[workingBranch]) === null || _allBranchConfig_workingBranch === void 0 ? void 0 : _allBranchConfig_workingBranch.targetBranch) || 'main';
238
+ const targetBranchExists = await localBranchExists(targetBranch);
239
+ if (targetBranchExists) {
240
+ logger.info(`DEV_TARGET_SYNC: Syncing working branch with target branch | Working: ${workingBranch} | Target: ${targetBranch} | Strategy: fast-forward`);
241
+ try {
242
+ await run(`git merge ${targetBranch} --ff-only`);
243
+ logger.info(`DEV_TARGET_MERGED_FF: Fast-forward merged target into working | Target: ${targetBranch} | Working: ${workingBranch} | Status: merged`);
244
+ } catch (error) {
245
+ // Fast-forward failed, might need regular merge
246
+ if (error.message && error.message.includes('Not possible to fast-forward')) {
247
+ logger.warn(`DEV_NO_FAST_FORWARD: Cannot fast-forward merge | Target: ${targetBranch} | Working: ${workingBranch} | Reason: Divergent history`);
248
+ logger.info(`DEV_REGULAR_MERGE_ATTEMPTING: Attempting regular merge | Strategy: fast-forward preferred | Purpose: Sync branches`);
249
+ try {
250
+ await run(`git merge ${targetBranch} -m "Merge ${targetBranch} into ${workingBranch} for sync"`);
251
+ logger.info(`DEV_TARGET_MERGED: Merged target into working | Target: ${targetBranch} | Working: ${workingBranch} | Status: merged`);
252
+ // Run npm install after merge
253
+ logger.info('DEV_POST_MERGE_INSTALL: Running npm install after merge | Command: npm install | Purpose: Update dependencies');
254
+ await run('npm install');
255
+ // Check if npm install created changes
256
+ const gitStatus = await run('git status --porcelain');
257
+ if (gitStatus.stdout.trim()) {
258
+ logger.info('DEV_POST_MERGE_COMMIT: Committing changes from npm install | Purpose: Finalize merge');
259
+ await run('git add -A');
260
+ await run('git commit -m "chore: update dependencies after merge"');
261
+ }
262
+ } catch (mergeError) {
263
+ if (mergeError.message && mergeError.message.includes('CONFLICT')) {
264
+ logger.error(`DEV_MERGE_CONFLICTS: Merge conflicts detected | Target: ${targetBranch} | Working: ${workingBranch} | Status: conflicts-detected`);
265
+ logger.error(`DEV_CONFLICT_RESOLUTION: Manual conflict resolution required:`);
266
+ logger.error(` Step 1: Resolve conflicts in the files`);
267
+ logger.error(` Step 2: Stage resolved files | Command: git add <resolved-files>`);
268
+ logger.error(` Step 3: Complete merge | Command: git commit`);
269
+ logger.error(` Step 4: Update dependencies | Command: npm install`);
270
+ logger.error(` Step 5: Resume development | Command: kodrdriv development`);
271
+ throw new Error(`Merge conflicts detected when merging ${targetBranch} into ${workingBranch}. Please resolve conflicts manually.`);
272
+ } else {
273
+ throw mergeError;
274
+ }
275
+ }
276
+ } else {
277
+ logger.warn(`DEV_TARGET_MERGE_FAILED: Could not merge target into working | Target: ${targetBranch} | Working: ${workingBranch} | Error: ${error.message}`);
278
+ }
279
+ }
280
+ } else {
281
+ logger.info(`DEV_TARGET_NOT_EXISTS: Target branch does not exist | Branch: ${targetBranch} | Action: Skipping target sync | Status: no-target-branch`);
282
+ }
283
+ } else {
284
+ logger.info('Would sync working branch with target branch (main) if it exists');
285
+ }
286
+ // Step 3: Merge latest changes from development branch if it exists
287
+ if (!isDryRun) {
288
+ const developmentBranchExists = await localBranchExists('development');
289
+ if (mergedDevelopmentIntoWorking) {
290
+ logger.info('DEV_ALREADY_MERGED: Already merged from development | Reason: Was on development branch | Action: Skipping');
291
+ } else if (developmentBranchExists) {
292
+ logger.info('DEV_DEVELOPMENT_MERGE: Merging latest changes from development branch | Source: development | Target: ' + workingBranch + ' | Purpose: Sync development changes');
293
+ try {
294
+ await run(`git merge development --no-ff -m "Merge latest development changes into ${workingBranch}"`);
295
+ logger.info('DEV_DEVELOPMENT_MERGED: Successfully merged development changes | Source: development | Target: ' + workingBranch + ' | Status: merged');
296
+ // Run npm install after merge to update dependencies
297
+ logger.info('DEV_DEVELOPMENT_INSTALL: Running npm install after merge | Command: npm install | Purpose: Update dependencies');
298
+ await run('npm install');
299
+ // Check if npm install created any changes (e.g., package-lock.json)
300
+ const gitStatus = await run('git status --porcelain');
301
+ if (gitStatus.stdout.trim()) {
302
+ logger.info('DEV_POST_MERGE_COMMIT: Committing changes from npm install | Purpose: Finalize merge');
303
+ await run('git add -A');
304
+ await run(`git commit -m "chore: update package-lock.json after merge"`);
305
+ logger.info('DEV_CHANGES_COMMITTED: Changes committed successfully | Status: committed');
306
+ }
307
+ } catch (error) {
308
+ if (error.message && error.message.includes('CONFLICT')) {
309
+ logger.error(`DEV_DEV_MERGE_CONFLICTS: Merge conflicts detected | Source: development | Target: ${workingBranch} | Status: conflicts-detected`);
310
+ logger.error(`DEV_DEV_CONFLICT_RESOLUTION: Manual conflict resolution required:`);
311
+ logger.error(` Step 1: Resolve conflicts in the files`);
312
+ logger.error(` Step 2: Stage resolved files | Command: git add <resolved-files>`);
313
+ logger.error(` Step 3: Complete merge | Command: git commit`);
314
+ logger.error(` Step 4: Update dependencies | Command: npm install`);
315
+ logger.error(` Step 5: Bump version | Command: npm version pre${incrementLevel} --preid=${prereleaseTag}`);
316
+ throw new Error(`Merge conflicts detected when merging development into ${workingBranch}. Please resolve conflicts manually.`);
317
+ } else {
318
+ logger.error(`DEV_DEV_MERGE_FAILED: Failed to merge development branch | Source: development | Target: ${workingBranch} | Error: ${error.message}`);
319
+ throw error;
320
+ }
321
+ }
322
+ } else {
323
+ logger.info('DEV_NO_DEV_BRANCH: Development branch does not exist | Branch: development | Action: Skipping merge step | Status: not-found');
324
+ }
325
+ } else {
326
+ logger.info('DEV_DEV_MERGE_DRY_RUN: Would merge development if exists | Mode: dry-run | Source: development | Target: working');
327
+ logger.info('DEV_INSTALL_DRY_RUN: Would run npm install after merge | Mode: dry-run | Command: npm install');
328
+ logger.info('DEV_COMMIT_DRY_RUN: Would commit npm install changes | Mode: dry-run');
329
+ }
330
+ // Step 4.5: Create retroactive tags if requested (one-time operation)
331
+ if ((_runConfig_development1 = runConfig.development) === null || _runConfig_development1 === void 0 ? void 0 : _runConfig_development1.createRetroactiveTags) {
332
+ var _runConfig_development3;
333
+ const tagPrefix = ((_runConfig_development3 = runConfig.development) === null || _runConfig_development3 === void 0 ? void 0 : _runConfig_development3.workingTagPrefix) || 'working/';
334
+ await createRetroactiveTags(workingBranch, isDryRun, logger, tagPrefix);
335
+ }
336
+ // Step 5: Check if we already have a proper development version
337
+ if (alreadyOnBranch && !mergedDevelopmentIntoWorking) {
338
+ // Check if current version is already a development version with the right tag
339
+ const fs = await import('fs/promises');
340
+ try {
341
+ const packageJson = JSON.parse(await fs.readFile('package.json', 'utf-8'));
342
+ const currentVersion = packageJson.version;
343
+ // If current version already has the dev tag, we're done
344
+ if (currentVersion.includes(`-${prereleaseTag}.`)) {
345
+ logger.info(`DEV_ALREADY_DEV_VERSION: Already on working branch with development version | Branch: ${workingBranch} | Version: ${currentVersion} | Status: no-bump-needed`);
346
+ return 'Already on working branch with development version';
347
+ }
348
+ } catch {
349
+ logger.debug('Could not check current version, proceeding with version bump');
350
+ }
351
+ }
352
+ // Step 5.5: Tag working branch with current release version BEFORE bumping
353
+ if (((_runConfig_development2 = runConfig.development) === null || _runConfig_development2 === void 0 ? void 0 : _runConfig_development2.tagWorkingBranch) !== false) {
354
+ try {
355
+ const fs = await import('fs/promises');
356
+ const packageJson = JSON.parse(await fs.readFile('package.json', 'utf-8'));
357
+ const currentVersion = packageJson.version;
358
+ // Only tag if current version is a release version (not already a dev version)
359
+ const isReleaseVersion = currentVersion && !currentVersion.includes('-dev.') && !currentVersion.includes('-alpha.') && !currentVersion.includes('-beta.') && !currentVersion.includes('-rc.');
360
+ if (isReleaseVersion) {
361
+ var _runConfig_development4;
362
+ const tagPrefix = ((_runConfig_development4 = runConfig.development) === null || _runConfig_development4 === void 0 ? void 0 : _runConfig_development4.workingTagPrefix) || 'working/';
363
+ const workingTagName = `${tagPrefix}v${currentVersion}`;
364
+ if (!isDryRun) {
365
+ logger.info(`DEV_TAG_RELEASE_VERSION: Current version is release version | Version: ${currentVersion} | Type: release | Action: Will tag before bump`);
366
+ logger.verbose(`Checking if tag ${workingTagName} exists...`);
367
+ // Check if tag already exists
368
+ const tagExistsResult = await run(`git tag -l "${workingTagName}"`);
369
+ const tagExists = tagExistsResult.stdout.trim() !== '';
370
+ if (tagExists) {
371
+ logger.info(`DEV_TAG_EXISTS: Tag already exists | Tag: ${workingTagName} | Action: Skipping tag creation | Status: already-tagged`);
372
+ } else {
373
+ // Create tag on current commit (working branch at release version)
374
+ logger.verbose(`Creating tag ${workingTagName} at current HEAD...`);
375
+ await run(`git tag ${workingTagName}`);
376
+ // Push tag to remote
377
+ logger.verbose(`Pushing tag ${workingTagName} to origin...`);
378
+ await run(`git push origin ${workingTagName}`);
379
+ logger.info(`DEV_TAG_CREATED: Tagged working branch | Tag: ${workingTagName} | Version: ${currentVersion} | Status: tagged-and-pushed`);
380
+ logger.info(`DEV_TAG_RELEASE_NOTES_HINT: Release notes can be generated | Version: v${currentVersion} | Command: kodrdriv release --from {previous-tag} --to ${workingTagName}`);
381
+ }
382
+ } else {
383
+ logger.info(`DEV_TAG_DRY_RUN: Would tag working branch | Mode: dry-run | Tag: ${workingTagName} | Version: ${currentVersion}`);
384
+ }
385
+ } else if (currentVersion) {
386
+ logger.verbose(`Current version is ${currentVersion} (prerelease), skipping tag creation`);
387
+ } else {
388
+ logger.debug('Could not determine current version, skipping tag creation');
389
+ }
390
+ } catch (error) {
391
+ if (!isDryRun) {
392
+ logger.warn(`DEV_TAG_FAILED: Could not tag working branch | Error: ${error.message} | Impact: Not critical | Alternative: Manual tagging`);
393
+ logger.warn('DEV_TAG_MANUAL: Manual tagging option available | Action: Tag manually later | Purpose: Mark release point');
394
+ } else {
395
+ logger.info('Would tag working branch with current release version if applicable');
396
+ }
397
+ // Don't throw - tagging is optional, continue with version bump
398
+ }
399
+ } else if (isDryRun) {
400
+ logger.info('Tagging disabled (--no-tag-working-branch)');
401
+ }
402
+ // Step 6: Bump version manually to avoid npm version's automatic git add
403
+ // Note: npm version --no-git-tag-version still runs "git add package.json package-lock.json"
404
+ // which fails when package-lock.json is gitignored
405
+ if ([
406
+ 'patch',
407
+ 'minor',
408
+ 'major'
409
+ ].includes(incrementLevel)) {
410
+ logger.info(`DEV_VERSION_BUMPING: Bumping version with prerelease tag | Level: ${incrementLevel} | Tag: ${prereleaseTag}`);
411
+ } else {
412
+ logger.info(`DEV_VERSION_EXPLICIT: Setting explicit version | Version: ${incrementLevel}-${prereleaseTag}.0 | Type: explicit`);
413
+ }
414
+ if (!isDryRun) {
415
+ try {
416
+ var _allBranchConfig_workingBranch1;
417
+ const storage = createStorage();
418
+ // CRITICAL FIX: After publish, we need to use the target branch's version as the base
419
+ // for incrementing, not the working branch's version (which might be an old dev version).
420
+ // This ensures that after publishing v0.0.15, we bump to 0.0.16-dev.0, not 0.0.15-dev.0.
421
+ const targetBranch = allBranchConfig && ((_allBranchConfig_workingBranch1 = allBranchConfig[workingBranch]) === null || _allBranchConfig_workingBranch1 === void 0 ? void 0 : _allBranchConfig_workingBranch1.targetBranch) || 'main';
422
+ let currentVersion;
423
+ try {
424
+ // Try to read version from target branch (main) to get the released version
425
+ const targetPackageResult = await run(`git show ${targetBranch}:package.json`);
426
+ const targetPackageJson = safeJsonParse(targetPackageResult.stdout, 'package.json from target branch');
427
+ const validatedTargetPkg = validatePackageJson(targetPackageJson, 'package.json from target branch');
428
+ currentVersion = validatedTargetPkg.version;
429
+ logger.info(`DEV_VERSION_SOURCE: Using version from target branch | Branch: ${targetBranch} | Version: ${currentVersion} | Purpose: Ensure proper increment after release`);
430
+ } catch (error) {
431
+ // Fallback: If target branch doesn't exist or can't be read, use current working branch version
432
+ logger.warn(`DEV_VERSION_FALLBACK: Could not read version from target branch ${targetBranch} | Error: ${error.message} | Fallback: Using current branch version`);
433
+ const pkgJsonContents = await storage.readFile('package.json', 'utf-8');
434
+ const pkgJson = safeJsonParse(pkgJsonContents, 'package.json');
435
+ const validatedPkgJson = validatePackageJson(pkgJson, 'package.json');
436
+ currentVersion = validatedPkgJson.version;
437
+ }
438
+ let newVersion;
439
+ if ([
440
+ 'patch',
441
+ 'minor',
442
+ 'major'
443
+ ].includes(incrementLevel)) {
444
+ // First increment the base version, then add prerelease tag
445
+ let baseVersion;
446
+ switch(incrementLevel){
447
+ case 'patch':
448
+ baseVersion = incrementPatchVersion(currentVersion);
449
+ break;
450
+ case 'minor':
451
+ baseVersion = incrementMinorVersion(currentVersion);
452
+ break;
453
+ case 'major':
454
+ baseVersion = incrementMajorVersion(currentVersion);
455
+ break;
456
+ default:
457
+ baseVersion = incrementPatchVersion(currentVersion);
458
+ }
459
+ newVersion = `${baseVersion}-${prereleaseTag}.0`;
460
+ } else {
461
+ // Explicit version like "3.5.0"
462
+ const cleanVersion = incrementLevel.replace(/^v/, '');
463
+ newVersion = `${cleanVersion}-${prereleaseTag}.0`;
464
+ }
465
+ // Read current package.json from working branch to update
466
+ const pkgJsonContents = await storage.readFile('package.json', 'utf-8');
467
+ const pkgJson = safeJsonParse(pkgJsonContents, 'package.json');
468
+ const validatedPkgJson = validatePackageJson(pkgJson, 'package.json');
469
+ // Update package.json with new version
470
+ validatedPkgJson.version = newVersion;
471
+ await storage.writeFile('package.json', JSON.stringify(validatedPkgJson, null, 2) + '\n', 'utf-8');
472
+ logger.info(`DEV_VERSION_BUMPED: Version bumped successfully | New Version: ${newVersion} | Status: completed`);
473
+ // Manually commit the version bump (package-lock.json is ignored)
474
+ await run('git add package.json');
475
+ await run(`git commit -m "chore: bump to ${newVersion}"`);
476
+ // Return appropriate message based on what actions were taken
477
+ if (mergedDevelopmentIntoWorking) {
478
+ return 'Merged development into working and ready for development';
479
+ } else if (branchCreated) {
480
+ return 'Created working branch with development version';
481
+ } else if (branchUpdated) {
482
+ return 'Updated working branch with development version';
483
+ } else if (alreadyOnBranch) {
484
+ return 'Already on working branch with development version';
485
+ } else {
486
+ return `Ready for development on ${workingBranch} with version ${newVersion}`;
487
+ }
488
+ } catch (error) {
489
+ logger.error(`DEV_VERSION_BUMP_FAILED: Failed to bump version | Error: ${error.message} | Impact: Version not updated`);
490
+ throw new Error(`Failed to bump ${incrementLevel} version: ${error.message}`);
491
+ }
492
+ } else {
493
+ if ([
494
+ 'patch',
495
+ 'minor',
496
+ 'major'
497
+ ].includes(incrementLevel)) {
498
+ logger.info(`Would bump version with prerelease tag: ${incrementLevel} --preid=${prereleaseTag}`);
499
+ } else {
500
+ logger.info(`Would set explicit version: ${incrementLevel}-${prereleaseTag}.0`);
501
+ }
502
+ // Return appropriate message based on what actions were taken
503
+ if (mergedDevelopmentIntoWorking) {
504
+ return 'Merged development into working and ready for development';
505
+ } else if (branchCreated) {
506
+ return 'Created working branch with development version';
507
+ } else if (branchUpdated) {
508
+ return 'Updated working branch with development version';
509
+ } else if (alreadyOnBranch) {
510
+ return 'Already on working branch with development version';
511
+ } else {
512
+ return `Ready for development on ${workingBranch} (dry run)`;
513
+ }
514
+ }
515
+ } catch (error) {
516
+ logger.error('Failed to prepare working branch for development:', error.message);
517
+ throw error;
518
+ }
519
+ };
520
+
521
+ // Helper function to read context files
522
+ async function readContextFiles(contextFiles, logger) {
523
+ if (!contextFiles || contextFiles.length === 0) {
524
+ return '';
525
+ }
526
+ const storage = createStorage();
527
+ const contextParts = [];
528
+ for (const filePath of contextFiles){
529
+ try {
530
+ const content = await storage.readFile(filePath, 'utf8');
531
+ contextParts.push(`## Context from ${filePath}\n\n${content}\n`);
532
+ logger.debug(`Read context from file: ${filePath}`);
533
+ } catch (error) {
534
+ logger.warn(`Failed to read context file ${filePath}: ${error.message}`);
535
+ }
536
+ }
537
+ return contextParts.join('\n---\n\n');
538
+ }
539
+ // Helper function to edit release notes using editor
540
+ async function editReleaseNotesInteractively(releaseSummary) {
541
+ const templateLines = [
542
+ '# Edit your release notes below. Lines starting with "#" will be ignored.',
543
+ '# The first line is the title, everything else is the body.',
544
+ '# Save and close the editor when you are done.'
545
+ ];
546
+ const content = `${releaseSummary.title}\n\n${releaseSummary.body}`;
547
+ const result = await editContentInEditor(content, templateLines, '.md');
548
+ const lines = result.content.split('\n');
549
+ const title = lines[0].trim();
550
+ const body = lines.slice(1).join('\n').trim();
551
+ return {
552
+ title,
553
+ body
554
+ };
555
+ }
556
+ // Helper function to improve release notes using LLM
557
+ async function improveReleaseNotesWithLLM(releaseSummary, runConfig, promptConfig, promptContext, outputDirectory, logContent, diffContent) {
558
+ // Get user feedback on what to improve using the editor
559
+ const releaseNotesContent = `${releaseSummary.title}\n\n${releaseSummary.body}`;
560
+ const userFeedback = await getLLMFeedbackInEditor('release notes', releaseNotesContent);
561
+ const improvementConfig = {
562
+ contentType: 'release notes',
563
+ createImprovedPrompt: async (promptConfig, currentSummary, promptContext)=>{
564
+ var _aiConfig_commands_release, _aiConfig_commands;
565
+ const improvementPromptContent = {
566
+ logContent: logContent,
567
+ diffContent: diffContent,
568
+ releaseFocus: `Please improve these release notes based on the user's feedback: "${userFeedback}".
569
+
570
+ Current release notes:
571
+ Title: "${currentSummary.title}"
572
+ Body: "${currentSummary.body}"
573
+
574
+ Please revise the release notes according to the user's feedback while maintaining accuracy and following good release note practices.`
575
+ };
576
+ const promptResult = await createReleasePrompt(promptConfig, improvementPromptContent, promptContext);
577
+ // Format the prompt into a proper request with messages
578
+ const aiConfig = toAIConfig(runConfig);
579
+ const modelToUse = ((_aiConfig_commands = aiConfig.commands) === null || _aiConfig_commands === void 0 ? void 0 : (_aiConfig_commands_release = _aiConfig_commands.release) === null || _aiConfig_commands_release === void 0 ? void 0 : _aiConfig_commands_release.model) || aiConfig.model || 'gpt-4o-mini';
580
+ return Formatter.create({
581
+ logger: getDryRunLogger(false)
582
+ }).formatPrompt(modelToUse, promptResult.prompt);
583
+ },
584
+ callLLM: async (request, runConfig, outputDirectory)=>{
585
+ var _aiConfig_commands_release, _aiConfig_commands, _aiConfig_commands_release1, _aiConfig_commands1;
586
+ const aiConfig = toAIConfig(runConfig);
587
+ const aiStorageAdapter = createStorageAdapter(outputDirectory);
588
+ const aiLogger = createLoggerAdapter(false);
589
+ const modelToUse = ((_aiConfig_commands = aiConfig.commands) === null || _aiConfig_commands === void 0 ? void 0 : (_aiConfig_commands_release = _aiConfig_commands.release) === null || _aiConfig_commands_release === void 0 ? void 0 : _aiConfig_commands_release.model) || aiConfig.model || 'gpt-4o-mini';
590
+ const openaiReasoning = ((_aiConfig_commands1 = aiConfig.commands) === null || _aiConfig_commands1 === void 0 ? void 0 : (_aiConfig_commands_release1 = _aiConfig_commands1.release) === null || _aiConfig_commands_release1 === void 0 ? void 0 : _aiConfig_commands_release1.reasoning) || aiConfig.reasoning;
591
+ return await createCompletionWithRetry(request.messages, {
592
+ model: modelToUse,
593
+ openaiReasoning,
594
+ responseFormat: {
595
+ type: 'json_object'
596
+ },
597
+ debug: runConfig.debug,
598
+ debugRequestFile: getOutputPath(outputDirectory, getTimestampedRequestFilename('release-improve')),
599
+ debugResponseFile: getOutputPath(outputDirectory, getTimestampedResponseFilename('release-improve')),
600
+ storage: aiStorageAdapter,
601
+ logger: aiLogger
602
+ });
603
+ },
604
+ processResponse: (response)=>{
605
+ return validateReleaseSummary(response);
606
+ }
607
+ };
608
+ return await improveContentWithLLM(releaseSummary, runConfig, promptConfig, promptContext, outputDirectory, improvementConfig);
609
+ }
610
+ // Helper function to generate self-reflection output for release notes using observability module
611
+ async function generateSelfReflection(agenticResult, outputDirectory, storage, logger) {
612
+ try {
613
+ const timestamp = new Date().toISOString().replace(/[:.]/g, '-').split('.')[0];
614
+ const reflectionPath = getOutputPath(outputDirectory, `agentic-reflection-release-${timestamp}.md`);
615
+ // Use new observability reflection generator
616
+ const report = await generateReflectionReport({
617
+ iterations: agenticResult.iterations || 0,
618
+ toolCallsExecuted: agenticResult.toolCallsExecuted || 0,
619
+ maxIterations: agenticResult.maxIterations || 30,
620
+ toolMetrics: agenticResult.toolMetrics || [],
621
+ conversationHistory: agenticResult.conversationHistory || [],
622
+ releaseNotes: agenticResult.releaseNotes,
623
+ logger
624
+ });
625
+ // Save the report to output directory
626
+ await storage.writeFile(reflectionPath, report, 'utf8');
627
+ logger.info('');
628
+ logger.info('═'.repeat(80));
629
+ logger.info('📊 SELF-REFLECTION REPORT GENERATED');
630
+ logger.info('═'.repeat(80));
631
+ logger.info('');
632
+ logger.info('📁 Location: %s', reflectionPath);
633
+ logger.info('');
634
+ logger.info('📈 Report Summary:');
635
+ const iterations = agenticResult.iterations || 0;
636
+ const toolCalls = agenticResult.toolCallsExecuted || 0;
637
+ const uniqueTools = new Set((agenticResult.toolMetrics || []).map((m)=>m.name)).size;
638
+ logger.info(` • ${iterations} iterations completed`);
639
+ logger.info(` • ${toolCalls} tool calls executed`);
640
+ logger.info(` • ${uniqueTools} unique tools used`);
641
+ logger.info('');
642
+ logger.info('💡 Use this report to:');
643
+ logger.info(' • Understand which tools were most effective');
644
+ logger.info(' • Identify performance bottlenecks');
645
+ logger.info(' • Optimize tool selection and usage patterns');
646
+ logger.info(' • Improve agentic release notes generation');
647
+ logger.info('');
648
+ logger.info('═'.repeat(80));
649
+ } catch (error) {
650
+ logger.warn('Failed to generate self-reflection report: %s', error.message);
651
+ }
652
+ }
653
+ // Interactive feedback loop for release notes
654
+ async function handleInteractiveReleaseFeedback(releaseSummary, runConfig, promptConfig, promptContext, outputDirectory, storage, logContent, diffContent) {
655
+ const logger = getDryRunLogger(false);
656
+ let currentSummary = releaseSummary;
657
+ while(true){
658
+ // Display the current release notes
659
+ logger.info('\nRELEASE_NOTES_GENERATED: Generated release notes from AI | Title Length: ' + currentSummary.title.length + ' | Body Length: ' + currentSummary.body.length);
660
+ logger.info('─'.repeat(50));
661
+ logger.info('RELEASE_NOTES_TITLE: %s', currentSummary.title);
662
+ logger.info('');
663
+ logger.info('RELEASE_NOTES_BODY: Release notes content:');
664
+ logger.info(currentSummary.body);
665
+ logger.info('─'.repeat(50));
666
+ // Get user choice
667
+ const userChoice = await getUserChoice('\nWhat would you like to do with these release notes?', [
668
+ STANDARD_CHOICES.CONFIRM,
669
+ STANDARD_CHOICES.EDIT,
670
+ STANDARD_CHOICES.SKIP,
671
+ STANDARD_CHOICES.IMPROVE
672
+ ], {
673
+ nonTtyErrorSuggestions: [
674
+ 'Use --dry-run to see the generated content without interaction'
675
+ ]
676
+ });
677
+ switch(userChoice){
678
+ case 'c':
679
+ return {
680
+ action: 'confirm',
681
+ finalSummary: currentSummary
682
+ };
683
+ case 'e':
684
+ try {
685
+ currentSummary = await editReleaseNotesInteractively(currentSummary);
686
+ } catch (error) {
687
+ logger.error(`RELEASE_NOTES_EDIT_FAILED: Unable to edit release notes | Error: ${error.message} | Impact: Using original notes`);
688
+ // Continue the loop to show options again
689
+ }
690
+ break;
691
+ case 's':
692
+ return {
693
+ action: 'skip',
694
+ finalSummary: currentSummary
695
+ };
696
+ case 'i':
697
+ try {
698
+ currentSummary = await improveReleaseNotesWithLLM(currentSummary, runConfig, promptConfig, promptContext, outputDirectory, logContent, diffContent);
699
+ } catch (error) {
700
+ logger.error(`RELEASE_NOTES_IMPROVE_FAILED: Unable to improve release notes | Error: ${error.message} | Impact: Using current version`);
701
+ // Continue the loop to show options again
702
+ }
703
+ break;
704
+ }
705
+ }
706
+ }
707
+ const execute$1 = async (runConfig)=>{
708
+ var _ref, _ref1, _ref2, _runConfig_excludedPatterns;
709
+ var _runConfig_release, _runConfig_release1, _runConfig_release2, _runConfig_release3, _runConfig_release4, _runConfig_release5, _runConfig_release6, _runConfig_release7, _runConfig_release8, _runConfig_release9, _runConfig_release10, _aiConfig_commands_release, _aiConfig_commands, _runConfig_release11, _aiConfig_commands_release1, _aiConfig_commands1, _runConfig_release12, _runConfig_release13;
710
+ const isDryRun = runConfig.dryRun || false;
711
+ const logger = getDryRunLogger(isDryRun);
712
+ // Get current branch to help determine best tag comparison
713
+ const currentBranch = ((_runConfig_release = runConfig.release) === null || _runConfig_release === void 0 ? void 0 : _runConfig_release.currentBranch) || await getCurrentBranch();
714
+ // Resolve the from reference with fallback logic if not explicitly provided
715
+ const fromRef = (_ref = (_runConfig_release1 = runConfig.release) === null || _runConfig_release1 === void 0 ? void 0 : _runConfig_release1.from) !== null && _ref !== void 0 ? _ref : await getDefaultFromRef(((_runConfig_release2 = runConfig.release) === null || _runConfig_release2 === void 0 ? void 0 : _runConfig_release2.fromMain) || false, currentBranch);
716
+ const toRef = (_ref1 = (_runConfig_release3 = runConfig.release) === null || _runConfig_release3 === void 0 ? void 0 : _runConfig_release3.to) !== null && _ref1 !== void 0 ? _ref1 : DEFAULT_TO_COMMIT_ALIAS;
717
+ logger.debug(`Using git references: from=${fromRef}, to=${toRef}`);
718
+ const log = await Log.create({
719
+ from: fromRef,
720
+ to: toRef,
721
+ limit: (_runConfig_release4 = runConfig.release) === null || _runConfig_release4 === void 0 ? void 0 : _runConfig_release4.messageLimit
722
+ });
723
+ let logContent = '';
724
+ const maxDiffBytes = (_ref2 = (_runConfig_release5 = runConfig.release) === null || _runConfig_release5 === void 0 ? void 0 : _runConfig_release5.maxDiffBytes) !== null && _ref2 !== void 0 ? _ref2 : DEFAULT_MAX_DIFF_BYTES;
725
+ const diff = await Diff.create({
726
+ from: fromRef,
727
+ to: toRef,
728
+ excludedPatterns: (_runConfig_excludedPatterns = runConfig.excludedPatterns) !== null && _runConfig_excludedPatterns !== void 0 ? _runConfig_excludedPatterns : DEFAULT_EXCLUDED_PATTERNS,
729
+ maxDiffBytes
730
+ });
731
+ let diffContent = '';
732
+ diffContent = await diff.get();
733
+ logContent = await log.get();
734
+ const promptConfig = {
735
+ overridePaths: runConfig.discoveredConfigDirs || [],
736
+ overrides: runConfig.overrides || false
737
+ };
738
+ // Helper function to determine versions for milestone lookup
739
+ const determineVersionsForMilestones = async ()=>{
740
+ var _runConfig_publish;
741
+ const versions = [];
742
+ // Get current package.json version to determine likely release version
743
+ try {
744
+ const storage = createStorage();
745
+ const packageJsonContents = await storage.readFile('package.json', 'utf-8');
746
+ const packageJson = safeJsonParse(packageJsonContents, 'package.json');
747
+ const currentVersion = packageJson.version;
748
+ if (currentVersion) {
749
+ // If it's a dev version (e.g., "0.1.1-dev.0"), extract base version
750
+ if (currentVersion.includes('-dev.')) {
751
+ const baseVersion = currentVersion.split('-')[0];
752
+ versions.push(baseVersion);
753
+ logger.debug(`Detected dev version ${currentVersion}, will check milestone for ${baseVersion}`);
754
+ } else {
755
+ // Use current version as-is
756
+ versions.push(currentVersion);
757
+ logger.debug(`Using current version ${currentVersion} for milestone lookup`);
758
+ }
759
+ }
760
+ } catch (error) {
761
+ logger.debug(`Failed to read package.json version: ${error.message}`);
762
+ }
763
+ // Handle edge case: if publish targetVersion is different from current version
764
+ if (((_runConfig_publish = runConfig.publish) === null || _runConfig_publish === void 0 ? void 0 : _runConfig_publish.targetVersion) && runConfig.publish.targetVersion !== 'patch' && runConfig.publish.targetVersion !== 'minor' && runConfig.publish.targetVersion !== 'major') {
765
+ const targetVersion = runConfig.publish.targetVersion;
766
+ if (!versions.includes(targetVersion)) {
767
+ versions.push(targetVersion);
768
+ logger.debug(`Added target version ${targetVersion} for milestone lookup`);
769
+ }
770
+ }
771
+ return versions;
772
+ };
773
+ // Get milestone issues if enabled
774
+ let milestoneIssuesContent = '';
775
+ const milestonesEnabled = !((_runConfig_release6 = runConfig.release) === null || _runConfig_release6 === void 0 ? void 0 : _runConfig_release6.noMilestones);
776
+ if (milestonesEnabled) {
777
+ logger.info('RELEASE_MILESTONE_CHECK: Checking for milestone issues | Purpose: Include in release notes | Source: GitHub milestones');
778
+ const versions = await determineVersionsForMilestones();
779
+ if (versions.length > 0) {
780
+ milestoneIssuesContent = await GitHub.getMilestoneIssuesForRelease(versions, 50000);
781
+ if (milestoneIssuesContent) {
782
+ logger.info('RELEASE_MILESTONE_INCLUDED: Incorporated milestone issues into context | Count: ' + ((milestoneIssuesContent === null || milestoneIssuesContent === void 0 ? void 0 : milestoneIssuesContent.length) || 0) + ' | Purpose: Enrich release notes');
783
+ } else {
784
+ logger.debug('No milestone issues found to incorporate');
785
+ }
786
+ } else {
787
+ logger.debug('No versions determined for milestone lookup');
788
+ }
789
+ } else {
790
+ logger.debug('Milestone integration disabled via --no-milestones');
791
+ }
792
+ // Always ensure output directory exists for request/response files
793
+ const outputDirectory = runConfig.outputDirectory || DEFAULT_OUTPUT_DIRECTORY;
794
+ const storage = createStorage();
795
+ await storage.ensureDirectory(outputDirectory);
796
+ // Create adapters for ai-service
797
+ const aiConfig = toAIConfig(runConfig);
798
+ const aiStorageAdapter = createStorageAdapter(outputDirectory);
799
+ const aiLogger = createLoggerAdapter(isDryRun);
800
+ // Read context from files if provided
801
+ const contextFromFiles = await readContextFiles((_runConfig_release7 = runConfig.release) === null || _runConfig_release7 === void 0 ? void 0 : _runConfig_release7.contextFiles, logger);
802
+ // Combine file context with existing context
803
+ const combinedContext = [
804
+ (_runConfig_release8 = runConfig.release) === null || _runConfig_release8 === void 0 ? void 0 : _runConfig_release8.context,
805
+ contextFromFiles
806
+ ].filter(Boolean).join('\n\n---\n\n');
807
+ // Run agentic release notes generation
808
+ const agenticResult = await runAgenticRelease({
809
+ fromRef,
810
+ toRef,
811
+ logContent,
812
+ diffContent,
813
+ milestoneIssues: milestoneIssuesContent,
814
+ releaseFocus: (_runConfig_release9 = runConfig.release) === null || _runConfig_release9 === void 0 ? void 0 : _runConfig_release9.focus,
815
+ userContext: combinedContext || undefined,
816
+ targetVersion: (_runConfig_release10 = runConfig.release) === null || _runConfig_release10 === void 0 ? void 0 : _runConfig_release10.version,
817
+ model: ((_aiConfig_commands = aiConfig.commands) === null || _aiConfig_commands === void 0 ? void 0 : (_aiConfig_commands_release = _aiConfig_commands.release) === null || _aiConfig_commands_release === void 0 ? void 0 : _aiConfig_commands_release.model) || aiConfig.model || 'gpt-4o',
818
+ maxIterations: ((_runConfig_release11 = runConfig.release) === null || _runConfig_release11 === void 0 ? void 0 : _runConfig_release11.maxAgenticIterations) || 30,
819
+ debug: runConfig.debug,
820
+ debugRequestFile: getOutputPath(outputDirectory, getTimestampedRequestFilename('release')),
821
+ debugResponseFile: getOutputPath(outputDirectory, getTimestampedResponseFilename('release')),
822
+ storage: aiStorageAdapter,
823
+ logger: aiLogger,
824
+ openaiReasoning: ((_aiConfig_commands1 = aiConfig.commands) === null || _aiConfig_commands1 === void 0 ? void 0 : (_aiConfig_commands_release1 = _aiConfig_commands1.release) === null || _aiConfig_commands_release1 === void 0 ? void 0 : _aiConfig_commands_release1.reasoning) || aiConfig.reasoning
825
+ });
826
+ const iterations = agenticResult.iterations || 0;
827
+ const toolCalls = agenticResult.toolCallsExecuted || 0;
828
+ logger.info(`🔍 Analysis complete: ${iterations} iterations, ${toolCalls} tool calls`);
829
+ // Generate self-reflection output if enabled
830
+ if ((_runConfig_release12 = runConfig.release) === null || _runConfig_release12 === void 0 ? void 0 : _runConfig_release12.selfReflection) {
831
+ await generateSelfReflection(agenticResult, outputDirectory, storage, logger);
832
+ }
833
+ // Apply stop-context filtering to release notes
834
+ const titleFilterResult = filterContent(agenticResult.releaseNotes.title, runConfig.stopContext);
835
+ const bodyFilterResult = filterContent(agenticResult.releaseNotes.body, runConfig.stopContext);
836
+ let releaseSummary = {
837
+ title: titleFilterResult.filtered,
838
+ body: bodyFilterResult.filtered
839
+ };
840
+ // Handle interactive mode
841
+ if (((_runConfig_release13 = runConfig.release) === null || _runConfig_release13 === void 0 ? void 0 : _runConfig_release13.interactive) && !isDryRun) {
842
+ requireTTY('Interactive mode requires a terminal. Use --dry-run instead.');
843
+ const interactivePromptContext = {
844
+ context: combinedContext || undefined,
845
+ directories: runConfig.contextDirectories
846
+ };
847
+ const interactiveResult = await handleInteractiveReleaseFeedback(releaseSummary, runConfig, promptConfig, interactivePromptContext, outputDirectory, storage, logContent, diffContent);
848
+ if (interactiveResult.action === 'skip') {
849
+ logger.info('RELEASE_ABORTED: Release notes generation aborted by user | Reason: User choice | Status: cancelled');
850
+ } else {
851
+ logger.info('RELEASE_FINALIZED: Release notes finalized and accepted | Status: ready | Next: Create release or save');
852
+ }
853
+ releaseSummary = interactiveResult.finalSummary;
854
+ }
855
+ // Save timestamped copy of release notes to output directory
856
+ try {
857
+ const timestampedFilename = getTimestampedReleaseNotesFilename();
858
+ const outputPath = getOutputPath(outputDirectory, timestampedFilename);
859
+ // Format the release notes as markdown
860
+ const releaseNotesContent = `# ${releaseSummary.title}\n\n${releaseSummary.body}`;
861
+ await storage.writeFile(outputPath, releaseNotesContent, 'utf-8');
862
+ logger.debug('Saved timestamped release notes: %s', outputPath);
863
+ } catch (error) {
864
+ logger.warn('RELEASE_SAVE_FAILED: Failed to save timestamped release notes | Error: %s | Impact: Notes not persisted to file', error.message);
865
+ }
866
+ if (isDryRun) {
867
+ logger.info('RELEASE_SUMMARY_COMPLETE: Generated release summary successfully | Status: completed');
868
+ logger.info('RELEASE_SUMMARY_TITLE: %s', releaseSummary.title);
869
+ logger.info('RELEASE_SUMMARY_BODY: %s', releaseSummary.body);
870
+ }
871
+ return releaseSummary;
872
+ };
873
+
874
+ const scanNpmrcForEnvVars = async (storage)=>{
875
+ const logger = getLogger();
876
+ const npmrcPath = path.join(process.cwd(), '.npmrc');
877
+ const envVars = [];
878
+ if (await storage.exists(npmrcPath)) {
879
+ try {
880
+ const npmrcContent = await storage.readFile(npmrcPath, 'utf-8');
881
+ // Match environment variable patterns like ${VAR_NAME} or $VAR_NAME
882
+ const envVarMatches = npmrcContent.match(/\$\{([^}]+)\}|\$([A-Z_][A-Z0-9_]*)/g);
883
+ if (envVarMatches) {
884
+ for (const match of envVarMatches){
885
+ // Extract variable name from ${VAR_NAME} or $VAR_NAME format
886
+ const varName = match.replace(/\$\{|\}|\$/g, '');
887
+ if (varName && !envVars.includes(varName)) {
888
+ envVars.push(varName);
889
+ }
890
+ }
891
+ }
892
+ } catch (error) {
893
+ logger.warn(`NPMRC_READ_FAILED: Unable to read .npmrc configuration file | Path: ${npmrcPath} | Error: ${error.message}`);
894
+ logger.verbose('NPMRC_READ_IMPACT: Environment variable detection for publishing may be affected due to failed .npmrc read');
895
+ }
896
+ } else {
897
+ logger.debug('NPMRC_NOT_FOUND: No .npmrc file present in current directory | Action: Skipping environment variable scan | Path: ' + npmrcPath);
898
+ }
899
+ return envVars;
900
+ };
901
+ /**
902
+ * Checks if .gitignore contains required patterns to prevent publishing
903
+ * development artifacts and sensitive files.
904
+ */ const checkGitignorePatterns = async (storage, isDryRun)=>{
905
+ const logger = getDryRunLogger(isDryRun);
906
+ const gitignorePath = path.join(process.cwd(), '.gitignore');
907
+ // Required patterns that must be present in .gitignore
908
+ const requiredPatterns = [
909
+ 'node_modules',
910
+ 'dist',
911
+ 'package-lock.json',
912
+ '.env',
913
+ 'output/',
914
+ 'coverage',
915
+ '.kodrdriv*'
916
+ ];
917
+ // Check if .gitignore exists
918
+ if (!await storage.exists(gitignorePath)) {
919
+ logger.error('GITIGNORE_MISSING: .gitignore file not found | Path: ' + gitignorePath + ' | Impact: Critical files may be committed');
920
+ logger.error('');
921
+ logger.error('GITIGNORE_REQUIRED: .gitignore is required for kodrdriv to work properly');
922
+ logger.error('GITIGNORE_PURPOSE: Prevents committing development artifacts and sensitive files');
923
+ logger.error('');
924
+ logger.error('GITIGNORE_CREATE: Create .gitignore with these patterns:');
925
+ for (const pattern of requiredPatterns){
926
+ logger.error(` ${pattern}`);
927
+ }
928
+ logger.error('');
929
+ throw new Error('.gitignore file is required but not found. Please create it with the required patterns.');
930
+ }
931
+ // Read .gitignore content
932
+ let gitignoreContent;
933
+ try {
934
+ gitignoreContent = await storage.readFile(gitignorePath, 'utf-8');
935
+ } catch (error) {
936
+ logger.error(`GITIGNORE_READ_FAILED: Unable to read .gitignore | Path: ${gitignorePath} | Error: ${error.message}`);
937
+ throw new Error(`Failed to read .gitignore: ${error.message}`);
938
+ }
939
+ // Parse .gitignore into lines, ignoring comments and empty lines
940
+ const gitignoreLines = gitignoreContent.split('\n').map((line)=>line.trim()).filter((line)=>line && !line.startsWith('#'));
941
+ // Check for missing patterns
942
+ const missingPatterns = [];
943
+ for (const pattern of requiredPatterns){
944
+ // Check if the pattern exists in any of the gitignore lines
945
+ const found = gitignoreLines.some((line)=>{
946
+ // Exact match
947
+ if (line === pattern) return true;
948
+ // Pattern with wildcard - check if the pattern or any matching line exists
949
+ if (pattern.includes('*')) {
950
+ const basePattern = pattern.replace('*', '');
951
+ // Accept exact wildcard pattern, base pattern, or any line starting with base
952
+ return line === pattern || line === basePattern || line.startsWith(basePattern);
953
+ }
954
+ // Pattern with trailing slash - check both with and without slash
955
+ if (pattern.endsWith('/')) {
956
+ const basePattern = pattern.slice(0, -1);
957
+ return line === basePattern || line === pattern || line.startsWith(pattern);
958
+ }
959
+ // Line with trailing slash - check if it matches the pattern (e.g., "node_modules/" matches "node_modules")
960
+ if (line.endsWith('/')) {
961
+ const lineBase = line.slice(0, -1);
962
+ return lineBase === pattern || line.startsWith(pattern + '/');
963
+ }
964
+ return false;
965
+ });
966
+ if (!found) {
967
+ missingPatterns.push(pattern);
968
+ }
969
+ }
970
+ // Report missing patterns (relaxed check - allow variations)
971
+ const criticalMissing = missingPatterns.filter((p)=>!p.includes('coverage') && p !== 'package-lock.json');
972
+ if (criticalMissing.length > 0) {
973
+ logger.error('GITIGNORE_INCOMPLETE: Required patterns missing from .gitignore | Path: ' + gitignorePath + ' | Count: ' + criticalMissing.length);
974
+ logger.error('');
975
+ logger.error('GITIGNORE_MISSING_PATTERNS: The following patterns must be added to .gitignore:');
976
+ for (const pattern of criticalMissing){
977
+ logger.error(` ${pattern}`);
978
+ }
979
+ logger.error('');
980
+ logger.error('GITIGNORE_WHY_REQUIRED: These patterns are required because:');
981
+ logger.error(' node_modules - Dependencies should not be committed');
982
+ logger.error(' dist - Build artifacts should not be committed');
983
+ logger.error(' .env - Environment variables may contain secrets');
984
+ logger.error(' output/ - Build output directory should not be committed');
985
+ logger.error(' .kodrdriv* - kodrdriv internal files should not be committed');
986
+ logger.error('');
987
+ throw new Error(`Missing required .gitignore patterns: ${criticalMissing.join(', ')}. Please add them to your .gitignore file.`);
988
+ } else if (missingPatterns.length > 0) {
989
+ logger.warn('GITIGNORE_VARIATION: Some patterns have variations in .gitignore (acceptable) | Patterns: ' + missingPatterns.join(', '));
990
+ }
991
+ logger.verbose('GITIGNORE_VERIFIED: All required patterns present in .gitignore | Path: ' + gitignorePath + ' | Status: valid');
992
+ };
993
+ /**
994
+ * Checks if package-lock.json contains file: dependencies (from npm link)
995
+ * and cleans them up if found by removing package-lock.json and regenerating it.
996
+ */ const cleanupNpmLinkReferences = async (isDryRun)=>{
997
+ const logger = getDryRunLogger(isDryRun);
998
+ const packageLockPath = path.join(process.cwd(), 'package-lock.json');
999
+ try {
1000
+ // Check if package-lock.json exists
1001
+ try {
1002
+ await fs.access(packageLockPath);
1003
+ } catch {
1004
+ // No package-lock.json, nothing to clean
1005
+ logger.verbose('PACKAGE_LOCK_NOT_FOUND: No package-lock.json file exists | Action: Skipping npm link cleanup | Path: ' + packageLockPath);
1006
+ return;
1007
+ }
1008
+ // Read and parse package-lock.json
1009
+ const packageLockContent = await fs.readFile(packageLockPath, 'utf-8');
1010
+ const packageLock = safeJsonParse(packageLockContent, packageLockPath);
1011
+ // Check for file: dependencies in the lockfile
1012
+ let hasFileReferences = false;
1013
+ // Check in packages (npm v7+)
1014
+ if (packageLock.packages) {
1015
+ for (const [pkgPath, pkgInfo] of Object.entries(packageLock.packages)){
1016
+ if (pkgInfo.resolved && typeof pkgInfo.resolved === 'string' && pkgInfo.resolved.startsWith('file:')) {
1017
+ // Check if it's a relative path (from npm link) rather than a workspace path
1018
+ const resolvedPath = pkgInfo.resolved.replace('file:', '');
1019
+ if (resolvedPath.startsWith('../') || resolvedPath.startsWith('./')) {
1020
+ hasFileReferences = true;
1021
+ logger.verbose(`NPM_LINK_DETECTED: Found npm link reference in packages section | Package: ${pkgPath} | Resolved: ${pkgInfo.resolved} | Type: relative_file_dependency`);
1022
+ break;
1023
+ }
1024
+ }
1025
+ }
1026
+ }
1027
+ // Check in dependencies (npm v6)
1028
+ if (!hasFileReferences && packageLock.dependencies) {
1029
+ for (const [pkgName, pkgInfo] of Object.entries(packageLock.dependencies)){
1030
+ if (pkgInfo.version && typeof pkgInfo.version === 'string' && pkgInfo.version.startsWith('file:')) {
1031
+ const versionPath = pkgInfo.version.replace('file:', '');
1032
+ if (versionPath.startsWith('../') || versionPath.startsWith('./')) {
1033
+ hasFileReferences = true;
1034
+ logger.verbose(`NPM_LINK_DETECTED: Found npm link reference in dependencies section | Package: ${pkgName} | Version: ${pkgInfo.version} | Type: relative_file_dependency`);
1035
+ break;
1036
+ }
1037
+ }
1038
+ }
1039
+ }
1040
+ if (hasFileReferences) {
1041
+ logger.info('NPM_LINK_CLEANUP_REQUIRED: Detected npm link references in package-lock.json | File: package-lock.json | Impact: Must be cleaned before publish');
1042
+ logger.info('NPM_LINK_CLEANUP_STARTING: Removing package-lock.json and regenerating clean version | Action: Remove file with relative dependencies');
1043
+ if (isDryRun) {
1044
+ logger.info('DRY_RUN_OPERATION: Would remove package-lock.json and regenerate it | Mode: dry-run | File: package-lock.json');
1045
+ } else {
1046
+ // Remove package-lock.json
1047
+ await fs.unlink(packageLockPath);
1048
+ logger.verbose('NPM_LINK_CLEANUP_FILE_REMOVED: Deleted package-lock.json containing npm link references | Path: ' + packageLockPath);
1049
+ // Regenerate clean package-lock.json
1050
+ logger.verbose('NPM_LOCK_REGENERATING: Executing npm install to regenerate package-lock.json from package.json | Command: npm install --package-lock-only --no-audit --no-fund');
1051
+ await runWithDryRunSupport('npm install --package-lock-only --no-audit --no-fund', isDryRun);
1052
+ logger.info('NPM_LOCK_REGENERATED: Successfully regenerated clean package-lock.json without link references | Path: ' + packageLockPath);
1053
+ }
1054
+ } else {
1055
+ logger.verbose('NPM_LINK_CHECK_CLEAN: No npm link references found in package-lock.json | Status: Ready for publish | File: ' + packageLockPath);
1056
+ }
1057
+ } catch (error) {
1058
+ // Log warning but don't fail - let npm update handle any issues
1059
+ logger.warn(`NPM_LINK_CHECK_FAILED: Unable to check or clean npm link references | Error: ${error.message} | Impact: Continuing with publish, npm will handle issues`);
1060
+ logger.verbose('PUBLISH_PROCESS_CONTINUING: Proceeding with publish workflow despite npm link check failure | Next: Standard npm publish validation');
1061
+ }
1062
+ };
1063
+ const validateEnvironmentVariables = (requiredEnvVars, isDryRun)=>{
1064
+ const logger = getDryRunLogger(isDryRun);
1065
+ const missingEnvVars = [];
1066
+ for (const envVar of requiredEnvVars){
1067
+ if (!process.env[envVar]) {
1068
+ missingEnvVars.push(envVar);
1069
+ }
1070
+ }
1071
+ if (missingEnvVars.length > 0) {
1072
+ if (isDryRun) {
1073
+ logger.warn(`ENV_VARS_MISSING: Required environment variables not set | Variables: ${missingEnvVars.join(', ')} | Mode: dry-run | Impact: Would fail in real publish`);
1074
+ } else {
1075
+ logger.error(`ENV_VARS_MISSING: Required environment variables not set | Variables: ${missingEnvVars.join(', ')} | Action: Must set before publish | Source: .npmrc configuration`);
1076
+ throw new Error(`Missing required environment variables: ${missingEnvVars.join(', ')}. Please set these environment variables before running publish.`);
1077
+ }
1078
+ }
1079
+ };
1080
+ const runPrechecks = async (runConfig, targetBranch)=>{
1081
+ var _runConfig_publish, _runConfig_publish1;
1082
+ const isDryRun = runConfig.dryRun || false;
1083
+ const logger = getDryRunLogger(isDryRun);
1084
+ const storage = createStorage();
1085
+ logger.info('PRECHECK_STARTING: Executing publish prechecks | Phase: validation | Target: ' + (targetBranch || 'default'));
1086
+ // Check .gitignore patterns first (critical requirement)
1087
+ logger.info('PRECHECK_GITIGNORE: Verifying .gitignore contains required patterns | Requirement: Must ignore development artifacts and sensitive files');
1088
+ if (isDryRun) {
1089
+ logger.info('PRECHECK_GITIGNORE: Would verify .gitignore patterns | Mode: dry-run | Patterns: node_modules, dist, package-lock.json, .env, output/, coverage, .kodrdriv*');
1090
+ } else {
1091
+ await checkGitignorePatterns(storage, isDryRun);
1092
+ }
1093
+ // Check if we're in a git repository
1094
+ try {
1095
+ if (isDryRun) {
1096
+ logger.info('PRECHECK_GIT_REPO: Would verify git repository | Mode: dry-run | Command: git rev-parse --git-dir');
1097
+ } else {
1098
+ await run('git rev-parse --git-dir');
1099
+ }
1100
+ } catch (error) {
1101
+ if (!isDryRun) {
1102
+ // Preserve the original error message to help with debugging
1103
+ const originalMessage = error.message || error.toString();
1104
+ throw new Error(`Not in a git repository or git command failed: ${originalMessage}. Please run this command from within a git repository.`);
1105
+ }
1106
+ }
1107
+ // Check for uncommitted changes
1108
+ logger.info('PRECHECK_GIT_STATUS: Checking for uncommitted changes | Command: git status --porcelain | Requirement: Clean working directory');
1109
+ try {
1110
+ if (isDryRun) {
1111
+ logger.info('PRECHECK_GIT_STATUS: Would verify clean working directory | Mode: dry-run | Command: git status --porcelain');
1112
+ } else {
1113
+ const { stdout } = await run('git status --porcelain');
1114
+ if (stdout.trim()) {
1115
+ throw new Error('Working directory has uncommitted changes. Please commit or stash your changes before running publish.');
1116
+ }
1117
+ }
1118
+ } catch (error) {
1119
+ if (!isDryRun) {
1120
+ // Preserve the original error message to help with debugging
1121
+ const originalMessage = error.message || error.toString();
1122
+ throw new Error(`Failed to check git status: ${originalMessage}. Please ensure you are in a valid git repository and try again.`);
1123
+ }
1124
+ }
1125
+ // Use the passed target branch or fallback to config/default
1126
+ const effectiveTargetBranch = targetBranch || ((_runConfig_publish = runConfig.publish) === null || _runConfig_publish === void 0 ? void 0 : _runConfig_publish.targetBranch) || 'main';
1127
+ // Check that we're not running from the target branch
1128
+ logger.info('PRECHECK_BRANCH: Verifying current branch is not target branch | Target: ' + effectiveTargetBranch + ' | Requirement: Must run from feature branch');
1129
+ if (isDryRun) {
1130
+ logger.info(`PRECHECK_BRANCH: Would verify current branch is not target branch | Mode: dry-run | Target: ${effectiveTargetBranch}`);
1131
+ } else {
1132
+ const currentBranch = await GitHub.getCurrentBranchName();
1133
+ if (currentBranch === effectiveTargetBranch) {
1134
+ throw new Error(`Cannot run publish from the target branch '${effectiveTargetBranch}'. Please switch to a different branch before running publish.`);
1135
+ }
1136
+ }
1137
+ // Check target branch sync with remote
1138
+ logger.info(`PRECHECK_BRANCH_SYNC: Checking target branch sync with remote | Branch: ${effectiveTargetBranch} | Remote: origin | Requirement: Branches must be synchronized`);
1139
+ if (isDryRun) {
1140
+ logger.info(`PRECHECK_BRANCH_SYNC: Would verify target branch is in sync with remote | Mode: dry-run | Branch: ${effectiveTargetBranch} | Remote: origin`);
1141
+ } else {
1142
+ // Only check if local target branch exists (it's okay if it doesn't exist locally)
1143
+ const targetBranchExists = await localBranchExists(effectiveTargetBranch);
1144
+ if (targetBranchExists) {
1145
+ const syncStatus = await isBranchInSyncWithRemote(effectiveTargetBranch);
1146
+ if (!syncStatus.inSync) {
1147
+ var _runConfig_publish2;
1148
+ logger.error(`BRANCH_SYNC_FAILED: Target branch not synchronized with remote | Branch: ${effectiveTargetBranch} | Status: out-of-sync | Impact: Cannot proceed with publish`);
1149
+ logger.error('');
1150
+ if (syncStatus.error) {
1151
+ logger.error(`BRANCH_SYNC_ERROR: ${syncStatus.error}`);
1152
+ } else if (syncStatus.localSha && syncStatus.remoteSha) {
1153
+ logger.error(`BRANCH_SYNC_DIVERGENCE: Local and remote commits differ | Local SHA: ${syncStatus.localSha.substring(0, 8)} | Remote SHA: ${syncStatus.remoteSha.substring(0, 8)}`);
1154
+ }
1155
+ // Check if agentic publish is enabled
1156
+ if ((_runConfig_publish2 = runConfig.publish) === null || _runConfig_publish2 === void 0 ? void 0 : _runConfig_publish2.agenticPublish) {
1157
+ logger.info('');
1158
+ logger.info('AGENTIC_PUBLISH_STARTING: Attempting automatic diagnosis and fix | Mode: agentic | Feature: AI-powered recovery');
1159
+ try {
1160
+ var _syncStatus_localSha, _syncStatus_remoteSha, _runConfig_publish3;
1161
+ const currentBranch = await GitHub.getCurrentBranchName();
1162
+ const agenticResult = await runAgenticPublish({
1163
+ targetBranch: effectiveTargetBranch,
1164
+ sourceBranch: currentBranch,
1165
+ issue: 'branch_sync',
1166
+ issueDetails: syncStatus.error || `Local SHA: ${(_syncStatus_localSha = syncStatus.localSha) === null || _syncStatus_localSha === void 0 ? void 0 : _syncStatus_localSha.substring(0, 8)}, Remote SHA: ${(_syncStatus_remoteSha = syncStatus.remoteSha) === null || _syncStatus_remoteSha === void 0 ? void 0 : _syncStatus_remoteSha.substring(0, 8)}`,
1167
+ workingDirectory: process.cwd(),
1168
+ maxIterations: ((_runConfig_publish3 = runConfig.publish) === null || _runConfig_publish3 === void 0 ? void 0 : _runConfig_publish3.agenticPublishMaxIterations) || 10,
1169
+ storage,
1170
+ logger,
1171
+ dryRun: runConfig.dryRun
1172
+ });
1173
+ // Display the formatted result
1174
+ const formattedResult = formatAgenticPublishResult(agenticResult);
1175
+ logger.info(formattedResult);
1176
+ if (agenticResult.success) {
1177
+ logger.info('AGENTIC_PUBLISH_SUCCESS: Issue resolved automatically | Status: ready-to-retry | Action: Re-running prechecks');
1178
+ // Re-run the sync check to verify it was fixed
1179
+ const reSyncStatus = await isBranchInSyncWithRemote(effectiveTargetBranch);
1180
+ if (reSyncStatus.inSync) {
1181
+ logger.info(`BRANCH_SYNC_VERIFIED: Target branch is now synchronized with remote | Branch: ${effectiveTargetBranch} | Status: in-sync`);
1182
+ return; // Continue with publish
1183
+ } else {
1184
+ logger.warn('AGENTIC_PUBLISH_VERIFICATION_FAILED: Branch still not in sync after agentic fix | Status: needs-attention');
1185
+ }
1186
+ }
1187
+ if (agenticResult.requiresManualIntervention) {
1188
+ throw new Error(`Target branch '${effectiveTargetBranch}' requires manual intervention. Please see the steps above.`);
1189
+ } else {
1190
+ throw new Error(`Agentic publish could not resolve the issue automatically. Please see the analysis above.`);
1191
+ }
1192
+ } catch (agenticError) {
1193
+ logger.warn(`AGENTIC_PUBLISH_FAILED: Agentic recovery failed | Error: ${agenticError.message} | Fallback: Manual steps`);
1194
+ // Fall through to manual steps
1195
+ }
1196
+ }
1197
+ logger.error('');
1198
+ logger.error('RESOLUTION_STEPS: Manual intervention required to sync branches:');
1199
+ logger.error(` Step 1: Switch to target branch | Command: git checkout ${effectiveTargetBranch}`);
1200
+ logger.error(` Step 2: Pull latest changes | Command: git pull origin ${effectiveTargetBranch}`);
1201
+ logger.error(' Step 3: Resolve merge conflicts if present');
1202
+ logger.error(' Step 4: Return to feature branch and retry publish');
1203
+ logger.error('');
1204
+ logger.error(`ALTERNATIVE_OPTION: Automatic sync available | Command: kodrdriv publish --sync-target | Branch: ${effectiveTargetBranch}`);
1205
+ logger.error(`ALTERNATIVE_OPTION_AI: AI-powered recovery available | Command: kodrdriv publish --agentic-publish | Branch: ${effectiveTargetBranch}`);
1206
+ throw new Error(`Target branch '${effectiveTargetBranch}' is not in sync with remote. Please sync the branch before running publish.`);
1207
+ } else {
1208
+ logger.info(`BRANCH_SYNC_VERIFIED: Target branch is synchronized with remote | Branch: ${effectiveTargetBranch} | Status: in-sync`);
1209
+ }
1210
+ } else {
1211
+ logger.info(`BRANCH_NOT_LOCAL: Target branch does not exist locally | Branch: ${effectiveTargetBranch} | Action: Will be created during publish process`);
1212
+ }
1213
+ }
1214
+ // Check GitHub Actions workflow configuration
1215
+ logger.info('PRECHECK_WORKFLOW: Checking GitHub Actions workflow configuration | Target: PR automation | Requirement: Workflows should trigger on pull requests');
1216
+ if (isDryRun) {
1217
+ logger.info('PRECHECK_WORKFLOW: Would check if GitHub Actions workflows are configured for pull requests | Mode: dry-run');
1218
+ } else {
1219
+ try {
1220
+ // TODO: Re-enable when checkWorkflowConfiguration is exported from github-tools
1221
+ // const workflowConfig = await GitHub.checkWorkflowConfiguration(effectiveTargetBranch);
1222
+ const workflowConfig = {
1223
+ hasWorkflows: true,
1224
+ hasPullRequestTriggers: true,
1225
+ workflowCount: 0,
1226
+ triggeredWorkflowNames: []
1227
+ };
1228
+ if (!workflowConfig.hasWorkflows) ; else if (!workflowConfig.hasPullRequestTriggers) ; else {
1229
+ logger.info(`WORKFLOW_CONFIGURED: Found workflows that will trigger on pull requests | Target Branch: ${effectiveTargetBranch} | Workflow Count: ${workflowConfig.triggeredWorkflowNames.length}`);
1230
+ for (const workflowName of workflowConfig.triggeredWorkflowNames){
1231
+ logger.info(`WORKFLOW_ACTIVE: ${workflowName} | Trigger: pull_request | Target: ${effectiveTargetBranch}`);
1232
+ }
1233
+ }
1234
+ } catch (error) {
1235
+ // Don't fail the precheck if we can't verify workflows
1236
+ // The wait logic will handle it later
1237
+ logger.debug(`WORKFLOW_CHECK_FAILED: Unable to verify workflow configuration | Error: ${error.message} | Impact: Will proceed with publish | Note: Wait logic will handle checks later`);
1238
+ }
1239
+ }
1240
+ // Check if prepublishOnly script exists in package.json
1241
+ logger.info('PRECHECK_PREPUBLISH: Checking for prepublishOnly script in package.json | Requirement: Must exist to run pre-flight checks | Expected: clean, lint, build, test');
1242
+ const packageJsonPath = path.join(process.cwd(), 'package.json');
1243
+ if (!await storage.exists(packageJsonPath)) {
1244
+ if (!isDryRun) {
1245
+ throw new Error('package.json not found in current directory.');
1246
+ } else {
1247
+ logger.warn('PACKAGE_JSON_NOT_FOUND: No package.json in current directory | Mode: dry-run | Impact: Cannot verify prepublishOnly script | Path: ' + packageJsonPath);
1248
+ }
1249
+ } else {
1250
+ var _packageJson_scripts;
1251
+ let packageJson;
1252
+ try {
1253
+ const packageJsonContents = await storage.readFile(packageJsonPath, 'utf-8');
1254
+ const parsed = safeJsonParse(packageJsonContents, packageJsonPath);
1255
+ packageJson = validatePackageJson(parsed, packageJsonPath);
1256
+ } catch (error) {
1257
+ if (!isDryRun) {
1258
+ throw new Error('Failed to parse package.json. Please ensure it contains valid JSON.');
1259
+ } else {
1260
+ logger.warn('PACKAGE_JSON_PARSE_FAILED: Unable to parse package.json | Mode: dry-run | Impact: Cannot verify prepublishOnly script | Path: ' + packageJsonPath + ' | Requirement: Valid JSON format');
1261
+ }
1262
+ }
1263
+ if (packageJson && !((_packageJson_scripts = packageJson.scripts) === null || _packageJson_scripts === void 0 ? void 0 : _packageJson_scripts.prepublishOnly)) {
1264
+ if (!isDryRun) {
1265
+ throw new Error('prepublishOnly script is required in package.json but was not found. Please add a prepublishOnly script that runs your pre-flight checks (e.g., clean, lint, build, test).');
1266
+ } else {
1267
+ logger.warn('PREPUBLISH_SCRIPT_MISSING: No prepublishOnly script found in package.json | Mode: dry-run | Requirement: Script must exist | Expected Tasks: clean, lint, build, test | Path: ' + packageJsonPath);
1268
+ }
1269
+ }
1270
+ }
1271
+ // Check required environment variables
1272
+ logger.verbose('PRECHECK_ENV_VARS: Checking required environment variables | Source: Configuration and .npmrc | Requirement: All required vars must be set');
1273
+ const coreRequiredEnvVars = ((_runConfig_publish1 = runConfig.publish) === null || _runConfig_publish1 === void 0 ? void 0 : _runConfig_publish1.requiredEnvVars) || [];
1274
+ const npmrcEnvVars = isDryRun ? [] : await scanNpmrcForEnvVars(storage); // Skip .npmrc scan in dry run
1275
+ const allRequiredEnvVars = [
1276
+ ...new Set([
1277
+ ...coreRequiredEnvVars,
1278
+ ...npmrcEnvVars
1279
+ ])
1280
+ ];
1281
+ if (allRequiredEnvVars.length > 0) {
1282
+ logger.verbose(`ENV_VARS_REQUIRED: Environment variables needed for publish | Variables: ${allRequiredEnvVars.join(', ')} | Count: ${allRequiredEnvVars.length} | Source: config + .npmrc`);
1283
+ validateEnvironmentVariables(allRequiredEnvVars, isDryRun);
1284
+ } else {
1285
+ logger.verbose('ENV_VARS_NONE: No required environment variables specified | Status: No validation needed | Source: config + .npmrc');
1286
+ }
1287
+ logger.info('PRECHECK_COMPLETE: All publish prechecks passed successfully | Status: Ready to proceed | Next: Execute publish workflow');
1288
+ };
1289
+ // Helper: deep-sort object keys for stable comparison
1290
+ const sortObjectKeys = (value)=>{
1291
+ if (Array.isArray(value)) {
1292
+ return value.map(sortObjectKeys);
1293
+ }
1294
+ if (value && typeof value === 'object') {
1295
+ const sorted = {};
1296
+ Object.keys(value).sort().forEach((key)=>{
1297
+ sorted[key] = sortObjectKeys(value[key]);
1298
+ });
1299
+ return sorted;
1300
+ }
1301
+ return value;
1302
+ };
1303
+ // Determine if there are substantive changes compared to the target branch (beyond just version bump)
1304
+ const isReleaseNecessaryComparedToTarget = async (targetBranch, isDryRun)=>{
1305
+ const logger = getDryRunLogger(isDryRun);
1306
+ // We compare current HEAD branch to the provided target branch
1307
+ const currentBranch = await GitHub.getCurrentBranchName();
1308
+ // Check if target branch exists before trying to compare
1309
+ try {
1310
+ // Validate target branch exists and is accessible
1311
+ await runSecure('git', [
1312
+ 'rev-parse',
1313
+ '--verify',
1314
+ targetBranch
1315
+ ]);
1316
+ } catch (error) {
1317
+ // Target branch doesn't exist or isn't accessible
1318
+ logger.verbose(`RELEASE_CHECK_NO_TARGET: Target branch does not exist or is not accessible | Branch: ${targetBranch} | Action: Proceeding with publish | Reason: First release to this branch`);
1319
+ return {
1320
+ necessary: true,
1321
+ reason: `Target branch '${targetBranch}' does not exist; first release to this branch`
1322
+ };
1323
+ }
1324
+ // If branches are identical, nothing to release
1325
+ const { stdout: namesStdout } = await runSecure('git', [
1326
+ 'diff',
1327
+ '--name-only',
1328
+ `${targetBranch}..${currentBranch}`
1329
+ ]);
1330
+ const changedFiles = namesStdout.split('\n').map((s)=>s.trim()).filter(Boolean);
1331
+ if (changedFiles.length === 0) {
1332
+ // No definitive signal; proceed with publish rather than skipping
1333
+ return {
1334
+ necessary: true,
1335
+ reason: 'No detectable changes via diff; proceeding conservatively'
1336
+ };
1337
+ }
1338
+ // If any files changed other than package.json (package-lock.json is gitignored), a release is necessary
1339
+ const nonVersionFiles = changedFiles.filter((f)=>f !== 'package.json');
1340
+ if (nonVersionFiles.length > 0) {
1341
+ return {
1342
+ necessary: true,
1343
+ reason: `Changed files beyond version bump: ${nonVersionFiles.join(', ')}`
1344
+ };
1345
+ }
1346
+ // Only package.json changed. Verify package.json change is only the version field
1347
+ try {
1348
+ // Read package.json content from both branches
1349
+ const { stdout: basePkgStdout } = await runSecure('git', [
1350
+ 'show',
1351
+ `${targetBranch}:package.json`
1352
+ ]);
1353
+ const { stdout: headPkgStdout } = await runSecure('git', [
1354
+ 'show',
1355
+ `${currentBranch}:package.json`
1356
+ ]);
1357
+ const basePkg = validatePackageJson(safeJsonParse(basePkgStdout, `${targetBranch}:package.json`), `${targetBranch}:package.json`);
1358
+ const headPkg = validatePackageJson(safeJsonParse(headPkgStdout, `${currentBranch}:package.json`), `${currentBranch}:package.json`);
1359
+ const { version: _baseVersion, ...baseWithoutVersion } = basePkg;
1360
+ const { version: _headVersion, ...headWithoutVersion } = headPkg;
1361
+ const baseSorted = sortObjectKeys(baseWithoutVersion);
1362
+ const headSorted = sortObjectKeys(headWithoutVersion);
1363
+ const equalExceptVersion = JSON.stringify(baseSorted) === JSON.stringify(headSorted);
1364
+ if (equalExceptVersion) {
1365
+ const currentVersion = headPkg.version;
1366
+ const targetVersion = basePkg.version;
1367
+ return {
1368
+ necessary: false,
1369
+ reason: `No meaningful changes detected:\n • Current version: ${currentVersion}\n • Target branch version: ${targetVersion}\n • Only package.json version field differs\n\n To force republish: Add meaningful code changes or use --force (not yet implemented)`
1370
+ };
1371
+ }
1372
+ // Other fields changed inside package.json
1373
+ return {
1374
+ necessary: true,
1375
+ reason: 'package.json changes beyond version field'
1376
+ };
1377
+ } catch (error) {
1378
+ // Conservative: if we cannot prove it is only a version change, proceed with release
1379
+ logger.verbose(`RELEASE_CHECK_COMPARISON_FAILED: Unable to conclusively compare package.json changes | Error: ${error.message} | Action: Proceeding conservatively with publish | Reason: Cannot verify version-only change`);
1380
+ return {
1381
+ necessary: true,
1382
+ reason: 'Could not compare package.json safely'
1383
+ };
1384
+ }
1385
+ };
1386
+ const handleTargetBranchSyncRecovery = async (runConfig, targetBranch)=>{
1387
+ const isDryRun = runConfig.dryRun || false;
1388
+ const logger = getDryRunLogger(isDryRun);
1389
+ logger.info(`BRANCH_SYNC_ATTEMPTING: Initiating sync of target branch with remote | Branch: ${targetBranch} | Remote: origin | Operation: fetch + merge`);
1390
+ if (isDryRun) {
1391
+ logger.info(`BRANCH_SYNC_DRY_RUN: Would attempt to sync branch with remote | Mode: dry-run | Branch: ${targetBranch} | Remote: origin`);
1392
+ return;
1393
+ }
1394
+ const syncResult = await safeSyncBranchWithRemote(targetBranch);
1395
+ if (syncResult.success) {
1396
+ logger.info(`BRANCH_SYNC_SUCCESS: Successfully synchronized branch with remote | Branch: ${targetBranch} | Remote: origin | Status: in-sync`);
1397
+ logger.info('BRANCH_SYNC_NEXT_STEP: Ready to proceed with publish | Action: Re-run publish command | Branch: ' + targetBranch);
1398
+ } else if (syncResult.conflictResolutionRequired) {
1399
+ logger.error(`BRANCH_SYNC_CONFLICTS: Sync failed due to merge conflicts | Branch: ${targetBranch} | Status: conflicts-detected | Resolution: Manual intervention required`);
1400
+ logger.error('');
1401
+ logger.error('CONFLICT_RESOLUTION_STEPS: Manual conflict resolution required:');
1402
+ logger.error(` Step 1: Switch to target branch | Command: git checkout ${targetBranch}`);
1403
+ logger.error(` Step 2: Pull and resolve conflicts | Command: git pull origin ${targetBranch}`);
1404
+ logger.error(' Step 3: Commit resolved changes | Command: git commit');
1405
+ logger.error(' Step 4: Return to feature branch and retry | Command: kodrdriv publish');
1406
+ logger.error('');
1407
+ throw new Error(`Target branch '${targetBranch}' has conflicts that require manual resolution.`);
1408
+ } else {
1409
+ logger.error(`BRANCH_SYNC_FAILED: Sync operation failed | Branch: ${targetBranch} | Error: ${syncResult.error} | Remote: origin`);
1410
+ throw new Error(`Failed to sync target branch: ${syncResult.error}`);
1411
+ }
1412
+ };
1413
+ const execute = async (runConfig)=>{
1414
+ var _runConfig_publish, _runConfig_publish1, _runConfig_publish2, _runConfig_publish3;
1415
+ const isDryRun = runConfig.dryRun || false;
1416
+ const logger = getDryRunLogger(isDryRun);
1417
+ const storage = createStorage();
1418
+ // Get current branch for branch-dependent targeting
1419
+ let currentBranch;
1420
+ if (isDryRun) {
1421
+ currentBranch = 'mock-branch';
1422
+ } else {
1423
+ currentBranch = await GitHub.getCurrentBranchName();
1424
+ // Fetch latest remote information to avoid conflicts
1425
+ logger.info('GIT_FETCH_STARTING: Fetching latest remote information | Remote: origin | Purpose: Avoid conflicts during publish | Command: git fetch origin');
1426
+ try {
1427
+ await run('git fetch origin');
1428
+ logger.info('GIT_FETCH_SUCCESS: Successfully fetched latest remote information | Remote: origin | Status: up-to-date');
1429
+ } catch (error) {
1430
+ logger.warn(`GIT_FETCH_FAILED: Unable to fetch from remote | Remote: origin | Error: ${error.message} | Impact: May cause conflicts if remote has changes`);
1431
+ }
1432
+ // Sync current branch with remote to avoid conflicts
1433
+ logger.info(`CURRENT_BRANCH_SYNC: Synchronizing current branch with remote | Branch: ${currentBranch} | Remote: origin | Purpose: Avoid conflicts during publish`);
1434
+ try {
1435
+ const remoteExists = await run(`git ls-remote --exit-code --heads origin ${currentBranch}`).then(()=>true).catch(()=>false);
1436
+ if (remoteExists) {
1437
+ // Use explicit fetch+merge instead of pull to avoid git config conflicts
1438
+ await runGitWithLock(process.cwd(), async ()=>{
1439
+ await run(`git fetch origin ${currentBranch}`);
1440
+ await run(`git merge origin/${currentBranch} --no-edit`);
1441
+ }, `sync ${currentBranch}`);
1442
+ logger.info(`CURRENT_BRANCH_SYNCED: Successfully synchronized current branch with remote | Branch: ${currentBranch} | Remote: origin/${currentBranch} | Status: in-sync`);
1443
+ } else {
1444
+ logger.info(`REMOTE_BRANCH_NOT_FOUND: No remote branch exists | Branch: ${currentBranch} | Remote: origin | Action: Will be created on first push`);
1445
+ }
1446
+ } catch (error) {
1447
+ if (error.message && error.message.includes('CONFLICT')) {
1448
+ logger.error(`MERGE_CONFLICTS_DETECTED: Conflicts found when syncing current branch with remote | Branch: ${currentBranch} | Remote: origin/${currentBranch} | Status: conflicts-require-resolution`);
1449
+ logger.error(`CONFLICT_RESOLUTION_REQUIRED: Manual intervention needed to resolve conflicts and continue:`);
1450
+ logger.error(` Step 1: Resolve conflicts in affected files`);
1451
+ logger.error(` Step 2: Stage resolved files | Command: git add <resolved-files>`);
1452
+ logger.error(` Step 3: Commit resolution | Command: git commit`);
1453
+ logger.error(` Step 4: Retry publish | Command: kodrdriv publish`);
1454
+ throw new Error(`Merge conflicts detected when syncing ${currentBranch} with remote. Please resolve conflicts manually.`);
1455
+ } else {
1456
+ logger.warn(`CURRENT_BRANCH_SYNC_FAILED: Unable to sync current branch with remote | Branch: ${currentBranch} | Remote: origin/${currentBranch} | Error: ${error.message} | Impact: May cause issues during publish`);
1457
+ }
1458
+ }
1459
+ }
1460
+ // Determine target branch and version strategy based on branch configuration
1461
+ let targetBranch = ((_runConfig_publish = runConfig.publish) === null || _runConfig_publish === void 0 ? void 0 : _runConfig_publish.targetBranch) || 'main';
1462
+ let branchDependentVersioning = false;
1463
+ // Check for branches configuration
1464
+ if (runConfig.branches && runConfig.branches[currentBranch]) {
1465
+ branchDependentVersioning = true;
1466
+ const branchConfig = runConfig.branches[currentBranch];
1467
+ if (branchConfig.targetBranch) {
1468
+ targetBranch = branchConfig.targetBranch;
1469
+ }
1470
+ logger.info(`BRANCH_DEPENDENT_TARGETING: Branch-specific configuration active | Source: ${currentBranch} | Target: ${targetBranch} | Feature: Branch-dependent versioning and targeting`);
1471
+ logger.info(`BRANCH_CONFIGURATION_SOURCE: Current branch | Branch: ${currentBranch} | Type: source`);
1472
+ logger.info(`BRANCH_CONFIGURATION_TARGET: Target branch for publish | Branch: ${targetBranch} | Type: destination`);
1473
+ // Look at target branch config to show version strategy
1474
+ const targetBranchConfig = runConfig.branches[targetBranch];
1475
+ if (targetBranchConfig === null || targetBranchConfig === void 0 ? void 0 : targetBranchConfig.version) {
1476
+ const versionType = targetBranchConfig.version.type;
1477
+ const versionTag = targetBranchConfig.version.tag;
1478
+ const versionIncrement = targetBranchConfig.version.increment;
1479
+ logger.info(`VERSION_STRATEGY: Target branch version configuration | Branch: ${targetBranch} | Type: ${versionType} | Tag: ${versionTag || 'none'} | Increment: ${versionIncrement ? 'enabled' : 'disabled'}`);
1480
+ }
1481
+ } else {
1482
+ logger.debug(`BRANCH_TARGETING_DEFAULT: No branch-specific configuration found | Branch: ${currentBranch} | Action: Using default target | Target: ${targetBranch}`);
1483
+ }
1484
+ // Handle --sync-target flag
1485
+ if ((_runConfig_publish1 = runConfig.publish) === null || _runConfig_publish1 === void 0 ? void 0 : _runConfig_publish1.syncTarget) {
1486
+ await handleTargetBranchSyncRecovery(runConfig, targetBranch);
1487
+ return; // Exit after sync operation
1488
+ }
1489
+ // Check if target branch exists and create it if needed
1490
+ logger.info(`TARGET_BRANCH_CHECK: Verifying target branch existence | Branch: ${targetBranch} | Action: Create if missing | Source: Current HEAD`);
1491
+ if (isDryRun) {
1492
+ logger.info(`TARGET_BRANCH_CHECK: Would verify target branch exists and create if needed | Mode: dry-run | Branch: ${targetBranch}`);
1493
+ } else {
1494
+ const targetBranchExists = await localBranchExists(targetBranch);
1495
+ if (!targetBranchExists) {
1496
+ // Check if it exists on remote
1497
+ const remoteExists = await remoteBranchExists(targetBranch);
1498
+ if (remoteExists) {
1499
+ logger.info(`TARGET_BRANCH_TRACKING: Target branch exists on remote but not locally, tracking origin/${targetBranch} | Branch: ${targetBranch}`);
1500
+ try {
1501
+ await runGitWithLock(process.cwd(), async ()=>{
1502
+ // Create local branch tracking remote
1503
+ await runSecure('git', [
1504
+ 'branch',
1505
+ targetBranch,
1506
+ `origin/${targetBranch}`
1507
+ ]);
1508
+ logger.info(`TARGET_BRANCH_CREATED: Successfully created local tracking branch | Branch: ${targetBranch} | Source: origin/${targetBranch}`);
1509
+ }, `track target branch ${targetBranch}`);
1510
+ } catch (error) {
1511
+ throw new Error(`Failed to track target branch '${targetBranch}': ${error.message}`);
1512
+ }
1513
+ } else {
1514
+ logger.info(`TARGET_BRANCH_CREATING: Target branch does not exist locally or on remote, creating from current branch | Branch: ${targetBranch} | Source: HEAD | Remote: origin`);
1515
+ try {
1516
+ // Wrap git branch and push operations with lock
1517
+ await runGitWithLock(process.cwd(), async ()=>{
1518
+ // Create the target branch from the current HEAD
1519
+ await runSecure('git', [
1520
+ 'branch',
1521
+ targetBranch,
1522
+ 'HEAD'
1523
+ ]);
1524
+ logger.info(`TARGET_BRANCH_CREATED: Successfully created target branch locally | Branch: ${targetBranch} | Source: HEAD`);
1525
+ // Push the new branch to origin
1526
+ await runSecure('git', [
1527
+ 'push',
1528
+ 'origin',
1529
+ targetBranch
1530
+ ]);
1531
+ logger.info(`TARGET_BRANCH_PUSHED: Successfully pushed new target branch to remote | Branch: ${targetBranch} | Remote: origin/${targetBranch}`);
1532
+ }, `create and push target branch ${targetBranch}`);
1533
+ } catch (error) {
1534
+ throw new Error(`Failed to create target branch '${targetBranch}': ${error.message}`);
1535
+ }
1536
+ }
1537
+ } else {
1538
+ logger.info(`TARGET_BRANCH_EXISTS: Target branch already exists locally | Branch: ${targetBranch} | Status: ready`);
1539
+ }
1540
+ }
1541
+ // Run prechecks before starting any work
1542
+ await runPrechecks(runConfig, targetBranch);
1543
+ // Early check: determine if a release is necessary compared to target branch
1544
+ logger.info('RELEASE_NECESSITY_CHECK: Evaluating if release is required | Comparison: current branch vs target | Target: ' + targetBranch + ' | Purpose: Avoid unnecessary publishes');
1545
+ try {
1546
+ const necessity = await isReleaseNecessaryComparedToTarget(targetBranch, isDryRun);
1547
+ if (!necessity.necessary) {
1548
+ logger.info(`\nRELEASE_SKIPPED: No meaningful changes detected, skipping publish | Reason: ${necessity.reason} | Target: ${targetBranch}`);
1549
+ // Emit a machine-readable marker so tree mode can detect skip and avoid propagating versions
1550
+ // CRITICAL: Use console.log to write to stdout (logger.info goes to stderr via winston)
1551
+ // eslint-disable-next-line no-console
1552
+ console.log('KODRDRIV_PUBLISH_SKIPPED');
1553
+ return;
1554
+ } else {
1555
+ logger.verbose(`RELEASE_PROCEEDING: Meaningful changes detected, continuing with publish | Reason: ${necessity.reason} | Target: ${targetBranch}`);
1556
+ }
1557
+ } catch (error) {
1558
+ // On unexpected errors, proceed with publish to avoid false negatives blocking releases
1559
+ logger.verbose(`RELEASE_NECESSITY_CHECK_ERROR: Unable to determine release necessity | Error: ${error.message} | Action: Proceeding conservatively with publish | Rationale: Avoid blocking valid releases`);
1560
+ }
1561
+ logger.info('RELEASE_PROCESS_STARTING: Initiating release workflow | Target: ' + targetBranch + ' | Phase: dependency updates and version management');
1562
+ let pr = null;
1563
+ let newVersion = ''; // Will be set during version determination phase
1564
+ if (isDryRun) {
1565
+ logger.info('PR_CHECK: Would check for existing pull request | Mode: dry-run | Action: Skip PR lookup');
1566
+ logger.info('PR_ASSUMPTION: Assuming no existing PR found | Mode: dry-run | Purpose: Demo workflow');
1567
+ } else {
1568
+ const branchName = await GitHub.getCurrentBranchName();
1569
+ pr = await GitHub.findOpenPullRequestByHeadRef(branchName);
1570
+ }
1571
+ if (pr) {
1572
+ logger.info(`PR_FOUND: Existing pull request detected for current branch | URL: ${pr.html_url} | Status: open`);
1573
+ } else {
1574
+ var _runConfig_publish4, _runConfig_publish5, _runConfig_publish6, _runConfig_publish7, _runConfig_publish8, _runConfig_publish9, _runConfig_publish10, _runConfig_publish11, _runConfig_publish12, _runConfig_publish13, _releaseConfig_release;
1575
+ logger.info('PR_NOT_FOUND: No open pull request exists for current branch | Action: Starting new release publishing process | Next: Prepare dependencies and version');
1576
+ // STEP 1: Prepare for release (update dependencies and run prepublish checks) with NO version bump yet
1577
+ logger.verbose('RELEASE_PREP_STARTING: Preparing for release | Phase: dependency management | Action: Switch from workspace to remote dependencies | Version Bump: Not yet applied');
1578
+ // Clean up any npm link references before updating dependencies
1579
+ if (!((_runConfig_publish4 = runConfig.publish) === null || _runConfig_publish4 === void 0 ? void 0 : _runConfig_publish4.skipLinkCleanup)) {
1580
+ logger.verbose('NPM_LINK_CHECK: Scanning package-lock.json for npm link references | File: package-lock.json | Purpose: Remove development symlinks before publish');
1581
+ await cleanupNpmLinkReferences(isDryRun);
1582
+ } else {
1583
+ logger.verbose('NPM_LINK_CLEANUP_SKIPPED: Skipping package-lock cleanup (tree publish workflow) | Reason: package-lock.json not included in npm publish');
1584
+ }
1585
+ // Update inter-project dependencies if --update-deps flag is present
1586
+ const updateDepsScope = (_runConfig_publish5 = runConfig.publish) === null || _runConfig_publish5 === void 0 ? void 0 : _runConfig_publish5.updateDeps;
1587
+ if (updateDepsScope) {
1588
+ logger.info(`INTER_PROJECT_DEPS_UPDATE: Updating inter-project dependencies | Scope: ${updateDepsScope} | Type: inter-project | Command: kodrdriv updates`);
1589
+ const Updates = await import('@grunnverk/commands-tree');
1590
+ const updatesConfig = {
1591
+ ...runConfig,
1592
+ dryRun: isDryRun,
1593
+ updates: {
1594
+ scope: updateDepsScope,
1595
+ interProject: true
1596
+ }
1597
+ };
1598
+ await Updates.updates(updatesConfig);
1599
+ }
1600
+ logger.verbose('DEPS_UPDATE_REGISTRY: Updating dependencies to latest versions from npm registry | Source: registry | Target: package.json');
1601
+ const updatePatterns = (_runConfig_publish6 = runConfig.publish) === null || _runConfig_publish6 === void 0 ? void 0 : _runConfig_publish6.dependencyUpdatePatterns;
1602
+ if (updatePatterns && updatePatterns.length > 0) {
1603
+ logger.verbose(`DEPS_UPDATE_PATTERNS: Updating dependencies matching specified patterns | Patterns: ${updatePatterns.join(', ')} | Count: ${updatePatterns.length} | Command: npm update`);
1604
+ const patternsArg = updatePatterns.join(' ');
1605
+ await runWithDryRunSupport(`npm update ${patternsArg} --legacy-peer-deps`, isDryRun);
1606
+ } else {
1607
+ logger.verbose('DEPS_UPDATE_ALL: No dependency patterns specified, updating all dependencies | Scope: all | Command: npm update');
1608
+ await runWithDryRunSupport('npm update --legacy-peer-deps', isDryRun);
1609
+ }
1610
+ logger.info('PREPUBLISH_SCRIPT_RUNNING: Executing prepublishOnly script | Script: prepublishOnly | Purpose: Run pre-flight checks (clean, lint, build, test)');
1611
+ await runWithDryRunSupport('npm run prepublishOnly', isDryRun, {}, true); // Use inherited stdio
1612
+ // STEP 2: Commit dependency updates if any (still no version bump)
1613
+ logger.verbose('DEPS_STAGING: Staging dependency updates for commit | Files: package.json | Command: git add | Note: Version bump not yet applied, package-lock.json ignored');
1614
+ // Skip package-lock.json as it's in .gitignore to avoid private registry refs
1615
+ const filesToStage = 'package.json';
1616
+ // Wrap git operations with repository lock to prevent .git/index.lock conflicts
1617
+ await runGitWithLock(process.cwd(), async ()=>{
1618
+ await runWithDryRunSupport(`git add ${filesToStage}`, isDryRun);
1619
+ }, 'stage dependency updates');
1620
+ logger.verbose('DEPS_COMMIT_CHECK: Checking for staged dependency updates | Command: git status | Purpose: Determine if commit needed');
1621
+ if (isDryRun) {
1622
+ logger.verbose('DEPS_COMMIT_DRY_RUN: Would create dependency update commit if changes are staged | Mode: dry-run');
1623
+ } else {
1624
+ if (await Diff.hasStagedChanges()) {
1625
+ logger.verbose('DEPS_COMMIT_CREATING: Staged dependency changes detected, creating commit | Files: ' + filesToStage + ' | Action: Execute commit command');
1626
+ // Commit also needs git lock
1627
+ await runGitWithLock(process.cwd(), async ()=>{
1628
+ await Commit.commit(runConfig);
1629
+ }, 'commit dependency updates');
1630
+ } else {
1631
+ logger.verbose('DEPS_COMMIT_SKIPPED: No dependency changes to commit | Files: ' + filesToStage + ' | Action: Skipping commit step');
1632
+ }
1633
+ }
1634
+ // STEP 3: Merge target branch into working branch (optional - now skipped by default since post-publish sync keeps branches in sync)
1635
+ const skipPreMerge = ((_runConfig_publish7 = runConfig.publish) === null || _runConfig_publish7 === void 0 ? void 0 : _runConfig_publish7.skipPrePublishMerge) !== false; // Default to true (skip)
1636
+ if (skipPreMerge) {
1637
+ logger.verbose(`PRE_MERGE_SKIPPED: Skipping pre-publish merge of target branch | Reason: Post-publish sync handles branch synchronization | Target: ${targetBranch} | Config: skipPrePublishMerge=true`);
1638
+ } else {
1639
+ logger.info(`PRE_MERGE_STARTING: Merging target branch into current branch | Target: ${targetBranch} | Purpose: Avoid version conflicts | Phase: pre-publish`);
1640
+ if (isDryRun) {
1641
+ logger.info(`Would merge ${targetBranch} into current branch`);
1642
+ } else {
1643
+ // Wrap entire merge process with git lock (involves fetch, merge, checkout, add, commit)
1644
+ await runGitWithLock(process.cwd(), async ()=>{
1645
+ // Fetch the latest target branch
1646
+ try {
1647
+ await run(`git fetch origin ${targetBranch}:${targetBranch}`);
1648
+ logger.info(`TARGET_BRANCH_FETCHED: Successfully fetched latest target branch | Branch: ${targetBranch} | Remote: origin/${targetBranch} | Purpose: Pre-merge sync`);
1649
+ } catch (fetchError) {
1650
+ logger.warn(`TARGET_BRANCH_FETCH_FAILED: Unable to fetch target branch | Branch: ${targetBranch} | Error: ${fetchError.message} | Impact: Proceeding without merge, PR may have conflicts`);
1651
+ logger.warn('MERGE_SKIPPED_NO_FETCH: Continuing without pre-merge | Reason: Target branch fetch failed | Impact: PR may require manual conflict resolution');
1652
+ }
1653
+ // Check if merge is needed (avoid unnecessary merge commits)
1654
+ try {
1655
+ const { stdout: mergeBase } = await run(`git merge-base HEAD ${targetBranch}`);
1656
+ const { stdout: targetCommit } = await run(`git rev-parse ${targetBranch}`);
1657
+ if (mergeBase.trim() === targetCommit.trim()) {
1658
+ logger.info(`MERGE_NOT_NEEDED: Current branch already up-to-date with target | Branch: ${targetBranch} | Status: in-sync | Action: Skipping merge`);
1659
+ } else {
1660
+ // Try to merge target branch into current branch
1661
+ let mergeSucceeded = false;
1662
+ try {
1663
+ await run(`git merge ${targetBranch} --no-edit -m "Merge ${targetBranch} to sync before version bump"`);
1664
+ logger.info(`MERGE_SUCCESS: Successfully merged target branch into current branch | Target: ${targetBranch} | Purpose: Sync before version bump`);
1665
+ mergeSucceeded = true;
1666
+ } catch (mergeError) {
1667
+ // If merge conflicts occur, check if they're only in version-related files
1668
+ const errorText = [
1669
+ mergeError.message || '',
1670
+ mergeError.stdout || '',
1671
+ mergeError.stderr || ''
1672
+ ].join(' ');
1673
+ if (errorText.includes('CONFLICT')) {
1674
+ logger.warn(`MERGE_CONFLICTS_DETECTED: Merge conflicts found, attempting automatic resolution | Target: ${targetBranch} | Strategy: Auto-resolve version files`);
1675
+ // Get list of conflicted files
1676
+ const { stdout: conflictedFiles } = await run('git diff --name-only --diff-filter=U');
1677
+ const conflicts = conflictedFiles.trim().split('\n').filter(Boolean);
1678
+ logger.verbose(`MERGE_CONFLICTS_LIST: Conflicted files detected | Files: ${conflicts.join(', ')} | Count: ${conflicts.length}`);
1679
+ // Check if conflicts are only in package.json (package-lock.json is gitignored)
1680
+ const versionFiles = [
1681
+ 'package.json'
1682
+ ];
1683
+ const nonVersionConflicts = conflicts.filter((f)=>!versionFiles.includes(f));
1684
+ if (nonVersionConflicts.length > 0) {
1685
+ logger.error(`MERGE_AUTO_RESOLVE_FAILED: Cannot auto-resolve conflicts in non-version files | Files: ${nonVersionConflicts.join(', ')} | Count: ${nonVersionConflicts.length} | Resolution: Manual intervention required`);
1686
+ logger.error('');
1687
+ logger.error('CONFLICT_RESOLUTION_REQUIRED: Manual steps to resolve conflicts:');
1688
+ logger.error(' Step 1: Resolve conflicts in the files listed above');
1689
+ logger.error(' Step 2: Stage resolved files | Command: git add <resolved-files>');
1690
+ logger.error(' Step 3: Complete merge commit | Command: git commit');
1691
+ logger.error(' Step 4: Resume publish process | Command: kodrdriv publish');
1692
+ logger.error('');
1693
+ throw new Error(`Merge conflicts in non-version files. Please resolve manually.`);
1694
+ }
1695
+ // Auto-resolve version conflicts by accepting current branch versions
1696
+ // (keep our working branch's version, which is likely already updated)
1697
+ logger.info(`MERGE_AUTO_RESOLVING: Automatically resolving version conflicts | Strategy: Keep current branch versions | Files: ${versionFiles.join(', ')}`);
1698
+ for (const file of conflicts){
1699
+ if (versionFiles.includes(file)) {
1700
+ await run(`git checkout --ours ${file}`);
1701
+ await run(`git add ${file}`);
1702
+ logger.verbose(`MERGE_FILE_RESOLVED: Resolved file using current branch version | File: ${file} | Strategy: checkout --ours`);
1703
+ }
1704
+ }
1705
+ // Complete the merge
1706
+ await run(`git commit --no-edit -m "Merge ${targetBranch} to sync before version bump (auto-resolved version conflicts)"`);
1707
+ logger.info(`MERGE_AUTO_RESOLVE_SUCCESS: Successfully auto-resolved version conflicts and completed merge | Target: ${targetBranch} | Files: ${versionFiles.join(', ')}`);
1708
+ mergeSucceeded = true;
1709
+ } else {
1710
+ // Not a conflict error, re-throw
1711
+ throw mergeError;
1712
+ }
1713
+ }
1714
+ // Only run npm install if merge actually happened
1715
+ if (mergeSucceeded) {
1716
+ // Run npm install to update package-lock.json based on merged package.json
1717
+ logger.info('POST_MERGE_NPM_INSTALL: Running npm install after merge | Purpose: Update package-lock.json based on merged package.json | Command: npm install');
1718
+ await run('npm install');
1719
+ logger.info('POST_MERGE_NPM_COMPLETE: npm install completed successfully | Status: Dependencies synchronized');
1720
+ // Commit any changes from npm install (e.g., package-lock.json updates)
1721
+ const { stdout: mergeChangesStatus } = await run('git status --porcelain');
1722
+ if (mergeChangesStatus.trim()) {
1723
+ logger.verbose('POST_MERGE_CHANGES_DETECTED: Changes detected after npm install | Action: Staging for commit | Command: git add');
1724
+ // Skip package-lock.json as it's in .gitignore to avoid private registry refs
1725
+ const filesToStagePostMerge = 'package.json';
1726
+ await run(`git add ${filesToStagePostMerge}`);
1727
+ if (await Diff.hasStagedChanges()) {
1728
+ logger.verbose('POST_MERGE_COMMIT: Committing post-merge changes | Files: ' + filesToStagePostMerge + ' | Purpose: Finalize merge');
1729
+ await Commit.commit(runConfig);
1730
+ }
1731
+ }
1732
+ }
1733
+ }
1734
+ } catch (error) {
1735
+ // Only catch truly unexpected errors here
1736
+ logger.error(`MERGE_UNEXPECTED_ERROR: Unexpected error during merge process | Error: ${error.message} | Target: ${targetBranch} | Action: Aborting publish`);
1737
+ throw error;
1738
+ }
1739
+ }, `merge ${targetBranch} into current branch`);
1740
+ }
1741
+ }
1742
+ // STEP 4: Determine and set target version AFTER checks, dependency commit, and target branch merge
1743
+ logger.info('Determining target version...');
1744
+ if (isDryRun) {
1745
+ logger.info('Would determine target version and update package.json');
1746
+ newVersion = '1.0.0'; // Mock version for dry run
1747
+ } else {
1748
+ var _runConfig_publish14;
1749
+ const packageJsonContents = await storage.readFile('package.json', 'utf-8');
1750
+ const parsed = safeJsonParse(packageJsonContents, 'package.json');
1751
+ const packageJson = validatePackageJson(parsed, 'package.json');
1752
+ const currentVersion = packageJson.version;
1753
+ let proposedVersion;
1754
+ let finalTargetBranch = targetBranch;
1755
+ if (branchDependentVersioning && runConfig.branches) {
1756
+ // Use branch-dependent versioning logic
1757
+ const branchDependentResult = await calculateBranchDependentVersion(currentVersion, currentBranch, runConfig.branches, targetBranch);
1758
+ proposedVersion = branchDependentResult.version;
1759
+ finalTargetBranch = branchDependentResult.targetBranch;
1760
+ logger.info(`VERSION_BRANCH_DEPENDENT_CALCULATED: Branch-dependent version calculated | Current: ${currentVersion} | Proposed: ${proposedVersion} | Strategy: branch-dependent`);
1761
+ logger.info(`TARGET_BRANCH_FINAL: Final target branch determined | Branch: ${finalTargetBranch} | Source: branch-dependent config`);
1762
+ // Update targetBranch for the rest of the function
1763
+ targetBranch = finalTargetBranch;
1764
+ } else {
1765
+ var _runConfig_publish15;
1766
+ // Use existing logic for backward compatibility
1767
+ const targetVersionInput = ((_runConfig_publish15 = runConfig.publish) === null || _runConfig_publish15 === void 0 ? void 0 : _runConfig_publish15.targetVersion) || 'patch';
1768
+ proposedVersion = calculateTargetVersion(currentVersion, targetVersionInput);
1769
+ }
1770
+ const targetTagName = `v${proposedVersion}`;
1771
+ const tagExists = await checkIfTagExists(targetTagName);
1772
+ // Smart tag conflict handling
1773
+ if (tagExists) {
1774
+ const { getNpmPublishedVersion, getTagInfo } = await import('@grunnverk/core');
1775
+ logger.warn(`TAG_ALREADY_EXISTS: Tag already exists in repository | Tag: ${targetTagName} | Status: conflict | Action: Check npm registry`);
1776
+ // Check if this version is published on npm
1777
+ const npmVersion = await getNpmPublishedVersion(packageJson.name);
1778
+ const tagInfo = await getTagInfo(targetTagName);
1779
+ if (npmVersion === proposedVersion) {
1780
+ var _runConfig_publish16;
1781
+ // Version is already published on npm
1782
+ logger.info(`VERSION_ALREADY_PUBLISHED: Version already published on npm registry | Version: ${proposedVersion} | Status: published | Action: Skipping`);
1783
+ logger.info(`PUBLISH_SKIPPED_DUPLICATE: Skipping publish operation | Reason: Package already at target version | Version: ${proposedVersion}`);
1784
+ logger.info('');
1785
+ logger.info('REPUBLISH_OPTIONS: Options if you need to republish:');
1786
+ logger.info(` Option 1: Bump version | Command: npm version patch (or minor/major)`);
1787
+ logger.info(` Option 2: Re-run publish | Command: kodrdriv publish`);
1788
+ logger.info('');
1789
+ if ((_runConfig_publish16 = runConfig.publish) === null || _runConfig_publish16 === void 0 ? void 0 : _runConfig_publish16.skipAlreadyPublished) {
1790
+ logger.info('PUBLISH_SKIPPED_FLAG: Skipping package due to flag | Flag: --skip-already-published | Version: ' + proposedVersion + ' | Status: skipped');
1791
+ // Emit skip marker for tree mode detection
1792
+ // eslint-disable-next-line no-console
1793
+ console.log('KODRDRIV_PUBLISH_SKIPPED');
1794
+ return; // Exit without error
1795
+ } else {
1796
+ throw new Error(`Version ${proposedVersion} already published. Use --skip-already-published to continue.`);
1797
+ }
1798
+ } else {
1799
+ var _tagInfo_commit, _runConfig_publish17;
1800
+ // Tag exists but version not on npm - likely failed previous publish
1801
+ logger.warn('');
1802
+ logger.warn('PUBLISH_SITUATION_ANALYSIS: Analyzing publish conflict situation | Tag: ' + targetTagName + ' | npm: ' + (npmVersion || 'not published'));
1803
+ logger.warn(`PUBLISH_ANALYSIS_TAG_EXISTS: Tag exists locally | Tag: ${targetTagName} | Commit: ${tagInfo === null || tagInfo === void 0 ? void 0 : (_tagInfo_commit = tagInfo.commit) === null || _tagInfo_commit === void 0 ? void 0 : _tagInfo_commit.substring(0, 8)}`);
1804
+ logger.warn(`PUBLISH_ANALYSIS_NPM_STATUS: npm registry status | Version: ${npmVersion || 'not published'} | Status: ${npmVersion ? 'published' : 'missing'}`);
1805
+ logger.warn(`PUBLISH_ANALYSIS_CONCLUSION: Previous publish likely failed after tag creation | Reason: Tag exists but not on npm | Resolution: Recovery needed`);
1806
+ logger.warn('');
1807
+ logger.warn('PUBLISH_RECOVERY_OPTIONS: Recovery options available:');
1808
+ logger.warn(' OPTION_1_FORCE: Force republish by deleting tag | Command: kodrdriv publish --force-republish');
1809
+ logger.warn(' OPTION_2_BUMP: Skip version and bump | Command: npm version patch && kodrdriv publish');
1810
+ logger.warn(' OPTION_3_MANUAL: Manually delete tag:');
1811
+ logger.warn(` Command: git tag -d ${targetTagName}`);
1812
+ logger.warn(` Command: git push origin :refs/tags/${targetTagName}`);
1813
+ logger.warn('');
1814
+ if ((_runConfig_publish17 = runConfig.publish) === null || _runConfig_publish17 === void 0 ? void 0 : _runConfig_publish17.forceRepublish) {
1815
+ logger.info('PUBLISH_FORCE_REPUBLISH: Force republish mode enabled | Action: Deleting existing tag | Tag: ' + targetTagName + ' | Purpose: Allow republish');
1816
+ if (!isDryRun) {
1817
+ const { runSecure } = await import('@grunnverk/git-tools');
1818
+ // Delete local tag
1819
+ try {
1820
+ await runSecure('git', [
1821
+ 'tag',
1822
+ '-d',
1823
+ targetTagName
1824
+ ]);
1825
+ logger.info(`TAG_DELETED_LOCAL: Deleted local tag | Tag: ${targetTagName} | Status: removed-local`);
1826
+ } catch (error) {
1827
+ logger.debug(`Could not delete local tag: ${error.message}`);
1828
+ }
1829
+ // Delete remote tag
1830
+ try {
1831
+ await runSecure('git', [
1832
+ 'push',
1833
+ 'origin',
1834
+ `:refs/tags/${targetTagName}`
1835
+ ]);
1836
+ logger.info(`TAG_DELETED_REMOTE: Deleted remote tag | Tag: ${targetTagName} | Remote: origin | Status: removed-remote`);
1837
+ } catch (error) {
1838
+ logger.debug(`Could not delete remote tag: ${error.message}`);
1839
+ }
1840
+ logger.info('PUBLISH_TAG_CLEANUP_COMPLETE: Tag deleted successfully | Status: ready-for-publish | Next: Continue with publish workflow');
1841
+ } else {
1842
+ logger.info('Would delete tags and continue with publish');
1843
+ }
1844
+ } else {
1845
+ throw new Error(`Tag ${targetTagName} already exists. Use --force-republish to override.`);
1846
+ }
1847
+ }
1848
+ }
1849
+ if ((_runConfig_publish14 = runConfig.publish) === null || _runConfig_publish14 === void 0 ? void 0 : _runConfig_publish14.interactive) {
1850
+ var _runConfig_publish18;
1851
+ newVersion = await confirmVersionInteractively(currentVersion, proposedVersion, (_runConfig_publish18 = runConfig.publish) === null || _runConfig_publish18 === void 0 ? void 0 : _runConfig_publish18.targetVersion);
1852
+ const confirmedTagName = `v${newVersion}`;
1853
+ const confirmedTagExists = await checkIfTagExists(confirmedTagName);
1854
+ if (confirmedTagExists) {
1855
+ var _runConfig_publish19;
1856
+ const { getNpmPublishedVersion } = await import('@grunnverk/core');
1857
+ const npmVersion = await getNpmPublishedVersion(packageJson.name);
1858
+ if (npmVersion === newVersion) {
1859
+ throw new Error(`Tag ${confirmedTagName} already exists and version is published on npm. Please choose a different version.`);
1860
+ } else if (!((_runConfig_publish19 = runConfig.publish) === null || _runConfig_publish19 === void 0 ? void 0 : _runConfig_publish19.forceRepublish)) {
1861
+ throw new Error(`Tag ${confirmedTagName} already exists. Use --force-republish to override.`);
1862
+ }
1863
+ // If forceRepublish is set, we'll continue (tag will be deleted later)
1864
+ }
1865
+ } else {
1866
+ newVersion = proposedVersion;
1867
+ }
1868
+ logger.info(`Bumping version from ${currentVersion} to ${newVersion}`);
1869
+ packageJson.version = newVersion;
1870
+ await storage.writeFile('package.json', JSON.stringify(packageJson, null, 2) + '\n', 'utf-8');
1871
+ logger.info(`Version updated in package.json: ${newVersion}`);
1872
+ }
1873
+ // STEP 5: Commit version bump as a separate commit
1874
+ logger.verbose('Staging version bump for commit');
1875
+ // Skip package-lock.json as it's in .gitignore to avoid private registry refs
1876
+ const filesToStageVersionBump = 'package.json';
1877
+ // Wrap git operations with lock
1878
+ await runGitWithLock(process.cwd(), async ()=>{
1879
+ await runWithDryRunSupport(`git add ${filesToStageVersionBump}`, isDryRun);
1880
+ }, 'stage version bump');
1881
+ if (isDryRun) {
1882
+ logger.verbose('Would create version bump commit');
1883
+ } else {
1884
+ if (await Diff.hasStagedChanges()) {
1885
+ logger.verbose('Creating version bump commit...');
1886
+ await runGitWithLock(process.cwd(), async ()=>{
1887
+ await Commit.commit(runConfig);
1888
+ }, 'commit version bump');
1889
+ } else {
1890
+ logger.verbose('No version changes to commit.');
1891
+ }
1892
+ }
1893
+ logger.info('Generating release notes...');
1894
+ // Use the existing currentBranch variable for tag detection
1895
+ logger.debug(`Current branch for release notes: ${currentBranch}`);
1896
+ // Create a modified config for release notes generation that includes the publish --from, --interactive, and --from-main options
1897
+ const releaseConfig = {
1898
+ ...runConfig
1899
+ };
1900
+ releaseConfig.release = {
1901
+ ...runConfig.release,
1902
+ currentBranch: currentBranch,
1903
+ version: newVersion,
1904
+ ...((_runConfig_publish8 = runConfig.publish) === null || _runConfig_publish8 === void 0 ? void 0 : _runConfig_publish8.from) && {
1905
+ from: runConfig.publish.from
1906
+ },
1907
+ ...((_runConfig_publish9 = runConfig.publish) === null || _runConfig_publish9 === void 0 ? void 0 : _runConfig_publish9.interactive) && {
1908
+ interactive: runConfig.publish.interactive
1909
+ },
1910
+ ...((_runConfig_publish10 = runConfig.publish) === null || _runConfig_publish10 === void 0 ? void 0 : _runConfig_publish10.fromMain) && {
1911
+ fromMain: runConfig.publish.fromMain
1912
+ }
1913
+ };
1914
+ if ((_runConfig_publish11 = runConfig.publish) === null || _runConfig_publish11 === void 0 ? void 0 : _runConfig_publish11.from) {
1915
+ logger.verbose(`Using custom 'from' reference for release notes: ${runConfig.publish.from}`);
1916
+ }
1917
+ if ((_runConfig_publish12 = runConfig.publish) === null || _runConfig_publish12 === void 0 ? void 0 : _runConfig_publish12.interactive) {
1918
+ logger.verbose('Interactive mode enabled for release notes generation');
1919
+ }
1920
+ if ((_runConfig_publish13 = runConfig.publish) === null || _runConfig_publish13 === void 0 ? void 0 : _runConfig_publish13.fromMain) {
1921
+ logger.verbose('Forcing comparison against main branch for release notes');
1922
+ }
1923
+ // Log self-reflection settings for debugging
1924
+ if ((_releaseConfig_release = releaseConfig.release) === null || _releaseConfig_release === void 0 ? void 0 : _releaseConfig_release.selfReflection) {
1925
+ logger.verbose('Self-reflection enabled for release notes generation');
1926
+ }
1927
+ const releaseSummary = await execute$1(releaseConfig);
1928
+ if (isDryRun) {
1929
+ logger.info('Would write release notes to RELEASE_NOTES.md and RELEASE_TITLE.md in output directory');
1930
+ } else {
1931
+ const outputDirectory = runConfig.outputDirectory || DEFAULT_OUTPUT_DIRECTORY;
1932
+ await storage.ensureDirectory(outputDirectory);
1933
+ const releaseNotesPath = getOutputPath(outputDirectory, 'RELEASE_NOTES.md');
1934
+ const releaseTitlePath = getOutputPath(outputDirectory, 'RELEASE_TITLE.md');
1935
+ await storage.writeFile(releaseNotesPath, releaseSummary.body, 'utf-8');
1936
+ await storage.writeFile(releaseTitlePath, releaseSummary.title, 'utf-8');
1937
+ logger.info(`Release notes and title generated and saved to ${releaseNotesPath} and ${releaseTitlePath}.`);
1938
+ }
1939
+ logger.info('Pushing to origin...');
1940
+ // Get current branch name and push explicitly to avoid pushing to wrong remote/branch
1941
+ const branchName = await GitHub.getCurrentBranchName();
1942
+ // Wrap git push with lock
1943
+ await runGitWithLock(process.cwd(), async ()=>{
1944
+ await runWithDryRunSupport(`git push origin ${branchName}`, isDryRun);
1945
+ }, `push ${branchName}`);
1946
+ logger.info('Creating pull request...');
1947
+ if (isDryRun) {
1948
+ logger.info('Would get commit title and create PR with GitHub API');
1949
+ pr = {
1950
+ number: 123,
1951
+ html_url: 'https://github.com/mock/repo/pull/123',
1952
+ labels: []
1953
+ };
1954
+ } else {
1955
+ const { stdout: rawCommitTitle } = await run('git log -1 --pretty=%B');
1956
+ // Apply stop-context filtering to PR title and body
1957
+ const commitTitle = filterContent(rawCommitTitle, runConfig.stopContext).filtered;
1958
+ const prBody = filterContent('Automated release PR.', runConfig.stopContext).filtered;
1959
+ pr = await GitHub.createPullRequest(commitTitle, prBody, branchName, targetBranch);
1960
+ if (!pr) {
1961
+ throw new Error('Failed to create pull request.');
1962
+ }
1963
+ logger.info(`Pull request created: ${pr.html_url} (${branchName} → ${targetBranch})`);
1964
+ }
1965
+ }
1966
+ logger.info(`Waiting for PR #${pr.number} checks to complete...`);
1967
+ if (!isDryRun) {
1968
+ // Check if we already know from prechecks that no workflows will trigger
1969
+ let shouldSkipWait = false;
1970
+ try {
1971
+ // TODO: Re-enable when checkWorkflowConfiguration is exported from github-tools
1972
+ // const workflowConfig = await GitHub.checkWorkflowConfiguration(targetBranch);
1973
+ const workflowConfig = {
1974
+ hasWorkflows: true,
1975
+ hasPullRequestTriggers: true,
1976
+ workflowCount: 0,
1977
+ triggeredWorkflowNames: []
1978
+ };
1979
+ if (!workflowConfig.hasWorkflows || !workflowConfig.hasPullRequestTriggers) ;
1980
+ } catch (error) {
1981
+ // If we can't verify, proceed with waiting to be safe
1982
+ logger.debug(`Could not verify workflow configuration for wait skip: ${error.message}`);
1983
+ }
1984
+ if (!shouldSkipWait) {
1985
+ var _runConfig_publish20, _runConfig_publish21, _runConfig_publish22;
1986
+ // Configure timeout and user confirmation behavior
1987
+ const timeout = ((_runConfig_publish20 = runConfig.publish) === null || _runConfig_publish20 === void 0 ? void 0 : _runConfig_publish20.checksTimeout) || KODRDRIV_DEFAULTS.publish.checksTimeout;
1988
+ const senditMode = ((_runConfig_publish21 = runConfig.publish) === null || _runConfig_publish21 === void 0 ? void 0 : _runConfig_publish21.sendit) || false;
1989
+ // sendit flag overrides skipUserConfirmation - if sendit is true, skip confirmation
1990
+ const skipUserConfirmation = senditMode || ((_runConfig_publish22 = runConfig.publish) === null || _runConfig_publish22 === void 0 ? void 0 : _runConfig_publish22.skipUserConfirmation) || false;
1991
+ await GitHub.waitForPullRequestChecks(pr.number, {
1992
+ timeout,
1993
+ skipUserConfirmation
1994
+ });
1995
+ }
1996
+ }
1997
+ const mergeMethod = ((_runConfig_publish2 = runConfig.publish) === null || _runConfig_publish2 === void 0 ? void 0 : _runConfig_publish2.mergeMethod) || 'squash';
1998
+ if (isDryRun) {
1999
+ logger.info(`Would merge PR #${pr.number} using ${mergeMethod} method`);
2000
+ } else {
2001
+ try {
2002
+ await GitHub.mergePullRequest(pr.number, mergeMethod, false); // Don't delete branch
2003
+ } catch (error) {
2004
+ // Check if this is a merge conflict error
2005
+ if (error.message && (error.message.includes('not mergeable') || error.message.includes('Pull Request is not mergeable') || error.message.includes('merge conflict'))) {
2006
+ logger.error(`PR_MERGE_CONFLICTS: Pull request has merge conflicts | PR Number: ${pr.number} | Status: conflicts | Resolution: Manual intervention required`);
2007
+ logger.error('');
2008
+ logger.error('PR_CONFLICT_RESOLUTION: Steps to resolve conflicts:');
2009
+ logger.error(` Step 1: Visit pull request | URL: ${pr.html_url}`);
2010
+ logger.error(' Step 2: Resolve merge conflicts | Method: GitHub UI or local');
2011
+ logger.error(' Step 3: Re-run publish command | Command: kodrdriv publish');
2012
+ logger.error('');
2013
+ logger.error('PR_AUTO_CONTINUE: Command will auto-detect existing PR | Behavior: Continues from where it left off | No re-creation needed');
2014
+ throw new Error(`Merge conflicts detected in PR #${pr.number}. Please resolve conflicts and re-run the command.`);
2015
+ } else {
2016
+ // Re-throw other merge errors
2017
+ throw error;
2018
+ }
2019
+ }
2020
+ }
2021
+ // Switch to target branch and pull latest changes
2022
+ logger.info(`Checking out target branch: ${targetBranch}...`);
2023
+ // Check for uncommitted changes and stash them if necessary
2024
+ let hasStashedChanges = false;
2025
+ if (!isDryRun) {
2026
+ const { stdout: statusOutput } = await runSecure('git', [
2027
+ 'status',
2028
+ '--porcelain'
2029
+ ]);
2030
+ if (statusOutput.trim()) {
2031
+ logger.info('PUBLISH_STASH_SAVING: Stashing uncommitted changes before checkout | Command: git stash push | Purpose: Protect changes during branch switch');
2032
+ await runSecure('git', [
2033
+ 'stash',
2034
+ 'push',
2035
+ '-m',
2036
+ 'kodrdriv: stash before checkout target branch'
2037
+ ]);
2038
+ hasStashedChanges = true;
2039
+ logger.info('PUBLISH_STASH_SUCCESS: Successfully stashed uncommitted changes | Status: saved | Name: kodrdriv stash');
2040
+ }
2041
+ }
2042
+ try {
2043
+ // Wrap git checkout and pull with lock
2044
+ await runGitWithLock(process.cwd(), async ()=>{
2045
+ await runWithDryRunSupport(`git checkout ${targetBranch}`, isDryRun);
2046
+ }, `checkout ${targetBranch}`);
2047
+ // Sync target branch with remote to avoid conflicts during PR creation
2048
+ if (!isDryRun) {
2049
+ logger.info(`PUBLISH_TARGET_SYNCING: Syncing target branch with remote | Branch: ${targetBranch} | Remote: origin | Purpose: Avoid PR conflicts`);
2050
+ try {
2051
+ const remoteExists = await run(`git ls-remote --exit-code --heads origin ${targetBranch}`).then(()=>true).catch(()=>false);
2052
+ if (remoteExists) {
2053
+ await runGitWithLock(process.cwd(), async ()=>{
2054
+ await run(`git fetch origin ${targetBranch}`);
2055
+ await run(`git merge origin/${targetBranch} --no-edit`);
2056
+ }, `sync ${targetBranch}`);
2057
+ logger.info(`PUBLISH_TARGET_SYNCED: Successfully synced target with remote | Branch: ${targetBranch} | Remote: origin | Status: in-sync`);
2058
+ } else {
2059
+ logger.info(`PUBLISH_TARGET_NO_REMOTE: No remote target branch found | Branch: ${targetBranch} | Remote: origin | Action: Will be created on first push`);
2060
+ }
2061
+ } catch (syncError) {
2062
+ if (syncError.message && syncError.message.includes('CONFLICT')) {
2063
+ logger.error(`PUBLISH_SYNC_CONFLICTS: Merge conflicts during target sync | Branch: ${targetBranch} | Remote: origin | Status: conflicts-detected`);
2064
+ logger.error(`PUBLISH_SYNC_RESOLUTION: Manual conflict resolution steps:`);
2065
+ logger.error(` Step 1: Checkout target | Command: git checkout ${targetBranch}`);
2066
+ logger.error(` Step 2: Pull and merge | Command: git pull origin ${targetBranch}`);
2067
+ logger.error(` Step 3: Resolve conflicts in files`);
2068
+ logger.error(` Step 4: Stage resolved files | Command: git add <resolved-files>`);
2069
+ logger.error(` Step 5: Complete merge | Command: git commit`);
2070
+ logger.error(` Step 6: Return to branch | Command: git checkout ${currentBranch}`);
2071
+ logger.error(` Step 7: Resume publish | Command: kodrdriv publish`);
2072
+ throw syncError;
2073
+ } else {
2074
+ logger.warn(`PUBLISH_SYNC_WARNING: Could not sync target with remote | Branch: ${targetBranch} | Remote: origin | Error: ${syncError.message}`);
2075
+ // Continue with publish process, but log the warning
2076
+ }
2077
+ }
2078
+ } else {
2079
+ logger.info(`Would sync ${targetBranch} with remote to avoid PR conflicts`);
2080
+ }
2081
+ } catch (error) {
2082
+ // Check if this is a merge conflict or sync issue
2083
+ if (!isDryRun && (error.message.includes('conflict') || error.message.includes('CONFLICT') || error.message.includes('diverged') || error.message.includes('non-fast-forward'))) {
2084
+ logger.error(`PUBLISH_TARGET_SYNC_FAILED: Failed to sync target branch with remote | Branch: ${targetBranch} | Remote: origin | Impact: Cannot proceed safely`);
2085
+ logger.error('');
2086
+ logger.error('PUBLISH_SYNC_RECOVERY_OPTIONS: Available recovery options:');
2087
+ logger.error(` OPTION_1_AUTO: Attempt automatic resolution | Command: kodrdriv publish --sync-target`);
2088
+ logger.error(` OPTION_2_MANUAL: Manually resolve conflicts:`);
2089
+ logger.error(` Step 1: Checkout target | Command: git checkout ${targetBranch}`);
2090
+ logger.error(` Step 2: Pull from remote | Command: git pull origin ${targetBranch}`);
2091
+ logger.error(` Step 3: Resolve conflicts and commit`);
2092
+ logger.error(` Step 4: Re-run publish | Command: kodrdriv publish`);
2093
+ logger.error('');
2094
+ logger.error('PUBLISH_STOPPED_SAFETY: Publish process stopped | Reason: Prevent data loss | Status: safe-to-recover');
2095
+ throw new Error(`Target branch '${targetBranch}' sync failed. Use recovery options above to resolve.`);
2096
+ } else {
2097
+ // Re-throw other errors
2098
+ throw error;
2099
+ }
2100
+ }
2101
+ // Restore stashed changes if we stashed them
2102
+ if (hasStashedChanges) {
2103
+ logger.info('PUBLISH_STASH_RESTORING: Restoring previously stashed changes | Command: git stash pop | Purpose: Restore working directory state');
2104
+ try {
2105
+ await runSecure('git', [
2106
+ 'stash',
2107
+ 'pop'
2108
+ ]);
2109
+ logger.info('PUBLISH_STASH_RESTORED: Successfully restored stashed changes | Status: restored | Stash: removed');
2110
+ } catch (stashError) {
2111
+ logger.warn(`PUBLISH_STASH_RESTORE_FAILED: Could not restore stashed changes | Error: ${stashError.message} | Impact: Changes still in stash`);
2112
+ logger.warn('PUBLISH_STASH_AVAILABLE: Changes available in git stash | Command: git stash list | Purpose: View and restore manually');
2113
+ }
2114
+ }
2115
+ // Update package.json on target branch to release version and commit
2116
+ // This ensures the tag points to a commit with the correct release version
2117
+ if (!isDryRun) {
2118
+ logger.info(`PUBLISH_VERSION_UPDATE_TARGET: Updating package.json on target branch to release version | Version: ${newVersion} | Branch: ${targetBranch}`);
2119
+ // Read current package.json on target branch
2120
+ const targetPackageJsonContents = await storage.readFile('package.json', 'utf-8');
2121
+ const targetPackageJson = safeJsonParse(targetPackageJsonContents, 'package.json');
2122
+ // Check if version update is needed
2123
+ if (targetPackageJson.version !== newVersion) {
2124
+ logger.info(`PUBLISH_VERSION_MISMATCH: Version mismatch detected on target | Current: ${targetPackageJson.version} | Expected: ${newVersion} | Action: Updating`);
2125
+ // Update version in package.json
2126
+ targetPackageJson.version = newVersion;
2127
+ await storage.writeFile('package.json', JSON.stringify(targetPackageJson, null, 2) + '\n', 'utf-8');
2128
+ logger.info(`PUBLISH_VERSION_UPDATED: Updated package.json version on target branch | Version: ${newVersion} | Branch: ${targetBranch}`);
2129
+ // Stage and commit the version update
2130
+ await runGitWithLock(process.cwd(), async ()=>{
2131
+ await runSecure('git', [
2132
+ 'add',
2133
+ 'package.json'
2134
+ ]);
2135
+ }, 'stage version update on target');
2136
+ // Check if there are staged changes before committing
2137
+ if (await Diff.hasStagedChanges()) {
2138
+ logger.info('PUBLISH_VERSION_COMMITTING: Committing version update to target branch | Purpose: Ensure tag points to correct version');
2139
+ await runGitWithLock(process.cwd(), async ()=>{
2140
+ await Commit.commit(runConfig);
2141
+ }, 'commit version update on target');
2142
+ logger.info('PUBLISH_VERSION_COMMITTED: Version update committed successfully');
2143
+ // Push the version update to remote
2144
+ logger.info(`PUBLISH_VERSION_PUSHING: Pushing version update to remote | Branch: ${targetBranch} | Remote: origin`);
2145
+ await runGitWithLock(process.cwd(), async ()=>{
2146
+ await runSecure('git', [
2147
+ 'push',
2148
+ 'origin',
2149
+ targetBranch
2150
+ ]);
2151
+ }, `push version update to ${targetBranch}`);
2152
+ logger.info('PUBLISH_VERSION_PUSHED: Version update pushed to remote successfully');
2153
+ } else {
2154
+ logger.verbose('PUBLISH_VERSION_NO_CHANGES: No changes to commit (version already correct)');
2155
+ }
2156
+ } else {
2157
+ logger.info(`PUBLISH_VERSION_CORRECT: Version already correct on target branch | Version: ${newVersion} | Branch: ${targetBranch}`);
2158
+ }
2159
+ }
2160
+ // Now create and push the tag on the target branch
2161
+ logger.info('Creating release tag...');
2162
+ let tagName;
2163
+ if (isDryRun) {
2164
+ logger.info('Would read package.json version and create git tag');
2165
+ tagName = 'v1.0.0'; // Mock version for dry run
2166
+ } else {
2167
+ // Use the newVersion we just set instead of reading from package.json
2168
+ // This ensures consistency even if the file read fails
2169
+ tagName = `v${newVersion}`;
2170
+ // Check if tag already exists locally
2171
+ try {
2172
+ // Validate tag name to prevent injection
2173
+ if (!validateGitRef(tagName)) {
2174
+ throw new Error(`Invalid tag name: ${tagName}`);
2175
+ }
2176
+ const { stdout } = await runSecure('git', [
2177
+ 'tag',
2178
+ '-l',
2179
+ tagName
2180
+ ]);
2181
+ if (stdout.trim() === tagName) {
2182
+ logger.info(`Tag ${tagName} already exists locally, skipping tag creation`);
2183
+ } else {
2184
+ await runGitWithLock(process.cwd(), async ()=>{
2185
+ await runSecure('git', [
2186
+ 'tag',
2187
+ tagName
2188
+ ]);
2189
+ }, `create tag ${tagName}`);
2190
+ logger.info(`Created local tag: ${tagName}`);
2191
+ }
2192
+ } catch (error) {
2193
+ // If git tag -l fails, create the tag anyway
2194
+ await runGitWithLock(process.cwd(), async ()=>{
2195
+ await runSecure('git', [
2196
+ 'tag',
2197
+ tagName
2198
+ ]);
2199
+ }, `create tag ${tagName}`);
2200
+ logger.info(`Created local tag: ${tagName}`);
2201
+ }
2202
+ // Check if tag exists on remote before pushing
2203
+ let tagWasPushed = false;
2204
+ try {
2205
+ const { stdout } = await runSecure('git', [
2206
+ 'ls-remote',
2207
+ 'origin',
2208
+ `refs/tags/${tagName}`
2209
+ ]);
2210
+ if (stdout.trim()) {
2211
+ logger.info(`Tag ${tagName} already exists on remote, skipping push`);
2212
+ } else {
2213
+ await runGitWithLock(process.cwd(), async ()=>{
2214
+ await runSecure('git', [
2215
+ 'push',
2216
+ 'origin',
2217
+ tagName
2218
+ ]);
2219
+ }, `push tag ${tagName}`);
2220
+ logger.info(`Pushed tag to remote: ${tagName}`);
2221
+ tagWasPushed = true;
2222
+ }
2223
+ } catch (error) {
2224
+ // If ls-remote fails, try to push anyway (might be a new remote)
2225
+ try {
2226
+ await runSecure('git', [
2227
+ 'push',
2228
+ 'origin',
2229
+ tagName
2230
+ ]);
2231
+ logger.info(`Pushed tag to remote: ${tagName}`);
2232
+ tagWasPushed = true;
2233
+ } catch (pushError) {
2234
+ if (pushError.message && pushError.message.includes('already exists')) {
2235
+ logger.info(`Tag ${tagName} already exists on remote, continuing...`);
2236
+ } else {
2237
+ throw pushError;
2238
+ }
2239
+ }
2240
+ }
2241
+ // If we just pushed a new tag, wait for GitHub to process it
2242
+ if (tagWasPushed) {
2243
+ logger.verbose('Waiting for GitHub to process the pushed tag...');
2244
+ await new Promise((resolve)=>setTimeout(resolve, 5000)); // 5 second delay
2245
+ }
2246
+ }
2247
+ logger.info('Creating GitHub release...');
2248
+ if (isDryRun) {
2249
+ var _runConfig_publish23;
2250
+ logger.info('Would read package.json version and create GitHub release with retry logic');
2251
+ const milestonesEnabled = !((_runConfig_publish23 = runConfig.publish) === null || _runConfig_publish23 === void 0 ? void 0 : _runConfig_publish23.noMilestones);
2252
+ if (milestonesEnabled) {
2253
+ logger.info('Would close milestone for released version');
2254
+ } else {
2255
+ logger.info('Would skip milestone closure (--no-milestones)');
2256
+ }
2257
+ } else {
2258
+ const outputDirectory = runConfig.outputDirectory || DEFAULT_OUTPUT_DIRECTORY;
2259
+ const releaseNotesPath = getOutputPath(outputDirectory, 'RELEASE_NOTES.md');
2260
+ const releaseTitlePath = getOutputPath(outputDirectory, 'RELEASE_TITLE.md');
2261
+ const releaseNotesContent = await storage.readFile(releaseNotesPath, 'utf-8');
2262
+ const releaseTitle = await storage.readFile(releaseTitlePath, 'utf-8');
2263
+ // Create release with retry logic to handle GitHub tag processing delays
2264
+ let retries = 3;
2265
+ while(retries > 0){
2266
+ try {
2267
+ var _runConfig_publish24;
2268
+ await GitHub.createRelease(tagName, releaseTitle, releaseNotesContent);
2269
+ logger.info(`GitHub release created successfully for tag: ${tagName}`);
2270
+ // Close milestone for this version if enabled
2271
+ const milestonesEnabled = !((_runConfig_publish24 = runConfig.publish) === null || _runConfig_publish24 === void 0 ? void 0 : _runConfig_publish24.noMilestones);
2272
+ if (milestonesEnabled) {
2273
+ logger.info('PUBLISH_MILESTONE_CLOSING: Closing milestone for released version | Action: Close GitHub milestone | Purpose: Mark release complete');
2274
+ const version = tagName.replace(/^v/, ''); // Remove 'v' prefix if present
2275
+ await GitHub.closeMilestoneForVersion(version);
2276
+ } else {
2277
+ logger.debug('Milestone integration disabled via --no-milestones');
2278
+ }
2279
+ break; // Success - exit retry loop
2280
+ } catch (error) {
2281
+ // Check if this is a tag-not-found error that we can retry
2282
+ const isTagNotFoundError = error.message && (error.message.includes('not found') || error.message.includes('does not exist') || error.message.includes('Reference does not exist'));
2283
+ if (isTagNotFoundError && retries > 1) {
2284
+ logger.verbose(`Tag ${tagName} not yet available on GitHub, retrying in 3 seconds... (${retries - 1} retries left)`);
2285
+ await new Promise((resolve)=>setTimeout(resolve, 3000));
2286
+ retries--;
2287
+ } else if (isTagNotFoundError) {
2288
+ // Tag not found error and we're out of retries
2289
+ throw new Error(`Tag ${tagName} was not found on GitHub after ${3 - retries + 1} attempts. This may indicate a problem with tag creation or GitHub synchronization.`);
2290
+ } else {
2291
+ // Not a tag-not-found error - re-throw the original error
2292
+ throw error;
2293
+ }
2294
+ }
2295
+ }
2296
+ }
2297
+ // Wait for release workflows to complete (if enabled)
2298
+ const waitForWorkflows = ((_runConfig_publish3 = runConfig.publish) === null || _runConfig_publish3 === void 0 ? void 0 : _runConfig_publish3.waitForReleaseWorkflows) !== false; // default to true
2299
+ if (waitForWorkflows) {
2300
+ logger.info('Waiting for release workflows...');
2301
+ if (isDryRun) {
2302
+ logger.info('Would monitor GitHub Actions workflows triggered by release');
2303
+ } else {
2304
+ var _runConfig_publish25, _runConfig_publish26, _runConfig_publish27, _runConfig_publish28;
2305
+ const workflowTimeout = ((_runConfig_publish25 = runConfig.publish) === null || _runConfig_publish25 === void 0 ? void 0 : _runConfig_publish25.releaseWorkflowsTimeout) || KODRDRIV_DEFAULTS.publish.releaseWorkflowsTimeout;
2306
+ const senditMode = ((_runConfig_publish26 = runConfig.publish) === null || _runConfig_publish26 === void 0 ? void 0 : _runConfig_publish26.sendit) || false;
2307
+ const skipUserConfirmation = senditMode || ((_runConfig_publish27 = runConfig.publish) === null || _runConfig_publish27 === void 0 ? void 0 : _runConfig_publish27.skipUserConfirmation) || false;
2308
+ // Get workflow names - either from config or auto-detect
2309
+ let workflowNames = (_runConfig_publish28 = runConfig.publish) === null || _runConfig_publish28 === void 0 ? void 0 : _runConfig_publish28.releaseWorkflowNames;
2310
+ if (!workflowNames || workflowNames.length === 0) {
2311
+ logger.info('No specific workflow names configured, auto-detecting workflows triggered by release events...');
2312
+ try {
2313
+ workflowNames = await GitHub.getWorkflowsTriggeredByRelease();
2314
+ if (workflowNames.length === 0) {
2315
+ logger.info('No workflows found that are triggered by release events.');
2316
+ } else {
2317
+ logger.info(`Auto-detected release workflows: ${workflowNames.join(', ')}`);
2318
+ }
2319
+ } catch (error) {
2320
+ logger.warn(`Failed to auto-detect release workflows: ${error.message}`);
2321
+ workflowNames = undefined; // Fall back to monitoring all workflows
2322
+ }
2323
+ }
2324
+ await GitHub.waitForReleaseWorkflows(tagName, {
2325
+ timeout: workflowTimeout,
2326
+ workflowNames,
2327
+ skipUserConfirmation
2328
+ });
2329
+ }
2330
+ } else {
2331
+ logger.verbose('Skipping waiting for release workflows (disabled in config).');
2332
+ }
2333
+ // Switch back to source branch and sync with target
2334
+ logger.info('');
2335
+ logger.info(`PUBLISH_POST_SYNC: Syncing source branch with target after publish | Purpose: Keep branches synchronized | Strategy: Reset and force push`);
2336
+ await runWithDryRunSupport(`git checkout ${currentBranch}`, isDryRun);
2337
+ if (!isDryRun) {
2338
+ // Sync target into source
2339
+ // Note: With squash merging, fast-forward will fail because commit histories diverge
2340
+ if (mergeMethod === 'squash') {
2341
+ // For squash merges, reset to target branch to avoid conflicts
2342
+ // The squash merge created a single commit on target that represents all source commits
2343
+ logger.info(`Resetting ${currentBranch} to ${targetBranch} (squash merge)...`);
2344
+ await run(`git reset --hard ${targetBranch}`);
2345
+ logger.info(`PUBLISH_BRANCH_RESET: Reset source branch to target | Source: ${currentBranch} | Target: ${targetBranch} | Status: synchronized`);
2346
+ // After squash merge and reset, we need to force push
2347
+ // This is safe because we just merged to main and are syncing working branch
2348
+ logger.info(`PUBLISH_FORCE_PUSHING: Force pushing synchronized branch | Branch: ${currentBranch} | Remote: origin | Purpose: Complete post-publish sync`);
2349
+ try {
2350
+ // Verify that remote working branch is ancestor of main (safety check)
2351
+ try {
2352
+ await run(`git fetch origin ${currentBranch}`);
2353
+ await run(`git merge-base --is-ancestor origin/${currentBranch} ${targetBranch}`);
2354
+ logger.verbose(`✓ Safety check passed: origin/${currentBranch} is ancestor of ${targetBranch}`);
2355
+ } catch {
2356
+ // Remote branch might not exist yet, or already in sync - both OK
2357
+ logger.verbose(`Remote ${currentBranch} does not exist or is already synced`);
2358
+ }
2359
+ // Use --force-with-lease for safer force push
2360
+ await run(`git push --force-with-lease origin ${currentBranch}`);
2361
+ logger.info(`PUBLISH_FORCE_PUSH_SUCCESS: Successfully force pushed to remote | Branch: ${currentBranch} | Remote: origin | Status: synchronized`);
2362
+ } catch (pushError) {
2363
+ // If force push fails, provide helpful message
2364
+ logger.warn(`PUBLISH_FORCE_PUSH_FAILED: Could not force push branch | Branch: ${currentBranch} | Remote: origin | Error: ${pushError.message}`);
2365
+ logger.warn(`PUBLISH_MANUAL_PUSH_NEEDED: Manual force push required | Action: Push manually`);
2366
+ logger.warn(`PUBLISH_MANUAL_PUSH_COMMAND: Force push command | Command: git push --force-with-lease origin ${currentBranch}`);
2367
+ }
2368
+ } else {
2369
+ // For merge/rebase methods, try to merge target back into source
2370
+ logger.info(`PUBLISH_MERGE_TARGET_BACK: Merging target back into source | Target: ${targetBranch} | Source: ${currentBranch} | Purpose: Sync branches after publish`);
2371
+ // Try fast-forward first (works with merge/rebase methods)
2372
+ // Use runSecure to avoid error output for expected failure
2373
+ let fastForwardSucceeded = false;
2374
+ try {
2375
+ await runSecure('git', [
2376
+ 'merge',
2377
+ targetBranch,
2378
+ '--ff-only'
2379
+ ]);
2380
+ fastForwardSucceeded = true;
2381
+ logger.info(`PUBLISH_MERGE_FF_SUCCESS: Fast-forward merged target into source | Target: ${targetBranch} | Source: ${currentBranch} | Status: merged`);
2382
+ } catch {
2383
+ logger.verbose(`Fast-forward merge not possible, performing regular merge...`);
2384
+ }
2385
+ if (!fastForwardSucceeded) {
2386
+ await run(`git merge ${targetBranch} --no-edit`);
2387
+ logger.info(`PUBLISH_MERGE_SUCCESS: Merged target into source | Target: ${targetBranch} | Source: ${currentBranch} | Status: merged`);
2388
+ }
2389
+ }
2390
+ let versionTag = 'dev'; // Default
2391
+ if (branchDependentVersioning && runConfig.branches) {
2392
+ const sourceBranchConfig = runConfig.branches[currentBranch];
2393
+ if (sourceBranchConfig === null || sourceBranchConfig === void 0 ? void 0 : sourceBranchConfig.version) {
2394
+ // Use configured version strategy for source branch
2395
+ if (sourceBranchConfig.version.incrementLevel) {
2396
+ `pre${sourceBranchConfig.version.incrementLevel}`;
2397
+ }
2398
+ if (sourceBranchConfig.version.tag) {
2399
+ versionTag = sourceBranchConfig.version.tag;
2400
+ }
2401
+ }
2402
+ }
2403
+ // Bump to next development version
2404
+ // Note: We manually update package.json instead of using npm version to avoid
2405
+ // npm's automatic "git add package.json package-lock.json" which fails when
2406
+ // package-lock.json is gitignored
2407
+ logger.info(`PUBLISH_DEV_VERSION_BUMPING: Bumping to next development version | Tag: ${versionTag} | Purpose: Prepare for next cycle`);
2408
+ try {
2409
+ // Read current package.json
2410
+ const pkgJsonContents = await storage.readFile('package.json', 'utf-8');
2411
+ const pkgJson = safeJsonParse(pkgJsonContents, 'package.json');
2412
+ const validatedPkgJson = validatePackageJson(pkgJson, 'package.json');
2413
+ const currentVer = validatedPkgJson.version;
2414
+ // Import incrementPrereleaseVersion from core
2415
+ const { incrementPrereleaseVersion } = await import('@grunnverk/core');
2416
+ const newVersion = incrementPrereleaseVersion(currentVer, versionTag);
2417
+ // Update package.json with new version
2418
+ validatedPkgJson.version = newVersion;
2419
+ await storage.writeFile('package.json', JSON.stringify(validatedPkgJson, null, 2) + '\n', 'utf-8');
2420
+ logger.info(`PUBLISH_DEV_VERSION_BUMPED: Version bumped successfully | New Version: ${newVersion} | Type: development | Status: completed`);
2421
+ // Manually commit the version bump (package-lock.json is ignored)
2422
+ await runGitWithLock(process.cwd(), async ()=>{
2423
+ await run('git add package.json');
2424
+ await run(`git commit -m "chore: bump to ${newVersion}"`);
2425
+ }, 'commit dev version bump');
2426
+ } catch (versionError) {
2427
+ logger.warn(`PUBLISH_DEV_VERSION_BUMP_FAILED: Failed to bump version | Error: ${versionError.message} | Impact: Version not updated`);
2428
+ logger.warn('PUBLISH_MANUAL_VERSION_BUMP: Manual version bump may be needed | Action: Bump manually for next cycle | Command: npm version');
2429
+ }
2430
+ // Push updated source branch
2431
+ logger.info(`PUBLISH_PUSH_SOURCE: Pushing updated source branch | Branch: ${currentBranch} | Remote: origin | Purpose: Push development version`);
2432
+ try {
2433
+ await runGitWithLock(process.cwd(), async ()=>{
2434
+ await run(`git push origin ${currentBranch}`);
2435
+ }, `push ${currentBranch}`);
2436
+ logger.info(`PUBLISH_PUSH_SOURCE_SUCCESS: Pushed source branch successfully | Branch: ${currentBranch} | Remote: origin | Status: pushed`);
2437
+ } catch (pushError) {
2438
+ logger.warn(`PUBLISH_PUSH_SOURCE_FAILED: Failed to push source branch | Branch: ${currentBranch} | Error: ${pushError.message} | Impact: Need manual push`);
2439
+ logger.warn(`PUBLISH_MANUAL_PUSH_COMMAND: Manual push command | Command: git push origin ${currentBranch}`);
2440
+ }
2441
+ } else {
2442
+ logger.info(`PUBLISH_MERGE_DRY_RUN: Would merge target into source | Mode: dry-run | Target: ${targetBranch} | Source: ${currentBranch} | Strategy: ff-only`);
2443
+ logger.info(`PUBLISH_VERSION_DRY_RUN: Would bump version to next development | Mode: dry-run | Action: Version bump`);
2444
+ logger.info(`PUBLISH_PUSH_DRY_RUN: Would push source to remote | Mode: dry-run | Branch: ${currentBranch} | Remote: origin`);
2445
+ }
2446
+ logger.info('');
2447
+ logger.info(`PUBLISH_COMPLETE: Publish workflow completed successfully | Branch: ${currentBranch} | Status: completed | Version: next-development`);
2448
+ };
2449
+
2450
+ /**
2451
+ * Run comprehensive pre-flight validation
2452
+ */ async function runPreflightValidation(runConfig, targetBranch) {
2453
+ const result = {
2454
+ valid: true,
2455
+ errors: [],
2456
+ warnings: []
2457
+ };
2458
+ const isDryRun = runConfig.dryRun || false;
2459
+ // Run all checks in parallel to collect all issues at once
2460
+ await Promise.all([
2461
+ checkGitRepository(result, isDryRun),
2462
+ checkGitStatus(result, isDryRun),
2463
+ checkCurrentBranch(result, targetBranch, isDryRun),
2464
+ checkTargetBranchSync(result, targetBranch, isDryRun),
2465
+ checkScripts(result),
2466
+ checkWorkspaceStructure(result, runConfig),
2467
+ checkCredentials(result, runConfig, isDryRun)
2468
+ ]);
2469
+ result.valid = result.errors.length === 0;
2470
+ return result;
2471
+ }
2472
+ /**
2473
+ * Check if we're in a git repository
2474
+ */ async function checkGitRepository(result, isDryRun) {
2475
+ try {
2476
+ if (!isDryRun) {
2477
+ await run('git rev-parse --git-dir');
2478
+ }
2479
+ } catch {
2480
+ result.errors.push({
2481
+ check: 'git_repository',
2482
+ message: 'Not in a git repository or git command failed',
2483
+ suggestion: 'Please run this command from within a git repository'
2484
+ });
2485
+ }
2486
+ }
2487
+ /**
2488
+ * Check for uncommitted changes
2489
+ */ async function checkGitStatus(result, isDryRun) {
2490
+ try {
2491
+ if (!isDryRun) {
2492
+ const { stdout } = await run('git status --porcelain');
2493
+ if (stdout.trim()) {
2494
+ const files = stdout.trim().split('\n').map((line)=>line.substring(3).trim());
2495
+ result.errors.push({
2496
+ check: 'git_status',
2497
+ message: 'Working directory has uncommitted changes',
2498
+ files: files,
2499
+ suggestion: 'Please commit or stash your changes before running publish'
2500
+ });
2501
+ }
2502
+ }
2503
+ } catch (error) {
2504
+ result.errors.push({
2505
+ check: 'git_status',
2506
+ message: `Failed to check git status: ${error.message}`,
2507
+ suggestion: 'Ensure you are in a valid git repository and try again'
2508
+ });
2509
+ }
2510
+ }
2511
+ /**
2512
+ * Check that we're not running from the target branch
2513
+ */ async function checkCurrentBranch(result, targetBranch, isDryRun) {
2514
+ try {
2515
+ if (!isDryRun && targetBranch) {
2516
+ const { stdout } = await run('git branch --show-current');
2517
+ const currentBranch = stdout.trim();
2518
+ if (currentBranch === targetBranch) {
2519
+ result.errors.push({
2520
+ check: 'current_branch',
2521
+ message: `Cannot run publish from the target branch '${targetBranch}'`,
2522
+ suggestion: 'Please switch to a different branch before running publish'
2523
+ });
2524
+ }
2525
+ }
2526
+ } catch (error) {
2527
+ result.errors.push({
2528
+ check: 'current_branch',
2529
+ message: `Failed to check current branch: ${error.message}`
2530
+ });
2531
+ }
2532
+ }
2533
+ /**
2534
+ * Check target branch sync with remote
2535
+ */ async function checkTargetBranchSync(result, targetBranch, isDryRun) {
2536
+ try {
2537
+ if (!isDryRun && targetBranch) {
2538
+ // Check if local target branch exists
2539
+ try {
2540
+ await run(`git rev-parse --verify ${targetBranch}`);
2541
+ // Check if in sync with remote
2542
+ const { stdout: localSha } = await run(`git rev-parse ${targetBranch}`);
2543
+ const { stdout: remoteSha } = await run(`git rev-parse origin/${targetBranch}`);
2544
+ if (localSha.trim() !== remoteSha.trim()) {
2545
+ result.warnings.push({
2546
+ check: 'target_branch_sync',
2547
+ message: `Target branch '${targetBranch}' is not in sync with remote`,
2548
+ suggestion: `Run: git checkout ${targetBranch} && git pull origin ${targetBranch}`
2549
+ });
2550
+ }
2551
+ } catch {
2552
+ // Branch doesn't exist locally - that's okay
2553
+ }
2554
+ }
2555
+ } catch (error) {
2556
+ // Non-critical - just warn
2557
+ result.warnings.push({
2558
+ check: 'target_branch_sync',
2559
+ message: `Could not verify target branch sync: ${error.message}`
2560
+ });
2561
+ }
2562
+ }
2563
+ /**
2564
+ * Check for required scripts in package.json
2565
+ */ async function checkScripts(result, _isDryRun) {
2566
+ const storage = createStorage();
2567
+ const packageJsonPath = path.join(process.cwd(), 'package.json');
2568
+ try {
2569
+ var _packageJson_scripts;
2570
+ if (!await storage.exists(packageJsonPath)) {
2571
+ result.errors.push({
2572
+ check: 'package_json',
2573
+ message: 'package.json not found in current directory',
2574
+ files: [
2575
+ packageJsonPath
2576
+ ]
2577
+ });
2578
+ return;
2579
+ }
2580
+ const content = await storage.readFile(packageJsonPath, 'utf-8');
2581
+ const packageJson = JSON.parse(content);
2582
+ if (!((_packageJson_scripts = packageJson.scripts) === null || _packageJson_scripts === void 0 ? void 0 : _packageJson_scripts.prepublishOnly)) {
2583
+ result.errors.push({
2584
+ check: 'prepublish_script',
2585
+ message: 'prepublishOnly script is required in package.json but was not found',
2586
+ files: [
2587
+ packageJsonPath
2588
+ ],
2589
+ suggestion: 'Add a prepublishOnly script that runs your pre-flight checks (e.g., clean, lint, build, test)'
2590
+ });
2591
+ }
2592
+ } catch (error) {
2593
+ result.errors.push({
2594
+ check: 'package_json',
2595
+ message: `Failed to check package.json: ${error.message}`,
2596
+ files: [
2597
+ packageJsonPath
2598
+ ]
2599
+ });
2600
+ }
2601
+ }
2602
+ /**
2603
+ * Check workspace structure (for tree operations)
2604
+ */ async function checkWorkspaceStructure(result, runConfig, _isDryRun) {
2605
+ // Only check if this looks like a tree operation
2606
+ if (runConfig.tree) {
2607
+ const storage = createStorage();
2608
+ const rootPath = process.cwd();
2609
+ const packageJsonPath = path.join(rootPath, 'package.json');
2610
+ try {
2611
+ if (!await storage.exists(packageJsonPath)) {
2612
+ result.errors.push({
2613
+ check: 'workspace_structure',
2614
+ message: 'No package.json found at workspace root',
2615
+ files: [
2616
+ packageJsonPath
2617
+ ],
2618
+ suggestion: 'Expected workspace root with package.json containing "workspaces" field'
2619
+ });
2620
+ }
2621
+ } catch (error) {
2622
+ result.warnings.push({
2623
+ check: 'workspace_structure',
2624
+ message: `Could not verify workspace structure: ${error.message}`
2625
+ });
2626
+ }
2627
+ }
2628
+ }
2629
+ /**
2630
+ * Check for required environment variables
2631
+ */ async function checkCredentials(result, runConfig, _isDryRun) {
2632
+ var _runConfig_publish;
2633
+ const requiredEnvVars = ((_runConfig_publish = runConfig.publish) === null || _runConfig_publish === void 0 ? void 0 : _runConfig_publish.requiredEnvVars) || [];
2634
+ const missingVars = [];
2635
+ for (const envVar of requiredEnvVars){
2636
+ if (!process.env[envVar]) {
2637
+ missingVars.push(envVar);
2638
+ }
2639
+ }
2640
+ if (missingVars.length > 0) {
2641
+ if (_isDryRun) {
2642
+ result.warnings.push({
2643
+ check: 'environment_variables',
2644
+ message: `Required environment variables not set: ${missingVars.join(', ')}`,
2645
+ suggestion: 'Would fail in real publish. Please set these environment variables.'
2646
+ });
2647
+ } else {
2648
+ result.errors.push({
2649
+ check: 'environment_variables',
2650
+ message: `Missing required environment variables: ${missingVars.join(', ')}`,
2651
+ suggestion: 'Please set these environment variables before running publish'
2652
+ });
2653
+ }
2654
+ }
2655
+ }
2656
+ /**
2657
+ * Log validation results
2658
+ */ function logValidationResults(result) {
2659
+ const logger = getLogger();
2660
+ if (result.errors.length > 0) {
2661
+ logger.error('');
2662
+ logger.error('VALIDATION_FAILED: Pre-flight validation failed | Errors: ' + result.errors.length + ' | Status: cannot-proceed');
2663
+ logger.error('');
2664
+ logger.error('VALIDATION_ERRORS: The following issues must be fixed:');
2665
+ result.errors.forEach((error, idx)=>{
2666
+ logger.error(` ${idx + 1}. [${error.check}] ${error.message}`);
2667
+ if (error.files && error.files.length > 0) {
2668
+ logger.error(` Files: ${error.files.join(', ')}`);
2669
+ }
2670
+ if (error.suggestion) {
2671
+ logger.error(` → ${error.suggestion}`);
2672
+ }
2673
+ });
2674
+ logger.error('');
2675
+ }
2676
+ if (result.warnings.length > 0) {
2677
+ logger.warn('');
2678
+ logger.warn('VALIDATION_WARNINGS: Pre-flight validation warnings | Warnings: ' + result.warnings.length + ' | Status: proceed-with-caution');
2679
+ logger.warn('');
2680
+ result.warnings.forEach((warning, idx)=>{
2681
+ logger.warn(` ${idx + 1}. [${warning.check}] ${warning.message}`);
2682
+ if (warning.suggestion) {
2683
+ logger.warn(` → ${warning.suggestion}`);
2684
+ }
2685
+ });
2686
+ logger.warn('');
2687
+ }
2688
+ if (result.valid) {
2689
+ logger.info('VALIDATION_PASSED: All pre-flight validation checks passed | Status: ready-to-proceed');
2690
+ }
2691
+ }
2692
+ /**
2693
+ * Throw ValidationError if validation failed
2694
+ */ function throwIfValidationFailed(result) {
2695
+ if (!result.valid) {
2696
+ throw new ValidationError(`Pre-flight validation failed with ${result.errors.length} error(s)`, {
2697
+ phase: 'pre_flight_validation'
2698
+ }, result.errors, result.warnings);
2699
+ }
2700
+ }
2701
+
2702
+ /**
2703
+ * Log dry-run report for single package publish
2704
+ */ function logDryRunReport(report) {
2705
+ const logger = getLogger();
2706
+ logger.info('');
2707
+ logger.info('═══════════════════════════════════════════════════════════');
2708
+ logger.info(' DRY RUN REPORT ');
2709
+ logger.info('═══════════════════════════════════════════════════════════');
2710
+ logger.info('');
2711
+ // Version changes
2712
+ logger.info('📦 VERSION CHANGES:');
2713
+ logger.info(` Current: ${report.versionChanges.current}`);
2714
+ logger.info(` Proposed: ${report.versionChanges.proposed}`);
2715
+ logger.info('');
2716
+ // Operations
2717
+ logger.info('🔧 OPERATIONS TO BE PERFORMED:');
2718
+ report.operations.forEach((op, idx)=>{
2719
+ logger.info(` ${idx + 1}. ${op.step}`);
2720
+ logger.info(` ${op.description}`);
2721
+ if (op.command) {
2722
+ logger.info(` Command: ${op.command}`);
2723
+ }
2724
+ if (op.files && op.files.length > 0) {
2725
+ logger.info(' Files:');
2726
+ op.files.forEach((file)=>{
2727
+ logger.info(` - ${file.path}`);
2728
+ if (file.changes) {
2729
+ logger.info(` ${file.changes}`);
2730
+ }
2731
+ });
2732
+ }
2733
+ });
2734
+ logger.info('');
2735
+ // Tags
2736
+ if (report.tags.length > 0) {
2737
+ logger.info('🏷️ TAGS TO BE CREATED:');
2738
+ report.tags.forEach((tag)=>{
2739
+ logger.info(` - ${tag}`);
2740
+ });
2741
+ logger.info('');
2742
+ }
2743
+ // NPM publish
2744
+ logger.info('📤 NPM PUBLISH:');
2745
+ logger.info(` Package: ${report.npmPublish.package}`);
2746
+ logger.info(` Version: ${report.npmPublish.version}`);
2747
+ if (report.npmPublish.files.length > 0) {
2748
+ logger.info(' Files to be published:');
2749
+ report.npmPublish.files.forEach((file)=>{
2750
+ logger.info(` - ${file}`);
2751
+ });
2752
+ }
2753
+ logger.info('');
2754
+ // PR details
2755
+ if (report.prDetails) {
2756
+ logger.info('🔀 PULL REQUEST:');
2757
+ logger.info(` Title: ${report.prDetails.title}`);
2758
+ logger.info(` Source: ${report.prDetails.sourceBranch}`);
2759
+ logger.info(` Target: ${report.prDetails.targetBranch}`);
2760
+ logger.info('');
2761
+ }
2762
+ logger.info('═══════════════════════════════════════════════════════════');
2763
+ logger.info(' END DRY RUN REPORT ');
2764
+ logger.info('═══════════════════════════════════════════════════════════');
2765
+ logger.info('');
2766
+ logger.info('ℹ️ This was a dry run. No actual changes were made.');
2767
+ logger.info(' To execute these operations, run without --dry-run flag.');
2768
+ logger.info('');
2769
+ }
2770
+ /**
2771
+ * Log dry-run report for tree operations
2772
+ */ function logTreeDryRunReport(report) {
2773
+ const logger = getLogger();
2774
+ logger.info('');
2775
+ logger.info('═══════════════════════════════════════════════════════════');
2776
+ logger.info(' TREE DRY RUN REPORT ');
2777
+ logger.info('═══════════════════════════════════════════════════════════');
2778
+ logger.info('');
2779
+ // Build order
2780
+ logger.info('📦 PACKAGE BUILD ORDER:');
2781
+ logger.info(` Total packages: ${report.buildOrder.length}`);
2782
+ logger.info(' Execution order:');
2783
+ report.buildOrder.forEach((pkg, idx)=>{
2784
+ logger.info(` ${idx + 1}. ${pkg}`);
2785
+ });
2786
+ logger.info('');
2787
+ // Package details
2788
+ logger.info('🔧 PACKAGE OPERATIONS:');
2789
+ report.packages.forEach((pkg)=>{
2790
+ logger.info(` [${pkg.order}] ${pkg.name}`);
2791
+ if (pkg.dependencies.length > 0) {
2792
+ logger.info(` Dependencies: ${pkg.dependencies.join(', ')}`);
2793
+ }
2794
+ if (pkg.versionChange) {
2795
+ logger.info(` Version: ${pkg.versionChange.from} → ${pkg.versionChange.to}`);
2796
+ }
2797
+ if (pkg.operations.length > 0) {
2798
+ logger.info(' Operations:');
2799
+ pkg.operations.forEach((op)=>{
2800
+ logger.info(` - ${op}`);
2801
+ });
2802
+ }
2803
+ logger.info('');
2804
+ });
2805
+ // Dependency updates
2806
+ if (report.dependencyUpdates.length > 0) {
2807
+ logger.info('🔗 INTER-PROJECT DEPENDENCY UPDATES:');
2808
+ report.dependencyUpdates.forEach((update)=>{
2809
+ logger.info(` ${update.package}`);
2810
+ logger.info(` ${update.dependency}: ${update.from} → ${update.to}`);
2811
+ });
2812
+ logger.info('');
2813
+ }
2814
+ logger.info('═══════════════════════════════════════════════════════════');
2815
+ logger.info(' END TREE DRY RUN REPORT ');
2816
+ logger.info('═══════════════════════════════════════════════════════════');
2817
+ logger.info('');
2818
+ logger.info('ℹ️ This was a dry run. No actual changes were made.');
2819
+ logger.info(' To execute these operations, run without --dry-run flag.');
2820
+ logger.info('');
2821
+ }
2822
+ /**
2823
+ * Create a dry-run report for a single package publish
2824
+ */ function createDryRunReport(currentVersion, proposedVersion, packageName, operations, targetBranch, sourceBranch) {
2825
+ return {
2826
+ operations,
2827
+ versionChanges: {
2828
+ current: currentVersion,
2829
+ proposed: proposedVersion
2830
+ },
2831
+ tags: [
2832
+ `v${proposedVersion}`
2833
+ ],
2834
+ npmPublish: {
2835
+ package: packageName,
2836
+ version: proposedVersion,
2837
+ files: [] // Would be populated by analyzing package.json files field
2838
+ },
2839
+ prDetails: targetBranch && sourceBranch ? {
2840
+ title: `Release ${proposedVersion}`,
2841
+ targetBranch,
2842
+ sourceBranch
2843
+ } : undefined
2844
+ };
2845
+ }
2846
+
2847
+ export { createDryRunReport, execute$2 as development, logDryRunReport, logTreeDryRunReport, logValidationResults, execute as publish, execute$1 as release, runPreflightValidation, throwIfValidationFailed };
2848
+ //# sourceMappingURL=index.js.map