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): move commands to seperate files #268

Merged
merged 3 commits into from
Jun 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 0 additions & 6 deletions rustfmt.toml

This file was deleted.

1 change: 1 addition & 0 deletions src/commands/check.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

10 changes: 10 additions & 0 deletions src/commands/completions.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
use clap::CommandFactory;
use mdsf::cli::{Cli, CompletionsCommandArguments};

pub fn run(args: &CompletionsCommandArguments) {
let mut cmd = Cli::command();

let cmd_name = cmd.get_name().to_string();

clap_complete::generate(args.shell, &mut cmd, cmd_name, &mut std::io::stdout());
}
44 changes: 44 additions & 0 deletions src/commands/format.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
use clap::builder::OsStr;
use mdsf::{cli::FormatCommandArguments, config::MdsfConfig, error::MdsfError, handle_file};

const MDSF_IGNORE_FILE_NAME: &str = ".mdsfignore";

pub fn run(args: FormatCommandArguments, dry_run: bool) -> Result<(), MdsfError> {
mdsf::DEBUG.swap(args.debug, core::sync::atomic::Ordering::Relaxed);

let conf = MdsfConfig::load()?;

mdsf::runners::set_javascript_runtime(conf.javascript_runtime);

let mut changed_file_count = 0;

if args.path.is_file() {
changed_file_count += u32::from(handle_file(&conf, &args.path, dry_run)?);
} else if args.path.is_dir() {
let mut walk_builder = ignore::WalkBuilder::new(args.path);

walk_builder
.standard_filters(true)
.parents(true)
.hidden(true)
.add_custom_ignore_filename(MDSF_IGNORE_FILE_NAME);

let md_ext = OsStr::from("md");

for entry in walk_builder.build().flatten() {
let file_path = entry.path();

if file_path.extension() == Some(&md_ext) {
changed_file_count += u32::from(handle_file(&conf, file_path, dry_run)?);
}
}
} else {
return Err(MdsfError::FileNotFound(args.path));
}

if dry_run && changed_file_count > 0 {
Err(MdsfError::CheckModeChanges(changed_file_count))
} else {
Ok(())
}
}
13 changes: 13 additions & 0 deletions src/commands/init.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
use mdsf::config::MdsfConfig;

pub fn run() -> std::io::Result<()> {
let current_dir = std::env::current_dir()?;

let conf = MdsfConfig::default();

let config = serde_json::to_string_pretty(&conf)?;

std::fs::write(current_dir.join("mdsf.json"), config)?;

Ok(())
}
36 changes: 36 additions & 0 deletions src/commands/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
use clap::Parser;
use mdsf::{
cli::{Cli, Commands, LogLevel},
error::MdsfError,
terminal::logging::setup_logger,
};

mod check;
mod completions;
mod format;
mod init;
mod schema;

pub fn execute_command() -> Result<(), MdsfError> {
match Cli::parse().command {
Commands::Format(args) => {
setup_logger(args.log_level.unwrap_or(LogLevel::Debug));

format::run(args, false)
}

Commands::Verify(args) => {
setup_logger(args.log_level.unwrap_or(LogLevel::Error));

format::run(args, true)
}

Commands::Init => init::run().map_err(MdsfError::from),
Commands::Schema => schema::run().map_err(MdsfError::from),
Commands::Completions(args) => {
completions::run(&args);

Ok(())
}
}
}
17 changes: 17 additions & 0 deletions src/commands/schema.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use mdsf::config::MdsfConfig;

pub fn run() -> std::io::Result<()> {
let mut p = std::env::current_dir()?;

let package_version = env!("CARGO_PKG_VERSION");

p.push(format!("schemas/v{package_version}"));

std::fs::create_dir_all(&p)?;

let schema = serde_json::to_string_pretty(&schemars::schema_for!(MdsfConfig))?;

std::fs::write(p.join("mdsf.schema.json"), schema)?;

Ok(())
}
112 changes: 3 additions & 109 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,115 +1,9 @@
use clap::{builder::OsStr, CommandFactory, Parser};
use mdsf::{
cli::{Cli, Commands, FormatCommandArguments, LogLevel},
config::MdsfConfig,
error::MdsfError,
handle_file,
terminal::{logging::setup_logger, print_error},
};
use mdsf::terminal::print_error;

