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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
#![warn(dead_code)]
#![warn(unused_imports)]
#![allow(clippy::all)]

//! # Bumps
//!
//! This module is responsible for managing the bumps in the monorepo.
use semver::{BuildMetadata, Prerelease, Version as SemVersion};
use serde::{Deserialize, Serialize};
use serde_json::json;
use std::fs::OpenOptions;
use std::io::{BufWriter, Write};
use std::path::{Path, PathBuf};

use super::changes::init_changes;
use super::conventional::ConventionalPackage;
use super::conventional::{get_conventional_for_package, ConventionalPackageOptions};
use super::git::{
    git_add, git_add_all, git_all_files_changed_since_sha, git_commit, git_config, git_current_sha,
    git_fetch_all, git_push, git_tag,
};
use super::packages::get_packages;
use super::packages::PackageInfo;
use super::paths::get_project_root_path;

#[cfg(feature = "napi")]
#[napi(string_enum)]
#[derive(Debug, Deserialize, Serialize, PartialEq)]
pub enum Bump {
    Major,
    Minor,
    Patch,
    Snapshot,
}

#[cfg(not(feature = "napi"))]
#[derive(Debug, Clone, Deserialize, Serialize, Copy, PartialEq)]
/// Enum representing the type of bump to be performed.
pub enum Bump {
    Major,
    Minor,
    Patch,
    Snapshot,
}

#[cfg(feature = "napi")]
#[napi(object)]
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq)]
pub struct BumpOptions {
    pub packages: Vec<String>,
    pub since: Option<String>,
    pub release_as: Bump,
    pub fetch_all: Option<bool>,
    pub fetch_tags: Option<bool>,
    pub sync_deps: Option<bool>,
    pub push: Option<bool>,
    pub cwd: Option<String>,
}

#[cfg(not(feature = "napi"))]
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq)]
/// Struct representing the options for the bump operation.
pub struct BumpOptions {
    pub packages: Vec<String>,
    pub since: Option<String>,
    pub release_as: Bump,
    pub fetch_all: Option<bool>,
    pub fetch_tags: Option<bool>,
    pub sync_deps: Option<bool>,
    pub push: Option<bool>,
    pub cwd: Option<String>,
}

#[cfg(not(feature = "napi"))]
#[derive(Debug, Clone, Deserialize, Serialize)]
/// Struct representing the bump package.
pub struct BumpPackage {
    pub from: String,
    pub to: String,
    pub release_as: Bump,
    pub conventional: ConventionalPackage,
}

#[cfg(feature = "napi")]
#[napi(object)]
#[derive(Debug, Clone, Deserialize, Serialize)]
pub struct BumpPackage {
    pub from: String,
    pub to: String,
    pub release_as: Bump,
    pub conventional: ConventionalPackage,
}

impl Bump {
    /// Bumps the version of the package to major.
    fn bump_major(version: String) -> SemVersion {
        let mut sem_version = SemVersion::parse(&version).unwrap();
        sem_version.major += 1;
        sem_version.minor = 0;
        sem_version.patch = 0;
        sem_version.pre = Prerelease::EMPTY;
        sem_version.build = BuildMetadata::EMPTY;
        sem_version
    }

    /// Bumps the version of the package to minor.
    fn bump_minor(version: String) -> SemVersion {
        let mut sem_version = SemVersion::parse(&version).unwrap();
        sem_version.minor += 1;
        sem_version.patch = 0;
        sem_version.pre = Prerelease::EMPTY;
        sem_version.build = BuildMetadata::EMPTY;
        sem_version
    }

    /// Bumps the version of the package to patch.
    fn bump_patch(version: String) -> SemVersion {
        let mut sem_version = SemVersion::parse(&version).unwrap();
        sem_version.patch += 1;
        sem_version.pre = Prerelease::EMPTY;
        sem_version.build = BuildMetadata::EMPTY;
        sem_version
    }

    /// Bumps the version of the package to snapshot appending the sha to the version.
    fn bump_snapshot(version: String) -> SemVersion {
        let sha = git_current_sha(None);
        let alpha = format!("alpha.{}", sha);

        let mut sem_version = SemVersion::parse(&version).unwrap();
        sem_version.pre = Prerelease::new(alpha.as_str()).unwrap_or(Prerelease::EMPTY);
        sem_version.build = BuildMetadata::EMPTY;
        sem_version
    }
}

