synopkg 14.0.1

Consistent dependency versions in large JavaScript Monorepos
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
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
use {
  crate::{dependency_type::DependencyType, group_selector::GroupSelector, packages::Packages},
  clap::{builder::ValueParser, crate_description, crate_name, crate_version, Arg, ArgMatches, Command},
  color_print::cformat,
  itertools::Itertools,
  log::LevelFilter,
  std::{env, path::PathBuf},
};

#[derive(Debug)]
pub enum Subcommand {
  Lint,
  Fix,
  Format,
  Update,
  List,
  Json,
  ListMismatches,
  LintSemverRanges,
  FixMismatches,
  SetSemverRanges,
  Prompt,
}

#[derive(Debug)]
pub enum SortBy {
  Count,
  Name,
}

#[derive(Debug)]
pub enum UpdateTarget {
  /// "*.*.*"
  Latest,
  /// "1.*.*"
  Minor,
  /// "1.2.*"
  Patch,
}

#[derive(Debug)]
pub struct Cli {
  /// Whether to check formatting instead of fixing it
  pub check: bool,
  /// Path to a specific config file to use
  pub config_path: Option<String>,
  /// The path to the root of the project
  pub cwd: PathBuf,
  /// Whether to disable ANSI color codes in terminal output
  pub disable_ansi: bool,
  /// Whether to simulate changes without writing them to disk
  pub dry_run: bool,
  /// - `--dependencies` to filter by dependency name
  pub dependencies: Vec<String>,
  /// - `--dependency-types` to filter by dependency type
  pub dependency_types: Vec<String>,
  /// - `--packages` to filter by package name
  pub packages: Vec<String>,
  /// - `--specifier-types` to filter by specifier type
  pub specifier_types: Vec<String>,
  /// Which severity levels of logging to display
  #[allow(dead_code)]
  pub log_levels: Vec<LevelFilter>,
  /// Whether to indicate that a dependency is a package developed locally
  pub show_hints: bool,
  /// Whether to output ignored dependencies regardless
  pub show_ignored: bool,
  /// Whether to list every affected instance of a dependency when listing
  /// version or semver range mismatches
  pub show_instances: bool,
  /// Whether to show the name of the status code for each dependency and
  /// instance, such as `HighestSemverMismatch`
  pub show_status_codes: bool,
  /// Whether to sort dependencies and instances by name A-Z or by descending
  /// count
  pub sort: SortBy,
  /// Glob patterns for package.json files to inspect
  pub source_patterns: Vec<String>,
  /// The subcommand that the user is running
  pub subcommand: Subcommand,
  /// How greedy npm updates should be
  pub target: UpdateTarget,
}

