跳至主要內容

deno install,腳本安裝程式

Deno 提供 deno install 以輕鬆安裝和分發可執行程式碼。

deno install [OPTIONS...] [URL] [SCRIPT_ARGS...] 將安裝位於 URL 的腳本,名稱為 EXE_NAME

此命令會建立一個精簡的可執行 shell 腳本,使用指定的 CLI 旗標和主模組呼叫 deno。它會放置在安裝根目錄的 bin 目錄中。

範例

$ deno install --allow-net --allow-read https://deno.land/std/http/file_server.ts
[1/1] Compiling https://deno.land/std/http/file_server.ts

✅ Successfully installed file_server.
/Users/deno/.deno/bin/file_server

若要變更可執行檔名稱,請使用 -n/--name

deno install --allow-net --allow-read -n serve https://deno.land/std/http/file_server.ts

可執行檔名稱預設會推論

  • 嘗試取得 URL 路徑的檔案字首。上述範例會變成 'file_server'。
  • 如果檔案字首是像 'main'、'mod'、'index' 或 'cli' 等通用名稱,且路徑沒有父路徑,則取得父路徑的檔案名稱。否則使用通用名稱。
  • 如果結果名稱有 '@...' 字尾,請移除它。

若要變更安裝根目錄,請使用 --root

deno install --allow-net --allow-read --root /usr/local https://deno.land/std/http/file_server.ts

安裝根目錄的決定順序如下

  • --root 選項
  • DENO_INSTALL_ROOT 環境變數
  • $HOME/.deno

如果需要,這些必須手動新增到路徑中。

echo 'export PATH="$HOME/.deno/bin:$PATH"' >> ~/.bashrc

您必須指定安裝時將用於執行腳本的權限。

deno install --allow-net --allow-read https://deno.land/std/http/file_server.ts -- -p 8080

上述命令會建立一個名為 file_server 的可執行檔,它會以網路和讀取權限執行,並繫結到埠 8080。

為了良好的實務,請使用 import.meta.main 慣用語法來指定可執行腳本中的進入點。

範例

// https://example.com/awesome/cli.ts
async function myAwesomeCli(): Promise<void> {
// -- snip --
}

if (import.meta.main) {
myAwesomeCli();
}

當您建立可執行腳本時,請務必讓使用者知道,方法是在您的儲存庫中新增範例安裝命令

# Install using deno install

$ deno install -n awesome_cli https://example.com/awesome/cli.ts

解除安裝

您可以使用 deno uninstall 指令解除安裝腳本。

$ deno uninstall file_server
deleted /Users/deno/.deno/bin/file_server
✅ Successfully uninstalled file_server

請參閱 deno uninstall -h 以取得更多詳細資訊。