/// Bumps the version of dev-dependencies and dependencies.
pub fn sync_bumps(bump_package: &BumpPackage, cwd: Option<String>) -> Vec<String> {
    let ref root = match cwd {
        Some(ref dir) => get_project_root_path(Some(PathBuf::from(dir))).unwrap(),
        None => get_project_root_path(None).unwrap(),
    };

    get_packages(Some(root.to_string()))
        .iter()
        .filter(|package| {
            let mut package_json_map = serde_json::Map::new();
            package_json_map.clone_from(package.pkg_json.as_object().unwrap());

            if package_json_map.contains_key("dependencies") {
                let dependencies_value = package_json_map.get_mut("dependencies").unwrap();
                let dependencies_value = dependencies_value.as_object_mut().unwrap();
                let has_dependency =
                    dependencies_value.contains_key(&bump_package.conventional.package_info.name);

                if has_dependency {
                    dependencies_value
                        .entry(bump_package.conventional.package_info.name.to_string())
                        .and_modify(|version| *version = json!(bump_package.to.to_string()));

                    package_json_map["dependencies"] = json!(dependencies_value);

                    let file = OpenOptions::new()
                        .write(true)
                        .truncate(true)
                        .open(&package.package_json_path)
                        .unwrap();
                    let writer = BufWriter::new(&file);
                    serde_json::to_writer_pretty(writer, &package_json_map).unwrap();

                    git_add(&root.to_string(), &package.package_json_path.to_owned())
                        .expect("Failed to add package.json");
                    git_commit(
                        format!(
                            "chore: update dependency {} in {}",
                            bump_package.conventional.package_info.name.to_string(),
                            package.name.to_string()
                        ),
                        None,
                        None,
                        Some(root.to_string()),
                    )
                    .expect("Failed to commit package.json");
                }

                return has_dependency;
            }

            if package_json_map.contains_key("devDependencies") {
                let dev_dependencies_value = package_json_map.get_mut("devDependencies").unwrap();
                let dev_dependencies_value = dev_dependencies_value.as_object_mut().unwrap();
                let has_dependency = dev_dependencies_value
                    .contains_key(&bump_package.conventional.package_info.name);

                if has_dependency {
                    dev_dependencies_value
                        .entry(bump_package.conventional.package_info.name.to_string())
                        .and_modify(|version| *version = json!(bump_package.to.to_string()));

                    package_json_map["devDependencies"] = json!(dev_dependencies_value);

                    let file = OpenOptions::new()
                        .write(true)
                        .truncate(true)
                        .open(&package.package_json_path)
                        .unwrap();
                    let writer = BufWriter::new(&file);
                    serde_json::to_writer_pretty(writer, &package_json_map).unwrap();

                    git_add(&root.to_string(), &package.package_json_path.to_owned())
                        .expect("Failed to add package.json");
                    git_commit(
                        format!(
                            "chore: update devDependency {} in {}",
                            bump_package.conventional.package_info.name.to_string(),
                            package.name.to_string()
                        ),
                        None,
                        None,
                        Some(root.to_string()),
                    )
                    .expect("Failed to commit package.json");
                }

                return has_dependency;
            }

            false
        })
        .map(|package| package.name.to_string())
        .collect::<Vec<String>>()
}

