外部命令能干什么呢?
外部命令可以讓您在HBuilderX中通過菜單、快捷鍵等方式調用外部程序或命令行
使用場景
- 壓縮圖片
- 文檔轉換(比如markdown轉pdf)
- 調用python、shell腳本
- 打開本地的某個程序
- 傳輸文件到服務器
- 操作服務器的某些服務(如啟動、停止、重啟nginx)
- 下載文件
- 安裝apk到手機
- 其它的自動化操作
外部命令在哪里?
菜單【工具】-->【外部命令】-->【自定義外部命令】
如下圖,您可以看到幾個例子

image.png
外部命令怎么用?
點擊菜單【工具】-->【外部命令】-->【自定義外部命令】,就可以自定義外部命令
最后再介紹吧,先看幾個例子。
例子1:調用外部python、shell等腳本
[{
"name":"調用python腳本",
"command":"python script.py",
"type" : "terminal",
"key":"alt+shift+p"
}]
例子2: 調用TinyPNG無損壓縮圖片
大部分情況下, 圖片都是需要壓縮的,為了 更快的打開網(wǎng)頁,節(jié)省流量
推薦:TinyPNGg官網(wǎng) 無損壓縮,良心網(wǎng)站,每月500張免費。
如下所示:YOUR_API_KEY是你申請的key, --output 可以指定目錄文件名,注意如果和當前圖片路徑一致,會覆蓋原先圖片
[{
"name":"調用TinyPNG無損壓縮圖片",
"command":"curl --user api:YOUR_API_KEY --data-binary @${file} -i https://api.tinify.com/shrink --output ${file}",
"type" : "terminal",
"key":"alt+shift+m"
}]
注意:curl是mac自帶的命令,windows上如需使用curl,請下載curl 安裝
例子3:下載文件
mac上下載文件的命令有:wget、curl
windows上下載文件的命令是:bitsadmin.exe
[{
"name":"下載文件",
"command":"wget -c ${userInput:輸入要下載的地址url}",
"type" : "terminal",
"key":"alt+shift+m"
}]
注意: ${userInput:彈框說明} 會在當前屏幕彈框,可以輸入內容
例子4: Mac: 復制項目到遠程linux服務器
scp是linux和mac上才能用的命令,windows上不可以使用哦
[{
"name":"scp傳輸項目到服務器",
"command":"scp -r ${projectDir} 用戶名@ip:服務器目錄路徑",
"type" : "terminal",
"key":"alt+shift+m"
}]
例子5: 遠程linux服務器 重啟/啟動nginx服務
[{
"name":"遠程服務器重啟nginx",
"command":"ssh 用戶@ip '/opt/nginx/sbin/nginx -s reload'",
"type" : "terminal",
"key":""
},
{
"name":"遠程服務器重啟nginx",
"command":"ssh 用戶@ip '/opt/nginx/sbin/nginx'",
"type" : "terminal",
"key":""
}]
例子6: 使用pandoc轉markdown為pdf、doc、html
pandoc是什么?
pandoc是一個軟件,是一個能把千奇百怪的文檔格式互相轉換的神器,是一把文檔轉換的瑞士軍刀。
安裝后,可以通過命令調用。pandoc官網(wǎng)
pandoc結合外部命令的例子
[{
"name": "Pandoc轉md為pdf",
"command": "pandoc ${file} -o ${fileBasename}.pdf",
"type": "terminal",
"key": ""
},
{
"name": "Pandoc轉md為doc",
"command": "pandoc ${file} -o ${fileBasename}.docx",
"type": "terminal",
"key": ""
},
{
"name": "Pandoc轉md為html",
"command": "pandoc ${file} -o ${fileBasename}.html",
"type": "terminal",
"key": ""
}
]
例子7: 安裝apk到Android手機
[
{
"name": "安裝apk到android手機",
"command": "adb install ${file}",
"type": "terminal",
"key": ""
}
]
外部命令通過key配置快捷鍵
如上的例子,key,可以配置快捷鍵哦
{
"name":"scp傳輸項目到服務器",
"command":"scp -r ${projectDir} 用戶名@ip:服務器目錄路徑",
"type" : "terminal",
"key":"alt+shift+m"
}
使用外部命令注意事項
因為變量和快捷鍵,所以外部命令強大。
//------------外部命令 變量說明------------//
"command"、"workingDir"中可使用預定義的變量來獲取當前文件的路徑信息
${file} 當前文件的完整路徑,比如 D:\files\test.txt
${fileName} 當前文件的文件名,比如 test.txt
${fileExtension} 當前文件的擴展名,比如 txt
${fileBasename} 當前文件僅包含文件名的部分,比如 test
${fileDir} 當前文件所在目錄的完整路徑,比如 D:\files
${projectDir} 當前文件所在項目的完整路徑,只有當前文件是項目管理器中某個項目下的文件時才起作用
示例傳送門
示例傳送門,github External-commands.json
復制粘貼即可使用