Skip to content

Commit

Permalink
reformat
Browse files Browse the repository at this point in the history
  • Loading branch information
strawmelonjuice committed Jan 1, 2024
1 parent f45d255 commit 2a329ce
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 40 deletions.
3 changes: 2 additions & 1 deletion src/contentservers/combiner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ pub(crate) fn combine_content(
.to_string();
let pagemetainfojson = serde_json::to_string(&p_met).unwrap();
let currentmode = crate::load_mode(mode_to_load).1;
let stylesheet: String = import_css_minified(format!("./cynthiaFiles/styles/{}",currentmode.stylefile));
let stylesheet: String =
import_css_minified(format!("./cynthiaFiles/styles/{}", currentmode.stylefile));
let handlebarfile = format!(
"./cynthiaFiles/templates/{}.handlebars",
if p_met.kind == "post" {
Expand Down
14 changes: 7 additions & 7 deletions src/files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ pub(crate) fn cacheplacer(fileid: String, contents: String) -> String {
}

pub(crate) fn import_js_minified(scriptfile: String) -> String {
return match cacheretriever(scriptfile.to_string(), 120) {
return match cacheretriever(scriptfile.to_string(), 1200) {
Ok(o) => fs::read_to_string(o).expect("Couldn't find or open a JS file."),
Err(_) => match jsruntime(true) {
BUNJSR => {
Expand All @@ -102,7 +102,7 @@ pub(crate) fn import_js_minified(scriptfile: String) -> String {
if output.status.success() {
let res: String = String::from_utf8_lossy(&output.stdout).parse().unwrap();
cacheplacer(scriptfile, format!(
"\n\r// Minified internally by Cynthia using Terser\n\n{res}\n\n\r// Cached after minifying, so might be ~2 minutes behind.\n\r"
"\n\r// Minified internally by Cynthia using Terser\n\n{res}\n\n\r// Cached after minifying, so might be ~20 minutes behind.\n\r"
))
} else {
logger(
Expand Down Expand Up @@ -141,11 +141,11 @@ pub(crate) fn import_js_minified(scriptfile: String) -> String {
"NPX".purple()
),
);
fs::read_to_string(scriptfile).expect("Couldn't find or open a JS file.")
fs::read_to_string(scriptfile).expect("Couldn't find or open a JS file.")
} else {
let res: String = String::from_utf8_lossy(&output.stdout).parse().unwrap();
cacheplacer(scriptfile, format!(
"\n\r// Minified internally by Cynthia using Terser\n\n{res}\n\n\r// Cached after minifying, so might be ~2 minutes behind.\n\r"
"\n\r// Minified internally by Cynthia using Terser\n\n{res}\n\n\r// Cached after minifying, so might be ~20 minutes behind.\n\r"
))
}
}
Expand All @@ -158,7 +158,7 @@ pub(crate) fn import_js_minified(scriptfile: String) -> String {
}

pub(crate) fn import_css_minified(stylefile: String) -> String {
return match cacheretriever(stylefile.to_string(), 120) {
return match cacheretriever(stylefile.to_string(), 1200) {
Ok(o) => fs::read_to_string(o).expect("Couldn't find or open a JS file."),
Err(_) => match jsruntime(true) {
BUNJSR => {
Expand All @@ -181,7 +181,7 @@ pub(crate) fn import_css_minified(stylefile: String) -> String {
if output.status.success() {
let res: String = String::from_utf8_lossy(&output.stdout).parse().unwrap();
cacheplacer(stylefile, format!(
"\n\r/* Minified internally by Cynthia using clean-css */\n\n{res}\n\n\r/* Cached after minifying, so might be ~2 minutes behind. */\n\r"
"\n\r/* Minified internally by Cynthia using clean-css */\n\n{res}\n\n\r/* Cached after minifying, so might be ~20 minutes behind. */\n\r"
))
} else {
logger(
Expand Down Expand Up @@ -224,7 +224,7 @@ pub(crate) fn import_css_minified(stylefile: String) -> String {
} else {
let res: String = String::from_utf8_lossy(&output.stdout).parse().unwrap();
cacheplacer(stylefile, format!(
"\n\r/* Minified internally by Cynthia using clean-css */\n\n{res}\n\n\r/* Cached after minifying, so might be ~2 minutes behind. */\n\r"
"\n\r/* Minified internally by Cynthia using clean-css */\n\n{res}\n\n\r/* Cached after minifying, so might be ~20 minutes behind. */\n\r"
))
}
}
Expand Down
34 changes: 14 additions & 20 deletions src/jsr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,28 +62,22 @@ pub(crate) fn noderunner(args: Vec<&str>, cwd: std::path::PathBuf) -> String {

pub(crate) fn jsruntime(mayfail: bool) -> &'static str {
return match std::process::Command::new(BUNJSR).arg("-v").output() {
Ok(_t) => {
BUNJSR
}
Err(_err) => {
match std::process::Command::new(NODEJSR).arg("-v").output() {
Ok(_t) => {
NODEJSR
}
Err(_err) => {
if !mayfail {
logger(
5,
String::from(
"No supported (Node.JS or Bun) Javascript runtimes found on path!",
),
);
std::process::exit(1);
}
""
Ok(_t) => BUNJSR,
Err(_err) => match std::process::Command::new(NODEJSR).arg("-v").output() {
Ok(_t) => NODEJSR,
Err(_err) => {
if !mayfail {
logger(
5,
String::from(
"No supported (Node.JS or Bun) Javascript runtimes found on path!",
),
);
std::process::exit(1);
}
""
}
}
},
};
}
pub(crate) fn jspm(mayfail: bool) -> &'static str {
Expand Down
30 changes: 20 additions & 10 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::{fs, path::Path, process, sync::Mutex};
use std::io::ErrorKind;
use std::{fs, path::Path, process, sync::Mutex};

use actix_web::{
get,
Expand All @@ -12,6 +12,7 @@ use dotenv::dotenv;
use jsonc_parser::parse_to_serde_value;
use mime::Mime;

use crate::files::import_js_minified;
use structs::*;

use crate::logger::logger;
Expand Down Expand Up @@ -124,8 +125,11 @@ async fn serves_e(id: web::Path<String>, pluginsmex: Data<Mutex<Vec<PluginMeta>>
let fileb = format!("./plugins/{}/{}/{fid}", plugin.name, s[0]);
let file = Path::new(&fileb);
mime = find_mimetype(&format!("{:?}", file.file_name().unwrap()));
body = fs::read_to_string(file)
.unwrap_or(String::from("Couldn't serve file."));
body = if mime == mime::TEXT_JAVASCRIPT {
import_js_minified(file.to_str().unwrap().to_string())
} else {
fs::read_to_string(file).unwrap_or(String::from("Couldn't serve file."))
};
};
}
}
Expand Down Expand Up @@ -169,13 +173,11 @@ async fn root(pluginsmex: Data<Mutex<Vec<PluginMeta>>>) -> impl Responder {
fn read_published_jsonc() -> Vec<CynthiaContentMetaData> {
let res: Vec<CynthiaContentMetaData> = if Path::new("./cynthiaFiles/published.yaml").exists() {
let file = "./cynthiaFiles/published.yaml".to_owned();
let unparsed_yaml =
fs::read_to_string(file).expect("Couldn't find or load that file.");
let unparsed_yaml = fs::read_to_string(file).expect("Couldn't find or load that file.");
serde_yaml::from_str(&unparsed_yaml).unwrap()
} else {
let file = "./cynthiaFiles/published.jsonc".to_owned();
let unparsed_json =
fs::read_to_string(file).expect("Couldn't find or load that file.");
let unparsed_json = fs::read_to_string(file).expect("Couldn't find or load that file.");
// println!("{}", unparsed_json);
let parsed_json: Option<serde_json::Value> =
parse_to_serde_value(unparsed_json.as_str(), &Default::default())
Expand All @@ -187,19 +189,27 @@ fn read_published_jsonc() -> Vec<CynthiaContentMetaData> {

fn load_mode(mode_name: String) -> CynthiaModeObject {
let file = format!("./cynthiaFiles/modes/{}.jsonc", mode_name).to_owned();
let unparsed_json = match fs::read_to_string(file){
let unparsed_json = match fs::read_to_string(file) {
Ok(s) => s,
Err(f) => {
if f.kind() == ErrorKind::NotFound {
if mode_name != *"default" {
logger(15, format!("Cynthia is missing the `{}´ mode for a page to be served. It will retry using the `default´ mode.", mode_name));
return load_mode(String::from("default"));
} else {
logger(5, String::from("Cynthia is missing the right mode for some pages to serve."));
logger(
5,
String::from("Cynthia is missing the right mode for some pages to serve."),
);
process::exit(1);
}
} else {
logger(5, String::from("Cynthia is having trouble loading the mode for some pages to serve."));
logger(
5,
String::from(
"Cynthia is having trouble loading the mode for some pages to serve.",
),
);
process::exit(1);
}
}
Expand Down
6 changes: 4 additions & 2 deletions src/subcommand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,12 @@ pub(crate) fn init() {
10,
String::from("Clean CynthiaConfig written! Please adjust then restart Cynthia!"),
);
if pluginmanjson.exists() && choice(
if pluginmanjson.exists()
&& choice(
String::from("Do you want to install recomended plugins"),
true,
) {
)
{
logger(
1,
format!(
Expand Down

0 comments on commit 2a329ce

Please sign in to comment.