-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmain.rs
172 lines (144 loc) · 4.44 KB
/
main.rs
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
#[cfg(windows)]
use std::os::windows;
use std::{
fs,
path::{Path, PathBuf},
};
#[cfg(windows)]
use anyhow::bail;
use anyhow::Result;
use clap::{Parser, ValueHint};
use crate::subcommand::{
install::InstallCommand, is_installed::IsInstalledCommand, list::ListCommand,
parse_version::ParseVersionCommand, switch::SwitchCommand, uninstall::UninstallCommand, Action,
};
mod archives;
mod constants;
mod files;
mod node_version;
mod subcommand;
#[derive(Parser, Clone, Debug)]
enum Subcommands {
List(ListCommand),
IsInstalled(IsInstalledCommand),
Install(InstallCommand),
Uninstall(UninstallCommand),
Use(SwitchCommand),
ParseVersion(ParseVersionCommand),
}
#[derive(Parser, Debug)]
#[command(
name = "nvm(-rust)",
author,
about,
version,
about = "Node Version Manager (but better, and in Rust)"
)]
pub struct Config {
/// Installation directory
#[arg(
id("install-dir"),
global(true),
long,
value_hint(ValueHint::DirPath),
env("NVM_DIR")
)]
dir: Option<PathBuf>,
/// bin directory
#[arg(
global(true),
long,
value_hint(ValueHint::DirPath),
env("NVM_SHIMS_DIR")
)]
shims_dir: Option<PathBuf>,
/// Accept any prompts needed for the command to complete
#[arg(global(true), short, long)]
force: bool,
#[command(subcommand)]
command: Subcommands,
}
impl Config {
pub fn get_dir(&self) -> PathBuf {
self.dir
.as_ref()
.map_or_else(Config::default_dir, |r| r.clone())
}
pub fn get_shims_dir(&self) -> PathBuf {
self.shims_dir
.as_ref()
.map_or_else(|| self.get_dir().join("shims"), |r| r.clone())
}
/// Path to directory containing node versions
fn get_versions_dir(&self) -> PathBuf {
self.get_dir().join("versions")
}
fn with_force(&self) -> Self {
Self {
force: true,
dir: Some(self.get_dir()),
shims_dir: Some(self.get_shims_dir()),
command: self.command.clone(),
}
}
#[cfg(windows)]
fn default_dir() -> PathBuf {
dirs::data_local_dir().unwrap().join("nvm-rust")
}
#[cfg(unix)]
fn default_dir() -> PathBuf {
dirs::home_dir().unwrap().join("nvm-rust")
}
}
fn ensure_dir_exists(path: &Path) {
if !path.exists() {
fs::create_dir_all(path).unwrap_or_else(|err| panic!("Could not create {path:?} - {err}"));
println!("Created nvm dir at {path:?}");
}
if !path.is_dir() {
panic!("{path:?} is not a directory! Please rename it.")
}
}
#[cfg(windows)]
const SYMLINK_ERROR: &str = "You do not seem to have permissions to create symlinks.
This is most likely due to Windows requiring Admin access for it unless you enable Developer Mode.
Either run the program as Administrator or enable Developer Mode:
https://docs.microsoft.com/en-us/windows/apps/get-started/enable-your-device-for-development#active-developer-mode
Read more:
https://blogs.windows.com/windowsdeveloper/2016/12/02/symlinks-windows-10";
#[cfg(windows)]
fn ensure_symlinks_work(config: &Config) -> Result<()> {
let target_path = &config.get_dir().join("test");
if windows::fs::symlink_dir(config.get_shims_dir(), target_path).is_err() {
bail!("{SYMLINK_ERROR}");
}
fs::remove_dir(target_path).expect("Could not remove test symlink...");
Ok(())
}
fn main() -> Result<()> {
let config: Config = Config::parse();
#[cfg(windows)]
let is_initial_run = !config.get_dir().exists();
ensure_dir_exists(&config.get_dir());
ensure_dir_exists(&config.get_versions_dir());
#[cfg(windows)]
if is_initial_run {
let result = ensure_symlinks_work(&config);
result?;
}
match config.command {
Subcommands::List(ref options) => ListCommand::run(&config, options),
Subcommands::IsInstalled(ref options) => IsInstalledCommand::run(&config, options),
Subcommands::Install(ref options) => InstallCommand::run(&config, options),
Subcommands::Uninstall(ref options) => UninstallCommand::run(&config, options),
Subcommands::Use(ref options) => SwitchCommand::run(&config, options),
Subcommands::ParseVersion(ref options) => ParseVersionCommand::run(&config, options),
#[allow(unreachable_patterns)]
_ => Ok(()),
}
}
#[test]
fn verify_cli() {
use clap::CommandFactory;
Config::command().debug_assert()
}