VSCode配置Code Runner + Deno直接運行ts代碼

最近LeetCode的一些題目開始支持ts了,所以我就開始用ts做一些題目。事實上現在vscode對ts的支持已經相當不錯了,包括語法高亮啥的。但是無論是什麼編輯器(包括我最愛的WebStorm),都不能直接運行ts代碼,這給本地調試帶來了很大的不便。

應該很多人知道vscode有個叫code runner的插件,可以很方便地去運行代碼,然後他給的辦法是去裝一個ts-node的npm包,但我覺得沒啥意思。正好一直聽說deno的1.0版本已經發布了,能夠直接運行ts代碼,就索性裝個deno,然後配置一下code runner的執行命令,一步到位。

deno官網的安裝說明已經很詳細了。比如我用的Windows(powershell):

iwr https://deno.land/x/install/install.ps1 -useb | iex

別的操作系統同理,看一下官方文檔就OK。

裝完之後,在vscode裏設置一下code runner的Executor Map屬性。在settings.json里加上這麼一段:

"code-runner.executorMap": {
    "typescript": "deno run $fullFileName"
}

然後就可以了。

可能有人對這個$fullFileName變量有疑問,這個其實是code runner插件的預置變量。按照作者的說法,有這麼幾個變量,可以在配置裏使用,會被自動替換。這幾個變量顧名思義:

  • Supported customized parameters

    • $workspaceRoot: The path of the folder opened in VS Code

    • $dir: The directory of the code file being run

    • $dirWithoutTrailingSlash: The directory of the code file being run without a trailing slash

    • $fullFileName: The full name of the code file being run

    • $fileName: The base name of the code file being run, that is the file without the directory

    • $fileNameWithoutExt: The base name of the code file being run without its extension

    • $driveLetter: The drive letter of the code file being run (Windows only)

    • $pythonPath: The path of Python interpreter (set by Python: Select Interpreter command)

Please take care of the back slash and the space in file path of the executor

  • Back slash: please use \\
  • If there ares spaces in file path, please use \" to surround your file path
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章