/// Get bumps version of the package. If sync_deps is true, it will also sync the dependencies and dev-dependencies.
/// It will also commit the changes to git.
pub fn get_bumps(options: BumpOptions) -> Vec<BumpPackage> {
    let ref root = match options.cwd {
        Some(ref dir) => get_project_root_path(Some(PathBuf::from(dir))).unwrap(),
        None => get_project_root_path(None).unwrap(),
    };

    let ref since = match options.since {
        Some(ref since) => since.to_string(),
        None => String::from("main"),
    };

    let release_as = options.release_as.to_owned();
    let mut bumps: Vec<BumpPackage> = vec![];

    if options.fetch_tags.is_some() {
        git_fetch_all(Some(root.to_string()), options.fetch_tags)
            .expect("No possible to fetch tags");
    }

    let packages = get_packages(Some(root.to_string()))
        .iter()
        .filter(|package| options.packages.contains(&package.name))
        .map(|package| package.to_owned())
        .collect::<Vec<PackageInfo>>();

    if packages.len() == 0 {
        return bumps;
    }

    for mut package in packages {
        let package_version = &package.version.to_string();
        let changelog_exists =
            Path::new(&format!("{}/CHANGELOG.md", package.package_path)).exists();

        let semversion = match release_as {
            Bump::Major => Bump::bump_major(package_version.to_string()),
            Bump::Minor => Bump::bump_minor(package_version.to_string()),
            Bump::Patch => Bump::bump_patch(package_version.to_string()),
            Bump::Snapshot => Bump::bump_snapshot(package_version.to_string()),
        };

        let title = match changelog_exists {
            true => None,
            false => Some("# What changed?".to_string()),
        };

        let changed_files =
            git_all_files_changed_since_sha(since.to_string(), Some(root.to_string()));
        let ref version = semversion.to_string();

        package.update_version(version.to_string());
        package.extend_changed_files(changed_files);

        let conventional = get_conventional_for_package(
            &package,
            options.fetch_all,
            Some(root.to_string()),
            &Some(ConventionalPackageOptions {
                version: Some(version.to_string()),
                title,
            }),
        );

        let bump = BumpPackage {
            from: package_version.to_string(),
            to: version.to_string(),
            release_as,
            conventional,
        };
        bumps.push(bump.to_owned());

        if options.sync_deps.unwrap_or(false) {
            let sync_packages = sync_bumps(&bump, Some(root.to_string()));

            if sync_packages.len() > 0 {
                let sync_bumps = get_bumps(BumpOptions {
                    packages: sync_packages,
                    since: Some(since.to_string()),
                    release_as: Bump::Patch,
                    fetch_all: options.fetch_all,
                    fetch_tags: options.fetch_tags,
                    sync_deps: Some(true),
                    push: Some(false),
                    cwd: Some(root.to_string()),
                });

                bumps.extend(sync_bumps);
            }
        }
    }

    bumps
}

/// Apply version bumps, commit and push changes. Returns a list of packages that have been updated.
/// Also generate changelog file and update dependencies and devDependencies in package.json.
pub fn apply_bumps(options: BumpOptions) -> Vec<BumpPackage> {
    let ref root = match options.cwd {
        Some(ref dir) => get_project_root_path(Some(PathBuf::from(dir))).unwrap(),
        None => get_project_root_path(None).unwrap(),
    };

    let ref changes_data = init_changes(Some(root.to_string()), &None);
    let git_user_name = changes_data.git_user_name.to_owned();
    let git_user_email = changes_data.git_user_email.to_owned();

    git_config(
        &git_user_name.unwrap_or(String::from("")),
        &git_user_email.unwrap_or(String::from("")),
        &root.to_string(),
    )
    .expect("Failed to set git user name and email");

    let bumps = get_bumps(options.to_owned());

    if bumps.len() != 0 {
        for bump in &bumps {
            let git_message = changes_data.message.to_owned();

            let ref bump_pkg_json_file_path =
                PathBuf::from(bump.conventional.package_info.package_json_path.to_string());
            let ref bump_changelog_file_path =
                PathBuf::from(bump.conventional.package_info.package_path.to_string())
                    .join(String::from("CHANGELOG.md"));

            // Write bump_pkg_json_file_path
            let bump_pkg_json_file = OpenOptions::new()
                .write(true)
                .append(false)
                .open(bump_pkg_json_file_path)
                .unwrap();
            let pkg_json_writer = BufWriter::new(bump_pkg_json_file);
            serde_json::to_writer_pretty(pkg_json_writer, &bump.conventional.package_info.pkg_json)
                .unwrap();

            // Write bump_changelog_file_path
            let mut bump_changelog_file = OpenOptions::new()
                .write(true)
                .create(true)
                .append(false)
                .open(bump_changelog_file_path)
                .unwrap();

            bump_changelog_file
                .write_all(bump.conventional.changelog_output.as_bytes())
                .unwrap();

            let ref package_tag = format!("{}@{}", bump.conventional.package_info.name, bump.to);

            git_add_all(&root.to_string()).expect("Failed to add all files to git");
            git_commit(
                git_message.unwrap_or(String::from("chore: release version")),
                None,
                None,
                Some(root.to_string()),
            )
            .unwrap();
            git_tag(
                package_tag.to_string(),
                Some(format!(
                    "chore: release {} to version {}",
                    bump.conventional.package_info.name, bump.to
                )),
                Some(root.to_string()),
            )
            .unwrap();

            if options.push.unwrap_or(false) {
                git_push(Some(root.to_string()), Some(true)).unwrap();
            }
        }
    }

    bumps
}

