Skip to content

Commit

Permalink
Clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
SteveBeeblebrox committed Jul 5, 2024
1 parent e37800c commit 4a89c61
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 129 deletions.
22 changes: 6 additions & 16 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::path::{Path, PathBuf};
use std::io::{self, Read as _};
use std::path::PathBuf;
use std::process::exit;
use std::panic;
use std::fs;
Expand Down Expand Up @@ -155,9 +155,6 @@ fn main() {

// Read input
let maybe_filename: Option<String> = carg!("INPUT").filter(|v| *v != "-").or_else(|| carg!("name")).map(|v| String::from(v));
let maybe_ext: Option<String> = maybe_filename.as_ref().and_then(|v|
Path::new(v.as_str()).extension().map(|s| String::from(s.to_str().expect("could not get extension from path")))
);

let text = match carg!("INPUT") {
Some("-") | None => {
Expand Down Expand Up @@ -201,19 +198,15 @@ fn main() {
let result = compile(text, &options).unwrap();

// Write output
let maybe_result_ext = maybe_ext.map(|ext| mtsc::util::get_result_ext(ext,&options));

match carg!("output") {
Some("-") | Some("") if cflag!("output") => print!("{}",result),
None | Some("") => {
match maybe_filename {
Some(ref filename) => {
let mut path = PathBuf::from(filename);
if let Some(result_ext) = maybe_result_ext {
path.set_extension(result_ext);
}
mtsc::util::update_path(&mut path,&options);

if maybe_filename.is_some() && is_same_file(path.as_path(),maybe_filename.as_ref().unwrap()).unwrap_or(false) {
if maybe_filename.is_some() && is_same_file(path.as_path(),maybe_filename.as_ref().unwrap()).unwrap_or_default() {
panic!("Output file is the same as the input");
}

Expand All @@ -226,14 +219,11 @@ fn main() {
let mut path = PathBuf::from(value);

if path.is_dir() {
path.push(maybe_filename.as_ref().unwrap_or(&String::from("out")));
}

if let Some(result_ext) = maybe_result_ext {
path.set_extension(result_ext);
path.push(maybe_filename.as_ref().unwrap_or(&String::from("out.js")));
mtsc::util::update_path(&mut path,&options);
}

if maybe_filename.is_some() && is_same_file(path.as_path(),maybe_filename.as_ref().unwrap()).unwrap_or(false) {
if maybe_filename.is_some() && is_same_file(path.as_path(),maybe_filename.as_ref().unwrap()).unwrap_or_default() {
panic!("Output file is the same as the input");
}

Expand Down
128 changes: 15 additions & 113 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use crate::Options;
#[cfg(all(feature = "compile", feature = "transpile"))]
pub use crate::TSMode;


pub fn all_ext_options() -> Options {
Options {
#[cfg(all(feature = "transpile", feature = "compile"))]
Expand All @@ -27,86 +26,6 @@ pub fn all_ext_options() -> Options {
}
}

/*
O U | R
-------
p p | p
p t | t
p c | t
t p | t
t t | t
t c | t
c p | c
c t | c
c c | c
*/
#[deprecated(note="use `update_options` instead")]
pub fn update_options_by_ext<'a, 'b>(ext: String, options: &'a mut Options, update_options: &'b Options) -> &'a mut Options {
let ext = ext.as_str();
match ext {
#[cfg(feature = "html")]
"html" => {
options.html |= update_options.html;
cfg_if! {
if #[cfg(all(feature = "compile", feature = "transpile"))] {
if update_options.ts > options.ts {
options.ts = TSMode::Transpile;
}
} else if #[cfg(feature = "transpile")] {
options.transpile |= update_options.transpile;
}
}
}

#[cfg(feature = "transpile")]
"jsx" => {
options.use_jsx |= update_options.use_jsx;
cfg_if! {
if #[cfg(all(feature = "compile", feature = "transpile"))] {
if update_options.ts > options.ts {
options.ts = TSMode::Transpile;
}
} else if #[cfg(feature = "transpile")] {
options.transpile |= update_options.transpile;
}
}
}

#[cfg(feature = "common")]
"ts" | "mts" | "tsx" => {
cfg_if! {
if #[cfg(all(feature = "compile", feature = "transpile"))] {
if update_options.ts > options.ts {
options.ts = update_options.ts;
}
} else if #[cfg(feature = "compile")] {
options.compile |= update_options.compile;
} else if #[cfg(feature = "transpile")] {
options.transpile |= update_options.transpile;
}
}

match ext {
"mts" => options.module |= update_options.module,

#[cfg(any(feature = "transpile", feature = "compile"))]
"tsx" => options.use_jsx |= update_options.use_jsx,

_ => {}
}
},

#[cfg(feature = "common")]
"mjs" => options.module |= update_options.module,

_ => {}
}

return options;
}

macro_rules! optional {
(#[cfg($meta:meta)] $expression:expr) => {
{
Expand All @@ -118,35 +37,6 @@ macro_rules! optional {
}
}

#[deprecated(note = "use `update_path` instead")]
pub fn get_result_ext(ext: String, options: &Options) -> String {
#[cfg(feature = "html")]
if options.html {
return String::from("html");
}

let result_ext = if
optional!(#[cfg(feature = "compile")] options.compile).unwrap_or_default()
|| optional!(#[cfg(feature = "transpile")] options.transpile).unwrap_or_default()
|| optional!(#[cfg(all(feature = "compile", feature = "transpile"))] options.ts != TSMode::Preserve).unwrap_or_default()
{
match ext.as_str() {
"ts" => "js",
"mts" => "mjs",

#[cfg(any(feature = "compile", feature = "transpile"))]
"tsx" if options.jsx_factory == None => "jsx",

_ => "js",
}
} else {
ext.as_str()
}
;

return String::from(if optional!(#[cfg(feature = "minify")] options.minify).unwrap_or_default() {"min."} else {""}) + result_ext;
}

use std::path::{Path,PathBuf};
pub enum OptionSource {
Mime(String),
Expand All @@ -155,6 +45,21 @@ pub enum OptionSource {
None
}

/*
O M | R
-------
p p | p
p t | t
p c | c
t p | t
t t | t
t c | c
c p | c
c t | c
c c | c
*/
pub fn update_options<'a>(source: OptionSource, options: &'a mut Options, mask: &'a Options) -> &'a mut Options {
enum InternalOptionSource<'a> {
Mime(&'a str),
Expand Down Expand Up @@ -220,8 +125,6 @@ pub fn update_options<'a>(source: OptionSource, options: &'a mut Options, mask:
return options;
}



pub fn update_path<'a>(path: &'a mut PathBuf, options: &'a Options) -> &'a PathBuf {
let initial_path = path.clone();

Expand Down Expand Up @@ -255,7 +158,6 @@ pub fn update_path<'a>(path: &'a mut PathBuf, options: &'a Options) -> &'a PathB
});
}


// For subextensions that should be discarded like the 'p' in '*.p.ts',
// removing the main extension will cause the subextension to be replaced later
// (If a path has a subextension, it will have a normal extension as well and the next block will be executed)
Expand Down

0 comments on commit 4a89c61

Please sign in to comment.