Skip to content

Commit 16031b6

Browse files
committed
feat(rcfile): skip npx etc entirely
1 parent eacc0cf commit 16031b6

File tree

1 file changed

+53
-70
lines changed

1 file changed

+53
-70
lines changed

src/rcfile/javascript.rs

Lines changed: 53 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -14,82 +14,65 @@ pub fn from_javascript_path(file_path: &Path) -> Result<Rcfile, RcfileError> {
1414
let escaped_file_path_for_nodejs = file_path.to_string_lossy().replace('\\', "\\\\");
1515
let nodejs_script = format!(
1616
r#"
17-
import('{escaped_file_path_for_nodejs}')
18-
.then(findConfig)
19-
.then((value) => {{
20-
if (isNonEmptyObject(value)) {{
21-
console.log(JSON.stringify({{
22-
_tag: 'Ok',
23-
value: JSON.stringify(value),
24-
source: 'import',
25-
}}));
26-
}} else {{
27-
tryRequire('Config expected at default export');
28-
}}
29-
}})
30-
.catch((err) => {{
31-
tryRequire(err.stack || err.message || 'Unknown error in import()');
32-
}});
17+
import 'tsx'
3318
34-
function tryRequire(importError) {{
35-
Promise.resolve(null)
36-
.then(() => require('{escaped_file_path_for_nodejs}'))
37-
.then(findConfig)
38-
.then((value) => {{
39-
if (isNonEmptyObject(value)) {{
40-
console.log(JSON.stringify({{
41-
_tag: 'Ok',
42-
value: JSON.stringify(value),
43-
source: 'require',
44-
}}));
45-
}} else {{
46-
console.log(JSON.stringify({{
47-
_tag: 'Err',
48-
importError,
49-
requireError: 'Config expected at module.exports',
50-
}}));
51-
}}
52-
}})
53-
.catch((err) => {{
54-
console.log(JSON.stringify({{
55-
_tag: 'Err',
56-
importError,
57-
requireError: err.stack || err.message || 'Unknown require error'
58-
}}));
59-
}});
60-
}};
61-
62-
function isNonEmptyObject(value) {{
63-
return value && typeof value === 'object' && value.constructor === Object && Object.keys(value).length > 0;
64-
}}
65-
66-
function findConfig(mod) {{
67-
return mod.default && mod.default.default ? mod.default.default : mod.default;
19+
import('{escaped_file_path_for_nodejs}')
20+
.then(findConfig)
21+
.then((value) => {{
22+
if (isNonEmptyObject(value)) {{
23+
console.log(JSON.stringify({{
24+
_tag: 'Ok',
25+
value: JSON.stringify(value),
26+
source: 'import',
27+
}}));
28+
}} else {{
29+
tryRequire('Config expected at default export');
6830
}}
69-
"#
70-
);
31+
}})
32+
.catch((err) => {{
33+
tryRequire(err.stack || err.message || 'Unknown error in import()');
34+
}});
7135
72-
let dir = file_path.parent().unwrap_or_else(|| Path::new("."));
73-
let runner = if dir.join("pnpm-lock.yaml").exists() || dir.join("pnpm-workspace.yaml").exists() {
74-
"pnpm"
75-
} else if dir.join("bun.lock").exists() || dir.join("bun.lockb").exists() {
76-
"bunx"
77-
} else {
78-
"npx"
79-
};
36+
function tryRequire(importError) {{
37+
Promise.resolve(null)
38+
.then(() => require('{escaped_file_path_for_nodejs}'))
39+
.then(findConfig)
40+
.then((value) => {{
41+
if (isNonEmptyObject(value)) {{
42+
console.log(JSON.stringify({{
43+
_tag: 'Ok',
44+
value: JSON.stringify(value),
45+
source: 'require',
46+
}}));
47+
}} else {{
48+
console.log(JSON.stringify({{
49+
_tag: 'Err',
50+
importError,
51+
requireError: 'Config expected at module.exports',
52+
}}));
53+
}}
54+
}})
55+
.catch((err) => {{
56+
console.log(JSON.stringify({{
57+
_tag: 'Err',
58+
importError,
59+
requireError: err.stack || err.message || 'Unknown require error'
60+
}}));
61+
}});
62+
}};
8063
81-
let mut args = vec![];
64+
function isNonEmptyObject(value) {{
65+
return value && typeof value === 'object' && value.constructor === Object && Object.keys(value).length > 0;
66+
}}
8267
83-
if runner == "pnpm" {
84-
args.push("dlx");
85-
}
86-
87-
args.push("tsx");
88-
args.push("-e");
89-
args.push(&nodejs_script);
68+
function findConfig(mod) {{
69+
return mod.default && mod.default.default ? mod.default.default : mod.default;
70+
}}
71+
"#
72+
);
9073

91-
Command::new(runner)
92-
.args(args)
74+
Command::new("node")
75+
.args(vec!["-e", &nodejs_script])
9376
.current_dir(file_path.parent().unwrap_or_else(|| Path::new(".")))
9477
.output()
9578
.map_err(RcfileError::NodeJsExecutionFailed)

0 commit comments

Comments
 (0)