forked from volta-cli/volta
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcli.rs
More file actions
133 lines (114 loc) · 4.19 KB
/
cli.rs
File metadata and controls
133 lines (114 loc) · 4.19 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
use clap::{builder::styling, ColorChoice, Parser};
use crate::command::{self, Command};
use volta_core::error::{ExitCode, Fallible};
use volta_core::session::Session;
use volta_core::style::{text_width, MAX_WIDTH};
#[derive(Parser)]
#[command(
about = "The JavaScript Launcher ⚡",
long_about = "The JavaScript Launcher ⚡
To install a tool in your toolchain, use `volta install`.
To pin your project's runtime or package manager, use `volta pin`.",
color = ColorChoice::Auto,
disable_version_flag = true,
styles = styles(),
term_width = text_width().unwrap_or(MAX_WIDTH),
)]
pub(crate) struct Volta {
#[command(subcommand)]
pub(crate) command: Option<Subcommand>,
/// Enables verbose diagnostics
#[arg(long, global = true)]
pub(crate) verbose: bool,
/// Prevents unnecessary output
#[arg(
long,
global = true,
conflicts_with = "verbose",
aliases = &["silent"]
)]
pub(crate) quiet: bool,
/// Prints the current version of Volta
#[arg(short, long)]
pub(crate) version: bool,
}
impl Volta {
pub(crate) fn run(self, session: &mut Session) -> Fallible<ExitCode> {
if self.version {
// suffix indicator for dev build
if cfg!(debug_assertions) {
println!("{}-dev", env!("CARGO_PKG_VERSION"));
} else {
println!("{}", env!("CARGO_PKG_VERSION"));
}
Ok(ExitCode::Success)
} else if let Some(command) = self.command {
command.run(session)
} else {
Volta::parse_from(["volta", "help"].iter()).run(session)
}
}
}
#[derive(clap::Subcommand)]
pub(crate) enum Subcommand {
/// Fetches a tool to the local machine
Fetch(command::Fetch),
/// Installs a tool in your toolchain
Install(command::Install),
/// Uninstalls a tool from your toolchain
Uninstall(command::Uninstall),
/// Pins your project's runtime or package manager
Pin(command::Pin),
/// Displays the current toolchain
#[command(alias = "ls")]
List(command::List),
/// Generates Volta completions
///
/// By default, completions will be generated for the value of your current shell,
/// shell, i.e. the value of `SHELL`. If you set the `<shell>` option, completions
/// will be generated for that shell instead.
///
/// If you specify a directory, the completions will be written to a file there;
/// otherwise, they will be written to `stdout`.
#[command(arg_required_else_help = true)]
Completions(command::Completions),
/// Locates the actual binary that will be called by Volta
Which(command::Which),
#[command(long_about = crate::command::r#use::USAGE, hide = true)]
Use(command::Use),
/// Enables Volta for the current user / shell
Setup(command::Setup),
/// Run a command with custom Node, npm, pnpm, and/or Yarn versions
Run(command::Run),
}
impl Subcommand {
pub(crate) fn run(self, session: &mut Session) -> Fallible<ExitCode> {
match self {
Subcommand::Fetch(fetch) => fetch.run(session),
Subcommand::Install(install) => install.run(session),
Subcommand::Uninstall(uninstall) => uninstall.run(session),
Subcommand::Pin(pin) => pin.run(session),
Subcommand::List(list) => list.run(session),
Subcommand::Completions(completions) => completions.run(session),
Subcommand::Which(which) => which.run(session),
Subcommand::Use(r#use) => r#use.run(session),
Subcommand::Setup(setup) => setup.run(session),
Subcommand::Run(run) => run.run(session),
}
}
}
fn styles() -> styling::Styles {
styling::Styles::plain()
.header(
styling::AnsiColor::Yellow.on_default()
| styling::Effects::BOLD
| styling::Effects::ITALIC,
)
.usage(
styling::AnsiColor::Yellow.on_default()
| styling::Effects::BOLD
| styling::Effects::ITALIC,
)
.literal(styling::AnsiColor::Green.on_default() | styling::Effects::BOLD)
.placeholder(styling::AnsiColor::BrightBlue.on_default())
}