@@ -9,14 +9,14 @@ use std::io::Write;
9
9
use std:: io:: { Error , ErrorKind } ;
10
10
use std:: path:: { Path , PathBuf } ;
11
11
use std:: time:: { SystemTime , UNIX_EPOCH } ;
12
- use std:: { env, fs} ;
12
+ use std:: fs;
13
+ use dotenv:: dotenv;
13
14
14
15
fn cachefolder ( ) -> PathBuf {
15
- let fl = env:: current_dir ( )
16
- . unwrap ( )
17
- . join ( "/.cynthiaTemp/cache/" )
16
+ let fl = PathBuf :: from ( "./.cynthiaTemp/cache/" )
18
17
. join ( format ! ( "{}" , std:: process:: id( ) ) )
19
18
. normalize ( ) ;
19
+ // logger(31, format!("Cache folder: {}", fl.display()));
20
20
fs:: create_dir_all ( & fl) . unwrap ( ) ;
21
21
fl
22
22
}
@@ -36,6 +36,7 @@ pub(crate) fn cacheretriever(file: String, max_age: u64) -> Result<PathBuf, Erro
36
36
if ( now - f. timestamp ) < max_age {
37
37
return Ok ( f. cachepath ) ;
38
38
} else if Path :: new ( & f. cachepath ) . exists ( ) {
39
+ logger ( 31 , format ! ( "Cache {}: {} at {}, reason: Too old!" , "removed" . red( ) , file, & f. cachepath. display( ) ) ) ;
39
40
fs:: remove_file ( Path :: new ( & f. cachepath ) ) . unwrap ( ) ;
40
41
} ;
41
42
}
@@ -59,6 +60,7 @@ pub(crate) fn cacheplacer(fileid: String, contents: String) -> String {
59
60
60
61
let mut cachedfile = File :: create ( cachepath. clone ( ) ) . unwrap ( ) ;
61
62
write ! ( cachedfile, "{}" , contents) . unwrap ( ) ;
63
+ logger ( 31 , format ! ( "Cache {}: {} in {}" , "placed" . green( ) , fileid, cachepath. display( ) ) ) ;
62
64
let new = CynthiaCacheIndexObject {
63
65
fileid,
64
66
cachepath,
@@ -79,7 +81,12 @@ pub(crate) fn cacheplacer(fileid: String, contents: String) -> String {
79
81
}
80
82
81
83
pub ( crate ) fn import_js_minified ( scriptfile : String ) -> String {
82
- return match cacheretriever ( scriptfile. to_string ( ) , 1200 ) {
84
+ dotenv ( ) . ok ( ) ;
85
+ let jscachelifetime: u64 = match std:: env:: var ( "JAVASCRIPT_CACHE_LIFETIME" ) {
86
+ Ok ( g) => g. parse :: < u64 > ( ) . unwrap ( ) ,
87
+ Err ( _) => 1200 ,
88
+ } ;
89
+ return match cacheretriever ( scriptfile. to_string ( ) , jscachelifetime) {
83
90
Ok ( o) => fs:: read_to_string ( o) . expect ( "Couldn't find or open a JS file." ) ,
84
91
Err ( _) => match jsruntime ( true ) {
85
92
BUNJSR => {
@@ -102,7 +109,7 @@ pub(crate) fn import_js_minified(scriptfile: String) -> String {
102
109
if output. status . success ( ) {
103
110
let res: String = String :: from_utf8_lossy ( & output. stdout ) . parse ( ) . unwrap ( ) ;
104
111
cacheplacer ( scriptfile, format ! (
105
- "\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 "
112
+ "\n \r // Minified internally by Cynthia using Terser\n \n {res}\n \n \r // Cached after minifying, so might be somewhat behind.\n \r "
106
113
) )
107
114
} else {
108
115
logger (
@@ -145,7 +152,7 @@ pub(crate) fn import_js_minified(scriptfile: String) -> String {
145
152
} else {
146
153
let res: String = String :: from_utf8_lossy ( & output. stdout ) . parse ( ) . unwrap ( ) ;
147
154
cacheplacer ( scriptfile, format ! (
148
- "\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 "
155
+ "\n \r // Minified internally by Cynthia using Terser\n \n {res}\n \n \r // Cached after minifying, so might be somewhat behind.\n \r "
149
156
) )
150
157
}
151
158
}
@@ -158,7 +165,11 @@ pub(crate) fn import_js_minified(scriptfile: String) -> String {
158
165
}
159
166
160
167
pub ( crate ) fn import_css_minified ( stylefile : String ) -> String {
161
- return match cacheretriever ( stylefile. to_string ( ) , 1200 ) {
168
+ let csscachelifetime: u64 = match std:: env:: var ( "STYLESHEET_CACHE_LIFETIME" ) {
169
+ Ok ( g) => g. parse :: < u64 > ( ) . unwrap ( ) ,
170
+ Err ( _) => 1200 ,
171
+ } ;
172
+ return match cacheretriever ( stylefile. to_string ( ) , csscachelifetime) {
162
173
Ok ( o) => fs:: read_to_string ( o) . expect ( "Couldn't find or open a JS file." ) ,
163
174
Err ( _) => match jsruntime ( true ) {
164
175
BUNJSR => {
@@ -181,7 +192,7 @@ pub(crate) fn import_css_minified(stylefile: String) -> String {
181
192
if output. status . success ( ) {
182
193
let res: String = String :: from_utf8_lossy ( & output. stdout ) . parse ( ) . unwrap ( ) ;
183
194
cacheplacer ( stylefile, format ! (
184
- "\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 "
195
+ "\n \r /* Minified internally by Cynthia using clean-css */\n \n {res}\n \n \r /* Cached after minifying, so might be somewhat behind. */\n \r "
185
196
) )
186
197
} else {
187
198
logger (
@@ -224,7 +235,7 @@ pub(crate) fn import_css_minified(stylefile: String) -> String {
224
235
} else {
225
236
let res: String = String :: from_utf8_lossy ( & output. stdout ) . parse ( ) . unwrap ( ) ;
226
237
cacheplacer ( stylefile, format ! (
227
- "\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 "
238
+ "\n \r /* Minified internally by Cynthia using clean-css */\n \n {res}\n \n \r /* Cached after minifying, so might be somewhat behind. */\n \r "
228
239
) )
229
240
}
230
241
}
0 commit comments