Help us understand the problem. What is going on with this article?

タスクの有無を確認する

More than 5 years have passed since last update.

grunt 内で使用されてるチェックを真似て持ってきた。

Gruntfile.js
  var path = require('path');

  /**
   * タスクの有無を確認します。
   *
   * @param   {…String} name タスク名
   * @returns {Boolean} 存在すれば true を返します。
   */
  function existsTasks()
  {
    var tasksdir
      , i = arguments.length
      , result = i > 0
      , root = path.resolve('node_modules')
      ;

    while (i--) {
      tasksdir = path.join(root, arguments[i], 'tasks');
      result = result && grunt.file.exists(tasksdir);
    }
    return result;
  }

▼使用例(タスクが存在する場合のみ、設定が行われる)

※LiveReload 機能は grunt-contrib-watch のオプションに統合されたとのこと。下の例は参考程度で見てください。

Gruntfile.js
  var config = {}
    , path = require('path');
    , lrSnippet
    , folderMount
    ;

  if (existsTasks('grunt-contrib-livereload', 'grunt-contrib-connect', 'grunt-regarde')) {

    lrSnippet = require('grunt-contrib-livereload/lib/utils').livereloadSnippet;
    folderMount = function folderMount(connect, point) {
      return connect.static(path.resolve(point));
    };

    config['connect'] = {
      livereload: {
        options: {
            port: 9001
          , middleware: function(connect, options) {
              return [lrSnippet, folderMount(connect, '.')]
            }
        }
      }
    };
    config['regarde'] = {
      fred: {
          files: []
        , tasks: ['livereload']
      }
    };

    grunt.task.loadNpmTasks('grunt-contrib-livereload');
    grunt.task.loadNpmTasks('grunt-contrib-connect');
    grunt.task.loadNpmTasks('grunt-regarde');
    grunt.task.registerTask('autoreload', ['livereload-start', 'connect', 'regarde']);
  }

  grunt.config.init(config);
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away