1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
//! Changes command implementation for Node.js bindings.
//!
//! # What
//!
//! This module implements the `changes` NAPI function that analyzes what files
//! and packages have changed since a given reference point. It supports multiple
//! analysis modes including working directory, commit range, and with versions.
//!
//! # How
//!
//! The function:
//! 1. Validates the input parameters (root path, since reference, mode)
//! 2. Calls `execute_changes` from `sublime_cli_tools`
//! 3. Captures the JSON output containing change analysis
//! 4. Returns a `JsonResponse<ChangesData>` with package changes and summary
//!
//! # Why
//!
//! Change analysis is essential for:
//! - Targeted builds: Only build packages that have changed
//! - Targeted tests: Only test affected packages
//! - Release planning: Understanding scope of changes
//! - CI/CD optimization: Reduce pipeline time by focusing on changed code
//!
//! # Examples
//!
//! ```typescript
//! import { changes } from '@websublime/workspace-tools';
//!
//! // Analyze changes since a branch (e.g., main)
//! const result = await changes({
//! root: '.',
//! since: 'main',
//! mode: 'commitRange'
//! });
//!
//! if (result.success) {
//! console.log(`Packages with changes: ${result.data.summary.packagesWithChanges}`);
//! console.log(`Total files changed: ${result.data.summary.totalFilesChanged}`);
//! console.log(`Total commits: ${result.data.summary.totalCommits}`);
//!
//! for (const pkg of result.data.packages) {
//! if (pkg.hasChanges) {
//! console.log(`\n${pkg.name}:`);
//! console.log(` Files: ${pkg.stats.filesModified} modified, ${pkg.stats.filesAdded} added`);
//! console.log(` Lines: +${pkg.stats.linesAdded} -${pkg.stats.linesDeleted}`);
//!
//! for (const file of pkg.files) {
//! console.log(` ${file.changeType}: ${file.path}`);
//! }
//! }
//! }
//! }
//!
//! // Analyze working directory changes (uncommitted)
//! const wdResult = await changes({
//! root: '.',
//! mode: 'workingDirectory'
//! });
//!
//! // Analyze changes between two commits
//! const rangeResult = await changes({
//! root: '.',
//! since: 'v1.0.0',
//! until: 'HEAD',
//! mode: 'commitRange'
//! });
//!
//! // Get changes for specific packages only
//! const filteredResult = await changes({
//! root: '.',
//! since: 'main',
//! packages: ['@scope/core', '@scope/utils']
//! });
//! ```
// TODO: will be implemented on story 9.2 - Changes Command
//
// Implementation outline:
//
// #[napi]
// pub async fn changes(params: ChangesParams) -> JsonResponse<ChangesData> {
// // 1. Validate parameters
// if let Err(e) = validate_root(¶ms.root) {
// return JsonResponse::from_error_info(e);
// }
//
// // 2. Determine analysis mode
// // - workingDirectory: Analyze uncommitted changes
// // - commitRange: Analyze changes between commits/refs
// // - withVersions: Analyze with version bump suggestions
//
// // 3. Create ChangesArgs from params
// let args = ChangesArgs {
// since: params.since,
// until: params.until,
// packages: params.packages,
// include_stats: params.include_stats.unwrap_or(true),
// ..Default::default()
// };
//
// // 4. Create Output with JSON format for capturing
// let mut buffer = Vec::new();
// let output = Output::new(OutputFormat::Json, &mut buffer, false);
//
// // 5. Determine workspace root path
// let root_path = Path::new(¶ms.root);
// let config_path = None; // Will use default config discovery
//
// // 6. Call execute_changes from CLI (commands/changes.rs)
// match execute_changes(&args, &output, root_path, config_path).await {
// Ok(()) => {
// // 7. Parse JSON response from buffer
// let json_str = String::from_utf8_lossy(&buffer);
// match serde_json::from_str::<JsonResponse<ChangesData>>(&json_str) {
// Ok(response) => response,
// Err(e) => JsonResponse::error(format!("Failed to parse changes response: {}", e)),
// }
// }
// Err(e) => JsonResponse::error(format!("Changes analysis failed: {}", e)),
// }
// }
//
// Note: The changes command relies on git operations to detect file changes.
// It uses the sublime_git_tools crate for git operations and sublime_pkg_tools
// for package mapping. The ChangesAnalyzer and PackageMapper from pkg crate
// handle the heavy lifting.