腳本安裝

腳本安裝

Deno 提供了腳本安裝器 deno install 讓使用者可以產生一個可執行腳本,簡單的範例如下:

deno install --allow-net --allow-read https://deno.land/std@0.73.0/http/file_server.ts

下完該命令後, Deno 會自動將該模組以及它的依賴模組快取到本地端:

Download https://deno.land/std@0.73.0/http/file_server.ts
Download https://deno.land/std@0.73.0/path/mod.ts
Download https://deno.land/std@0.73.0/http/server.ts
Download https://deno.land/std@0.73.0/flags/mod.ts
Download https://deno.land/std@0.73.0/_util/assert.ts
Download https://deno.land/std@0.73.0/path/_constants.ts
Download https://deno.land/std@0.73.0/path/win32.ts
Download https://deno.land/std@0.73.0/path/posix.ts
Download https://deno.land/std@0.73.0/path/common.ts
Download https://deno.land/std@0.73.0/path/separator.ts
Download https://deno.land/std@0.73.0/path/_interface.ts
Download https://deno.land/std@0.73.0/path/glob.ts
Download https://deno.land/std@0.73.0/encoding/utf8.ts
Download https://deno.land/std@0.73.0/io/bufio.ts
Download https://deno.land/std@0.73.0/async/mod.ts
Download https://deno.land/std@0.73.0/http/_io.ts
Download https://deno.land/std@0.73.0/path/_util.ts
Download https://deno.land/std@0.73.0/textproto/mod.ts
Download https://deno.land/std@0.73.0/http/http_status.ts
Download https://deno.land/std@0.73.0/bytes/mod.ts
Download https://deno.land/std@0.73.0/async/deferred.ts
Download https://deno.land/std@0.73.0/async/delay.ts
Download https://deno.land/std@0.73.0/async/mux_async_iterator.ts
Download https://deno.land/std@0.73.0/async/pool.ts
✅ Successfully installed file_server
C:\Users\UserName\.deno\bin\file_server.cmd

我們可以在命令列的最後看到,相關腳本已經被放到 C:\Users\UserName\.deno\bin\ 的路徑中:

% generated by deno install %
@deno.exe "run" "--allow-read" "--allow-net" "C:\Users\eric\.deno\bin\file_server.js" %*

自訂腳本檔名

我們可以使用 -n/--name 指定腳本名稱:

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

如此一來,在 Windows 10 環境執行後,我們會得到 serve.cmd 的腳本檔案。

筆者會提到 Windows 10 只是因為我今天是用實驗室的電腦編寫文章跟測試功能,其他作業系統平台同樣適用上面提到的方法唷!

更改安裝目錄

根據上面的範例,其實不難發現, deno 腳本的預設安裝目錄為: .deno/bin/

若我們希望改變腳本的存放目錄,可以使用 --root : (該範例適用 Linux 環境)

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

安裝優先權:

  • --root 選項

  • DENO_INSTALL_ROOT 環境變數

  • $HOME/.deno

Last updated