简体   繁体   English

如何从 shell 脚本调用 deno?

[英]How do I invoke deno from a shell script?

I have a script I'd like to run... maybe something like this:我有一个我想运行的脚本......也许是这样的:

#!/usr/bin/env deno

console.log('hello world');

The shebang part is confusing. shebang部分令人困惑。 When I do it as above, with the filename dev , I get:当我像上面那样使用文件名dev时,我得到:

error: Found argument './dev' which wasn't expected, or isn't valid in this context

When I try to change the shebang to #!/usr/bin/env deno run当我尝试将 shebang 更改为#!/usr/bin/env deno run

I get我明白了

/usr/bin/env: 'deno run': No such file or directory

Any thoughts on how to pull this off?关于如何解决这个问题的任何想法?

In order to pass command options to deno via env , you need the -S parameter for env.为了通过env将命令选项传递给deno ,您需要 env 的-S参数。

For example, the following is the minimal shebang you should use for self-running a script with Deno.例如,以下是使用 Deno 自行运行脚本时应使用的最小 shebang。

#!/usr/bin/env -S deno run

Complex Example:复杂示例:

The following script/shebang will run Deno as silently as possible, with all permissions and will assume there is an import_map.json file next to the script file.以下脚本/shebang 将尽可能以静默方式运行 Deno,并拥有所有权限,并假设脚本文件旁边有一个import_map.json文件。

#!/usr/bin/env -S deno -q run --allow-all --import-map="${_}/../import_map.json"
// deno-language: ts

// get file and directory name
const __filename = import.meta.url.replace("file://", "");
const __dirname = __filename.split("/").slice(0, -1).join("/");

The // deno-language: ts will help if you're creating a script without a file extension.如果您要创建没有文件扩展名的脚本, // deno-language: ts会有所帮助。

The lines with __filename and __dirname will give you variables similar to Node's execution.带有__filename__dirname的行将为您提供类似于 Node 执行的变量。


Script Installer脚本安装程序

Deno also provides a convenient method for installing a script with it's permissions and dependencies in the configured distribution location. Deno 还提供了一种方便的方法,用于在配置的分发位置安装具有权限和依赖关系的脚本。

See: Deno Manual - Script installer请参阅: Deno 手册 - 脚本安装程序

Stand-Alone Executable独立可执行文件

As of Deno 1.6, you can now build stand-alone executables from your script as well.从 Deno 1.6 开始,您现在也可以从脚本构建独立的可执行文件。

See: Deno Manual - Compiler - Compiling Executables请参阅: Deno 手册 - 编译器 - 编译可执行文件

Deno 1.6.0德诺 1.6.0

can compile to a standalone binary / executable file, no need for a custom Shebang:可以编译成独立的二进制/可执行文件,不需要自定义 Shebang:

$ deno compile --unstable main.js 
# or add custom output path
$ deno compile --unstable main.js -o /my/path/main
# both create ./main executable (e.g. on Linux)

# execute script directly
$ ./main 

More infos更多信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM