title: Webpack Plugin: run command after assets are written to file system
body: <p>I've seen this question: <a href="https://stackoverflow.com/questions/30312715/run-command-after-webpack-build">Run command after webpack build</a> and I modified the sample code provided there.</p>
  
  <p>To describe my scenario, I'm trying to take the assets output by webpack and deploy them to a mounted network drive. I'm using <code>ncp</code> to copy a directory and the actual copy works fine, it just seems that when webpack calls either the <code>after-emit</code> event or the <code>done</code> event, it is not actually done emitting or writing the files to the file system. My copy ends up copying either empty files or partially written files.</p>
  
  <p>Here is the entire plugin:</p>
  
  <pre><code>'use strict';
  var ncp = require('ncp').ncp;
  
  function WebPackDeployAfterBuild(options) {
      var defaultOptions = {
  
      };
  
      this.options = Object.assign(defaultOptions, options);
  }
  
  WebPackDeployAfterBuild.prototype.apply = function(compiler) {
      const options = this.options;
  
      compiler.plugin("done", (stats) =&gt; {
              console.log(Object.keys(stats.compilation.assets));
              console.log("\nExecuting Deploy on done...");
              ncp(options.from, options.to, function(err) {
                  if (err) {
                      console.error("Err in ncp");
                  }
                  console.log(`Finished deploying ${options.from} to ${options.to}`);
              });
      });
  
      compiler.plugin("after-emit", (compilation, callback) =&gt; {
          console.log(Object.keys(compilation.assets));
          console.log("\nExecuting Deploy on after-emit...");
          ncp(options.from, options.to, function(err) {
              if (err) {
                  console.error("Err in ncp");
              }
              console.log(`Finished deploying ${options.from} to ${options.to}`);
              callback();
          });
      });
  };
  
  module.exports = WebPackDeployAfterBuild;
  </code></pre>
  
  <p>Here is the usage in my <code>webpack.config.js</code> file:</p>
  
  <pre><code>config.plugins.push(
          new WebPackDeployAfterBuild({
              from: path.resolve(__dirname, './myOutputPath'),
              to: 'Q:/'
          })
      )
  </code></pre>
  
  <p>I've included both the <code>done</code> and <code>after-emit</code> handlers, but in practice I've been commenting one out. I only need to run the event once, but both of these events are firing too soon. Is there another event I can use?</p>
  
  <p>E: Forgot to mention, I am also running this on a webpack --watch task. I want to save a file, have webpack process it and immediately deploy it to my server while I develop. If there is a better dev workflow, I'm open to suggestions.</p>
  
  <p>E2: Here's a log of what happens:</p>
  
  <pre><code>2ms optimize chunk assets
  7ms optimize assets
   95% emit
  [ 'scripts/app.bundle.js', 'scripts/vendor.bundle.js' ]
  
  Executing Deploy After build...
  11ms emit
  Hash: a005eef9ae47a167f791
  Version: webpack 1.13.2
  Time: 278ms
                  Asset     Size  Chunks             Chunk Names
  scripts/app.bundle.js  61.1 kB       0  [emitted]  scripts/app
      + 20 hidden modules
  Child html-webpack-plugin for "index.html":
          + 3 hidden modules
  Finished deploying C:\Users\redacted\Source\myprojectOutput to Q:/
  </code></pre>
  
  <p>You can see that the logging I do occurs before the <code>11ms emit</code> message, I would expect it to be after that...</p>