text: /*jshint node:true*/
  'use strict';
  
  var _ = require('lodash');
  var path = require('path');
  require('js-yaml');
  
  var CLEAN_DIRS = [
    'components',
    'dist',
    'build'
  ];
  
  var ASSET_HOST = process.env.ASSET_HOST;
  
  var BROWSERS = require('./node_modules/browsers-yaml/browsers');
  var browsers = _.filter(BROWSERS, function(browser) {
    if (browser.browserName === 'internet explorer' && browser.version === '8') {
      return false;
    }
    return true;
  });
  
  module.exports = function(grunt) {
    var DIST_OUTPUT_PATH = path.join(__dirname, 'dist');
    var COMPONENT_BIN_PATH = path.join(__dirname,
                                       'node_modules/component/bin/component');
  
    var UGLIFY_BIN_PATH = path.join(__dirname,
                                    'node_modules/uglify-js/bin/uglifyjs');
  
    function getSemver() {
      return require('./component').version;
    }
  
    function getName() {
      return require('./component').name;
    }
  
    function getSemverURLPath() {
      return ASSET_HOST + '/widgets/rich-text-editor/v' + getSemver();
    }
  
    grunt.loadNpmTasks('grunt-contrib-clean');
    grunt.loadNpmTasks('grunt-contrib-connect');
    grunt.loadNpmTasks('grunt-saucelabs');
  
    grunt.initConfig({
      clean: CLEAN_DIRS,
      connect: {
        server: {
          options: {
            base: "",
            port: 9999
          }
        }
      },
      'saucelabs-mocha': {
        all: {
          options: {
            urls: ["http://localhost:9999/test/index.html"],
              tunnelTimeout: 5,
            build: process.env.TRAVIS_JOB_ID,
            concurrency: 8,
            browsers: browsers,
            testname: "mocha tests",
            tags: ["master"]
          }
        }
      },
    });
  
    grunt.registerTask('test', ['build', 'connect', 'saucelabs-mocha']);
  
    // Jshint default task
    grunt.registerTask('build', [
      'clean',
      'component:install',
      'component:build:dev'
    ]);
  
    grunt.registerTask('build:prod', [
      'clean',
      'component:install',
      'component:build:prod',
      'minify'
    ]);
  
    grunt.registerTask('component:install', function() {
      var done = this.async();
  
      var install = {
        cmd: COMPONENT_BIN_PATH,
        args: [ 'install', '-d' ]
      };
  
      // Build the component.
  
      grunt.log.writeln('component install - start');
      grunt.util.spawn(install, function(err) {
        if (err) {
          return done(err);
        }
  
        grunt.log.writeln('component install - done');
  
        done();
      });
    });
  
    grunt.registerTask('component:build:dev', function() {
      var done = this.async();
  
      var args = ['build', '-c', '-d'];
  
      var build = {
        cmd: COMPONENT_BIN_PATH,
        args: args
      };
  
      // Build the component.
      grunt.util.spawn(build, function(err) {
        if (err) {
          return done(err);
        }
  
        done();
      });
    });
  
    grunt.registerTask('component:build:prod', function() {
      var done = this.async();
  
      // To prevent having to write a custom build.js file, we take advantage
      // of their lack of escaping in order to build up the widgets namespace.
  
      // Yeah, this is hacky.
      // In the following code string's context, `this` is the window object.
      var exportName = '"];' +
        'this.goinstant = this.goinstant || {};' +
        'this.goinstant.widgets = this.goinstant.widgets || {};' +
        'this.goinstant.widgets["RichTextEditor';
  
      var args = [
        'build', '-c',
        '-n', getName(),
        '-s', exportName,
        '-o', DIST_OUTPUT_PATH
      ];
  
      var build = {
        cmd: COMPONENT_BIN_PATH,
        args: args
      };
  
      // Build the component.
      grunt.util.spawn(build, function(err) {
        if (err) {
          return done(err);
        }
  
        done();
      });
    });
  
    grunt.registerTask('minify', function() {
      var done = this.async();
  
      var componentDir = DIST_OUTPUT_PATH;
  
      var jsFilePath = path.join(componentDir, getName() + '.js');
      var minJsFilePath = path.join(componentDir, getName() + '.min.js');
  
      var uglify = {
        cmd: UGLIFY_BIN_PATH,
        args: [
          '-o',
          minJsFilePath,
          jsFilePath
        ]
      };
  
      grunt.util.spawn(uglify, done);
    });
  };
  