impl Cli {
  /// Parse all command-line arguments from the user into a `Cli` struct
  pub fn parse() -> Self {
    fn from_arg_matches(subcommand: Subcommand, matches: &ArgMatches) -> Cli {
      Cli {
        check: (matches!(&subcommand, Subcommand::Format | Subcommand::Update)) && matches.get_flag("check"),
        config_path: matches.get_one::<String>("config").cloned(),
        cwd: env::current_dir().unwrap(),
        dependencies: get_patterns(matches, "dependencies"),
        dependency_types: get_patterns(matches, "dependency-types"),
        disable_ansi: matches.get_flag("no-ansi"),
        dry_run: (matches!(&subcommand, Subcommand::Fix | Subcommand::Format | Subcommand::Update)) && matches.get_flag("dry-run"),
        log_levels: get_log_levels(matches),
        packages: get_patterns(matches, "packages"),
        show_hints: should_show(matches, "hints"),
        show_ignored: should_show(matches, "ignored"),
        show_instances: should_show(matches, "instances"),
        show_status_codes: should_show(matches, "statuses"),
        sort: get_order_by(matches),
        source_patterns: get_patterns(matches, "source"),
        specifier_types: get_patterns(matches, "specifier-types"),
        subcommand,
        target: get_target(matches),
      }
    }

    match create().get_matches().subcommand() {
      Some(("lint", matches)) => from_arg_matches(Subcommand::Lint, matches),
      Some(("fix", matches)) => from_arg_matches(Subcommand::Fix, matches),
      Some(("format", matches)) => from_arg_matches(Subcommand::Format, matches),
      Some(("update", matches)) => from_arg_matches(Subcommand::Update, matches),
      Some(("list", matches)) => from_arg_matches(Subcommand::List, matches),
      Some(("json", matches)) => from_arg_matches(Subcommand::Json, matches),
      Some(("list-mismatches", _)) => Cli {
        check: false,
        config_path: None,
        cwd: env::current_dir().unwrap(),
        dependencies: vec![],
        dependency_types: vec![],
        disable_ansi: false,
        dry_run: false,
        log_levels: vec![],
        packages: vec![],
        show_hints: false,
        show_ignored: false,
        show_instances: false,
        show_status_codes: false,
        sort: SortBy::Name,
        source_patterns: vec![],
        specifier_types: vec![],
        subcommand: Subcommand::ListMismatches,
        target: UpdateTarget::Latest,
      },
      Some(("lint-semver-ranges", _)) => Cli {
        check: false,
        config_path: None,
        cwd: env::current_dir().unwrap(),
        dependencies: vec![],
        dependency_types: vec![],
        disable_ansi: false,
        dry_run: false,
        log_levels: vec![],
        packages: vec![],
        show_hints: false,
        show_ignored: false,
        show_instances: false,
        show_status_codes: false,
        sort: SortBy::Name,
        source_patterns: vec![],
        specifier_types: vec![],
        subcommand: Subcommand::LintSemverRanges,
        target: UpdateTarget::Latest,
      },
      Some(("fix-mismatches", _)) => Cli {
        check: false,
        config_path: None,
        cwd: env::current_dir().unwrap(),
        dependencies: vec![],
        dependency_types: vec![],
        disable_ansi: false,
        dry_run: false,
        log_levels: vec![],
        packages: vec![],
        show_hints: false,
        show_ignored: false,
        show_instances: false,
        show_status_codes: false,
        sort: SortBy::Name,
        source_patterns: vec![],
        specifier_types: vec![],
        subcommand: Subcommand::FixMismatches,
        target: UpdateTarget::Latest,
      },
      Some(("set-semver-ranges", _)) => Cli {
        check: false,
        config_path: None,
        cwd: env::current_dir().unwrap(),
        dependencies: vec![],
        dependency_types: vec![],
        disable_ansi: false,
        dry_run: false,
        log_levels: vec![],
        packages: vec![],
        show_hints: false,
        show_ignored: false,
        show_instances: false,
        show_status_codes: false,
        sort: SortBy::Name,
        source_patterns: vec![],
        specifier_types: vec![],
        subcommand: Subcommand::SetSemverRanges,
        target: UpdateTarget::Latest,
      },
      Some(("prompt", _)) => Cli {
        check: false,
        config_path: None,
        cwd: env::current_dir().unwrap(),
        dependencies: vec![],
        dependency_types: vec![],
        disable_ansi: false,
        dry_run: false,
        log_levels: vec![],
        packages: vec![],
        show_hints: false,
        show_ignored: false,
        show_instances: false,
        show_status_codes: false,
        sort: SortBy::Name,
        source_patterns: vec![],
        specifier_types: vec![],
        subcommand: Subcommand::Prompt,
        target: UpdateTarget::Latest,
      },
      _ => {
        std::process::exit(1);
      }
    }
  }

  /// Create a `GroupSelector` struct based on the provided command line options
  /// which relate to filtering of packages and dependencies.
  ///
  /// `GroupSelector` is the same struct as used by `VersionGroup` and
  /// `SemverGroup` and this CLI `GroupSelector`, when configured, serves as a
  /// single `VersionGroup` which overrides all those set in config.
  pub fn get_filters(&self, packages: &Packages, all_dependency_types: &[DependencyType]) -> Option<GroupSelector> {
    if self.dependencies.is_empty() && self.dependency_types.is_empty() && self.packages.is_empty() && self.specifier_types.is_empty() {
      None
    } else {
      Some(GroupSelector::new(
        /* all_packages: */ packages,
        /* include_dependencies: */ self.dependencies.clone(),
        /* include_dependency_types: */ self.dependency_types.clone(),
        /* alias_name: */ "CLI filters".to_string(),
        /* include_packages: */ self.packages.clone(),
        /* include_specifier_types: */ self.specifier_types.clone(),
        /* all_dependency_types: */ all_dependency_types,
      ))
    }
  }
}

