1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-22 23:34:47 -05:00

Support shebang

This commit is contained in:
Ryan Dahl 2018-11-15 05:43:19 -05:00
parent d7abdfe754
commit f88fa2dcf8
2 changed files with 29 additions and 3 deletions

View file

@ -86,7 +86,6 @@ export default function denoMain() {
const cwd = startResMsg.cwd();
log("cwd", cwd);
// TODO handle shebang.
for (let i = 1; i < startResMsg.argvLength(); i++) {
args.push(startResMsg.argv(i));
}

View file

@ -218,7 +218,7 @@ impl DenoDir {
self.resolve_module(module_specifier, containing_file)?;
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,
Err(err) => {
if err.kind() == ErrorKind::NotFound {
@ -233,9 +233,11 @@ impl DenoDir {
} else {
return Err(err);
}
},
}
};
out.source_code = filter_shebang(out.source_code);
let result =
self.load_cache(out.filename.as_str(), out.source_code.as_str());
match result {
@ -941,3 +943,28 @@ fn test_map_content_type() {
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");
}