<commit_before>async = require 'async'
chalk = require 'chalk'
defaults = require './defaults'
gulp = require 'gulp'
lodash = require 'lodash'
winston = require 'winston'

{Compiler} = require './compiler'
{serve} = require './service'


class GLP
  constructor: (task, @configurator) ->
    @configure task

  configure: (@task) ->
    @configurator.get (err, configuration) =>
      if err
        winston.err 'Configuration error: ' + err.toString()

      else
        # Partition a subset of the config if one of these names exists
        for partition in ['glp', 'config']
          subset = configuration[partition]
          configuration = subset if subset?

        # Inherit default options
        nextConfiguration = lodash.merge {}, defaults
        configuration = lodash.merge nextConfiguration, configuration

        lodash.merge configuration, configuration.tasks[@task]

        @initialize configuration

  eventWatched: (filetype) -> (options) =>
    {type, path} = options

    if @configuration.static.enabled
      relatedFiles = @outputs[filetype]
    else
      relatedFiles = [path]

    @liveReload?.changed
      body:
        files: relatedFiles

  initialize: (@configuration) ->
    watchStatus = chalk.white 'enabled' if @configuration.watch
    watchStatus ?= chalk.white 'disabled'
    winston.debug 'watch mode is ' + watchStatus

    serve.call @, @ if @configuration.static.enabled

    @run()

  run: ->
    @outputs = {}
    types = lodash.keys @configuration.files
    lodash.map types, @compile

  compile: (type) =>
    @outputs[type] = []
    allInputs = []

    gulp.task type, =>
      winston.info 'Running task: ' + chalk.white type

      outputs = @configuration.files[type]
      throw new Error 'No inputs defined for ' + type unless outputs?

      compiler = new Compiler @

      for output, inputs of outputs
        @outputs[type].push compiler.compile type, output, inputs
        allInputs = lodash.unique lodash.flatten allInputs.concat inputs

    gulp.start type

    if @configuration.watch
      stream = gulp.watch allInputs, [type]
      stream.on 'change', @eventWatched type


module.exports = {GLP}
<commit_msg>Fixed issue with reloading not occuring at the expected time.<commit_after>async = require 'async'
chalk = require 'chalk'
defaults = require './defaults'
gulp = require 'gulp'
lodash = require 'lodash'
winston = require 'winston'

{Compiler} = require './compiler'
{serve} = require './service'


class GLP
  changedFiles: []

  constructor: (task, @configurator) ->
    @configure task

  configure: (@task) ->
    @configurator.get (err, configuration) =>
      if err
        winston.err 'Configuration error: ' + err.toString()

      else
        # Partition a subset of the config if one of these names exists
        for partition in ['glp', 'config']
          subset = configuration[partition]
          configuration = subset if subset?

        # Inherit default options
        nextConfiguration = lodash.merge {}, defaults
        configuration = lodash.merge nextConfiguration, configuration

        lodash.merge configuration, configuration.tasks[@task]

        @initialize configuration

  eventWatched: (filetype) -> (options) =>
    if @configuration.static.enabled
      @changedFiles = @changedFiles.concat @outputs[filetype]
    else
      {type, path} = options
      @changedFiles.push path

  reload: ->
    console.log 'Reloading', @changedFiles
    @liveReload?.changed
      body:
        files: @changedFiles

    @changedFiles = []

  initialize: (@configuration) ->
    watchStatus = chalk.white 'enabled' if @configuration.watch
    watchStatus ?= chalk.white 'disabled'
    winston.debug 'watch mode is ' + watchStatus

    serve.call @, @ if @configuration.static.enabled

    @run()

  run: ->
    @outputs = {}
    types = lodash.keys @configuration.files
    lodash.map types, @compile

  compile: (type) =>
    @outputs[type] = []
    allInputs = []

    gulp.task type, =>
      winston.info 'Running task: ' + chalk.white type

      outputs = @configuration.files[type]
      throw new Error 'No inputs defined for ' + type unless outputs?

      compiler = new Compiler @

      for output, inputs of outputs
        @outputs[type].push compiler.compile type, output, inputs
        allInputs = lodash.unique lodash.flatten allInputs.concat inputs

    gulp.start type

    if @configuration.watch
      gulp.on 'stop', (args...) => @reload args...
      gulp.watch allInputs, [type]
        .on 'change', @eventWatched type


module.exports = {GLP}

{'fraction_non_alphanumeric': 0.10861013986013986, 'fraction_numerical': 0.0, 'mean_word_length': 2.8237259816207185, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 23, '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': '177153', 'n_tokens_mistral': 1306, 'n_tokens_neox': 1255, 'n_words': 380}