-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.rs
More file actions
259 lines (235 loc) · 9.16 KB
/
setup.rs
File metadata and controls
259 lines (235 loc) · 9.16 KB
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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
use clap::Args;
use socket_patch_core::package_json::find::{find_package_json_files, WorkspaceType};
use socket_patch_core::package_json::update::{update_package_json, UpdateStatus};
use std::io::{self, Write};
use std::path::{Path, PathBuf};
use crate::output::stdin_is_tty;
#[derive(Args)]
pub struct SetupArgs {
/// Working directory
#[arg(long, default_value = ".")]
pub cwd: PathBuf,
/// Preview changes without modifying files
#[arg(short = 'd', long = "dry-run", default_value_t = false)]
pub dry_run: bool,
/// Skip confirmation prompt
#[arg(short = 'y', long, default_value_t = false)]
pub yes: bool,
/// Output results as JSON
#[arg(long, default_value_t = false)]
pub json: bool,
}
pub async fn run(args: SetupArgs) -> i32 {
if !args.json {
println!("Searching for package.json files...");
}
let find_result = find_package_json_files(&args.cwd).await;
// For pnpm monorepos, only update root package.json.
// pnpm runs root postinstall on `pnpm install`, so workspace-level
// postinstall scripts are unnecessary. Individual workspaces may not
// have `@socketsecurity/socket-patch` as a dependency, causing
// `npx @socketsecurity/socket-patch apply` to fail due to pnpm's
// strict module isolation.
let package_json_files = match find_result.workspace_type {
WorkspaceType::Pnpm => find_result
.files
.into_iter()
.filter(|loc| loc.is_root)
.collect(),
_ => find_result.files,
};
if package_json_files.is_empty() {
if args.json {
println!("{}", serde_json::to_string_pretty(&serde_json::json!({
"status": "no_files",
"updated": 0,
"alreadyConfigured": 0,
"errors": 0,
"files": [],
})).unwrap());
} else {
println!("No package.json files found");
}
return 0;
}
if !args.json {
println!("Found {} package.json file(s)", package_json_files.len());
}
// Preview changes (always preview first)
let mut preview_results = Vec::new();
for loc in &package_json_files {
let result = update_package_json(&loc.path, true).await;
preview_results.push(result);
}
// Display preview
let to_update: Vec<_> = preview_results
.iter()
.filter(|r| r.status == UpdateStatus::Updated)
.collect();
let already_configured: Vec<_> = preview_results
.iter()
.filter(|r| r.status == UpdateStatus::AlreadyConfigured)
.collect();
let errors: Vec<_> = preview_results
.iter()
.filter(|r| r.status == UpdateStatus::Error)
.collect();
if !args.json {
println!("\nPackage.json files to be updated:\n");
if !to_update.is_empty() {
println!("Will update:");
for result in &to_update {
let rel_path = pathdiff(&result.path, &args.cwd);
println!(" + {rel_path}");
if result.old_script.is_empty() {
println!(" Current: (no postinstall script)");
} else {
println!(" Current: \"{}\"", result.old_script);
}
println!(" New: \"{}\"", result.new_script);
}
println!();
}
if !already_configured.is_empty() {
println!("Already configured (will skip):");
for result in &already_configured {
let rel_path = pathdiff(&result.path, &args.cwd);
println!(" = {rel_path}");
}
println!();
}
if !errors.is_empty() {
println!("Errors:");
for result in &errors {
let rel_path = pathdiff(&result.path, &args.cwd);
println!(
" ! {}: {}",
rel_path,
result.error.as_deref().unwrap_or("unknown error")
);
}
println!();
}
}
if to_update.is_empty() {
if args.json {
println!("{}", serde_json::to_string_pretty(&serde_json::json!({
"status": "already_configured",
"updated": 0,
"alreadyConfigured": already_configured.len(),
"errors": errors.len(),
"files": preview_results.iter().map(|r| {
serde_json::json!({
"path": r.path,
"status": match r.status {
UpdateStatus::Updated => "updated",
UpdateStatus::AlreadyConfigured => "already_configured",
UpdateStatus::Error => "error",
},
"error": r.error,
})
}).collect::<Vec<_>>(),
})).unwrap());
} else {
println!("All package.json files are already configured with socket-patch!");
}
return 0;
}
// If not dry-run, ask for confirmation
if !args.dry_run {
if !args.yes && !args.json {
if !stdin_is_tty() {
// Non-interactive: default to yes with warning
eprintln!("Non-interactive mode detected, proceeding automatically.");
} else {
print!("Proceed with these changes? (y/N): ");
io::stdout().flush().unwrap();
let mut answer = String::new();
io::stdin().read_line(&mut answer).unwrap();
let answer = answer.trim().to_lowercase();
if answer != "y" && answer != "yes" {
println!("Aborted");
return 0;
}
}
}
if !args.json {
println!("\nApplying changes...");
}
let mut results = Vec::new();
for loc in &package_json_files {
let result = update_package_json(&loc.path, false).await;
results.push(result);
}
let updated = results.iter().filter(|r| r.status == UpdateStatus::Updated).count();
let already = results.iter().filter(|r| r.status == UpdateStatus::AlreadyConfigured).count();
let errs = results.iter().filter(|r| r.status == UpdateStatus::Error).count();
if args.json {
println!("{}", serde_json::to_string_pretty(&serde_json::json!({
"status": if errs > 0 { "partial_failure" } else { "success" },
"updated": updated,
"alreadyConfigured": already,
"errors": errs,
"files": results.iter().map(|r| {
serde_json::json!({
"path": r.path,
"status": match r.status {
UpdateStatus::Updated => "updated",
UpdateStatus::AlreadyConfigured => "already_configured",
UpdateStatus::Error => "error",
},
"error": r.error,
})
}).collect::<Vec<_>>(),
})).unwrap());
} else {
println!("\nSummary:");
println!(" {updated} file(s) updated");
println!(" {already} file(s) already configured");
if errs > 0 {
println!(" {errs} error(s)");
}
}
if errs > 0 { 1 } else { 0 }
} else {
let updated = preview_results.iter().filter(|r| r.status == UpdateStatus::Updated).count();
let already = preview_results.iter().filter(|r| r.status == UpdateStatus::AlreadyConfigured).count();
let errs = preview_results.iter().filter(|r| r.status == UpdateStatus::Error).count();
if args.json {
println!("{}", serde_json::to_string_pretty(&serde_json::json!({
"status": "dry_run",
"wouldUpdate": updated,
"alreadyConfigured": already,
"errors": errs,
"dryRun": true,
"files": preview_results.iter().map(|r| {
serde_json::json!({
"path": r.path,
"status": match r.status {
UpdateStatus::Updated => "updated",
UpdateStatus::AlreadyConfigured => "already_configured",
UpdateStatus::Error => "error",
},
"oldScript": r.old_script,
"newScript": r.new_script,
"error": r.error,
})
}).collect::<Vec<_>>(),
})).unwrap());
} else {
println!("\nSummary:");
println!(" {updated} file(s) would be updated");
println!(" {already} file(s) already configured");
if errs > 0 {
println!(" {errs} error(s)");
}
}
0
}
}
fn pathdiff(path: &str, base: &Path) -> String {
let p = Path::new(path);
p.strip_prefix(base)
.map(|r| r.display().to_string())
.unwrap_or_else(|_| path.to_string())
}