使用Gruntjs与grunt-nodemon、watch和jshint

5

我正在尝试使用这三个插件运行GruntJS,以便它可以监视更改并首先:对文件进行lint,然后重新加载express服务器。 我在下面的配置中遇到的问题是,如果jshint对文件进行了lint,则nodemon不会运行,反之亦然。

// Gruntfile.js

// our wrapper function (required by grunt and its plugins)
// all configuration goes inside this function
module.exports = function(grunt) {

  // ===========================================================================
  // CONFIGURE GRUNT ===========================================================
  // ===========================================================================
  grunt.initConfig({

    // get the configuration info from package.json ----------------------------
    // this way we can use things like name and version (pkg.name)
    pkg: grunt.file.readJSON('package.json'),

    // all of our configuration will go here

    // configure jshint to validate js files -----------------------------------
  jshint: {
      options: {
        reporter: require('jshint-stylish') // use jshint-stylish to make our errors look and read good
      },

    // when this task is run, lint the Gruntfile and all js files in src
      build: ['Grunfile.js', 'routes/*.js']
    },

    watch: {

      // for scripts, run jshint and uglify
      scripts: {
        files: 'routes/*.js',
        tasks: ['jshint']
      }
    },

    concurrent: {
      dev: {

        tasks: ['jshint', 'nodemon', 'watch'],
        options: {
          logConcurrentOutput: true
        }
      }
    }, // concurrent

    nodemon: {
      dev: {
        script: './server.js'
      }
    } // nodemon


  });

  // ===========================================================================
  // LOAD GRUNT PLUGINS ========================================================
  // ===========================================================================
  // we can only load these if they are in our package.json
  // make sure you have run npm install so our app can find these
  grunt.loadNpmTasks('grunt-contrib-jshint');
  grunt.loadNpmTasks('grunt-contrib-watch');
  grunt.loadNpmTasks('grunt-nodemon');


      grunt.registerTask('default', '', function() {
    var taskList = [
        'jshint',
        'nodemon',
        'watch'
    ];
    grunt.task.run(taskList);
});

};

编辑(澄清):

第一次运行grunt时,jshint对文件进行了代码检查,然后nodemon启动,之后jshint就不再进行代码检查了。

输出:

grunt
Running "default" task

Running "jshint:build" (jshint) task

✔︎ No problems


Running "nodemon:dev" (nodemon) task
[nodemon] v1.2.1
[nodemon] to restart at any time, enter `rs`
[nodemon] watching: *.*
[nodemon] starting `node ./server.js`
Express server listening on port 3000

你如何运行gruntjs? - Kevin Labécot
只需在控制台中输入grunt。 - mdv
我记得在使用Grunt时遇到过类似的问题,但是我从未解决过。你可以看看 gulp,它处理并发更好(默认情况下所有内容都并行运行)。 - jgillich
很遗憾听到这个消息,你可以在一个终端中运行nodemon,在另一个终端中运行grunt,但如果我们能够使用一个工具/命令运行所有好东西就太酷了。 - mdv
2个回答

4

真是个傻瓜错误。我没有加载grunt-concurrent,只需安装grunt-concurrent并将其添加到Kelz的函数中,现在它可以正常运行:)。 谢谢大家。

最终代码:

// Gruntfile.js

// our wrapper function (required by grunt and its plugins)
// all configuration goes inside this function
module.exports = function(grunt) {
  // ===========================================================================
  // CONFIGURE GRUNT ===========================================================
  // ===========================================================================
  grunt.initConfig({

    // get the configuration info from package.json ----------------------------
    // this way we can use things like name and version (pkg.name)
    pkg: grunt.file.readJSON('package.json'),

    // all of our configuration will go here

    // configure jshint to validate js files -----------------------------------
    jshint: {
      options: {
        reporter: require('jshint-stylish') // use jshint-stylish to make our errors look and read good
      },

      // when this task is run, lint the Gruntfile and all js files in src
      build: ['Grunfile.js', 'routes/*.js']
    },

    watch: {
      // for scripts, run jshint and uglify
      scripts: {
        files: 'routes/*.js',
        tasks: ['jshint']
      }
    }, // watch

    nodemon: {
      dev: {
        script: './server.js'
      }
    }, // nodemon

    concurrent: {
      dev: {
        tasks: ['jshint', 'nodemon', 'watch'],
        options: {
          logConcurrentOutput: true
        }
      }
    } // concurrent
  });

  // ===========================================================================
  // LOAD GRUNT PLUGINS ========================================================
  // ===========================================================================
  // we can only load these if they are in our package.json
  // make sure you have run npm install so our app can find these
  grunt.loadNpmTasks('grunt-concurrent');
  grunt.loadNpmTasks('grunt-contrib-jshint');
  grunt.loadNpmTasks('grunt-contrib-watch');
  grunt.loadNpmTasks('grunt-nodemon');

  grunt.registerTask('default', '', function() {
    var taskList = [
        'concurrent',
        'jshint',
        'nodemon',
        'watch'
    ];
    grunt.task.run(taskList);
  });
};

3
请尝试将其作为函数任务运行:
grunt.registerTask('default', '', function() {
    var taskList = [
        'jshint',
        'nodemon',
        'watch'
    ];
    grunt.task.run(taskList);
});

编辑:我使用的另一种方法是通过使用grunt-express-server来实现自动重新运行包括express在内的任务,适用于你的设置:

module.exports = function(grunt) {
    grunt.initConfig({
        pkg: grunt.file.readJSON("package.json"),
        watch: {
            express: {
                files: ['routes/*.js'],
                tasks: ['jshint', 'express:dev'],
                options: {
                    spawn: false
                }
            }
        },
        express: {
            dev: {
                options: {
                    script: 'app.js',
                }
            }
        },
        jshint: {
            options: {
                node: true
            },
            all: {
                src: ['routes/*.js']
            }
        }
    });

    grunt.loadNpmTasks('grunt-express-server');
    grunt.loadNpmTasks('grunt-contrib-jshint');

    grunt.registerTask('default', '', function() {
        var taskList = [
            'jshint',
            'express:dev',
            'watch'
        ];
        grunt.task.run(taskList);
    });
};

相同的结果,nodemon 启动了服务器,但 jshint 没有进行代码检查 :( - mdv
从你的编辑来看,它运行正常,不是吗?或者说当你保存正在监视的文件时,它没有重新启动并运行jshint? - Kelz
第一次运行“grunt”时,它会对文件进行代码检查,然后启动nodemon。 当nodemon启动服务器后,如果我修改一个.js文件并保存它,nodemon会重新加载服务器,但jshint不再进行代码检查。 - mdv
我已经解决了你的问题,但没有使用nodemon,而是使用grunt-express-server - 如果你不一定要使用nodemon,我可以更新我的答案来展示这个。 - Kelz

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接