<commit_before>'use strict';

const chalk = require('chalk');
let logger = require('heimdalljs-logger')('ember-cli:watcher');
const CoreObject = require('core-object');

let Watcher = CoreObject.extend({
  verbose: true,

  init() {
    this._super.apply(this, arguments);

    let options = this.buildOptions();

    logger.info('initialize %o', options);

    this.watcher = this.watcher || this.constructWatcher(options);

    this.watcher.on('error', this.didError.bind(this));
    this.watcher.on('change', this.didChange.bind(this));
  },

  constructWatcher(options) {
    return new (require('ember-cli-broccoli-sane-watcher'))(this.builder, options);
  },

  didError(error) {
    logger.info('didError %o', error);
    this.ui.writeError(error);
    this.analytics.trackError({
      description: error && error.message,
    });
  },

  then() {
    return this.watcher.then.apply(this.watcher, arguments);
  },

  didChange(results) {
    logger.info('didChange %o', results);

    let totalTime = results.totalTime / 1e6;

    this.ui.writeLine('');
    this.ui.writeLine(chalk.green(`Build successful - ${Math.round(totalTime)}ms.`));

    this.analytics.track({
      name: 'ember rebuild',
      message: `broccoli rebuild time: ${totalTime}ms`,
    });

    /*
     * We use the `rebuild` category in our analytics setup for both builds
     * and rebuilds. This is a bit confusing, but the actual thing we
     * delineate on in the reports is the `variable` value below. This is
     * used both here and in `lib/tasks/build.js`.
     */
    this.analytics.trackTiming({
      category: 'rebuild',
      variable: 'rebuild time',
      label: 'broccoli rebuild time',
      value: Number(totalTime),
    });
  },

  on() {
    this.watcher.on.apply(this.watcher, arguments);
  },

  off() {
    this.watcher.off.apply(this.watcher, arguments);
  },

  buildOptions() {
    let watcher = this.options && this.options.watcher;

    if (watcher && ['polling', 'watchman', 'node', 'events'].indexOf(watcher) === -1) {
      throw new Error(`Unknown watcher type --watcher=[polling|watchman|node] but was: ${watcher}`);
    }

    return {
      verbose: this.verbose,
      poll: watcher === 'polling',
      watchman: watcher === 'watchman' || watcher === 'events',
      node: watcher === 'node',
    };
  },
});<commit_msg>models/watcher: Convert to ES6 class extending CoreObject<commit_after>'use strict';

const chalk = require('chalk');
let logger = require('heimdalljs-logger')('ember-cli:watcher');
const CoreObject = require('core-object');

class Watcher extends CoreObject {
  constructor(_options) {
    super(_options);

    this.verbose = true;

    let options = this.buildOptions();

    logger.info('initialize %o', options);

    this.watcher = this.watcher || this.constructWatcher(options);

    this.watcher.on('error', this.didError.bind(this));
    this.watcher.on('change', this.didChange.bind(this));
  }

  constructWatcher(options) {
    return new (require('ember-cli-broccoli-sane-watcher'))(this.builder, options);
  }

  didError(error) {
    logger.info('didError %o', error);
    this.ui.writeError(error);
    this.analytics.trackError({
      description: error && error.message,
    });
  }

  then() {
    return this.watcher.then.apply(this.watcher, arguments);
  }

  didChange(results) {
    logger.info('didChange %o', results);

    let totalTime = results.totalTime / 1e6;

    this.ui.writeLine('');
    this.ui.writeLine(chalk.green(`Build successful - ${Math.round(totalTime)}ms.`));

    this.analytics.track({
      name: 'ember rebuild',
      message: `broccoli rebuild time: ${totalTime}ms`,
    });

    /*
     * We use the `rebuild` category in our analytics setup for both builds
     * and rebuilds. This is a bit confusing, but the actual thing we
     * delineate on in the reports is the `variable` value below. This is
     * used both here and in `lib/tasks/build.js`.
     */
    this.analytics.trackTiming({
      category: 'rebuild',
      variable: 'rebuild time',
      label: 'broccoli rebuild time',
      value: Number(totalTime),
    });
  }

  on() {
    this.watcher.on.apply(this.watcher, arguments);
  }

  off() {
    this.watcher.off.apply(this.watcher, arguments);
  }

  buildOptions() {
    let watcher = this.options && this.options.watcher;

    if (watcher && ['polling', 'watchman', 'node', 'events'].indexOf(watcher) === -1) {
      throw new Error(`Unknown watcher type --watcher=[polling|watchman|node] but was: ${watcher}`);
    }

    return {
      verbose: this.verbose,
      poll: watcher === 'polling',
      watchman: watcher === 'watchman' || watcher === 'events',
      node: watcher === 'node',
    };
  }
}
{'fraction_non_alphanumeric': 0.1582962492053401, 'fraction_numerical': 0.0014833651197287562, 'mean_word_length': 3.2484248424842486, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '2603172', 'n_tokens_mistral': 1542, 'n_tokens_neox': 1484, 'n_words': 338}