'use strict';

const
    fs = require('fs'),
    path = require('path'),
    basedir = path.dirname(path.dirname(__dirname)),
    webdir = 'www',
    excludes = [
        'node_modules',
        'static',
        'views'
    ];

function info(s) {
    console.log('[INFO] ' + s);
}

function error(s) {
    console.log('[ERROR] ' + s);
}

function checkUseStrict(jsfile) {
    //
}

function checkFile(f) {
    var
        content = fs.readFileSync(f, 'utf-8');
    if (!content.startsWith('\'use strict\';\n')) {
        error('missing use strict!');
    }
}

function scanFiles(dir) {
    var
        files = fs.readdirSync(dir);
    files.filter((f) => {
        return ! f.startsWith('.') && f.endsWith('.js');
    }).forEach((f) => {
        info(`check file ${f}...`);
        checkFile(`${dir}/${f}`);
    });
}

function scanDir(dir) {
    var
        dirs,
        pwd = `${basedir}/${dir}`;
    info(`scan dir ${pwd}...`);
    scanFiles(pwd);
    dirs = fs.readdirSync(pwd);
    dirs.filter((d) => {
        if (d.startsWith('.')) {
            return false;
        }
        var st = fs.statSync(`${pwd}/${d}`);
        if (st.isDirectory()) {
            if (dir===webdir && excludes.indexOf(d) >= 0) {
                return false;
            }
            return true;
        }
        return false;
    }).forEach((d) => {
        scanDir(`${dir}/${d}`);
    });
}

info(`set base dir: ${basedir}`);

scanDir(webdir);

@@ -262,14 +262,29 @@
 og('
-%5BINFO%5D
+%5Cx1b%5B32m%5BINFO%5D%5Cx1b%5B0m
  ' +
@@ -332,67 +332,38 @@
 og('
-%5BERROR%5D ' + s);%0A%7D%0A%0Afunction checkUseStrict(jsfile) %7B%0A    //
+%5Cx1b%5B31m%5BERROR%5D%5Cx1b%5B0m ' + s);
 %0A%7D%0A%0A
@@ -725,16 +725,19 @@
 %0A       
+ //
  info(%60c

add color log