#[cfg(test)]
mod tests {
    use super::*;

    use crate::manager::PackageManager;
    use crate::packages::get_changed_packages;
    use crate::paths::get_project_root_path;
    use crate::utils::create_test_monorepo;
    use std::fs::remove_dir_all;
    use std::fs::File;
    use std::io::Write;
    use std::process::Command;
    use std::process::Stdio;

    fn create_package_change(monorepo_dir: &PathBuf) -> Result<(), Box<dyn std::error::Error>> {
        let js_path = monorepo_dir.join("packages/package-b/index.js");

        let branch = Command::new("git")
            .current_dir(&monorepo_dir)
            .arg("checkout")
            .arg("-b")
            .arg("feat/message")
            .stdout(Stdio::piped())
            .spawn()
            .expect("Git branch problem");

        branch.wait_with_output()?;

        let mut js_file = File::create(&js_path)?;
        js_file
            .write_all(r#"export const message = "hello";"#.as_bytes())
            .unwrap();

        let add = Command::new("git")
            .current_dir(&monorepo_dir)
            .arg("add")
            .arg(".")
            .stdout(Stdio::piped())
            .spawn()
            .expect("Git add problem");

        add.wait_with_output()?;

        let commit = Command::new("git")
            .current_dir(&monorepo_dir)
            .arg("commit")
            .arg("-m")
            .arg("feat: message to the world")
            .stdout(Stdio::piped())
            .spawn()
            .expect("Git commit problem");

        commit.wait_with_output()?;

        Ok(())
    }

    #[test]
    fn test_get_bumps() -> Result<(), Box<dyn std::error::Error>> {
        let ref monorepo_dir = create_test_monorepo(&PackageManager::Npm)?;
        let project_root = get_project_root_path(Some(monorepo_dir.to_path_buf()));

        create_package_change(monorepo_dir)?;

        let ref root = project_root.unwrap().to_string();

        let packages = get_changed_packages(Some(String::from("main")), Some(root.to_string()))
            .iter()
            .map(|package| package.name.to_string())
            .collect::<Vec<String>>();

        let bumps = get_bumps(BumpOptions {
            packages,
            since: Some(String::from("main")),
            release_as: Bump::Minor,
            fetch_all: None,
            fetch_tags: None,
            sync_deps: Some(true),
            push: Some(false),
            cwd: Some(root.to_string()),
        });

        assert_eq!(bumps.len(), 2);
        remove_dir_all(&monorepo_dir)?;
        Ok(())
    }

    #[test]
    fn test_apply_bumps() -> Result<(), Box<dyn std::error::Error>> {
        let ref monorepo_dir = create_test_monorepo(&PackageManager::Npm)?;
        let project_root = get_project_root_path(Some(monorepo_dir.to_path_buf()));

        create_package_change(monorepo_dir)?;

        let ref root = project_root.unwrap().to_string();

        let packages = get_changed_packages(Some(String::from("main")), Some(root.to_string()))
            .iter()
            .map(|package| package.name.to_string())
            .collect::<Vec<String>>();

        let main_branch = Command::new("git")
            .current_dir(&monorepo_dir)
            .arg("checkout")
            .arg("main")
            .stdout(Stdio::piped())
            .spawn()
            .expect("Git checkout main problem");

        main_branch.wait_with_output()?;

        let merge_branch = Command::new("git")
            .current_dir(&monorepo_dir)
            .arg("merge")
            .arg("feat/message")
            .stdout(Stdio::piped())
            .spawn()
            .expect("Git merge problem");

        merge_branch.wait_with_output()?;

        let bump_options = BumpOptions {
            packages,
            since: Some(String::from("main")),
            release_as: Bump::Minor,
            fetch_all: None,
            fetch_tags: None,
            sync_deps: Some(true),
            push: Some(false),
            cwd: Some(root.to_string()),
        };

        let bumps = apply_bumps(bump_options);

        assert_eq!(bumps.len(), 2);
        remove_dir_all(&monorepo_dir)?;
        Ok(())
    }
}