#[allow(unused)]
use std::{
collections::HashSet,
env, fs,
path::{Path, PathBuf},
process::Command,
time::Instant,
};
use crate::{
commands::{BuildOutput, FailedBuildOutput},
error_handling::emit_ezno_diagnostic,
utilities::print_to_cli,
};
use argh::FromArgs;
#[derive(FromArgs, Debug)]
struct TopLevel {
#[argh(subcommand)]
nested: CompilerSubCommand,
}
#[derive(FromArgs, Debug)]
#[argh(subcommand)]
enum CompilerSubCommand {
Info(Info),
Build(BuildArguments),
ASTExplorer(crate::ast_explorer::ExplorerArguments),
Check(CheckArguments),
#[cfg(not(target_family = "wasm"))]
Repl(crate::repl::ReplArguments),
}
#[derive(FromArgs, Debug)]
#[argh(subcommand, name = "info")]
struct Info {}
#[derive(FromArgs, PartialEq, Debug)]
#[argh(subcommand, name = "build")]
pub(crate) struct BuildArguments {
#[argh(positional)]
pub input: PathBuf,
#[argh(positional)]
pub output: Option<PathBuf>,
#[argh(option, short = 'd')]
pub definition_file: Option<PathBuf>,
#[argh(switch, short = 'm')]
pub minify: bool,
#[argh(switch)]
pub no_comments: bool,
#[argh(switch)]
pub source_maps: bool,
#[argh(switch)]
pub non_standard_syntax: bool,
#[argh(switch)]
pub non_standard_library: bool,
#[argh(switch)]
pub optimise: bool,
#[cfg(not(target_family = "wasm"))]
#[argh(switch)]
pub timings: bool,
}
#[derive(FromArgs, PartialEq, Debug)]
#[argh(subcommand, name = "check")]
pub(crate) struct CheckArguments {
#[argh(positional)]
pub input: PathBuf,
#[argh(option, short = 'd')]
pub definition_file: Option<PathBuf>,
#[argh(switch)]
pub watch: bool,
}
#[allow(unused)]
fn file_system_resolver(path: &Path) -> Option<String> {
if path.to_str() == Some("BLANK") {
return Some(String::new());
}
match fs::read_to_string(path) {
Ok(source) => Some(source),
Err(_) => None,
}
}
pub fn run_cli<T: crate::ReadFromFS, U: crate::WriteToFS, V: crate::CLIInputResolver>(
cli_arguments: &[&str],
read_file: T,
write_file: U,
cli_input_resolver: V,
) {
let command = match FromArgs::from_args(&["ezno-cli"], cli_arguments) {
Ok(TopLevel { nested }) => nested,
Err(err) => {
print_to_cli(format_args!("{}", err.output));
return;
}
};
match command {
CompilerSubCommand::Info(_) => {
crate::utilities::print_info();
}
CompilerSubCommand::Build(build_config) => {
let output_path = build_config.output.unwrap_or("ezno_output.js".into());
let output = crate::commands::build(
&read_file,
&build_config.input,
build_config.definition_file.as_deref(),
&output_path,
crate::commands::BuildConfig { strip_whitespace: build_config.minify },
None,
);
match output {
Ok(BuildOutput { diagnostics, fs, outputs }) => {
for output in outputs {
write_file(output.output_path.as_path(), output.content);
}
for diagnostic in diagnostics.into_iter() {
emit_ezno_diagnostic(diagnostic, &fs).unwrap();
}
}
Err(FailedBuildOutput { fs, diagnostics }) => {
for diagnostic in diagnostics.into_iter() {
emit_ezno_diagnostic(diagnostic, &fs).unwrap();
}
}
}
}
CompilerSubCommand::ASTExplorer(mut repl) => repl.run(read_file, cli_input_resolver),
CompilerSubCommand::Check(check_arguments) => {
let CheckArguments { input, watch: _, definition_file } = check_arguments;
let (diagnostics, _others) =
crate::commands::check(&read_file, &input, definition_file.as_deref());
let fs = match _others {
Ok(data) => data.module_contents,
Err(data) => data,
};
for diagnostic in diagnostics.into_iter() {
emit_ezno_diagnostic(diagnostic, &fs).unwrap();
}
}
#[cfg(not(target_family = "wasm"))]
CompilerSubCommand::Repl(argument) => crate::repl::run_deno_repl(cli_input_resolver, argument),
}
}