use anyhow::Result;
use log::info;
use serde::Deserialize;
use std::collections::HashMap;
use std::fs::File;
use std::io::BufReader;
use std::path::PathBuf;
use crate::utils;
#[derive(Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct WorkspaceConfig {
#[serde(rename = "$schema")]
pub schema: Option<String>,
pub version: u32,
pub new_project_root: Option<String>,
pub default_project: Option<String>,
pub projects: Option<HashMap<String, Project>>,
pub cli: Option<CliOptions>,
}
#[derive(Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct Project {
pub project_type: ProjectType,
pub cli: Option<CliOptions>,
pub prefix: Option<String>,
pub root: Option<String>,
pub source_root: Option<String>,
}
#[derive(PartialEq, Deserialize, Debug)]
#[serde(rename_all = "lowercase")]
pub enum ProjectType {
Application,
Library,
}
#[derive(Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct CliOptions {
pub default_collection: Option<String>,
pub package_manager: Option<PackageManager>,
pub warnings: Option<CliWarnings>,
}
#[derive(Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct CliWarnings {
pub version_mismatch: bool,
}
#[derive(Deserialize, Debug)]
#[serde(rename_all = "lowercase")]
pub enum PackageManager {
Npm,
Cnpm,
Yarn,
Pnpm,
}
pub fn read_config(path: PathBuf) -> Result<WorkspaceConfig> {
let file = File::open(path)?;
let reader = BufReader::new(file);
let config: WorkspaceConfig = serde_json::from_reader(reader)?;
Ok(config)
}
pub fn list_projects(config: &WorkspaceConfig) -> Result<()> {
let projects = &config.projects.as_ref().unwrap();
for (key, value) in projects.iter() {
println!("{} ({:?})", key, value.project_type);
}
Ok(())
}
pub fn list_projects_by_type(config: &WorkspaceConfig, project_type: ProjectType) -> Result<()> {
let projects = &config.projects.as_ref().unwrap();
for (key, value) in projects.iter() {
if value.project_type == project_type {
println!("{}", key);
}
}
Ok(())
}
pub fn new_application(name: &str, dir: &PathBuf) -> Result<bool> {
info!("Creating new workspace: {}", name);
std::fs::create_dir_all(dir)?;
let args = &["new", name, "--skip-install", "--skip-git"];
let result = utils::exec_command(dir, "ng", args);
Ok(result)
}