'use strict';
var fs = require('fs');
var _path = require('path');

var async = require('async');
var cpr = require('cpr');
var webpack = require('webpack');
var webpackConfig = require('../config/build');

var utils = require('./utils');


module.exports = function(config) {
  process.env.BUILD_DEV = 1;

  return new Promise(function(resolve, reject) {
    webpackConfig(config).then(function(c) {
      webpack(c, function(err) {
        if(err) {
          return reject(err);
        }

        var buildDir = _path.join(process.cwd(), './.antwar/build');
        var renderPage = require(_path.join(buildDir, 'bundleStaticPage.js'));

        async.parallel([
          fs.writeFile.bind(null,
            _path.join(buildDir, 'index.html'),
            renderPage('/antwar_devindex', null)
          ),
          cpr.bind(null,
            './assets',
            _path.join(buildDir, 'assets')),
          utils.copyExtraAssets.bind(null, buildDir, config.assets),
        ], function(err) {
          if(err) {
            return reject(err);
          }

          resolve();
        });
      });
    }).catch(function(err) {
      reject(err);
    });
  });
};

'use strict';
var fs = require('fs');
var _path = require('path');

var async = require('async');
var cpr = require('cpr');
var webpack = require('webpack');
var webpackConfig = require('../config/build');

var utils = require('./utils');


module.exports = function(config) {
  process.env.BUILD_DEV = 1;

  return new Promise(function(resolve, reject) {
    webpackConfig(config).then(function(c) {
      webpack(c, function(err) {
        if(err) {
          return reject(err);
        }

        var buildDir = _path.join(process.cwd(), './.antwar/build');
        var renderPage = require(_path.join(buildDir, 'bundleStaticPage.js'));

        async.parallel([
          fs.writeFile.bind(null,
            _path.join(buildDir, 'index.html'),
            renderPage('/antwar_devindex', null)
          ),
          copyIfExists.bind(null, './assets', _path.join(buildDir, 'assets')),
          utils.copyExtraAssets.bind(null, buildDir, config.assets),
        ], function(err) {
          if(err) {
            return reject(err);
          }

          resolve();
        });
      });
    }).catch(function(err) {
      reject(err);
    });
  });
};

// TODO: push to utils
function copyIfExists(from, to, cb) {
  fs.exists(from, function(exists) {
    if(exists) {
      cpr(from, to, cb);
    }
    else {
      cb();
    }
  });
}

Copy dev_index assets only if they exist