<commit_before>/*
 * grunt-contrib-watch
 * http://gruntjs.com/
 *
 * Copyright (c) 2013 "Cowboy" <NAME>, contributors
 * Licensed under the MIT license.
 */

'use strict';

var path = require('path');
var EE = require('events').EventEmitter;
var util = require('util');

// Track which targets to run after reload
var reloadTargets = [];

module.exports = function(grunt) {

  var TaskRun = require('./taskrun')(grunt);
  var livereload = require('./livereload')(grunt);

  function Runner() {
    EE.call(this);
    // Name of the task
    this.name = 'watch';
    // Options for the runner
    this.options = {};
    // Function to close the task
    this.done = function() {};
    // Targets available to task run
    this.targets = Object.create(null);
    // The queue of task runs
    this.queue = [];
    // Whether we're actively running tasks
    this.running = false;
    // If a nospawn task has ran (and needs the watch to restart)
    this.nospawn = false;
    // Set to true before run() to reload task
    this.reload = false;
    // For re-queuing arguments with the task that originally ran this
    this.nameArgs = [];
    // A list of changed files to feed to task runs for livereload
    this.changedFiles = Object.create(null);
  }
  util.inherits(Runner, EE);

  // Init a task for taskrun
  Runner.prototype.init = function init(name, defaults, done) {
    var self = this;

    self.name = name || grunt.task.current.name || 'watch';
    self.options = self._options(grunt.config([self.name, 'options']) || {}, defaults || {});
    self.reload = false;
    self.nameArgs = (grunt.task.current.nameArgs) ? grunt.task.current.nameArgs : self.name;

    // Function to call when closing the task
    self.done = done || grunt.task.current.async();

    // If a default livereload server for all targets
    // Use task level unless target level overrides
    var taskLRConfig = grunt.config([self.name, 'options', 'livereload']);
    if (self.options.target && taskLRConfig) {
      var targetLRConfig = grunt.config([self.name, self.options.target, 'options', 'livereload']);
      if (targetLRConfig) {
        // Dont use task level as target level will be used instead
        taskLRConfig = false;
      }
    }
    if (taskLRConfig) {
      self.livereload = livereload(taskLRConfig);
    }

    if (self.running) {
      // If previously running, complete the last run
      self.complete();
    } else if (reloadTargets.length > 0) {
      // If not previously running but has items in the queue, needs run
      self.queue = reloadTargets;
      reloadTargets = [];
      self.run();
    }

    // Return the targets normalized
    var targets = self._getTargets(self.name);

    // Check whether target's tasks should run at start w/ atBegin option
    if (self.running === false) {
      self.queue = targets.filter(function(tr) {
        return tr.options.atBegin === true && tr.tasks.length > 0;
      }).map(function(tr) {
        return tr.name;
      });
      if (self.queue.length > 0) {
        self.run();
      }
    }

    return targets;
  };

  // Normalize targets from config<commit_msg>Fix running tasks after a gruntfile is reloaded. Fixes GH-184.<commit_after>/*
 * grunt-contrib-watch
 * http://gruntjs.com/
 *
 * Copyright (c) 2013 "Cowboy" <NAME>, contributors
 * Licensed under the MIT license.
 */

'use strict';

var path = require('path');
var EE = require('events').EventEmitter;
var util = require('util');

// Track which targets to run after reload
var reloadTargets = [];

module.exports = function(grunt) {

  var TaskRun = require('./taskrun')(grunt);
  var livereload = require('./livereload')(grunt);

  function Runner() {
    EE.call(this);
    // Name of the task
    this.name = 'watch';
    // Options for the runner
    this.options = {};
    // Function to close the task
    this.done = function() {};
    // Targets available to task run
    this.targets = Object.create(null);
    // The queue of task runs
    this.queue = [];
    // Whether we're actively running tasks
    this.running = false;
    // If a nospawn task has ran (and needs the watch to restart)
    this.nospawn = false;
    // Set to true before run() to reload task
    this.reload = false;
    // For re-queuing arguments with the task that originally ran this
    this.nameArgs = [];
    // A list of changed files to feed to task runs for livereload
    this.changedFiles = Object.create(null);
  }
  util.inherits(Runner, EE);

  // Init a task for taskrun
  Runner.prototype.init = function init(name, defaults, done) {
    var self = this;

    self.name = name || grunt.task.current.name || 'watch';
    self.options = self._options(grunt.config([self.name, 'options']) || {}, defaults || {});
    self.reload = false;
    self.nameArgs = (grunt.task.current.nameArgs) ? grunt.task.current.nameArgs : self.name;

    // Function to call when closing the task
    self.done = done || grunt.task.current.async();

    // If a default livereload server for all targets
    // Use task level unless target level overrides
    var taskLRConfig = grunt.config([self.name, 'options', 'livereload']);
    if (self.options.target && taskLRConfig) {
      var targetLRConfig = grunt.config([self.name, self.options.target, 'options', 'livereload']);
      if (targetLRConfig) {
        // Dont use task level as target level will be used instead
        taskLRConfig = false;
      }
    }
    if (taskLRConfig) {
      self.livereload = livereload(taskLRConfig);
    }

    // Return the targets normalized
    var targets = self._getTargets(self.name);

    if (self.running) {
      // If previously running, complete the last run
      self.complete();
    } else if (reloadTargets.length > 0) {
      // If not previously running but has items in the queue, needs run
      self.queue = reloadTargets;
      reloadTargets = [];
      self.run();
    } else {
      // Check whether target's tasks should run at start w/ atBegin option
      self.queue = targets.filter(function(tr) {
        return tr.options.atBegin === true && tr.tasks.length > 0;
      }).map(function(tr) {
        return tr.name;
      });
      if (self.queue.length > 0) {
        self.run();
      }
    }

    return targets;
  };

  // Normalize targets from config
{'fraction_non_alphanumeric': 0.1272842841252185, 'fraction_numerical': 0.0027014142698236136, 'mean_word_length': 2.642361111111111, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '777831', 'n_tokens_mistral': 1924, 'n_tokens_neox': 1833, 'n_words': 586}