<commit_before>/*
 * grunt-iconprocessor-imweb-tpl
 * 
 *
 * Copyright (c) 2015 moxhe
 * Licensed under the MIT license.
 */

'use strict';
var os = require('os');
// var fs = require('fs');
var path = require('path');
var async = require('async');
var _ = require('underscore');
var svgParser = require('./lib/svg_parser.js');
var concurrencyCount = (os.cpus().length || 1) * 4;
var win32 = process.platform === 'win32';

module.exports = function(grunt) {

  // Can be followed by: letters (A-Za-z), digits (0-9), hyphens ("-"), and underscores ("_")
  var iconReg = /i-[A-Za-z0-9\-\_]+/g;
  var lineBreak = win32 ? '\r\n' : '\n';
  var svgTmp = '.svgtmp/';

  // put utils function here
  function isExist(filepath) {
      if (!grunt.file.exists(filepath)) {
        grunt.log.warn('Source file "' + filepath + '" not found.');
        return false;
      } else {
        return true;
      }
  }

  function extractIcons(filepath) {
    var content = grunt.file.read(filepath);
    var icons = content.match(iconReg);
    if (!icons) {
      icons = [];
    } else {
      icons = icons.map(function(icon) {
        return icon.replace(/i-/, '');
      });
    }
    return icons = _.uniq(icons);
  }

  function extractSvgs(svgDir, icons) {
    icons.forEach(function(icon) {
      var svgPath = svgDir + '/' + icon + '.svg';
      var content = grunt.file.read(svgPath);
      var svgDest = svgTmp + icon + '.svg';
      grunt.file.write(svgDest, content);
    });
  }

  grunt.registerMultiTask('iconprocessor_imweb_tpl', 'extract all icon names from imweb-tpl files', function() {
    // tell grunt that this task is async.
    // var cb = this.async();
    // Merge task-specific and/or target-specific options with these defaults.
    // svgDir, outputDir
    var options = this.options();
    var svgDir = options.svgDir;

    if (!svgDir) {
      grunt.log.warn('You don\'t have svg dir config.');
      return false;
    } else if (!grunt.file.isDir(svgDir)) {
      grunt.log.warn('Invalid svg dir.');
      return false;
    }

    async.eachLimit(this.files, concurrencyCount, function(file, next) {

      var allicons = [];
      var outputDir = file.dest;
      var src = file.src.filter(isExist).map(extractIcons).forEach(function(item, index) {
        allicons = allicons.concat(item);
      });

      if (!grunt.file.exists(outputDir)) {
        grunt.file.mkdir(outputDir);
      }

      extractSvgs(svgDir, allicons);
      svgParser.parse(svgTmp, outputDir);
      grunt.file.delete(svgTmp);
      console.log(outputDir + ' dir iconfont generation finished.');

      grunt.file.write('tmp/icons', allicons.join(lineBreak));
      console.log('tmp/icons write finished.');

    }, null);

  });
};<commit_msg>fix: program exit when read unexist svg. and optimaze icon RegExp<commit_after>/*
 * grunt-iconprocessor-imweb-tpl
 * 
 *
 * Copyright (c) 2015 moxhe
 * Licensed under the MIT license.
 */

'use strict';
var os = require('os');
// var fs = require('fs');
var path = require('path');
var async = require('async');
var _ = require('underscore');
var svgParser = require('./lib/svg_parser.js');
var concurrencyCount = (os.cpus().length || 1) * 4;
var win32 = process.platform === 'win32';

module.exports = function(grunt) {

  // Can be followed by: letters (A-Za-z), digits (0-9), hyphens ("-"), and underscores ("_")
  var iconReg = /\bi-[A-Za-z0-9\-\_]+/g;
  var lineBreak = win32 ? '\r\n' : '\n';
  var svgTmp = '.svgtmp/';

  // put utils function here
  function isExist(filepath) {
      if (!grunt.file.exists(filepath)) {
        grunt.log.warn('Source file "' + filepath + '" not found.');
        return false;
      } else {
        return true;
      }
  }

  function extractIcons(filepath) {
    var content = grunt.file.read(filepath);
    var icons = content.match(iconReg);
    if (!icons) {
      icons = [];
    } else {
      icons = icons.map(function(icon) {
        return icon.replace(/i-/, '');
      });
    }
    // if (icons.length) {
    //   console.log('From ' + filepath + ' find icons:\n\t' + _.uniq(icons).join(','));
    // }
    return icons = _.uniq(icons);
  }

  function extractSvgs(svgDir, icons) {
    icons.forEach(function(icon) {
      var svgPath = svgDir + '/' + icon + '.svg';
      if (!grunt.file.exists(svgPath)) {
        grunt.log.warn(icon + '.svg not found in svg floder.');
        return false;
      }
      var content = grunt.file.read(svgPath);
      var svgDest = svgTmp + icon + '.svg';
      grunt.file.write(svgDest, content);
    });
  }

  grunt.registerMultiTask('iconprocessor_imweb_tpl', 'extract all icon names from imweb-tpl files', function() {
    // tell grunt that this task is async.
    // var cb = this.async();
    // Merge task-specific and/or target-specific options with these defaults.
    // svgDir, outputDir
    var options = this.options();
    var svgDir = options.svgDir;

    if (!svgDir) {
      grunt.log.warn('You don\'t have svg dir config.');
      return false;
    } else if (!grunt.file.isDir(svgDir)) {
      grunt.log.warn('Invalid svg dir.');
      return false;
    }

    async.eachLimit(this.files, concurrencyCount, function(file, next) {

      var allicons = [];
      var outputDir = file.dest;
      var src = file.src.filter(isExist).map(extractIcons).forEach(function(item, index) {
        allicons = allicons.concat(item);
      });
      // confirm single
      allicons = _.uniq(allicons);

      if (!grunt.file.exists(outputDir)) {
        grunt.file.mkdir(outputDir);
      }

      extractSvgs(svgDir, allicons);
      svgParser.parse(svgTmp, outputDir);
      grunt.file.delete(svgTmp);
      console.log(outputDir + ' dir iconfont generation finished.');

      grunt.file.write(outputDir + '/icons', allicons.join(lineBreak));
      console.log(outputDir + '/icons file write finished.');

    }, null);

  });
};
{'fraction_non_alphanumeric': 0.16172965611167858, 'fraction_numerical': 0.005447735784814436, 'mean_word_length': 2.6536069651741294, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, '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': '4870588', 'n_tokens_mistral': 1999, 'n_tokens_neox': 1948, 'n_words': 447}