外部命令能幹什麼呢?

外部命令可以讓您在HBuilderX中通過菜單、快捷鍵等方式調用外部程序或命令行

使用場景

  • 壓縮文件與解壓
  • 壓縮圖片
  • 文檔轉換(比如markdown轉pdf)
  • 調用python、shell腳本
  • 打開本地的某個程序
  • 傳輸文件到服務器
  • 操作服務器的某些服務(如啓動、停止、重啓nginx)
  • 下載文件
  • 安裝apk到手機
  • 上傳應用到應用分發網站(比如蒲公英)
  • 批量壓縮
  • 其它的自動化操作
  • 上傳文件到七牛雲、阿里雲等

外部命令在哪裏?

菜單【工具】—>【外部命令】

如下圖,您可以看到幾個例子

外部命令能幹什麼呢? - 图1

點擊菜單【工具】—>【外部命令】—>【自定義外部命令】,就可以自定義外部命令,格式爲json。

外部命令示例

例子1:壓縮、解壓

windows例子:

注意: windows的軟件大部分安裝在Program Files目錄,需要注意空格與斜槓。如下例子

  1. [{
  2. "name": "文件: 壓縮7z格式",
  3. "command": "\"C:/Program\ Files/7-Zip/7z.exe\" a ${file}.7z ${file}",
  4. "type": "process",
  5. "key": ""
  6. },
  7. {
  8. "name": "文件: 壓縮zip格式",
  9. "command": [
  10. "C:/Program Files/7-Zip/7z.exe",
  11. "a",
  12. "${file}.zip",
  13. "${file}"
  14. ],
  15. "type": "process",
  16. "key": ""
  17. },
  18. {
  19. "name": "文件: 解壓",
  20. "command": "\"C:/Program Files/7-Zip/7z.exe\" x ${file}",
  21. "type": "shell",
  22. "key": ""
  23. }
  24. ]

Mac例子:

  1. [
  2. {
  3. "name": "壓縮目錄爲bz2",
  4. "command": "cd ${fileDir} && tar -jcvf ${fileBasename}.tar.bz2 ${fileBasename}",
  5. "type": "terminal",
  6. "key": "alt+shift+e"
  7. },
  8. {
  9. "name": "解壓zip包",
  10. "command": "unzip ${file}",
  11. "type": "terminal",
  12. "key": "alt+shift+e"
  13. }
  14. ]

例子2:調用外部python、shell等腳本

  1. [{
  2. "name":"調用python腳本",
  3. "command":"python script.py",
  4. "type" : "terminal",
  5. "key":"alt+shift+p"
  6. }]

例子3: 調用TinyPNG無損壓縮圖片

大部分情況下, 圖片都是需要壓縮的,爲了 更快的打開網頁,節省流量 推薦:TinyPNGg官網 無損壓縮,良心網站,每月500張免費。

如下所示:YOUR_API_KEY是你申請的key, —output 可以指定目錄文件名,注意如果和當前圖片路徑一致,會覆蓋原先圖片

  1. [{
  2. "name":"調用TinyPNG無損壓縮圖片",
  3. "command":"curl --user api:YOUR_API_KEY --data-binary @${file} -i https://api.tinify.com/shrink --output ${file}",
  4. "type" : "terminal",
  5. "key":"alt+shift+m"
  6. }]

注意:curl是mac自帶的命令,windows上如需使用curl,請下載curl 安裝

例子4:下載文件

mac上下載文件的命令有:wget、curl windows上下載文件的命令是:bitsadmin.exe

  1. [{
  2. "name":"下載文件",
  3. "command":"wget -c ${userInput:輸入要下載的地址url}",
  4. "type" : "terminal",
  5. "key":"alt+shift+m"
  6. }]

注意: ${userInput:彈框說明} 會在當前屏幕彈框,可以輸入內容

例子5: Mac: 複製項目到遠程linux服務器

scp是linux和mac上才能用的命令,windows上不可以使用哦

  1. [{
  2. "name":"scp傳輸項目到服務器",
  3. "command":"scp -r ${projectDir} 用戶名@ip:服務器目錄路徑",
  4. "type" : "terminal",
  5. "key":"alt+shift+m"
  6. }]

例子6: 遠程linux服務器 重啓/啓動nginx服務

  1. [{
  2. "name":"遠程服務器重啓nginx",
  3. "command":"ssh 用戶@ip '/opt/nginx/sbin/nginx -s reload'",
  4. "type" : "terminal",
  5. "key":""
  6. },
  7. {
  8. "name":"遠程服務器重啓nginx",
  9. "command":"ssh 用戶@ip '/opt/nginx/sbin/nginx'",
  10. "type" : "terminal",
  11. "key":""
  12. }]

例子7: 使用pandoc轉markdown爲pdf、doc、html

pandoc是什麼? pandoc是一個軟件,是一個能把千奇百怪的文檔格式互相轉換的神器,是一把文檔轉換的瑞士軍刀。 安裝後,可以通過命令調用。pandoc官網

pandoc結合外部命令的例子

  1. [{
  2. "name": "Pandoc轉md爲pdf",
  3. "command": "pandoc ${file} -o ${fileBasename}.pdf",
  4. "type": "terminal",
  5. "key": ""
  6. },
  7. {
  8. "name": "Pandoc轉md爲doc",
  9. "command": "pandoc ${file} -o ${fileBasename}.docx",
  10. "type": "terminal",
  11. "key": ""
  12. },
  13. {
  14. "name": "Pandoc轉md爲html",
  15. "command": "pandoc ${file} -o ${fileBasename}.html",
  16. "type": "terminal",
  17. "key": ""
  18. }
  19. ]

例子8: 安裝apk到Android手機

  1. [
  2. {
  3. "name": "安裝apk到android手機",
  4. "command": "adb install ${file}",
  5. "type": "terminal",
  6. "key": ""
  7. }
  8. ]

例子9: 【蒲公英】內測應用上傳

  1. [
  2. {
  3. "name": "【蒲公英】內測應用上傳",
  4. "command": "curl -F 'file=@${file}' -F 'uKey=xxxxxxx' -F '_api_key=xxxxxx' https://upload.pgyer.com/apiv1/app/upload",
  5. "type": "terminal",
  6. "key": "alt+shift+m"
  7. }
  8. ]

說明:uKey_api_key需要自己申請。 網址:https://www.pgyer.com/doc/api

例子10:使用ftp

參考網友貢獻http://ask.dcloud.net.cn/article/35459

例子11:使用顏色選擇器

該插件目前無需外部命令配置,具體見其文檔:http://ext.dcloud.net.cn/plugin?id=146

例子12: 批量壓縮js文件

find是mac上的命令。windows請自行編寫批處理

  1. [{
  2. "name": "js批量壓縮",
  3. "command": "for i in `find ${projectDir} -path ${projectDir}'/unpackage' -prune -o -name '*.js' -and ! -iname '*.min.js'`;do `/Applications/HBuilderX-Alpha.app/Contents/HBuilderX/plugins/compress-babel-minify/node_modules/.bin/minify ${i} --out-file ${i%.js*}.min.js 2>/dev/null`;[ $? -ne 0 ] && echo && echo '壓縮錯誤的文件:'${i}; done",
  4. "type": "shell",
  5. "key": "alt+shift+e"
  6. }]

外部命令配置快捷鍵

如上的例子,key,可以配置快捷鍵哦

  1. {
  2. "name":"scp傳輸項目到服務器",
  3. "command":"scp -r ${projectDir} 用戶名@ip:服務器目錄路徑",
  4. "type" : "terminal",
  5. "key":"alt+shift+m"
  6. }

外部命令變量說明

因爲變量和快捷鍵,所以外部命令強大。

  1. //------------外部命令 變量說明------------//
  2. "command""workingDir"中可使用預定義的變量來獲取當前文件的路徑信息
  3. ${file} 當前文件的完整路徑,比如 D:\files\test.txt
  4. ${fileName} 當前文件的文件名,比如 test.txt
  5. ${fileExtension} 當前文件的擴展名,比如 txt
  6. ${fileBasename} 當前文件僅包含文件名的部分,比如 test
  7. ${fileDir} 當前文件所在目錄的完整路徑,比如 D:\files
  8. ${projectDir} 當前文件所在項目的完整路徑,只有當前文件是項目管理器中某個項目下的文件時才起作用