8
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

タスクの有無を確認する

Last updated at Posted at 2013-08-07

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);
8
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?