<commit_before><commit_msg>Added auto update script<commit_after>/* jshint node: true */
'use strict';

let packageDependencies = require('../package.json').dependencies;
let semver = require('semver');
let RegClient = require('npm-registry-client');
let RSVP = require('rsvp');
let npm = require('ember-cli/lib/utilities/npm');

let packages = Object.keys(packageDependencies)
  .filter((packageName) => /^d3\-/.test(packageName))
  .map((packageName) => ({ name: packageName, version: packageDependencies[packageName] }));

let client = new RegClient();

let npmOptions = {
  loglevel: 'error',
  color: 'always',
  // by default, do install peoples optional deps
  'optional': true,
  'save': true,
  'save-exact': true
};

let updates = [];
let updatePromises = [];

packages.forEach((pkg) => {
  let uri = `https://registry.npmjs.org/${pkg.name}`;
  let params = { timeout: 1000 };

  updatePromises.push(new RSVP.Promise(function(resolve, reject) {
    client.get(uri, params, function(error, data, raw, res) {
      if (!error) {
        let newVersions = Object.keys(data.versions).filter((version) => semver.gtr(version, pkg.version));
        let updateVersion = newVersions.splice(-1);
        if (newVersions.length > 0) {
          console.log(`Udpate ${pkg.name} ${pkg.version} => ${updateVersion}`);
          updates.push({ name: pkg.name, oldVersion: pkg.version, newVersion: updateVersion });

          npm('install', [`${pkg.name}@${updateVersion}`], npmOptions)
            .then(resolve, reject);
        } else {
          // console.log(`${pkg.name} UP TO DATE`);
        }
      } else {
        reject(error);
      }
    });
  }));
});

RSVP.all(updatePromises).then((updates) => {
  console.log('DONE');
  console.log(updates);
}).catch((err) => {
  console.log('FAILED');
  console.error(err);
});

// console.log(updates);

{'fraction_non_alphanumeric': 0.18402590393955748, 'fraction_numerical': 0.0037776578521316784, 'mean_word_length': 3.5665024630541873, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 11, 'https://': 1, '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': '1854753', 'n_tokens_mistral': 606, 'n_tokens_neox': 587, 'n_words': 121}