Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support shebangs #1197

Merged
merged 2 commits into from
Nov 16, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
41 changes: 34 additions & 7 deletions src/deno_dir.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright 2018 the Deno authors. All rights reserved. MIT license.
use dirs;
use errors::DenoError;
use errors;
use errors::DenoResult;
use errors::ErrorKind;
use fs as deno_fs;
Expand Down Expand Up @@ -208,7 +208,7 @@ impl DenoDir {
self: &Self,
module_specifier: &str,
containing_file: &str,
) -> Result<CodeFetchOutput, DenoError> {
) -> Result<CodeFetchOutput, errors::DenoError> {
debug!(
"code_fetch. module_specifier {} containing_file {}",
module_specifier, containing_file
Expand All @@ -218,24 +218,26 @@ 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 {
// For NotFound, change the message to something better.
return Err(DenoError::from(std::io::Error::new(
std::io::ErrorKind::NotFound,
return Err(errors::new(
ErrorKind::NotFound,
format!(
"Cannot resolve module \"{}\" from \"{}\"",
module_specifier, containing_file
),
)));
));
} else {
return Err(err);
}
}
Ok(out) => out,
};

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");
}