/**
 * @file check-version
 * @author xiaowu
 * @email fe.xiaowu@gmail.com
 */

'use strict';

import request from 'request';

import config from '../config.json';
import header from '../header.json';

let rule = config.rule;

let resPromise = [];

rule.forEach((val) => {
    let defer = new Promise((resolve, reject) => {

        // console.info('开始加载：' + val.url);

        request.get({
            headers: header,
            url: val.url
        }, (error, response, body) => {
            if (error) {
                reject({
                    errcode: 2,
                    errmsg: error,
                    name: val.name
                });
            }
            else if (response.statusCode !== 200) {
                reject({
                    errcode: 1,
                    errmsg: response.statusCode,
                    name: val.name
                });
            }
            else {
                let reg = new RegExp(val.reg);
                let match = val.match || '$1';
                match = Math.floor(match.replace('$', '')) || 0;

                try {
                    body = body.match(reg);
                    if (body && body.length > match - 1) {
                        body = body[match];
                    }

                }
                catch (e) {
                    body = null;
                }

                val.version = body;

                resolve(val);
            }
        });
    });

    resPromise.push(defer);
});

Promise.all(resPromise).then(data => {
    data.forEach(val => {
        console.log(val.name + ' => ' + val.version);
    });
}, err => {
    console.error(err);
});

@@ -524,48 +524,24 @@
     
-reject(%7B%0A                    
+val.
 errcode
-: 2,
+ = 1;
 %0A   
@@ -557,26 +557,27 @@
     
-    
+val.
 errmsg
-:
+ =
  error
-,
+;
 %0A   
@@ -593,44 +593,18 @@
     
-    name: val.name%0A                %7D
+reject(val
 );%0A 
@@ -688,48 +688,24 @@
     
-reject(%7B%0A                    
+val.
 errcode
-: 1,
+ = 2;
 %0A   
@@ -721,19 +721,20 @@
     
-    
+val.
 errmsg
-:
+ =
  res
@@ -749,17 +749,17 @@
 atusCode
-,
+;
 %0A       
@@ -771,44 +771,18 @@
     
-    name: val.name%0A                %7D
+reject(val
 );%0A 

update error message