Skip to content

Commit

Permalink
Add Logger module for logging functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
arjunkomath committed Apr 27, 2024
1 parent 4e2f24a commit 66b7b14
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 16 deletions.
26 changes: 10 additions & 16 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
use std::collections::HashMap;
use std::env;
use std::path::PathBuf;
use std::sync::Arc;
use std::{env, println};

use anyhow::{Context, Result};
use builder::{cache, Worker};
use clap::{Parser, Subcommand};
use dev::server::Clients;
use directories::ProjectDirs;
use owo_colors::OwoColorize;
use shared::logger::Logger;
use shared::utils;
use tokio::net::TcpListener;
use tokio::sync::Mutex;
Expand Down Expand Up @@ -73,33 +73,27 @@ async fn main() -> Result<()> {
ProjectDirs::from("rs", "cli", "RustyInk").context("Failed to get project directories")?;
let cache_dir = project_dirs.cache_dir().to_string_lossy().to_string();
let cache = cache::Cache::new(cache_dir)?;
let log = Logger::new();

match args.command {
Commands::New { project_dir, theme } => {
println!("{}...", "\n- Creating new project".bold());
log.activity("Creating new project");

match create::project(&project_dir, &theme).await {
Err(e) => {
println!("- {}", e.to_string().red().bold());
log.error(&e.to_string());
}
_ => {
// Create settings file
create::settings_file(&project_dir)?;

println!(
"- Project created in {}",
project_dir.display().blue().bold()
);
log.info(&format!("Project created in {}", project_dir.display()));
}
}
}
Commands::Dev { input_dir, watch } => {
if utils::path_to_string(&input_dir)? == utils::path_to_string(&env::current_dir()?)? {
println!(
"{}",
"\nSorry, you cannot use current directory as input directory as output is written to it!"
.red()
.bold()
log.error(
"Sorry, you cannot use current directory as input directory as output is written to it!"
);

return Ok(());
Expand All @@ -112,7 +106,7 @@ async fn main() -> Result<()> {

// Trigger a build
if let Err(e) = worker.build() {
println!("- Build failed -> {}", e.to_string().red().bold());
log.error(&format!("Build failed -> {}", e));
}

// Start dev server
Expand All @@ -139,7 +133,7 @@ async fn main() -> Result<()> {
let worker = Worker::prod(&input_dir)?;

if let Err(e) = worker.build() {
println!("- Build failed -> {}", e.to_string().red().bold());
log.error(&format!("Build failed -> {}", e));
}
}
Commands::Clean {} => {
Expand Down
21 changes: 21 additions & 0 deletions src/shared/logger.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
use owo_colors::OwoColorize;

pub struct Logger {}

impl Logger {
pub fn new() -> Self {
Logger {}
}

pub fn info(&self, message: &str) {
println!("- {}", message.blue().bold());
}

pub fn activity(&self, message: &str) {
println!("\n- {}...", message.bold());
}

pub fn error(&self, message: &str) {
println!("- Error: {}", message.to_string().red().bold());
}
}
1 change: 1 addition & 0 deletions src/shared/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pub mod logger;
pub mod settings;
pub mod utils;

0 comments on commit 66b7b14

Please sign in to comment.