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
mod commands;
mod dirs;
mod download;
mod extract;
mod index;
mod link;
mod platform;
mod resolve;
mod shell;
#[cfg(windows)]
mod toast;
mod version;
mod version_file;
#[cfg(windows)]
mod winpath;
use anyhow::Result;
use clap::{CommandFactory, Parser, Subcommand};
use commands::{env, install, list, switch, update};
/// Simple Node Environment: a fast, cross-platform Node.js version manager.
#[derive(Parser)]
#[command(name = "sne", version, disable_version_flag = true)]
struct Cli {
/// Print version
#[arg(short = 'v', visible_short_alias = 'V', long = "version", action = clap::ArgAction::Version)]
print_version: Option<bool>,
#[command(subcommand)]
command: Cmd,
}
#[derive(Subcommand)]
enum Cmd {
/// Install one or more versions (`22`, `lts`, `latest`, `lts/iron`, `>=20 <23`).
///
/// With no version, installs what the project asks for in .nvmrc, .node-version
/// or package.json.
#[command(visible_alias = "i")]
Install {
versions: Vec<String>,
/// Also install the global npm packages of this installed version, at the same versions.
#[arg(long, value_name = "VERSION")]
reinstall_packages_from: Option<String>,
},
/// Switch this shell to an installed version.
///
/// With no version, uses what the project asks for.
#[command(visible_alias = "u")]
Use {
version: Option<String>,
/// Used by the shell hook: switch only if the project asks for a different version.
#[arg(long, hide = true, conflicts_with = "version")]
auto: bool,
},
/// Set the version that new shells start with.
#[command(visible_alias = "d")]
Default { version: String },
/// List installed versions.
#[command(visible_alias = "l", alias = "list")]
Ls,
/// List versions available to install.
#[command(name = "ls-remote", visible_alias = "lr", alias = "list-remote")]
LsRemote {
/// Only show versions matching this (`22`, `>=20`, `lts/iron`).
filter: Option<String>,
/// Only show LTS releases.
#[arg(long)]
lts: bool,
},
/// Remove an installed version.
#[command(visible_alias = "rm", alias = "un")]
Uninstall { version: String },
/// Show the version active in this shell.
#[command(visible_alias = "c")]
Current,
/// Show the path of the `node` executable for a version.
#[command(visible_alias = "w")]
Which { version: Option<String> },
/// Run a command with a specific version: `sne exec 20 -- npm test`.
#[command(visible_alias = "x")]
Exec {
version: String,
#[arg(last = true, required = true)]
command: Vec<String>,
},
/// Name a version (`sne alias work 20`), or list aliases when given no arguments.
Alias {
name: Option<String>,
#[arg(requires = "name")]
version: Option<String>,
},
/// Remove an alias.
Unalias { name: String },
/// Print the setup code for your shell.
///
/// Add one line to your shell profile:
///
/// bash/zsh: eval "$(sne env --use-on-cd)"
/// fish: sne env --use-on-cd | source
/// PowerShell: sne env --use-on-cd | Out-String | Invoke-Expression
/// cmd: FOR /f "tokens=*" %i IN ('sne env --shell cmd') DO CALL %i
#[command(verbatim_doc_comment)]
Env {
/// Shell to generate code for. Detected when omitted.
#[arg(long, value_enum)]
shell: Option<shell::Shell>,
/// Switch versions automatically when you `cd` into a project.
#[arg(long)]
use_on_cd: bool,
},
/// Print a shell completion script.
Completions { shell: clap_complete::Shell },
/// Check that sne is set up correctly in this shell.
#[command(visible_alias = "dr")]
Doctor,
/// Update sne itself to the latest version.
#[command(visible_alias = "up")]
Update,
/// Commands about sne itself: `sne self check` looks for a new version.
#[command(name = "self")]
Own {
#[command(subcommand)]
action: OwnAction,
},
/// Desktop notifications about new sne versions: `sne notify on`, `sne notify off`.
///
/// When on, sne checks every 4 hours in the background. With no argument, shows the setting.
Notify {
#[arg(value_enum)]
state: Option<Toggle>,
},
}
#[derive(Subcommand)]
enum OwnAction {
/// Ask GitHub whether a newer version of sne exists.
Check {
/// Also show a desktop notification, once per new version.
#[arg(long)]
notify: bool,
},
/// Update sne itself to the latest version (same as `sne update`).
Update,
}
#[derive(Clone, Copy, clap::ValueEnum)]
enum Toggle {
On,
Off,
}
fn run() -> Result<i32> {
match Cli::parse().command {
Cmd::Install {
versions,
reinstall_packages_from,
} => install::run(versions, reinstall_packages_from)?,
Cmd::Use { auto: true, .. } => switch::use_auto()?,
Cmd::Use { version, .. } => switch::use_version(version)?,
Cmd::Default { version } => switch::set_default(&version)?,
Cmd::Ls => list::installed()?,
Cmd::LsRemote { filter, lts } => list::remote(filter, lts)?,
Cmd::Uninstall { version } => switch::uninstall(&version)?,
Cmd::Current => switch::current()?,
Cmd::Which { version } => switch::which(version)?,
Cmd::Exec { version, command } => return switch::exec(&version, &command),
Cmd::Alias { name, version } => switch::alias(name, version)?,
Cmd::Unalias { name } => switch::unalias(&name)?,
Cmd::Env { shell, use_on_cd } => env::run(shell, use_on_cd)?,
Cmd::Completions { shell } => {
clap_complete::generate(shell, &mut Cli::command(), "sne", &mut std::io::stdout());
}
Cmd::Doctor => env::doctor()?,
Cmd::Update
| Cmd::Own {
action: OwnAction::Update,
} => update::update()?,
Cmd::Own {
action: OwnAction::Check { notify },
} => update::check(notify)?,
Cmd::Notify {
state: Some(Toggle::On),
} => update::notify_on()?,
Cmd::Notify {
state: Some(Toggle::Off),
} => update::notify_off()?,
Cmd::Notify { state: None } => update::notify_status(),
}
Ok(0)
}
fn main() {
match run() {
Ok(code) => std::process::exit(code),
Err(err) => {
eprintln!("sne: {err:#}");
std::process::exit(1);
}
}
}