Skip to content

Commit

Permalink
Support shebang
Browse files Browse the repository at this point in the history
  • Loading branch information
ry committed Nov 15, 2018
1 parent 1b215af commit 65a1abb
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 3 deletions.
1 change: 0 additions & 1 deletion js/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand Down
31 changes: 29 additions & 2 deletions src/deno_dir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down Expand Up @@ -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");
}

0 comments on commit 65a1abb

Please sign in to comment.