Skip to content

Latest commit

 

History

History
322 lines (236 loc) · 10.6 KB

deno_cmd.md

File metadata and controls

322 lines (236 loc) · 10.6 KB

Deno常用命令简介

前言

安装了Deno(v1.9.2)之后,执行帮助deno -h命令,就可以看到相关的命令参数列表

> deno -h
deno 1.9.2
A secure JavaScript and TypeScript runtime

Docs: https://deno.land/manual
Modules: https://deno.land/std/ https://deno.land/x/
Bugs: https://github.com/denoland/deno/issues

To start the REPL:

  deno

To execute a script:

  deno run https://deno.land/std/examples/welcome.ts

To evaluate code in the shell:

  deno eval "console.log(30933 + 404)"

USAGE:
    deno [OPTIONS] [SUBCOMMAND]

OPTIONS:
    -h, --help                     Prints help information
    -L, --log-level <log-level>    Set log level [possible values: debug, info]
    -q, --quiet                    Suppress diagnostic output
        --unstable                 Enable unstable features and APIs
    -V, --version                  Prints version information

SUBCOMMANDS:
    bundle         Bundle module and dependencies into single file
    cache          Cache the dependencies
    compile        Compile the script into a self contained executable
    completions    Generate shell completions
    coverage       Print coverage reports
    doc            Show documentation for a module
    eval           Eval script
    fmt            Format source files
    help           Prints this message or the help of the given subcommand(s)
    info           Show info about cache or info related to source file
    install        Install script as an executable
    lint           Lint source files
    lsp            Start the language server
    repl           Read Eval Print Loop
    run            Run a JavaScript or TypeScript program
    test           Run tests
    types          Print runtime TypeScript declarations
    upgrade        Upgrade deno executable to given version

ENVIRONMENT VARIABLES:
    DENO_AUTH_TOKENS     A semi-colon separated list of bearer tokens and
                         hostnames to use when fetching remote modules from
                         private repositories
                         (e.g. "abcde12345@deno.land;54321edcba@github.com")
    DENO_CERT            Load certificate authority from PEM encoded file
    DENO_DIR             Set the cache directory
    DENO_INSTALL_ROOT    Set deno install's output directory
                         (defaults to $HOME/.deno/bin)
    DENO_WEBGPU_TRACE    Directory to use for wgpu traces
    HTTP_PROXY           Proxy address for HTTP requests
                         (module downloads, fetch)
    HTTPS_PROXY          Proxy address for HTTPS requests
                         (module downloads, fetch)
    NO_COLOR             Set to disable color
    NO_PROXY             Comma-separated list of hosts which do not use a proxy
                         (module downloads, fetch)

使用方式

deno [子命令] [子命令的flags(可以多个)] [参数args]

例如允许所有权限执行脚本文件

deno run --allow-all mod.ts

例如允许所有权限 + 重新加载编译 执行脚本文件

deno run --allow-all  --reload mod.ts

子命令操作

子命令 run

  • 作用:

    • 运行文件程序
  • 说明:

    • 通过一个js/ts 的文件路径或者线上URL路径,来运行一个程序
deno run mod.ts


deno run https://xxx.xx/mod.ts

子命令 run 帮助说明

> deno run -h

# 会出现以下说明

deno-run 
Run a program given a filename or url to the module.

By default all programs are run in sandbox without access to disk, network or
ability to spawn subprocesses.
  deno run https://deno.land/std/examples/welcome.ts

Grant all permissions:
  deno run -A https://deno.land/std/http/file_server.ts

Grant permission to read from disk and listen to network:
  deno run --allow-read --allow-net https://deno.land/std/http/file_server.ts

Grant permission to read whitelisted files from disk:
  deno run --allow-read=/etc https://deno.land/std/http/file_server.ts
  
Deno allows specifying the filename '-' to read the file from stdin.
  curl https://deno.land/std/examples/welcome.ts | target/debug/deno run -

USAGE:
    deno run [OPTIONS] <SCRIPT_ARG>...

OPTIONS:
    -A, --allow-all                    Allow all permissions
        --allow-env                    Allow environment access
        --allow-hrtime                 Allow high resolution time measurement
        --allow-net=<allow-net>        Allow network access
        --allow-plugin                 Allow loading plugins
        --allow-read=<allow-read>      Allow file system read access
        --allow-run                    Allow running subprocesses
        --allow-write=<allow-write>    Allow file system write access
        --cached-only                  Require that remote dependencies are already cached
        --cert <FILE>                  Load certificate authority from PEM encoded file
    -c, --config <FILE>                Load tsconfig.json configuration file
    -h, --help                         Prints help information
        --importmap <FILE>             UNSTABLE: Load import map file
        --inspect=<HOST:PORT>          activate inspector on host:port (default: 127.0.0.1:9229)
        --inspect-brk=<HOST:PORT>      activate inspector on host:port and break at start of user script
        --lock <FILE>                  Check the specified lock file
        --lock-write                   Write lock file. Use with --lock.
    -L, --log-level <log-level>        Set log level [possible values: debug, info]
        --no-remote                    Do not resolve remote modules
    -q, --quiet                        Suppress diagnostic output
    -r, --reload=<CACHE_BLACKLIST>     Reload source code cache (recompile TypeScript)
        --seed <NUMBER>                Seed Math.random()
        --unstable                     Enable unstable APIs
        --v8-flags=<v8-flags>          Set V8 command line options. For help: --v8-flags=--help

