Skip to content

Commit

Permalink
fix: don't apply fixes that can be applied by module-level requirejs-…
Browse files Browse the repository at this point in the history
…config.js - fixes #36
  • Loading branch information
shakyShane committed Oct 28, 2018
1 parent 82cfeea commit e0f656f
Show file tree
Hide file tree
Showing 9 changed files with 277 additions and 539 deletions.
18 changes: 8 additions & 10 deletions src/lib/presets/m2/handlers/config_capture.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,14 @@ pub fn handle(original_request: &HttpRequest<AppState>) -> FutResp {
let client_config_clone = original_request.state().rjs_client_config.clone();
apply_to_proxy_body(&original_request, move |b| {
let c2 = client_config_clone.clone();
if let Ok(rjs) = RequireJsClientConfig::from_generated_string(b.to_string()) {
info!("parsed rjs = {:#?}", rjs);
let mut w = c2.lock().expect("unwrapped client_config_clone");
w.deps = rjs.deps.clone();
w.config = rjs.config.clone();
w.shim = rjs.shim.clone();
w.paths = rjs.paths.clone();
w.map = rjs.map.clone();
info!("next rjs = {:#?}", w);
}
match RequireJsClientConfig::update_in_place(b.to_string(), c2) {
Ok(..) => {
/* no op */
}
Err(e) => {
eprintln!("Could not update `RequireJsClientConfig` in place, e = {}", e);
}
};
b
})
}
33 changes: 12 additions & 21 deletions src/lib/presets/m2/handlers/config_post.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,38 +4,29 @@ use app_state::AppState;
use futures::{Future, Stream};
use presets::m2::preset_m2::FutResp;
use presets::m2::requirejs_config::RequireJsClientConfig;
use serde_json;
use std;

///
/// This handler accepts the incoming RequireJS merged
/// config from the client
///
pub fn handle(req: &HttpRequest<AppState>) -> FutResp {
let a = req.state().rjs_client_config.clone();
pub fn handle(original_request: &HttpRequest<AppState>) -> FutResp {
let client_config_clone = original_request.state().rjs_client_config.clone();

req.payload()
original_request
.payload()
.concat2()
.from_err()
.and_then(move |body| {
let result: Result<RequireJsClientConfig, serde_json::Error> =
serde_json::from_str(std::str::from_utf8(&body).unwrap());
//
match result {
Ok(next_config) => {
let mut mutex = a.lock().unwrap();
mutex.base_url = next_config.base_url;
mutex.map = next_config.map;
mutex.config = next_config.config;
mutex.paths = next_config.paths;
mutex.shim = next_config.shim;

Ok(HttpResponse::Ok()
let c2 = client_config_clone.clone();
match std::str::from_utf8(&body[..]) {
Ok(body) => match RequireJsClientConfig::update_in_place(body.to_string(), c2) {
Ok(()) => Ok(HttpResponse::Ok()
.content_type("application/json")
.body("ok"))
}
.body("ok")),
Err(e) => Ok(super::err_response::create(e.to_string())),
},
Err(e) => Ok(super::err_response::create(e.to_string())),
}
})
.responder()
}).responder()
}
47 changes: 36 additions & 11 deletions src/lib/presets/m2/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@ pub enum OutputError {
Conversion,
}

impl OutputError {
pub fn to_string(&self) -> String {
match self {
OutputError::ParseJs => "OutputError::ParseJs".into(),
OutputError::Serialize => "OutputError::Serialize".into(),
OutputError::Conversion => "OutputError::Conversion".into(),
}
}
}

impl ParsedConfig {
///
/// Parse a Magento 2 generated requirejs-config.js file pulling out
Expand Down Expand Up @@ -110,10 +120,9 @@ fn process_shim(xs: &Vec<ObjectMember>, output: &mut ParsedConfig) {
.map(|s| serde_json::Value::String(s))
.collect();

let mut map_item = output
output
.shim
.entry(strip_literal(s))
.or_insert(serde_json::Value::Array(as_serde));
.insert(strip_literal(s), serde_json::Value::Array(as_serde));
}
Expression::Object(vs) => {
let mut m = serde_json::Map::new();
Expand Down Expand Up @@ -154,10 +163,9 @@ fn process_shim(xs: &Vec<ObjectMember>, output: &mut ParsedConfig) {
_ => {}
}
}
let mut map_item = output
output
.shim
.entry(strip_literal(s))
.or_insert(serde_json::Value::Object(m));
.insert(strip_literal(s), serde_json::Value::Object(m));
}
_ => { /* */ }
}
Expand All @@ -178,8 +186,10 @@ fn process_config(xs: &Vec<ObjectMember>, output: &mut ParsedConfig) {
key: ObjectKey::Literal(s),
value,
} => {
let mut map_item =
output.config.entry(strip_literal(s).to_string()).or_insert(HashMap::new());
let mut map_item = output
.config
.entry(strip_literal(s).to_string())
.or_insert(HashMap::new());

match value {
Expression::Object(vs) => {
Expand Down Expand Up @@ -287,7 +297,10 @@ fn process_map(xs: &Vec<ObjectMember>, output: &mut ParsedConfig) {
key: ObjectKey::Literal(k),
value: Expression::Literal(Value::String(v)),
} => {
map_item.insert(strip_literal(k).to_string(), strip_literal(v));
map_item.insert(
strip_literal(k).to_string(),
strip_literal(v),
);
}
_ => { /* */ }
}
Expand Down Expand Up @@ -411,6 +424,18 @@ mod tests {
};
require.config(config);
})();
(function() {
var config = {
shim: {
"jquery/jquery-migrate": {
"deps": [
"jquery",
'jquery/jquery.cookie'
]
}
}
}
})();
"#;

let o = ParsedConfig::from_str(input).expect("parses fixture");
Expand All @@ -431,7 +456,8 @@ mod tests {
"shim": {
"jquery/jquery-migrate": {
"deps": [
"jquery"
"jquery",
"jquery/jquery.cookie"
]
},
"paypalInContextExpressCheckout": {
Expand Down Expand Up @@ -461,7 +487,6 @@ mod tests {
let expected: serde_json::Value = serde_json::from_str(&from).expect("serde from (fixture)");
let actual = serde_json::to_value(&o).expect("Output serialized");

// println!("{:#?}", actual);
assert_eq!(actual, expected);

let _as_require: RequireJsClientConfig =
Expand Down

0 comments on commit e0f656f

Please sign in to comment.