Skip to main content

Library/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
41						let Output = compile_matches
42							.get_one::<String>("Output")
43							.expect("Cannot get Output.")
44							.to_owned();
45
46						let Target = compile_matches
47							.get_one::<String>("Target")
48							.cloned()
49							.unwrap_or_else(|| "es2024".to_string());
50
51						let Module = compile_matches
52							.get_one::<String>("Module")
53							.cloned()
54							.unwrap_or_else(|| "esmodule".to_string());
55
56						let _SourceMaps = compile_matches.get_flag("SourceMaps");
57
58						let UseDefineForClassFields = compile_matches.get_flag("UseDefineForClassFields");
59
60						let Parallel = compile_matches.get_flag("Parallel");
61
62						// Create VSCode-compatible config
63						let Config = crate::Struct::SWC::CompilerConfig {
64							Target,
65							Module:Module.clone(),
66							Strict:true,
67							EmitDecoratorsMetadata:true,
68							TreeShaking:true,
69							Minify:false,
70							ModuleFormat:crate::Struct::SWC::ModuleFormat::from_str(&Module),
71						};
72
73						// Call the compile function with output directory
74						let _ = crate::Fn::SWC::Compile::Fn(
75							crate::Struct::SWC::Option {
76								entry:vec![vec![Input.clone()]],
77								separator:std::path::MAIN_SEPARATOR,
78								pattern:".ts".to_string(),
79								config:Config,
80								output:Output,
81								use_define_for_class_fields:UseDefineForClassFields,
82							},
83							Parallel,
84						)
85						.await;
86					} else {
87						// Default behavior - run Entry/Sequential or Parallel
88						let Option = Entry::Struct::Fn(&Option::Struct::Fn(Struct::Fn()));
89
90						match Option.Parallel {
91							true => {
92								Parallel::Fn(Option).await;
93							},
94
95							false => {
96								Sequential::Fn(Option).await;
97							},
98						};
99					}
100				})
101			}),
102		}
103	}
104}
105
106use std::pin::Pin;
107
108use futures::Future;
109
110pub mod Entry;
111
112pub mod Option;
113
114use crate::Fn::Binary::Command::{Parallel, Sequential};