const MDSF_IGNORE_FILE_NAME: &str = ".mdsfignore";

fn format_command(args: FormatCommandArguments, dry_run: bool) -> Result<(), MdsfError> {
mdsf::DEBUG.swap(args.debug, core::sync::atomic::Ordering::Relaxed);

let conf = MdsfConfig::load()?;

mdsf::runners::set_javascript_runtime(conf.javascript_runtime);

let mut changed_file_count = 0;

if args.path.is_file() {
changed_file_count += u32::from(handle_file(&conf, &args.path, dry_run)?);
} else if args.path.is_dir() {
let mut walk_builder = ignore::WalkBuilder::new(args.path);

walk_builder
.standard_filters(true)
.parents(true)
.hidden(true)
.add_custom_ignore_filename(MDSF_IGNORE_FILE_NAME);

let ignore_path = std::env::current_dir()?.join(MDSF_IGNORE_FILE_NAME);

if ignore_path.is_file() {
walk_builder.add_ignore(ignore_path);
}

let md_ext = OsStr::from("md");

for entry in walk_builder.build().flatten() {
let file_path = entry.path();

if file_path.extension() == Some(&md_ext) {
changed_file_count += u32::from(handle_file(&conf, file_path, dry_run)?);
}
}
} else {
return Err(MdsfError::FileNotFound(args.path));
}

if dry_run && changed_file_count > 0 {
Err(MdsfError::CheckModeChanges(changed_file_count))
} else {
Ok(())
}
}

fn init_config_command() -> std::io::Result<()> {
let current_dir = std::env::current_dir()?;

let conf = MdsfConfig::default();

let config = serde_json::to_string_pretty(&conf)?;

std::fs::write(current_dir.join("mdsf.json"), config)?;

Ok(())
}

fn generate_schema_command() -> std::io::Result<()> {
let mut p = std::env::current_dir()?;

let package_version = env!("CARGO_PKG_VERSION");

p.push(format!("schemas/v{package_version}"));

std::fs::create_dir_all(&p)?;

let schema = serde_json::to_string_pretty(&schemars::schema_for!(MdsfConfig))?;

std::fs::write(p.join("mdsf.schema.json"), schema)?;

Ok(())
}
mod commands;

fn main() {
let command_result = match Cli::parse().command {
Commands::Format(args) => {
setup_logger(args.log_level.unwrap_or(LogLevel::Debug));

format_command(args, false)
}

Commands::Verify(args) => {
setup_logger(args.log_level.unwrap_or(LogLevel::Error));

format_command(args, true)
}

Commands::Init => init_config_command().map_err(MdsfError::from),
Commands::Schema => generate_schema_command().map_err(MdsfError::from),
Commands::Completions(args) => {
let mut cmd = Cli::command();
let cmd_name = cmd.get_name().to_string();

clap_complete::generate(args.shell, &mut cmd, cmd_name, &mut std::io::stdout());

Ok(())
}
};

if let Err(error) = command_result {
if let Err(error) = commands::execute_command() {
print_error(&error);

std::process::exit(1);
Expand Down
4 changes: 3 additions & 1 deletion src/runners/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use core::sync::atomic::{AtomicU8, Ordering};

use bun::new_bunx_cmd;
use deno::new_deno_cmd;
use node::new_npx_cmd;
use schemars::JsonSchema;

use self::{bun::new_bunx_cmd, deno::new_deno_cmd, node::new_npx_cmd};
use crate::terminal::print_unknown_javascript_runtime;

mod bun;
Expand Down
Loading