mirror of
https://github.com/denoland/deno.git
synced 2024-12-23 07:44:48 -05:00
Support shebang
This commit is contained in:
parent
d7abdfe754
commit
f88fa2dcf8
2 changed files with 29 additions and 3 deletions
|
@ -86,7 +86,6 @@ export default function denoMain() {
|
||||||
const cwd = startResMsg.cwd();
|
const cwd = startResMsg.cwd();
|
||||||
log("cwd", cwd);
|
log("cwd", cwd);
|
||||||
|
|
||||||
// TODO handle shebang.
|
|
||||||
for (let i = 1; i < startResMsg.argvLength(); i++) {
|
for (let i = 1; i < startResMsg.argvLength(); i++) {
|
||||||
args.push(startResMsg.argv(i));
|
args.push(startResMsg.argv(i));
|
||||||
}
|
}
|
||||||
|
|
|
@ -218,7 +218,7 @@ impl DenoDir {
|
||||||
self.resolve_module(module_specifier, containing_file)?;
|
self.resolve_module(module_specifier, containing_file)?;
|
||||||
|
|
||||||
let result = self.get_source_code(module_name.as_str(), filename.as_str());
|
let result = self.get_source_code(module_name.as_str(), filename.as_str());
|
||||||
let out = match result {
|
let mut out = match result {
|
||||||
Ok(out) => out,
|
Ok(out) => out,
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
if err.kind() == ErrorKind::NotFound {
|
if err.kind() == ErrorKind::NotFound {
|
||||||
|
@ -233,9 +233,11 @@ impl DenoDir {
|
||||||
} else {
|
} else {
|
||||||
return Err(err);
|
return Err(err);
|
||||||
}
|
}
|
||||||
},
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
out.source_code = filter_shebang(out.source_code);
|
||||||
|
|
||||||
let result =
|
let result =
|
||||||
self.load_cache(out.filename.as_str(), out.source_code.as_str());
|
self.load_cache(out.filename.as_str(), out.source_code.as_str());
|
||||||
match result {
|
match result {
|
||||||
|
@ -941,3 +943,28 @@ fn test_map_content_type() {
|
||||||
msg::MediaType::Unknown
|
msg::MediaType::Unknown
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn filter_shebang(code: String) -> String {
|
||||||
|
if !code.starts_with("#!") {
|
||||||
|
return code;
|
||||||
|
}
|
||||||
|
match code.find('\n') {
|
||||||
|
None => {
|
||||||
|
return String::from("");
|
||||||
|
}
|
||||||
|
Some(i) => {
|
||||||
|
let (_, rest) = code.split_at(i);
|
||||||
|
return String::from(rest);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_filter_shebang() {
|
||||||
|
assert_eq!(filter_shebang("".to_string()), "");
|
||||||
|
assert_eq!(filter_shebang("#".to_string()), "#");
|
||||||
|
assert_eq!(filter_shebang("#!".to_string()), "");
|
||||||
|
assert_eq!(filter_shebang("#!\n\n".to_string()), "\n\n");
|
||||||
|
let code = "#!/usr/bin/env deno\nconsole.log('hello');\n".to_string();
|
||||||
|
assert_eq!(filter_shebang(code), "\nconsole.log('hello');\n");
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue