外部命令能干什么呢?

外部命令可以让您在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} 当前文件所在项目的完整路径,只有当前文件是项目管理器中某个项目下的文件时才起作用