Skip to main content

Rest/Struct/Binary/
Command.rs

1/// Represents the structure for binary command execution.
2///
3/// This struct holds various fields related to the command execution, including
4/// the separator for file paths and a function to execute the command
5/// asynchronously.
6pub struct Struct {
7	/// The separator used for file paths.
8	pub Separator:Option::Separator,
9
10	/// A boxed asynchronous function that returns a pinned future.
11	pub Fn:Box<dyn Fn() -> Pin<Box<dyn Future<Output = ()> + Send + 'static>> + Send + 'static>,
12}
13
14impl Struct {
15	/// Creates a new instance of the Struct.
16	///
17	/// This function initializes the Struct with the default file path
18	/// separator and an asynchronous function that executes the command based
19	/// on the provided options. The function determines whether to execute the
20	/// command in parallel or sequentially based on the `Parallel` flag in the
21	/// options.
22	///
23	/// # Returns
24	///
25	/// Returns a new instance of Struct.
26	pub fn Fn() -> Self {
27		Self {
28			Separator:std::path::MAIN_SEPARATOR,
29
30			Fn:Box::new(|| {
31				Box::pin(async move {
32					let Command = crate::Fn::Binary::Command::Fn();
33
34					// Check if compile subcommand was invoked
35					if let Some(compile_matches) = Command.subcommand_matches("compile") {
36						let Input = compile_matches
37							.get_one::<String>("Input")
38							.expect("Cannot get Input.")
39							.to_owned();
40						let Output = compile_matches
41							.get_one::<String>("Output")
42							.expect("Cannot get Output.")
43							.to_owned();
44						let Target = compile_matches
45							.get_one::<String>("Target")
46							.cloned()
47							.unwrap_or_else(|| "es2024".to_string());
48						let Module = compile_matches
49							.get_one::<String>("Module")
50							.cloned()
51							.unwrap_or_else(|| "esmodule".to_string());
52						let _SourceMaps = compile_matches.get_flag("SourceMaps");
53						let UseDefineForClassFields = compile_matches.get_flag("UseDefineForClassFields");
54						let Parallel = compile_matches.get_flag("Parallel");
55
56						// Create VSCode-compatible config
57						let Config = crate::Struct::SWC::CompilerConfig {
58							Target,
59							Module:Module.clone(),
60							Strict:true,
61							EmitDecoratorsMetadata:true,
62							TreeShaking:true,
63							Minify:false,
64							ModuleFormat:crate::Struct::SWC::ModuleFormat::from_str(&Module),
65						};
66
67						// Call the compile function with output directory
68						let _ = crate::Fn::SWC::Compile::Fn(
69							crate::Struct::SWC::Option {
70								entry:vec![vec![Input.clone()]],
71								separator:std::path::MAIN_SEPARATOR,
72								pattern:".ts".to_string(),
73								config:Config,
74								output:Output,
75								use_define_for_class_fields:UseDefineForClassFields,
76							},
77							Parallel,
78						)
79						.await;
80					} else {
81						// Default behavior - run Entry/Sequential or Parallel
82						let Option = Entry::Struct::Fn(&Option::Struct::Fn(Struct::Fn()));
83
84						match Option.Parallel {
85							true => {
86								Parallel::Fn(Option).await;
87							},
88
89							false => {
90								Sequential::Fn(Option).await;
91							},
92						};
93					}
94				})
95			}),
96		}
97	}
98}
99
100use std::pin::Pin;
101
102use futures::Future;
103
104pub mod Entry;
105
106pub mod Option;
107
108use crate::Fn::Binary::Command::{Parallel, Sequential};