Skip to content

Commit f2c8eb3

Browse files
committed
fix(config): locate config in JS/TS rcfiles correctly
1 parent 73d9e26 commit f2c8eb3

File tree

2 files changed

+13
-7
lines changed

2 files changed

+13
-7
lines changed

src/rcfile/error.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
use {serde::Deserialize, serde_json::Value, thiserror::Error};
1+
use {serde::Deserialize, thiserror::Error};
22

33
#[derive(Debug, Deserialize)]
44
#[serde(tag = "_tag")]
55
pub enum NodeJsResult {
66
#[serde(rename = "Ok")]
7-
Success { value: Value },
7+
Success { value: String },
88
#[serde(rename = "Err")]
99
Error {
1010
#[serde(rename = "importError")]

src/rcfile/javascript.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,13 @@ pub fn from_javascript_path(file_path: &Path) -> Result<Rcfile, RcfileError> {
1515
let nodejs_script = format!(
1616
r#"
1717
import('{escaped_file_path_for_nodejs}')
18-
.then((mod) => mod.default)
18+
.then(findConfig)
1919
.then((value) => {{
2020
if (isNonEmptyObject(value)) {{
2121
console.log(JSON.stringify({{
2222
_tag: 'Ok',
23-
value,
23+
value: JSON.stringify(value),
24+
source: 'import',
2425
}}));
2526
}} else {{
2627
tryRequire('Config expected at default export');
@@ -33,12 +34,13 @@ pub fn from_javascript_path(file_path: &Path) -> Result<Rcfile, RcfileError> {
3334
function tryRequire(importError) {{
3435
Promise.resolve(null)
3536
.then(() => require('{escaped_file_path_for_nodejs}'))
36-
.then((mod) => mod.default || mod)
37+
.then(findConfig)
3738
.then((value) => {{
3839
if (isNonEmptyObject(value)) {{
3940
console.log(JSON.stringify({{
4041
_tag: 'Ok',
41-
value,
42+
value: JSON.stringify(value),
43+
source: 'require',
4244
}}));
4345
}} else {{
4446
console.log(JSON.stringify({{
@@ -60,6 +62,10 @@ pub fn from_javascript_path(file_path: &Path) -> Result<Rcfile, RcfileError> {
6062
function isNonEmptyObject(value) {{
6163
return value && typeof value === 'object' && value.constructor === Object && Object.keys(value).length > 0;
6264
}}
65+
66+
function findConfig(mod) {{
67+
return mod.default && mod.default.default ? mod.default.default : mod.default;
68+
}}
6369
"#
6470
);
6571

@@ -83,7 +89,7 @@ pub fn from_javascript_path(file_path: &Path) -> Result<Rcfile, RcfileError> {
8389
})
8490
.and_then(|json_str| serde_json::from_str::<NodeJsResult>(&json_str).map_err(RcfileError::JsonParseFailed))
8591
.and_then(|response| match response {
86-
NodeJsResult::Success { value } => serde_json::from_value(value).map_err(RcfileError::InvalidConfig),
92+
NodeJsResult::Success { value } => serde_json::from_str::<Rcfile>(&value).map_err(RcfileError::InvalidConfig),
8793
NodeJsResult::Error {
8894
import_error,
8995
require_error,

0 commit comments

Comments
 (0)