add cli option to read Sass from stdin

This commit is contained in:
Connor Skees 2020-07-16 00:20:51 -04:00
parent fb724b8bee
commit 179b368ef8

View File

@ -1,13 +1,13 @@
use std::{ use std::{
fs::File, fs::File,
io::{stdout, BufWriter, Write}, io::{stdin, stdout, BufWriter, Read, Write},
path::Path, path::Path,
}; };
use clap::{arg_enum, App, AppSettings, Arg}; use clap::{arg_enum, App, AppSettings, Arg};
#[cfg(not(feature = "wasm"))] #[cfg(not(feature = "wasm"))]
use grass::{from_path, Options}; use grass::{from_path, from_string, Options};
arg_enum! { arg_enum! {
#[derive(PartialEq, Debug)] #[derive(PartialEq, Debug)]
@ -34,12 +34,11 @@ fn main() -> std::io::Result<()> {
let matches = App::new("grass") let matches = App::new("grass")
.setting(AppSettings::ColoredHelp) .setting(AppSettings::ColoredHelp)
.version(env!("CARGO_PKG_VERSION")) .version(env!("CARGO_PKG_VERSION"))
.about("SCSS Compiler in rust") .about("A near-feature-complete Sass compiler written purely in Rust")
.version_short("v") .version_short("v")
.arg( .arg(
Arg::with_name("STDIN") Arg::with_name("STDIN")
.long("stdin") .long("stdin")
.hidden(true)
.help("Read the stylesheet from stdin"), .help("Read the stylesheet from stdin"),
) )
.arg( .arg(
@ -162,7 +161,7 @@ fn main() -> std::io::Result<()> {
) )
.arg( .arg(
Arg::with_name("INPUT") Arg::with_name("INPUT")
.required(true) .required_unless("STDIN")
.help("SCSS files"), .help("SCSS files"),
) )
.arg( .arg(
@ -201,15 +200,26 @@ fn main() -> std::io::Result<()> {
&mut stdout_write &mut stdout_write
}; };
if let Some(name) = matches.value_of("INPUT") { buf_out.write_all(
buf_out.write_all( if let Some(name) = matches.value_of("INPUT") {
from_path(name, &options) from_path(name, &options)
.unwrap_or_else(|e| { } else if matches.is_present("STDIN") {
eprintln!("{}", e); from_string(
std::process::exit(1) {
}) let mut buffer = String::new();
.as_bytes(), stdin().read_to_string(&mut buffer)?;
)?; buffer
} },
options,
)
} else {
unreachable!()
}
.unwrap_or_else(|e| {
eprintln!("{}", e);
std::process::exit(1)
})
.as_bytes(),
)?;
Ok(()) Ok(())
} }