<commit_before>var extend = require('../extend'),
  route = require('../route'),
  util = require('../util'),
  file = util.file,
  inherits = require('util').inherits,
  async = require('async'),
  colors = require('colors'),
  fs = require('fs'),
  _ = require('underscore'),
  publicDir = hexo.public_dir,
  cache = {};

var process = function(cache, callback){
  var list = route.list(),
    keys = Object.keys(list),
    generate = require('../generate');

  console.log('Generating.');

  async.parallel([
    function(next){
      fs.exists(publicDir, function(exist){
        if (!exist) return next();
        file.empty(publicDir, keys.concat(cache), next);
      });
    },
    function(next){
      var queue = async.queue(function(key, next){
        list[key](function(err, result){
          if (err) throw new Error('Generate Error: ' + key);

          if (result.readable){
            if (hexo.debug) console.log('Copying %s', result.path.bold);
            file.copy(result.path, publicDir + result.source, next);
          } else {
            if (cache[key] !== result){
              cache[key] = result;
              if (hexo.debug) console.log('Writing %s', (publicDir + key).bold);
              file.write(publicDir + key, result, next);
            } else {
              next();
            }
          }
        });
      }, 512);

      queue.push(keys, function(err){
        if (err) throw err;
      });

      queue.drain = callback;
    }
  ]);
};

extend.console.register('generate', 'Generate static files', function(args){
  var ignoreTheme = args.t || args.theme ? true : false,
    //watch = args.match(/-w|--watch/i) ? true : false,
    watch = false,
    start = new Date(),
    generate = require('../generate'),
    watchFn = require('../watch');

  console.log('Loading.');
  hexo.emit('generateBefore');

  generate({ignore: ignoreTheme}, function(err, cache){
    process(cache, function(){
      if (watch){
        console.log('Hexo is watching file changes. Press Ctrl+C to stop.');
        watchFn(function(ev, callback){
          var oldList = Object.keys(route.list());
          generate({watch: true}, function(err, cache){
            var newList = Object.keys(route.list());

            oldList.forEach(function(item){
              if (newList.indexOf(item) === -1) route.destroy(item);
            });

            process(cache, callback);
          });
        });
      } else {
        var finish = new Date(),
          elapsed = (finish.getTime() - start.getTime()) / 1000;
        console.log('Site generated in %ss.', elapsed.toFixed(3));
        hexo.emit('generateAfter');
      }<commit_msg>Generate: Add "-d" and "--deploy" option: Deploy automatically after generate<commit_after>var extend = require('../extend'),
  route = require('../route'),
  util = require('../util'),
  file = util.file,
  spawn = util.spawn,
  inherits = require('util').inherits,
  async = require('async'),
  colors = require('colors'),
  fs = require('fs'),
  _ = require('underscore'),
  publicDir = hexo.public_dir,
  cache = {};

var process = function(cache, callback){
  var list = route.list(),
    keys = Object.keys(list),
    generate = require('../generate');

  console.log('Generating.');

  async.parallel([
    function(next){
      fs.exists(publicDir, function(exist){
        if (!exist) return next();
        file.empty(publicDir, keys.concat(cache), next);
      });
    },
    function(next){
      var queue = async.queue(function(key, next){
        list[key](function(err, result){
          if (err) throw new Error('Generate Error: ' + key);

          if (result.readable){
            if (hexo.debug) console.log('Copying %s', result.path.bold);
            file.copy(result.path, publicDir + result.source, next);
          } else {
            if (cache[key] !== result){
              cache[key] = result;
              if (hexo.debug) console.log('Writing %s', (publicDir + key).bold);
              file.write(publicDir + key, result, next);
            } else {
              next();
            }
          }
        });
      }, 512);

      queue.push(keys, function(err){
        if (err) throw err;
      });

      queue.drain = callback;
    }
  ]);
};

extend.console.register('generate', 'Generate static files', function(args){
  var ignore = args.t || args.theme ? true : false,
    //watch = args.match(/-w|--watch/i) ? true : false,
    watch = false,
    deploy = args.d || args.deploy ? true : false,
    start = new Date(),
    generate = require('../generate'),
    watchFn = require('../watch');

  console.log('Loading.');
  hexo.emit('generateBefore');

  generate({ignore: ignore}, function(err, cache){
    process(cache, function(){
      if (watch){
        console.log('Hexo is watching file changes. Press Ctrl+C to stop.');
        watchFn(function(ev, callback){
          var oldList = Object.keys(route.list());
          generate({watch: true}, function(err, cache){
            var newList = Object.keys(route.list());

            oldList.forEach(function(item){
              if (newList.indexOf(item) === -1) route.destroy(item);
            });

            process(cache, callback);
          });
        });
      } else {
        var finish = new Date(),
          elapsed = (finish.getTime() - start.getTime()) / 1000;
        console.log('Site generated in %ss.', elapsed.toFixed(3));
        hexo.emit('generateAfter');
        if (deploy){
          spawn({
            command: 'hexo',
            args: ['deploy']
          });
        }
      }
{'fraction_non_alphanumeric': 0.1692362854069559, 'fraction_numerical': 0.0032269630692004303, 'mean_word_length': 2.219272937103289, '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': '1957826', 'n_tokens_mistral': 1645, 'n_tokens_neox': 1605, 'n_words': 369}