9

让 vim 保存后自动刷新微信小程序

 3 years ago
source link: https://zhuanlan.zhihu.com/p/22685287
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

让 vim 保存后自动刷新微信小程序

开源开发者,vimer
如果你也觉得每次保存文件都要去手工刷新项目太麻烦,可以使用本文的办法解决这个烦恼。

微信开发者工具支持刷新和重建的快捷键,所以我们可以发送一段 apple script 脚本来达到刷新/重建项目的效果, 从而免去手工切换再使用快捷键的麻烦。例如:

tell application "wechatwebdevtools"
  activate
  delay 0.2
  tell application "System Events"
    key code {55, 15}
  end tell
end tell

这段代码可以使用命令 osascript 执行,效果就是聚焦微信开发者工具,然后发送 Command + R 刷新界面。

这里的问题是我们还是需要每次刷新都要切换到微信开发者工具界面,这对于开发的流畅度还是有影响的。

一个简单的解决办法就是修改开发者工具源码,让它支持全局快捷键,经过一番查找,找到了文件: /Applications/wechatwebdevtools.app/Contents/Resources/app.nw/app/dist/common/menu/menu.js, 添加代码:

function registShortcut(key, onactive) {

  var option = {
    key : key,
    active : onactive,
    failed : function(msg) {
      // :(, fail to register the |key| or couldn't parse the |key|.
      console.log(msg);
    }
  };

  // Create a shortcut with |option|.
  var shortcut = new nw.Shortcut(option);

  // Register global desktop shortcut, which can work without focus.
  nw.App.registerGlobalHotKey(shortcut);
}
registShortcut("Command+Shift+R", function () {
  e.reload()
})
registShortcut("Command+Shift+B", function () {
  e.reBuild()
})

到 init 函数下面就可以让开发者工具支持全局刷新的热键了(Command+Shift+R 和 Command+Shift+B), 完整文件下载

然后只需要让 vim 保存 wxss 和 wxml 文件后发送快捷键就可以了。使用一键安装命令:

curl https://raw.githubusercontent.com/chemzqm/vim-macos/master/autoload/macos.vim > ~/.vim/autoload/macos.vim

安装 vim-macos 插件, 然后在 ~/.vimrc 中配置:

autocmd BufWritePost *.wxml call macos#keycodes('command', 'shift', 'r')
autocmd BufWritePost *.wxss call macos#keycodes('command', 'shift', 'r')
autocmd BufWritePost ~/wechat-dev/**/*.js call macos#keycodes('command', 'shift', 'b')

大功告成, 以后每次保存 wxml 和 wxss 之后 vim 就会发送按键码到操作系统,然后开发者工具接收后就会自动刷新或者重建项目了。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK