'use strict';

/**
 * Created by godsong on 16/8/8.
 */
var spawn = require('child_process').spawn;
var chalk = require('chalk');

var version = require('../../package.json').version;
exports.run = function () {
  var npm = spawn(process.platform === 'win32' ? 'npm.cmd' : 'npm', ['show', 'weex-devtool', 'version']);
  npm.stdout.on('data', function (data) {
    var latestVersion = data.toString().trim();
    if (getVersionValue(version) < getVersionValue(latestVersion)) {
      console.log(chalk.yellow('\nNew version[' + latestVersion + '] of Weex debugger detected! Please update.( weex update weex-devtool@latest )'));
    }
  });
};
function getVersionValue(version) {
  var sum = 0;
  version.split('.').filter(function (n) {
    return isFinite(n);
  }).forEach(function (n, i, arr) {
    sum += Math.pow(10, (arr.length - i - 1) * 4) * n;
  });
  return sum;
}