'use strict';

var async = require('async');
var extend = require('extend-shallow');
var extname = require('gulp-extname');
var Scaffold = require('scaffold');

var utils = require('../../lib/utils');
var assemble = require('../..');
var app = assemble();

/**
 * Define a scaffold (`site` and `blog` are "targets")
 */

var scaffold = new Scaffold({
  site: {
    data: {title: 'My Site'},
    options: {
      cwd: 'src',
      destBase: 'dist'
    },
    files: [
      {src: 'posts/*.md', dest: 'blog/', data: {title: 'My Blog'}},
      {src: 'pages/*.hbs'}
    ]
  },
  docs: {
    cwd: 'src/docs',
    destBase: 'dist/docs',
    files: [
      {src: '*.hbs', data: {title: 'whatever'}},
      {src: '*.md', data: {title: 'documentation'}}
    ]
  }
});

/**
 * Register engines for rendering templates
 * (we'll let assemble automatically select the engine by file extension)
 */

app.engine('hbs', require('engine-handlebars'));
app.engine('md', require('engine-base'));

/**
 * Tasks
 */

app.task('scaffold', function(done) {
  async.eachOf(scaffold, function(target, name, cb) {
    if (!target.hasOwnProperty('files')) {
      cb();
      return;
    }

    // log out a time-stamped message for each "files" definition
    utils.logTask(app.name, ':scaffold:' + name);

    async.each(target.files, function(files, next) {
      if (!files.src || !files.src.length) {
        next();
        return;
      }

      var opts = extend(target.options, files.options);
      var data = extend(target.data, files.data);

      app.src(files.src, opts)
        .pipe(app.renderFile(data))
        .on('error', next)
        .pipe(extname())
        .pipe(app.dest(files.dest))
        .on('error', next)
        .on('finish', next);
    }, cb);
  }, done);
});

app.task('default', ['scaffold']);

/**
 * Expose our instance of assemble
 */

module.exports = app;

@@ -983,367 +983,244 @@
 %0A * 
-Tasks%0A */%0A%0Aapp.task('scaffold', function(done) %7B%0A  async.eachOf(scaffold, function(target, name, cb) %7B%0A    if (!target.hasOwnProperty('files')) %7B%0A      cb();%0A      return;%0A    %7D%0A%0A    // log out a time-stamped message for each %22files%22 definition%0A    utils.logTask(app.name, ':scaffold:' + name);%0A%0A    async.each(target.files, function(files, next
+Register a pipeline plugin to be used on the files processed%0A * by the %60scaffold%60 method%0A */%0A%0Aapp.plugin('render', app.renderFile);%0A%0A/**%0A * Tasks%0A */%0A%0Aapp.task('scaffold', function(cb) %7B%0A  app.scaffold(scaffold, function(err
 ) %7B%0A
-  
     if (
 !fil
@@ -1219,423 +1219,79 @@
 if (
-!files.src %7C%7C !files.src.length) %7B%0A        next();%0A        return;%0A      %7D%0A%0A      var opts = extend(target.options, files.options)
+err) throw err
 ;%0A    
- 
- var data = extend(target.data, f
+ut
 il
-e
 s.
-data);%0A%0A      app.src(files.src, opts)%0A        .pipe(app.renderFile(data))%0A        .on('error', next)%0A        .pipe(extname())%0A        .pipe(app.dest(files.dest))%0A        .on('error', next)%0A        .on('finish', next
+timestamp('finished scaffold'
 );%0A   
- %7D,
  cb
+(
 );%0A  %7D
-, done
 );%0A%7D

use `scaffold` method in example