美文网首页
文件变动监听神器watchexec的使用指南(go文件变动重启服

文件变动监听神器watchexec的使用指南(go文件变动重启服

作者: mudssky | 来源:发表于2022-11-20 16:20 被阅读0次

    对于golang和rust这类编译型语言,我们写服务端项目的时候,当我们保存文件的时候,重新编译项目并执行是一个很常见的需求。(其实还有就是保存后自动执行测试,不过因为我不怎么写测试,暂时还是用不到这种功能。)

    watchexec就是一个监听到文件变动就执行命令的命令行工具。

    https://github.com/watchexec/watchexec

    有很多类似的工具,比如node生态的nodemon用来热重启node的项目

    其实go语言这边也有类似的程序,比如air(https://github.com/cosmtrek/air),是国人开发的热启动项目的工具。

    但是air我用过确实不好用,我保存一次它编译两次。还有就是一大屏log输出很烦,它的命令行也没什么配置项目可言。

    帮助文档

    ❯ watchexec.exe -h
    watchexec 1.20.6
    Execute commands when watched files change
    
    USAGE:
        watchexec.exe [OPTIONS] <command>...
    
    OPTIONS:
        -h, --help       Print help information
        -V, --version    Print version information
    
    Command options:
        -E, --env <name=value>   添加环境变量
        -n, --no-shell            Do not wrap command in a shell. Deprecated: use --shell=none instead.
            --no-environment      Do not set WATCHEXEC_*_PATH environment variables for the command
            --no-process-group    Do not use a process group when running the command
            --shell <shell>      使用不同的shell, `none`表示不使用shell. 比如我们在windows下可以使用powershell Try --shell=powershell, which will become the default in 2.0.
            --workdir <path>    改变命令行的工作路径
        <command>...         需要执行的命令
    
    Filtering options:
        -e, --exts <extensions>       筛选指定扩展名(通过逗号分隔,获取文件列表的扩展名)
        -f, --filter <pattern>         使用glob表达式过滤文件
        -i, --ignore <pattern>         忽略glob匹配的文件
            --no-default-ignore        Skip auto-ignoring of commonly ignored globs
            --no-global-ignore         Skip auto-loading of global or environment-wide ignore files
            --no-meta                  Ignore metadata changes
            --no-project-ignore        跳过自动导入项目的ignore文件  (.gitignore, .ignore, etc)
            --no-vcs-ignore            Skip auto-loading of VCS (Git, etc) ignore files
            --project-origin <path>    Override the project origin: the directory from which ignore files are detected
        -w, --watch <path>            监听一个特定的文件或路径
    
    Output options:
        -c, --clear    执行命令前清屏
        -N, --notify  命令执行完时发送通知
    
    Behaviour options:
        -d, --debounce <milliseconds>           变动到命令执行间隔的时间,默认是 50ms
            --delay-run <seconds>                延迟执行命令
            --force-poll <interval>              Force polling mode (interval in milliseconds)
            --on-busy-update <on-busy-update>    Select the behaviour to use when receiving events while the command is running. Current default is queue, will change to do-nothing in 2.0. [possible values: do-nothing, queue, restart, signal]
        -p, --postpone                           Wait until first change to execute command
        -r, --restart                            如果进程还在运行则重启  --on-busy-update=restart的缩写
        -s, --signal <signal>                    Specify the signal to send when using --on-busy-update=signal
    
    Debugging options:
            --log-file <path>    Write debugging messages to file in JSON format (use for bug reports)
            --print-events       Print events that trigger actions
        -v, --verbose            Print debugging messages (-v, -vv, -vvv, -vvvv; use -vvv for bug reports)
    

    go语言为例

    启动一个gin的项目,在项目根目录执行

    watchexec.exe --restart  --exts go --no-project-ignore   go run .\main.go
    

    其中 --no-project-ignore是因为默认情况下会导入gitignore,因为我的项目里面用到了swag,gitignore默认忽略了swagger生成的文件,但是我希望swag文档的变动也能重启服务器,所以需要加上--no-project-ignore不导入gitignore文件。

    相关文章

      网友评论

          本文标题:文件变动监听神器watchexec的使用指南(go文件变动重启服

          本文链接:https://www.haomeiwen.com/subject/huswxdtx.html