Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(cli): cleanup info command #7204

Merged
merged 12 commits into from
Oct 17, 2023
48 changes: 13 additions & 35 deletions tooling/cli/src/info/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT

use super::{SectionItem, Status};
use super::SectionItem;
use crate::helpers::framework;
use std::{fs::read_to_string, path::PathBuf};

Expand All @@ -14,15 +14,11 @@ pub fn items(app_dir: Option<&PathBuf>, tauri_dir: Option<PathBuf>) -> Vec<Secti
let config = config_guard.as_ref().unwrap();

let bundle_or_build = if config.tauri.bundle.active {
"bundle".to_string()
"bundle"
} else {
"build".to_string()
"build"
};
items.push(SectionItem::new(
move || Some((format!("build-type: {bundle_or_build}"), Status::Neutral)),
|| None,
false,
));
items.push(SectionItem::new().description(format!("build-type: {bundle_or_build}")));

let csp = config
.tauri
Expand All @@ -31,42 +27,24 @@ pub fn items(app_dir: Option<&PathBuf>, tauri_dir: Option<PathBuf>) -> Vec<Secti
.clone()
.map(|c| c.to_string())
.unwrap_or_else(|| "unset".to_string());
items.push(SectionItem::new(
move || Some((format!("CSP: {csp}"), Status::Neutral)),
|| None,
false,
));
items.push(SectionItem::new().description(format!("CSP: {csp}")));

let dist_dir = config.build.dist_dir.to_string();
items.push(SectionItem::new(
move || Some((format!("distDir: {dist_dir}"), Status::Neutral)),
|| None,
false,
));
let dist_dir = &config.build.dist_dir;
items.push(SectionItem::new().description(format!("distDir: {dist_dir}")));

let dev_path = config.build.dev_path.to_string();
items.push(SectionItem::new(
move || Some((format!("devPath: {dev_path}"), Status::Neutral)),
|| None,
false,
));
let dev_path = &config.build.dev_path;
items.push(SectionItem::new().description(format!("devPath: {dev_path}")));

if let Some(app_dir) = app_dir {
if let Ok(package_json) = read_to_string(app_dir.join("package.json")) {
let (framework, bundler) = framework::infer_from_package_json(&package_json);

if let Some(framework) = framework {
items.push(SectionItem::new(
move || Some((format!("framework: {framework}"), Status::Neutral)),
|| None,
false,
));
items.push(SectionItem::new().description(format!("framework: {framework}")));
}

if let Some(bundler) = bundler {
items.push(SectionItem::new(
move || Some((format!("bundler: {bundler}"), Status::Neutral)),
|| None,
false,
));
items.push(SectionItem::new().description(format!("bundler: {bundler}")));
}
}
}
Expand Down
174 changes: 51 additions & 123 deletions tooling/cli/src/info/env_nodejs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-License-Identifier: MIT

use super::{cross_command, VersionMetadata};
use super::{SectionItem, Status};
use super::{cross_command, ActionResult, SectionItem, VersionMetadata};
use colored::Colorize;

pub fn items(metadata: &VersionMetadata) -> (Vec<SectionItem>, Option<String>) {
let yarn_version = cross_command("yarn")
fn manager_version(package_manager: &str) -> Option<String> {
cross_command(package_manager)
.arg("-v")
.output()
.map(|o| {
Expand All @@ -19,129 +18,58 @@ pub fn items(metadata: &VersionMetadata) -> (Vec<SectionItem>, Option<String>) {
}
})
.ok()
.unwrap_or_default();
let yarn_version_c = yarn_version.clone();
.unwrap_or_default()
}

pub fn items(metadata: &VersionMetadata) -> (Vec<SectionItem>, Option<String>) {
let yarn = manager_version("yarn");
let node_target_ver = metadata.js_cli.node.replace(">= ", "");

(
vec![
SectionItem::new(
move || {
cross_command("node")
.arg("-v")
.output()
.map(|o| {
if o.status.success() {
let v = String::from_utf8_lossy(o.stdout.as_slice()).to_string();
let v = v
.split('\n')
.next()
.unwrap()
.strip_prefix('v')
.unwrap_or_default()
.trim();
Some((
format!("node: {}{}", v, {
let version = semver::Version::parse(v).unwrap();
let target_version = semver::Version::parse(node_target_ver.as_str()).unwrap();
if version < target_version {
format!(
" ({}, latest: {})",
"outdated".red(),
target_version.to_string().green()
)
} else {
"".into()
}
}),
Status::Neutral,
))
} else {
None
}
})
.ok()
.unwrap_or_default()
},
|| None,
false,
),
SectionItem::new(
|| {
cross_command("pnpm")
.arg("-v")
.output()
.map(|o| {
if o.status.success() {
let v = String::from_utf8_lossy(o.stdout.as_slice()).to_string();
Some((
format!("pnpm: {}", v.split('\n').next().unwrap()),
Status::Neutral,
))
} else {
None
}
})
.ok()
.unwrap_or_default()
},
|| None,
false,
),
SectionItem::new(
|| {
cross_command("bun")
.arg("-v")
.output()
.map(|o| {
if o.status.success() {
let v = String::from_utf8_lossy(o.stdout.as_slice()).to_string();
Some((
format!("bun: {}", v.split('\n').next().unwrap()),
Status::Neutral,
))
} else {
None
}
})
.ok()
.unwrap_or_default()
},
|| None,
false,
),
SectionItem::new(
move || {
yarn_version_c
.as_ref()
.map(|v| (format!("yarn: {v}"), Status::Neutral))
},
|| None,
false,
),
SectionItem::new(
|| {
cross_command("npm")
.arg("-v")
.output()
.map(|o| {
if o.status.success() {
let v = String::from_utf8_lossy(o.stdout.as_slice()).to_string();
Some((
format!("npm: {}", v.split('\n').next().unwrap()),
Status::Neutral,
))
} else {
None
}
})
.ok()
.unwrap_or_default()
},
|| None,
false,
),
SectionItem::new().action(move || {
cross_command("node")
.arg("-v")
.output()
.map(|o| {
if o.status.success() {
let v = String::from_utf8_lossy(o.stdout.as_slice()).to_string();
let v = v
.split('\n')
.next()
.unwrap()
.strip_prefix('v')
.unwrap_or_default()
.trim();
ActionResult::Description(format!("node: {}{}", v, {
let version = semver::Version::parse(v).unwrap();
let target_version = semver::Version::parse(node_target_ver.as_str()).unwrap();
if version < target_version {
format!(
" ({}, latest: {})",
"outdated".red(),
target_version.to_string().green()
)
} else {
"".into()
}
}))
} else {
ActionResult::None
}
})
.ok()
.unwrap_or_default()
}),
SectionItem::new().action(|| {
manager_version("pnpm")
.map(|v| format!("pnpm: {}", v))
.into()
}),
SectionItem::new().description_opt(yarn.as_ref().map(|v| (format!("yarn: {v}")))),
SectionItem::new().action(|| manager_version("npm").map(|v| format!("npm: {}", v)).into()),
SectionItem::new().action(|| manager_version("bun").map(|v| format!("bun: {}", v)).into()),
],
yarn_version,
yarn,
)
}
Loading
Loading