diff options
author | Ryan Dahl <ry@tinyclouds.org> | 2018-11-15 05:43:19 -0500 |
---|---|---|
committer | Ryan Dahl <ry@tinyclouds.org> | 2018-11-16 08:05:13 +0800 |
commit | f88fa2dcf8385e507310adc8f813945d90bd7286 (patch) | |
tree | a7b285c9fc57de75a825cbf84fbf4a98693533e8 /src | |
parent | d7abdfe754b1e95c71e0f1956c3e9a1448fa9df1 (diff) |
Support shebang
Diffstat (limited to 'src')
-rw-r--r-- | src/deno_dir.rs | 31 |
1 files changed, 29 insertions, 2 deletions
diff --git a/src/deno_dir.rs b/src/deno_dir.rs index ee114a654..a7b9f97d6 100644 --- a/src/deno_dir.rs +++ b/src/deno_dir.rs @@ -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"); +} |