var walk = require('walk');
var request = require('request');

var logger = require('./logger');
var prepare = require('./prepare');

// walk the filesystem from . to find directories that contain a _deconst.json file.
var options = {
  followLinks: false,
};

walker = walk.walk(".", options);

walker.on('directories', function (root, stats, callback) {
  logger.debug('Traversing directories: %s', root);

  // Don't traverse into dot or common build directories.
  for (var i = stats.length; i--; i >= 0) {
    var name = stats[i].name;
    if (/^\./.test(name) || name === '_build' || name === '_site') {
      stats.splice(i, 1);
    }
  }

  callback();
});

walker.on('files', function (root, stats, callback) {
  var hasContent = stats.some(function (each) {
    return each.name === '_deconst.json';
  });

  if (hasContent) {
    logger.info('Deconst content directory: %s', root);

    prepare.prepare(root, callback);
  } else {
    callback();
  }
});

walker.on('errors', function (root, stats, callback) {
  logger.error('Error walking %s', root, {
    errors: stats.map(function (e) { return e.error; })
  });

  callback();
});

walker.on('end', function () {
  logger.debug('Walk completed');
});

var walk = require('walk');
var request = require('request');

var logger = require('./logger');
var prepare = require('./prepare');

prepare.connect();

// walk the filesystem from . to find directories that contain a _deconst.json file.
var options = {
  followLinks: false,
};

walker = walk.walk(".", options);

walker.on('directories', function (root, stats, callback) {
  logger.debug('Traversing directories: %s', root);

  // Don't traverse into dot or common build directories.
  for (var i = stats.length; i--; i >= 0) {
    var name = stats[i].name;
    if (/^\./.test(name) || name === '_build' || name === '_site') {
      stats.splice(i, 1);
    }
  }

  callback();
});

walker.on('files', function (root, stats, callback) {
  var hasContent = stats.some(function (each) {
    return each.name === '_deconst.json';
  });

  if (hasContent) {
    logger.info('Deconst content directory: %s', root);

    prepare.prepare(root, callback);
  } else {
    callback();
  }
});

walker.on('errors', function (root, stats, callback) {
  logger.error('Error walking %s', root, {
    errors: stats.map(function (e) { return e.error; })
  });

  callback();
});

walker.on('end', function () {
  logger.debug('Walk completed');
});

Connect to Docker before walking.
Connect to Docker before walking.
