<commit_before>/*
 * grunt-reporter
 * https://github.com/bmac/grunt-reporter
 *
 * Copyright (c) 2013 bmac
 * Licensed under the MIT license.
 */

'use strict';

var prompt = require('prompt'),
    Q = require('q'),
    fs = require('fs'),
    _ = require('lodash'),
    request = require('superagent'),
    globule = require('globule'),
    readJson = require('read-package-json');



// attempts to read the token from the file
var readTokenFromFile = function(fileName) {
  return Q.ninvoke(fs, 'readFile', fileName, 'utf8');
};

var writeTokenToFile = function(token, fileName) {
  return Q.ninvoke(fs, 'writeFile', fileName, token).then(function() {
    return token;
  });
};

var requestNewToken = function(options) {
  var schema = {
    properties: {
      username: {
        pattern: /^[a-zA-Z\s\-]+$/,
        message: 'Name must be only letters, spaces, or dashes',
        required: true
      },
      password: {
        hidden: true
      }
    }
  };
  prompt.start();
  return Q.ninvoke(prompt, 'get', schema).then(function(arg) {
    return Q.ninvoke(request.post(options.tokenRequestUri)
        .send({
          username: arg.username,
          password: <PASSWORD>,
          // client_id: options.client_id,
          // // Not Really a secret but the passport library requires this param
          // client_secret: options.client_secret,
          grant_type: 'password'
        }), 'end');
  }).then(function(arg) {
    if (arg.status !== 200) {
      throw arg.text;
    }
    return arg.body.token;
  });
};



var getToken = function(options) {
  return readTokenFromFile(options.tokenFile).then(null, function() {
    // Error case, request a new token then save it to the file
    return requestNewToken(options).then(function(token) {
      return writeTokenToFile(token, options.tokenFile);
    });
  });
};


var getTestStats = function(testOutputFile) {
  return Q.spread([
    Q.ninvoke(fs, 'readFile', testOutputFile, 'utf8'),
    Q.nfcall(readJson, 'package.json')
  ], function(output, pkg) {
    var jsonReport = JSON.parse(output);
    var stats = jsonReport.stats;
    stats.testsPassed = jsonReport.passes;
    stats.name = pkg.name;
    return stats;
  });
};

var getFiles = function(pattern) {
  var files = globule.find([pattern, '!node_modules/**/*.js']);
  var promises = files.map(function(fileName) {
    return Q.ninvoke(fs, 'readFile', fileName, 'utf8').then(function(fileText) {
      return {
        fileName: fileName,
        fileText: fileText
      };
    });
  });
  return Q.all(promises);
};

module.exports = function(grunt) {

  // Please see the Grunt documentation for more information regarding task
  // creation: http://gruntjs.com/creating-tasks

  grunt.registerMultiTask('reporter', 'The best Grunt plugin ever.', function() {

    var done = this.async();

    // Merge task-specific and/or target-specific options with these defaults.
    var options = this.options({
      tokenFile: '.accessToken',
      testOutputFile: 'out.json',
      tokenRequestUri: 'http://www.tophat.io/api/v1/oauth/token',
      testReportUri: 'http://www.tophat.io/api/v1/testSnapshot',
      uploadFiles: '**/*.js'
    });

    // Find local auth token.
    // If it does not exist request username and password
    // request and store new auth token.

    var token = getToken(options);<commit_msg>Update urls to point to the correct resources<commit_after>/*
 * grunt-reporter
 * https://github.com/bmac/grunt-reporter
 *
 * Copyright (c) 2013 bmac
 * Licensed under the MIT license.
 */

'use strict';

var prompt = require('prompt'),
    Q = require('q'),
    fs = require('fs'),
    _ = require('lodash'),
    request = require('superagent'),
    globule = require('globule'),
    readJson = require('read-package-json');



// attempts to read the token from the file
var readTokenFromFile = function(fileName) {
  return Q.ninvoke(fs, 'readFile', fileName, 'utf8');
};

var writeTokenToFile = function(token, fileName) {
  return Q.ninvoke(fs, 'writeFile', fileName, token).then(function() {
    return token;
  });
};

var requestNewToken = function(options) {
  var schema = {
    properties: {
      username: {
        pattern: /^[a-zA-Z\s\-]+$/,
        message: 'Name must be only letters, spaces, or dashes',
        required: true
      },
      password: {
        hidden: true
      }
    }
  };
  prompt.start();
  return Q.ninvoke(prompt, 'get', schema).then(function(arg) {
    return Q.ninvoke(request.post(options.tokenRequestUri)
        .send({
          username: arg.username,
          password: <PASSWORD>,
          // client_id: options.client_id,
          // // Not Really a secret but the passport library requires this param
          // client_secret: options.client_secret,
          grant_type: 'password'
        }), 'end');
  }).then(function(arg) {
    if (arg.status !== 200) {
      throw arg.text;
    }
    return arg.body.token;
  });
};



var getToken = function(options) {
  return readTokenFromFile(options.tokenFile).then(null, function() {
    // Error case, request a new token then save it to the file
    return requestNewToken(options).then(function(token) {
      return writeTokenToFile(token, options.tokenFile);
    });
  });
};


var getTestStats = function(testOutputFile) {
  return Q.spread([
    Q.ninvoke(fs, 'readFile', testOutputFile, 'utf8'),
    Q.nfcall(readJson, 'package.json')
  ], function(output, pkg) {
    var jsonReport = JSON.parse(output);
    var stats = jsonReport.stats;
    stats.testsPassed = jsonReport.passes;
    stats.name = pkg.name;
    return stats;
  });
};

var getFiles = function(pattern) {
  var files = globule.find([pattern, '!node_modules/**/*.js']);
  var promises = files.map(function(fileName) {
    return Q.ninvoke(fs, 'readFile', fileName, 'utf8').then(function(fileText) {
      return {
        fileName: fileName,
        fileText: fileText
      };
    });
  });
  return Q.all(promises);
};

module.exports = function(grunt) {

  // Please see the Grunt documentation for more information regarding task
  // creation: http://gruntjs.com/creating-tasks

  grunt.registerMultiTask('reporter', 'The best Grunt plugin ever.', function() {

    var done = this.async();

    // Merge task-specific and/or target-specific options with these defaults.
    var options = this.options({
      tokenFile: '.accessToken',
      testOutputFile: 'out.json',
      tokenRequestUri: 'http://www.tophat.io/api/v2/auth',
      testReportUri: 'http://www.tophat.io/api/v2/test_snapshot',
      uploadFiles: '**/*.js'
    });

    // Find local auth token.
    // If it does not exist request username and password
    // request and store new auth token.

    var token = getToken(options);
{'fraction_non_alphanumeric': 0.14338671411625148, 'fraction_numerical': 0.0035587188612099642, 'mean_word_length': 2.9056166763173135, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 2, 'lorem ipsum': 0, 'www.': 4, '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': '2834932', 'n_tokens_mistral': 2113, 'n_tokens_neox': 2071, 'n_words': 512}