node.js - 与 Grunt 同时运行 `watch` 和 `nodemon`

标签 node.js gruntjs watch jshint nodemon

我刚开始使用 Grunt,想运行 grunt-contrib-watch [GitHub page]每次修改文件时检查我的 JavaScript(使用 grunt-contrib-jshint [GitHub page] )并运行 grunt-nodemon [GitHub page]同样,同时使用 grunt-concurrent [GitHub page] .

据我了解(我显然不了解)我的 Gruntfile 应该:

  1. 默认运行并发
  2. 并发 运行watch
  3. watch 每次修改文件时运行jshint

Gruntfile.js

module.exports = function (grunt) {
    grunt.initConfig({
        concurrent: {
            dev: [
                'watch'
            ],
            options: {
                logConcurrentOutput: true
            }
        },
        jshint: {
            server: [
                '**/*.js',
                '!node_modules/**/*.js'
            ],
            options: {
                node: true
            }
        },
        watch: {
            all: [
                '**/*/.js',
                '!node_modules/**/*.js'
            ],
            tasks: [
                'jshint'
            ]
        }
    });

    grunt.loadNpmTasks('grunt-concurrent');
    grunt.loadNpmTasks('grunt-contrib-jshint');
    grunt.loadNpmTasks('grunt-contrib-watch');

    grunt.registerTask('default', [
        'concurrent:dev'/*,
        'jshint',
        'watch'*/
    ]);
};

    grunt.loadNpmTasks('grunt-concurrent');
    grunt.loadNpmTasks('grunt-contrib-jshint');
    grunt.loadNpmTasks('grunt-contrib-watch');

    grunt.registerTask('default', [
        'concurrent:dev'
    ]);
};

注意我还没有将 grunt-nodemon 添加到组合中。

看起来 concurrent 正在运行 watch 但当我修改文件时,它似乎 jshint 没有运行。我当然没有在终端中得到任何输出(我认为 logConcurrentOutput: true 会这样做)。

这是我在终端中得到的输出:

Running "concurrent:dev" (concurrent) task
Running "watch" task
Waiting...    


Done, without errors.

我还想在第一次运行 default 任务(以及修改文件时)时运行 jshint

任何人都可以阐明我哪里出错了吗?

谢谢!

最佳答案

如果没有找到要“监视”的文件,则监视 任务退出;根据 this issue .

为了轻松地告诉 watchjshint 任务监视相同的文件,我使用 Grunt 的模板引擎来引用相同的 Array 文件作为jshint 任务。

然后我将 jshint 添加到要运行的并发任务列表中,这样它会在最初运行并在我修改文件时运行(使用 watch)。

这是我的工作 Gruntfile:

module.exports = function (grunt) {
    grunt.initConfig({
        concurrent: {
            dev: [
                'jshint',
                'watch'
            ],
            options: {
                logConcurrentOutput: true
            }
        },
        jshint: {
            server: [
                '**/*.js',
                '!node_modules/**/*.js'
            ],
            options: {
                node: true
            }
        },
        watch: {
            files: '<%= jshint.server %>',
            tasks: [
                'jshint'
            ]
        }
    });

    grunt.loadNpmTasks('grunt-concurrent');
    grunt.loadNpmTasks('grunt-contrib-jshint');
    grunt.loadNpmTasks('grunt-contrib-watch');

    grunt.registerTask('default', [
        'concurrent'
    ]);
};

关于node.js - 与 Grunt 同时运行 `watch` 和 `nodemon`,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21479322/

相关文章:

testing - 使用 Grunt 运行时指定 QUnit 模块

gruntjs - 向 grunt 供应商添加样式 css 文件

javascript - 有没有办法在串联文件之间添加回车符?

php - 像 Facebook 这样使用 PHP 的社交网站如何在不使服务器重载的情况下让脚本检查新闻源?

node.js - joi模块中定义整数变量的最小和最大长度的函数是什么

javascript - Passport.js 不适用于 sails 套接字环境

javascript - AngularJS:带去抖动的 $watch

java - Java中如何观察完整文件系统的变化?

javascript - app.render [TypeError : Cannot set property 'content' of undefined] Using Express with Gaikan in Node. js

javascript - 如何在 .cmd 或 .bat 文件中运行 Node 实用程序