 module.exports =
   description: 'Create a new file from template'
   initialize: (log, config, env) ->
-    templates    = config.templates
+    @templates    = config.templates
     templateName = process.argv[3]
     @cwd         = process.cwd()
     @folders     = [

      "#{@cwd}/templates"
    ]

    for template, dest of templates
      if template is templateName
        if _.isArray(dest)
          for tmpl in dest
            # async.series
            @buildTemplate tmpl, templates[tmpl]
        else
          @buildTemplate template, dest

  buildTemplate: (template, dest) ->
    templatePath = @getTemplatePath(template)

    unless templatePath

    templateSrc   = fs.readFileSync(templatePath, 'utf-8')
    srcVariables  = @extractVariables(templateSrc)

    @promptVariables template, pathVariables, (data) ->
      fullPath = mustache.to_html(dest, data)

      @promptVariables template, srcVariables, (data) ->
        content = mustache.to_html(templateSrc, data)
        
        # do not overwrite
        helper.createPathTree "#{@cwd}/#{fullPath}"
        fs.writeFileSync("#{@cwd}/#{fullPath}", content);
        log.info template, '-->', fullPath
        callback.call @, null, true

  promptVariables: (template, variables, callback) ->
    callbacks = []

    for varName in variables
      do (varName) ->
        callbacks.push (cb) ->
          program.prompt "#{template} - #{varName}: ", (answer) ->
            obj          = {}
            obj[varName] = answer
            cb(null, obj)
