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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
//! Dependency upgrade module for detecting and applying external package upgrades.
//!
//! **What**: Provides functionality to detect available upgrades for external npm packages,
//! apply those upgrades with optional filtering, and manage rollback on failures.
//!
//! **How**: This module integrates with npm registries (including private registries) to fetch
//! package metadata, compares current versions with available versions, and updates package.json
//! files. It supports dry-run mode, automatic changeset creation, and backup/rollback mechanisms.
//!
//! **Why**: To enable safe, automated dependency upgrades with fine-grained control over which
//! packages and versions to upgrade, supporting both security patches and feature updates while
//! maintaining project stability.
//!
//! # Features
//!
//! - **Upgrade Detection**: Detect available upgrades for external dependencies
//! - **Selective Upgrades**: Filter by patch/minor/major, specific packages, or dependencies
//! - **Registry Support**: Support for npm registry, private registries, and scoped packages
//! - **.npmrc Integration**: Read authentication and registry configuration from .npmrc
//! - **Dry-Run Mode**: Preview changes before applying them
//! - **Automatic Changeset**: Optionally create changesets for applied upgrades
//! - **Backup/Rollback**: Automatic backup and rollback on failure
//! - **Concurrency**: Parallel package metadata fetching for performance
//!
//! # Example
//!
//! ```rust,ignore
//! use sublime_pkg_tools::upgrade::{
//! detect_upgrades, apply_upgrades, DetectionOptions, UpgradeSelection
//! };
//! use sublime_pkg_tools::config::PackageToolsConfig;
//! use sublime_standard_tools::filesystem::FileSystemManager;
//! use std::path::PathBuf;
//!
//! # async fn example() -> Result<(), Box<dyn std::error::Error>> {
//! let workspace_root = PathBuf::from(".");
//! let fs = FileSystemManager::new();
//! let config = PackageToolsConfig::default();
//!
//! // Detect available upgrades
//! let options = DetectionOptions::all();
//! let available = detect_upgrades(&workspace_root, options, &fs).await?;
//! println!("Found {} available upgrades", available.packages.len());
//!
//! // Apply patch upgrades only (dry run)
//! let selection = UpgradeSelection::patch_only();
//! let result = apply_upgrades(available.packages.clone(), selection, true, &fs).await?;
//! println!("Would upgrade {} dependencies", result.applied.len());
//!
//! // Apply for real
//! let result = apply_upgrades(available.packages, selection, false, &fs).await?;
//! println!("Upgraded {} dependencies", result.applied.len());
//! # Ok(())
//! # }
//! ```
//!
//! # Upgrade Selection
//!
//! Control which upgrades to apply using selection criteria:
//!
//! ```rust,ignore
//! use sublime_pkg_tools::upgrade::{
//! detect_upgrades, apply_upgrades, DetectionOptions, UpgradeSelection
//! };
//! use sublime_standard_tools::filesystem::FileSystemManager;
//! use std::path::PathBuf;
//!
//! # async fn example() -> Result<(), Box<dyn std::error::Error>> {
//! # let workspace_root = PathBuf::from(".");
//! # let fs = FileSystemManager::new();
//! # let available = detect_upgrades(&workspace_root, DetectionOptions::all(), &fs).await?;
//! // Only patch upgrades
//! let selection = UpgradeSelection::patch_only();
//! apply_upgrades(available.packages.clone(), selection, false, &fs).await?;
//!
//! // Patch and minor upgrades
//! let selection = UpgradeSelection::minor_and_patch();
//! apply_upgrades(available.packages.clone(), selection, false, &fs).await?;
//!
//! // Specific packages only
//! let selection = UpgradeSelection::packages(vec!["express".to_string(), "lodash".to_string()]);
//! apply_upgrades(available.packages.clone(), selection, false, &fs).await?;
//!
//! // Specific dependencies only
//! let selection = UpgradeSelection::dependencies(vec!["react".to_string()]);
//! apply_upgrades(available.packages, selection, false, &fs).await?;
//! # Ok(())
//! # }
//! ```
//!
//! # Private Registry Support
//!
//! Configure private registries and authentication:
//!
//! ```toml
//! [package_tools.upgrade.registry]
//! default_registry = "https://registry.npmjs.org"
//! timeout_secs = 30
//! retry_attempts = 3
//! read_npmrc = true
//!
//! [package_tools.upgrade.registry.scoped]
//! "@myorg" = "https://npm.myorg.com"
//! "@internal" = "https://registry.internal.corp"
//! ```
//!
//! # Automatic Changeset Creation
//!
//! Automatically create changesets for applied upgrades:
//!
//! ```rust,ignore
//! use sublime_pkg_tools::upgrade::{
//! detect_upgrades, apply_with_changeset, DetectionOptions, UpgradeSelection
//! };
//! use sublime_pkg_tools::changeset::ChangesetManager;
//! use sublime_pkg_tools::config::PackageToolsConfig;
//! use sublime_standard_tools::filesystem::FileSystemManager;
//! use std::path::PathBuf;
//!
//! # async fn example() -> Result<(), Box<dyn std::error::Error>> {
//! let workspace_root = PathBuf::from(".");
//! let fs = FileSystemManager::new();
//! let config = PackageToolsConfig::default();
//!
//! // Detect upgrades
//! let options = DetectionOptions::all();
//! let available = detect_upgrades(&workspace_root, options, &fs).await?;
//!
//! // Apply upgrades with automatic changeset creation
//! let manager = ChangesetManager::new(&workspace_root).await?;
//! let selection = UpgradeSelection::patch_only();
//! let result = apply_with_changeset(
//! available.packages,
//! selection,
//! false,
//! &workspace_root,
//! &config.upgrade,
//! Some(&manager),
//! &fs
//! ).await?;
//!
//! if let Some(changeset_id) = result.changeset_id {
//! println!("Created changeset: {}", changeset_id);
//! }
//! # Ok(())
//! # }
//! ```
//!
//! # Rollback on Failure
//!
//! Automatic rollback when upgrades fail:
//!
//! ```rust,ignore
//! use sublime_pkg_tools::upgrade::UpgradeManager;
//!
//! # async fn example() -> Result<(), Box<dyn std::error::Error>> {
//! # let manager: UpgradeManager = todo!();
//! // TODO: will be implemented on story 9.5
//! // // Upgrades are automatically rolled back on failure
//! // match manager.apply_upgrades(selection, false).await {
//! // Ok(result) => println!("Success: {} upgrades applied", result.applied.len()),
//! // Err(e) => {
//! // // Automatic rollback has occurred
//! // println!("Upgrade failed and was rolled back: {}", e);
//! // }
//! // }
//! //
//! // // Or manually rollback the last operation
//! // manager.rollback_last().await?;
//! # Ok(())
//! # }
//! ```
//!
//! # Module Structure
//!
//! This module will contain:
//! - `manager`: The main `UpgradeManager` for orchestrating upgrade operations
//! - `registry`: Registry client for fetching package metadata
//! - `npmrc`: .npmrc parser for registry and authentication configuration
//! - `selection`: Upgrade selection and filtering logic
//! - `result`: Result types for upgrade operations
//! - `backup`: Backup and rollback mechanisms
// Registry module for NPM package metadata queries (Story 9.1 - IMPLEMENTED)
// Detection module for upgrade detection (Story 9.3 - IMPLEMENTED)
// Application module for applying upgrades (Story 9.4 - IMPLEMENTED)
// Re-export registry public types
pub use ;
// Re-export detection public types and functions
pub use ;
// Re-export application public types and functions
pub use ;
// Backup module for backup and rollback (Story 9.5 - IMPLEMENTED)
// Re-export backup public types
pub use ;
// Automatic changeset creation (Story 9.6 - IMPLEMENTED)
// Integrated in application module via apply_with_changeset function
// Manager module for orchestrating upgrade operations (Story 9.7 - IMPLEMENTED)
// Re-export manager public types
pub use UpgradeManager;
// Tests module