ARGS:
    <SCRIPT_ARG>...    script args

run 主要功能

  • 编译tsjs代码和sourceMap文件,存在 $HOME/.deno/gen/ 目录下
  • 并且jssourceMap文件 都是以hash值命名的
  • 只要代码不做变更,都会一直执行编译后的文件
  • 如果代码变更了,会重新执行编译,并且生成新的hash命名的jssourceMap文件
run 权限相关flags
  • deno run --allow-net mod.ts 执行代码 允许直接使用网络权限
  • deno run --allow-read mod.ts 执行代码 允许直接使用文件读权限
  • deno run --allow-write mod.ts 执行代码 允许直接使用文件写权限
  • deno run --allow-run mod.ts 执行代码 允许直接执行子程序
  • deno run --allow-env mod.ts 执行代码 允许直接使用操作环境权限
  • deno run --allow-hrtime mod.ts 执行代码 允许测量高分辨率时间
  • deno run --allow-all mod.ts 执行代码 允许以上所有权限
run 其他flags
  • deno run -h 查看帮助文档
  • deno run -D mod.tsdeno --log-debug 输出执行底层日志
  • deno run --no-prompt mod.ts 执行代码时不显示提示
  • deno run --v8-flags mod.ts 设置V8命令行参数

子命令 eval

  • 作用: 直接执行脚本代码的字符串

例如:

deno eval "console.log('hello world! ' + new Date().getTime())"
> deno eval -h

# 会出现以下说明
deno-eval 
Evaluate JavaScript from the command line.
  deno eval "console.log('hello world')"

To evaluate as TypeScript:
  deno eval -T "const v: string = 'hello'; console.log(v)"

This command has implicit access to all permissions (--allow-all).

USAGE:
    deno eval [OPTIONS] <code>

OPTIONS:
        --cert <FILE>                Load certificate authority from PEM encoded file
    -h, --help                       Prints help information
        --inspect=<HOST:PORT>        activate inspector on host:port (default: 127.0.0.1:9229)
        --inspect-brk=<HOST:PORT>    activate inspector on host:port and break at start of user script
    -L, --log-level <log-level>      Set log level [possible values: debug, info]
    -p, --print                      print result to stdout
    -q, --quiet                      Suppress diagnostic output
    -T, --ts                         Treat eval input as TypeScript
        --unstable                   Enable unstable APIs
        --v8-flags=<v8-flags>        Set V8 command line options. For help: --v8-flags=--help

ARGS:
    <code>

子命令fetch

  • 作用:
    • 获取远程在线的依赖模块
  • 说明:
    • 下载和编译远程依赖模块,并保存在本地
    • 以及递归获取和编译模块的所有依赖
    • 编译后无需运行代码
    • 在后续项目程序应用相同远程代码时候,无需重新下载和编译,除非使用了--reload的flag

fetch 使用方式

deno fetch https://deno.land/welcome.ts
# 1. 将会把 https://deno.land/welcome.ts 下载到本地
# 2. 下载后会编译成js,缓存在本地
# 3. 再次执行 deno fetch https://deno.land/welcome.ts,将会直接从缓存获取
# 4. 本地其他项目如果有 import 'https://deno.land/welcome.ts' 的时候,将会直接从缓存获取


deno fetch --reload https://deno.land/welcome.ts
# 1. 将会重新把 https://deno.land/welcome.ts 下载到本地
# 2. 下载后会编译成js,覆盖上一次的缓存

子命令 fmt

  • 作用:
    • 自动格式化Deno项目JavaScriptTypeScript 源码格式

fmt 使用方式

deno fmt mod.ts 

# 将会格式化 mod.ts 的源码格式

子命令 info

  • 作用:
    • 显示Deno项目代码文件的相关信息
  • 说明
    • local 支持显示文件在本地的绝对路径
    • type 支持显示文件类型,主要支持 JavaScript, TypeScriptJSON 三种格式
    • compiled 只支持TypeScript类型的文件,显示TypeScript编译后JavaScript代码文件在本地的绝对路径
    • map 只支持TypeScript类型的文件,显示TypeScript编译后sourceMap文件在本地的绝对路径
    • deps 支持显示文件里代码的依赖模块,并显示依赖树

info 使用方式

deno info mod.ts 

# 将会显示对应的文件信息
# 例如
local: /Users/xxx/mod.ts
type: TypeScript
compiled: /Users/xxx/Library/Caches/deno/gen/${md5}.js
map: /Users/xxx/Library/Caches/deno/gen/${md5}.js.map
deps:
file:///Users/xxx/mod.ts
  └─┬ file:///Users/xxx/dep_01.ts
    ├─┬ file:///Users/xxx/dep_02.ts
    │ └─┬ file:///Users/xxx/dep_03.ts
    │   ├─┬ file:///Users/xxx/dep_04.ts
    │   │ └── file:///Users/xxx/dep_05.ts
    │   └── file:///Users/xxx/dep_06.ts
    ├─┬ file:///Users/xxx/dep_07.ts
    │ └── file:///Users/xxx/dep_08.ts
    └── file:///Users/xxx/dep_09.ts