fn create() -> Command {
  Command::new(crate_name!())
    .about(crate_description!())
    .version(crate_version!())
    .subcommand(
      Command::new("lint")
        .about("Lint all versions and ranges and exit with 0 or 1 based on whether all files match your Synopkg configuration file")
        .after_long_help(additional_help())
        .arg(config_option("lint"))
        .arg(dependencies_option("lint"))
        .arg(dependency_types_option("lint"))
        .arg(log_levels_option("lint"))
        .arg(no_ansi_option("lint"))
        .arg(show_option_versions("lint"))
        .arg(sort_option("lint"))
        .arg(source_option("lint"))
        .arg(specifier_types_option("lint")),
    )
    .subcommand(
      Command::new("fix")
        .about("Ensure that multiple packages requiring the same dependency use the same version")
        .after_long_help(additional_help())
        .arg(config_option("fix"))
        .arg(dependencies_option("fix"))
        .arg(dependency_types_option("fix"))
        .arg(dry_run_option("fix"))
        .arg(log_levels_option("fix"))
        .arg(no_ansi_option("fix"))
        .arg(show_option_versions("fix"))
        .arg(sort_option("fix"))
        .arg(source_option("fix"))
        .arg(specifier_types_option("fix")),
    )
    .subcommand(
      Command::new("format")
        .about("Sort package.json fields into a predictable order and nested fields alphabetically")
        .after_long_help(additional_help())
        .arg(
          Arg::new("check")
            .long("check")
            .long_help(cformat!(r#"Lint formatting instead of fixing it"#))
            .action(clap::ArgAction::SetTrue),
        )
        .arg(config_option("format"))
        .arg(dry_run_option("format"))
        .arg(log_levels_option("format"))
        .arg(no_ansi_option("format"))
        .arg(source_option("format")),
    )
    .subcommand(
      Command::new("update")
        .about("Update to the latest versions on the npm registry")
        .after_long_help(additional_help())
        .arg(
          Arg::new("check")
            .long("check")
            .long_help(cformat!(r#"Check versions are up to date instead of updating them"#))
            .action(clap::ArgAction::SetTrue),
        )
        .arg(config_option("update"))
        .arg(dependencies_option("update"))
        .arg(dependency_types_option("update"))
        .arg(dry_run_option("update"))
        .arg(log_levels_option("update"))
        .arg(no_ansi_option("update"))
        .arg(source_option("update"))
        .arg(specifier_types_option("update"))
        .arg(target_option("update")),
    )
    .subcommand(
      Command::new("list")
        .about("Query and inspect all dependencies in your project, both valid and invalid")
        .after_long_help(additional_help())
        .arg(config_option("list"))
        .arg(dependencies_option("list"))
        .arg(dependency_types_option("list"))
        .arg(log_levels_option("list"))
        .arg(no_ansi_option("list"))
        .arg(show_option_list("list"))
        .arg(sort_option("list"))
        .arg(source_option("list"))
        .arg(specifier_types_option("list")),
    )
    .subcommand(
      Command::new("json")
        .about("Output all dependencies as flattened JSON objects")
        .after_long_help(additional_help())
        .arg(config_option("json"))
        .arg(dependencies_option("json"))
        .arg(dependency_types_option("json"))
        .arg(log_levels_option("json"))
        .arg(no_ansi_option("json"))
        .arg(sort_option("json"))
        .arg(source_option("json"))
        .arg(specifier_types_option("json")),
    )
    .subcommand(
      Command::new("list-mismatches")
        .about("DEPRECATED: Use 'synopkg lint' instead")
        .hide(true)
        .disable_help_flag(true)
        .disable_version_flag(true),
    )
    .subcommand(
      Command::new("lint-semver-ranges")
        .about("DEPRECATED: Use 'synopkg lint' instead")
        .hide(true)
        .disable_help_flag(true)
        .disable_version_flag(true),
    )
    .subcommand(
      Command::new("fix-mismatches")
        .about("DEPRECATED: Use 'synopkg fix' instead")
        .hide(true)
        .disable_help_flag(true)
        .disable_version_flag(true),
    )
    .subcommand(
      Command::new("set-semver-ranges")
        .about("DEPRECATED: Use 'synopkg fix' instead")
        .hide(true)
        .disable_help_flag(true)
        .disable_version_flag(true),
    )
    .subcommand(
      Command::new("prompt")
        .about("MISSING: Not yet implemented in v14")
        .hide(true)
        .disable_help_flag(true)
        .disable_version_flag(true),
    )
}

fn config_option(_command: &str) -> Arg {
  Arg::new("config").long("config").value_name("PATH").long_help(cformat!(
    r#"Path to a specific config file to use. When set, config file discovery is skipped."#
  ))
}

fn dependencies_option(command: &str) -> Arg {
  let short_help = "Only include dependencies whose name matches this glob pattern";
  Arg::new("dependencies")
    .long("dependencies")
    .help(short_help)
    .long_help(cformat!(
      r#"{short_help}

<bold><underline>Important:</underline></bold>
You <underline>must</> add quotes around your filter so your shell doesn't
interpret it.

<bold><underline>Examples:</underline></bold>
<dim>Exact match for "react"</>
<dim>$</dim> <blue><bold>synopkg {command}</bold> --dependencies 'react'</>
<dim>Substring match for "react"</>
<dim>$</dim> <blue><bold>synopkg {command}</bold> --dependencies '**react**'</>
<dim>All dependencies under the AWS SDK scope</>
<dim>$</dim> <blue><bold>synopkg {command}</bold> --dependencies '@aws-sdk/**'</>
<dim>Exact match for "react" or "webpack" (2 approaches)</>
<dim>$</dim> <blue><bold>synopkg {command}</bold> --dependencies 'react' --dependencies 'webpack'</>
<dim>$</dim> <blue><bold>synopkg {command}</bold> --dependencies '{has_braces}'</>
<dim>Substring match for "react" or "webpack"  (2 approaches)</>
<dim>$</dim> <blue><bold>synopkg {command}</bold> --dependencies '**react**' --dependencies '**webpack**'</>
<dim>$</dim> <blue><bold>synopkg {command}</bold> --dependencies '**{has_braces}**'</>"#,
      has_braces = "{react,webpack}"
    ))
    .action(clap::ArgAction::Append)
    .value_name("dependency-name-pattern")
}

fn show_option_versions(command: &str) -> Arg {
  let short_help = "Control what information is displayed in terminal output";
  Arg::new("show")
    .long("show")
    .help(short_help)
    .long_help(cformat!(
      r#"{short_help}

<bold><underline>Values:</underline></bold>
<blue>instances</>  Show every instance of every dependency
<blue>hints</>      Show a hint alongside dependencies developed in this repo
<blue>statuses</>   Show specifically how/why a dependency or instance is valid or invalid
<blue>all</>        Shorthand to enable all of the above
<blue>none</>       Shorthand to disable all of the above

<bold><underline>Examples:</underline></bold>
<dim>Only opt into showing status codes</dim>
<dim>$</dim> <blue><bold>synopkg {command}</bold> --show statuses</>
<dim>Show all instances, not just their names</dim>
<dim>$</dim> <blue><bold>synopkg {command}</bold> --show instances</>
<dim>Show highest level of detail</dim>
<dim>$</dim> <blue><bold>synopkg {command}</bold> --show all</>
<dim>Show lowest level of detail</dim>
<dim>$</dim> <blue><bold>synopkg {command}</bold> --show none</>"#
    ))
    .value_delimiter(',')
    .value_parser(["hints", "instances", "statuses", "all", "none"])
    .value_name("comma-separated-detail-names")
    .default_value("all")
}

fn show_option_list(command: &str) -> Arg {
  let short_help = "Control what information is displayed in terminal output";
  Arg::new("show")
    .long("show")
    .help(short_help)
    .long_help(cformat!(
      r#"{short_help}

<bold><underline>Values:</underline></bold>
<blue>instances</>  Show every instance of every dependency
<blue>hints</>      Show a hint alongside dependencies developed in this repo
<blue>statuses</>   Show specifically how/why a dependency or instance is valid or invalid
<blue>all</>        Shorthand to enable all of the above
<blue>none</>       Shorthand to disable all of the above

<bold><underline>Examples:</underline></bold>
<dim>Only opt into showing status codes</dim>
<dim>$</dim> <blue><bold>synopkg {command}</bold> --show statuses</>
<dim>Show all instances, not just their names</dim>
<dim>$</dim> <blue><bold>synopkg {command}</bold> --show instances</>
<dim>Show ignored dependencies and instances</dim>
<dim>$</dim> <blue><bold>synopkg {command}</bold> --show ignored</>
<dim>Show highest level of detail</dim>
<dim>$</dim> <blue><bold>synopkg {command}</bold> --show all</>
<dim>Choose only some values</dim>
<dim>$</dim> <blue><bold>synopkg {command}</bold> --show hints,statuses</>
<dim>Show lowest level of detail</dim>
<dim>$</dim> <blue><bold>synopkg {command}</bold> --show none</>"#
    ))
    .value_delimiter(',')
    .value_parser(["hints", "ignored", "instances", "statuses", "all", "none"])
    .value_name("comma-separated-detail-names")
    .default_value("hints,statuses")
}

fn sort_option(command: &str) -> Arg {
  let short_help = "Change the order in which dependencies are displayed";
  Arg::new("sort")
    .long("sort")
    .help(short_help)
    .long_help(cformat!(
      r#"{short_help}

<bold><underline>Examples:</underline></bold>
<dim>Sort by count, in descending order</>
<dim>$</dim> <blue><bold>synopkg {command}</bold> --sort count</>
<dim>Sort A-Z by name</>
<dim>$</dim> <blue><bold>synopkg {command}</bold> --sort name</>"#
    ))
    .action(clap::ArgAction::Set)
    .value_parser(["count", "name"])
    .value_name("choice")
    .default_value("name")
}

fn specifier_types_option(command: &str) -> Arg {
  let short_help = "Only include instances whose version specifiers are of the given type(s)";
  Arg::new("specifier-types")
    .long("specifier-types")
    .help(short_help)
    .long_help(cformat!(
      r#"{short_help}

<bold><underline>Values:</underline></bold>
<blue>alias</>               <yellow>npm:@preact/compat</>
<blue>exact</>               <yellow>1.2.3</>, <yellow>1.2.3-alpha</>, <yellow>1.2.3-rc.1</>
<blue>file</>                <yellow>file:./path/to/package</>
<blue>git</>                 <yellow>git+https://github.com/user/repo.git</>
<blue>latest</>              <yellow>latest</>, <yellow>*</>
<blue>major</>               <yellow>1</>
<blue>minor</>               <yellow>1.2</>
<blue>missing</>             A local package.json with a missing .version
<blue>range</>               <yellow>^1.2.3</>, <yellow>^1.2.3-alpha</>, <yellow>^1.2.3-rc.1</>
<blue>range-complex</>       <yellow>^1.2.3-alpha || ~1.2.3-rc.1</>
<blue>range-major</>         <yellow>^1</>
<blue>range-minor</>         <yellow>^1.2</>
<blue>tag</>                 <yellow>alpha</>
<blue>unsupported</>         <yellow>wtf|#|broken</>
<blue>url</>                 <yellow>https://example.com/package</>
<blue>workspace-protocol</>  <yellow>workspace:*</>

<bold><underline>Examples:</underline></bold>
<dim>Exact versions only</>
<dim>$</dim> <blue><bold>synopkg {command}</bold> --show instances --specifier-types exact
<dim>Missing or unsupported versions</>
<dim>$</dim> <blue><bold>synopkg {command}</bold> --show instances --specifier-types missing,unsupported
<dim>Latest or workspace protocol only</>
<dim>$</dim> <blue><bold>synopkg {command}</bold> --show instances --specifier-types latest,workspace-protocol"#
    ))
    .value_delimiter(',')
    .value_parser([
      "alias",
      "exact",
      "file",
      "git",
      "latest",
      "major",
      "minor",
      "missing",
      "range",
      "range-complex",
      "range-major",
      "range-minor",
      "tag",
      "unsupported",
      "url",
      "workspace-protocol",
    ])
    .value_name("comma-separated-specifier-type-names")
}

fn dependency_types_option(command: &str) -> Arg {
  let short_help = "Only include dependencies of the given type(s)";
  Arg::new("dependency-types")
    .long("dependency-types")
    .help(short_help)
    .long_help(cformat!(
      r#"{short_help}

<bold><underline>Built-in Types:</underline></bold>
<blue>dev</>            devDependencies
<blue>local</>          version
<blue>overrides</>      overrides
<blue>peer</>           peerDependencies
<blue>pnpmOverrides</>  pnpm.overrides
<blue>prod</>           dependencies
<blue>resolutions</>    resolutions

<bold><underline>Custom Types:</underline></bold>
See <blue>https://envrs.github.io/synopkg/config/custom-types/</>

<bold><underline>Examples:</underline></bold>
<dim>devDependencies only</>
<dim>$</dim> <blue><bold>synopkg {command}</bold> --dependency-types dev
<dim>dependencies and devDependencies only</>
<dim>$</dim> <blue><bold>synopkg {command}</bold> --dependency-types dev,prod"#
    ))
    .value_delimiter(',')
    .value_name("comma-separated-dependency-type-names")
}

fn dry_run_option(command: &str) -> Arg {
  let short_help = "Simulate changes without writing them to disk";
  Arg::new("dry-run")
    .long("dry-run")
    .help(short_help)
    .long_help(cformat!(
      r#"{short_help}

<bold><underline>Examples:</underline></bold>
<dim>$</dim> <blue><bold>synopkg {command}</bold> --dry-run</>"#
    ))
    .action(clap::ArgAction::SetTrue)
}

fn log_levels_option(command: &str) -> Arg {
  let short_help = "Control how detailed the log output should be";
  Arg::new("log-levels")
    .long("log-levels")
    .help(short_help)
    .long_help(cformat!(
      r#"{short_help}

<bold><underline>Examples:</underline></bold>
<dim>Turn off logging completely</dim>
<dim>$</dim> <blue><bold>synopkg {command}</bold> --log-levels off</>
<dim>Only show verbose debugging logs</dim>
<dim>$</dim> <blue><bold>synopkg {command}</bold> --log-levels debug</>
<dim>Show everything</dim>
<dim>$</dim> <blue><bold>synopkg {command}</bold> --log-levels error,warn,info,debug</>"#
    ))
    .value_delimiter(',')
    .value_parser(["off", "error", "warn", "info", "debug"])
    .value_name("comma-separated-log-level-names")
    .default_value("error,warn,info")
}

fn no_ansi_option(command: &str) -> Arg {
  let short_help = "Disable ANSI colored output and terminal hyperlinks";
  Arg::new("no-ansi")
    .long("no-ansi")
    .help(short_help)
    .long_help(cformat!(
      r#"{short_help}

<bold><underline>Examples:</underline></bold>
<dim>$</dim> <blue><bold>synopkg {command}</bold> --no-ansi</>"#
    ))
    .action(clap::ArgAction::SetTrue)
}

fn source_option(command: &str) -> Arg {
  let short_help = "A list of quoted glob patterns for package.json files to read from";
  Arg::new("source")
    .long("source")
    .help(short_help)
    .long_help(cformat!(
      r#"{short_help}

<bold><underline>Examples:</underline></bold>
<dim>$</dim> <blue><bold>synopkg {command}</bold> --source 'package.json' --source 'apps/*/package.json'</>

<bold><underline>Resolving Packages:</underline></bold>
Patterns are discovered in the following order, first one wins:

1. <blue>--source</> CLI options
2. <blue>.source</> property of your synopkg config file
3. <blue>.workspaces.packages</> property of package.json (yarn)
4. <blue>.workspaces</> property of package.json (npm and yarn)
5. <blue>.packages</> property of pnpm-workspace.yaml
6. <blue>.packages</> property of lerna.json
7. Default to <blue>["package.json","packages/*/package.json"]</>"#
    ))
    .action(clap::ArgAction::Append)
    .value_parser(ValueParser::new(validate_source))
    .value_name("file-pattern")
}

fn target_option(command: &str) -> Arg {
  let short_help = "Limit updates to only those within the semver portion";
  Arg::new("target")
    .long("target")
    .help(short_help)
    .long_help(cformat!(
      r#"{short_help}

<bold><underline>Examples:</underline></bold>
<dim>Accept any update in latest (x.x.x)</dim>
<dim>$</dim> <blue><bold>synopkg {command}</bold> --target latest</>
<dim>Only update minor versions (1.x.x)</dim>
<dim>$</dim> <blue><bold>synopkg {command}</bold> --target minor</>
<dim>Only update patch versions (1.2.x)</dim>
<dim>$</dim> <blue><bold>synopkg {command}</bold> --target patch</>"#
    ))
    .action(clap::ArgAction::Set)
    .value_parser(["latest", "minor", "patch"])
    .value_name("greediness")
    .default_value("latest")
}

fn additional_help() -> String {
  cformat!(
    r#"<bold><underline>References:</underline></bold>
- Documentation: <blue><underline>https://envrs.github.io/synopkg</></>"#
  )
}

fn validate_source(value: &str) -> Result<String, String> {
  if value.ends_with("package.json") {
    Ok(value.to_string())
  } else {
    Err("must end with 'package.json'".to_string())
  }
}

fn get_order_by(matches: &ArgMatches) -> SortBy {
  matches
    .try_get_one::<String>("sort")
    .ok()
    .flatten()
    .map(|sort| match sort.as_str() {
      "count" => SortBy::Count,
      "name" => SortBy::Name,
      _ => unreachable!(),
    })
    .unwrap_or(SortBy::Name)
}

fn get_patterns(matches: &ArgMatches, option_name: &str) -> Vec<String> {
  matches
    .try_get_many::<String>(option_name)
    .ok()
    .flatten()
    .map(|source| source.into_iter().map(|source| source.to_owned()).collect_vec())
    .unwrap_or_default()
}

fn get_target(matches: &ArgMatches) -> UpdateTarget {
  matches
    .try_get_one::<String>("target")
    .ok()
    .flatten()
    .map(|target| match target.as_str() {
      "latest" => UpdateTarget::Latest,
      "minor" => UpdateTarget::Minor,
      "patch" => UpdateTarget::Patch,
      _ => unreachable!(),
    })
    .unwrap_or(UpdateTarget::Latest)
}

fn should_show(matches: &ArgMatches, name: &str) -> bool {
  matches
    .try_get_many::<String>("show")
    .ok()
    .flatten()
    .map(|show| {
      let names = show.collect_vec();
      if names.contains(&&"all".to_string()) {
        true
      } else if names.contains(&&"none".to_string()) {
        false
      } else {
        names.contains(&&name.to_string())
      }
    })
    .unwrap_or(false)
}

fn get_log_levels(matches: &ArgMatches) -> Vec<LevelFilter> {
  let chosen_values = matches
    .try_get_many::<String>("log-levels")
    .ok()
    .flatten()
    .unwrap_or_default()
    .collect_vec();
  vec![
    ("off", LevelFilter::Off),
    ("error", LevelFilter::Error),
    ("warn", LevelFilter::Warn),
    ("info", LevelFilter::Info),
    ("debug", LevelFilter::Debug),
  ]
  .into_iter()
  .filter(|(name, _)| {
    chosen_values
      .iter()
      .any(|choice| choice == &&"all".to_string() || choice == &&name.to_string())
  })
  .map(|(_, level)| level)
  .collect_vec()
}