let fs = require('../core/fs.js');
let utils = require('../utils/utils.js');

class TemplateGenerator {
  constructor(templateName) {
    this.templateName = templateName;
  }

  downloadAndGenerate(uri, destinationFolder, name) {
    let {url, filePath} = this.getExternalProject(uri);
    let tmpFilePath = fs.tmpDir(filePath);

    fs.mkdirpSync(utils.dirname(tmpFilePath));
    utils.downloadFile(url, tmpFilePath, () => {
      let fspath = utils.joinPath(destinationFolder, name);
      const decompress = require('decompress');

      decompress(tmpFilePath, fspath, {
        map: file => {
          let fixed_path = file.path.split('/');
          fixed_path.shift(); // remove first directory
          file.path = utils.joinPath(...fixed_path);
          return file;
        }
      });
    });
  }

  generate(destinationFolder, name) {
    let templatePath = fs.embarkPath(utils.joinPath('templates', this.templateName));
    console.log(__('Initializing Embark Template....').green);
    let fspath = utils.joinPath(destinationFolder, name);

    fs.copySync(templatePath, fspath);
    utils.cd(fspath);
    utils.sed('package.json', '%APP_NAME%', name);
    if (fs.existsSync('dot.gitignore')) {
      fs.moveSync('dot.gitignore', '.gitignore');
    }

    if (name === 'embark_demo') {
      console.log(__('Installing packages...').green);
      utils.runCmd('npm install');
    }

    console.log(__('Init complete').green);
    console.log('\n' + __('App ready at ').green + fspath);

    if (name === 'embark_demo') {
      console.log('-------------------'.yellow);
      console.log(__('Next steps:').green);
      console.log(('-> ' + ('cd ' + fspath).bold.cyan).green);
      console.log('-> '.green + 'embark run'.bold.cyan);
      console.log(__('For more info go to http://embark.status.im').green);
    }
  }

  getExternalProject(uri) {
    let match = uri.match(
      /\.[a-z]+\/([-a-zA-Z0-9@:%_+.~#?&\/=]+)/
    );

    let url, folder;

    if (uri.startsWith('http')) {
      url = uri + "/archive/master.zip";
      folder = match[1];
    } else if (uri.startsWith('github')) {
      url = "https://" + uri + "/archive/master.zip";
      folder = match[1];
    } else if (uri.split('/').length === 2) {
      url = "https://github.com/" + uri + "/archive/master.zip";
      folder = uri;
    } else if (uri.indexOf('/') === -1) {
      url = "https://github.com/embark-framework/embark-" + uri + "-template/archive/master.zip";
      folder = "embark-framework/embark-" + uri + "-template";
    }

    return {
      url,
      filePath: utils.joinPath(".embark/templates/", folder, "archive.zip")
    };
  }

}

module.exports = TemplateGenerator;

@@ -216,32 +216,55 @@
 Folder, name) %7B%0A
+    const self = this;%0A
     let %7Burl, fi
@@ -341,24 +341,95 @@
 r(filePath);
+%0A    console.log(__('Installing Template from ' + uri + '....').green);
 %0A%0A    fs.mkd
@@ -889,307 +889,838 @@
   %7D)
-;%0A    %7D);%0A  %7D%0A%0A  generate(destinationFolder, name) %7B%0A    let templatePath = fs.embarkPath(utils.joinPath('templates', this.templateName));%0A    console.log(__('Initializing Embark Template....').green);%0A    let fspath = utils.joinPath(destinationFolder, name);%0A%0A    fs.copySync(templatePath, fspath);
+.then(() =%3E %7B%0A        self.installTemplate(fspath, name, true);%0A      %7D);%0A    %7D);%0A  %7D%0A%0A  generate(destinationFolder, name) %7B%0A    console.log(__('Initializing Embark Template....').green);%0A%0A    let templatePath = fs.embarkPath(utils.joinPath('templates', this.templateName));%0A    let fspath = utils.joinPath(destinationFolder, name);%0A    fs.copySync(templatePath, fspath);%0A%0A    this.installTemplate(fspath, name, (name === 'embark_demo'));%0A%0A    if (name === 'embark_demo') %7B%0A      console.log('-------------------'.yellow);%0A      console.log(__('Next steps:').green);%0A      console.log(('-%3E ' + ('cd ' + fspath).bold.cyan).green);%0A      console.log('-%3E '.green + 'embark run'.bold.cyan);%0A      console.log(__('For more info go to http://embark.status.im').green);%0A    %7D%0A  %7D%0A%0A  installTemplate(templatePath, name, installPackages) %7B
 %0A   
@@ -1729,19 +1729,25 @@
 tils.cd(
-fsp
+templateP
 ath);%0A  
@@ -1791,24 +1791,25 @@
 E%25', name);%0A
+%0A
     if (fs.e
@@ -1903,38 +1903,31 @@
     if (
-name === 'embark_demo'
+installPackages
 ) %7B%0A    
@@ -2118,346 +2118,22 @@
 n + 
-fspath);%0A%0A    if (name === 'embark_demo') %7B%0A      console.log('-------------------'.yellow);%0A      console.log(__('Next steps:').green);%0A      console.log(('-%3E ' + ('cd ' + fspath).bold.cyan).green);%0A      console.log('-%3E '.green + 'embark run'.bold.cyan);%0A      console.log(__('For more info go to http://embark.status.im').green);%0A    %7D
+templatePath);
 %0A  %7D

refactor install template code