const chalk = require('chalk');
const compress = require('./compress').compress;
const exec = require('child_process').exec;
const execSync = require('child_process').execSync;
const fs = require('fs');
const mkdirp = require('mkdirp');
const p = require('path');
const projectSettings = require('./projectSettings');
const rmdir = require('rimraf');
const shaderGen = require('./shadergen').shaderGen;
const utils = require('./utils');
const walk = require('walk');


function moveCursorToColumn(col) {
  return '\x1B[' + col + 'G';
}

function renderOK() {
  console.log(moveCursorToColumn(72) +
    chalk.grey('[') + chalk.green('OK') + chalk.grey(']'));
}

function renderWarn() {
  console.log(moveCursorToColumn(70) +
    chalk.grey('[') + chalk.yellow('WARN') + chalk.grey(']'));
}

function renderError() {
  console.log(moveCursorToColumn(69) +
    chalk.grey('[') + chalk.red('ERROR') + chalk.grey(']'));
}

function res(projectPath, callback) {
  const walker = walk.walk(projectPath + '/res/' , {followLinks: false});
  const files = [];
  console.log(chalk.yellow('\nCollecting files from res/'));
  walker.on('file', function(root, stat, next) {

    /* hacks to ensure slashes in path are correct.
     * TODO: is there a bug in walker that causes
     * these things to happen?  */
    root += '/';
    root = root.replace(/\/\//g, '/');

    const file = fs.readFileSync(root + stat.name);
    process.stdout.write('- Assimilating ' + chalk.grey('res/') + chalk.magenta(stat.name));
    files.push('FILES[\'' + root.slice(projectPath.length + 1) + stat.name + '\']=\'' +
      file.toString('base64') + '\'');
    renderOK();
    next();
  });
  walker.on('end', function(){
    process.stdout.write(chalk.yellow('\nMerging assimilated files'));
    callback('FILES={};' + files.join(';') + ';');
    renderOK();
  });
}

const compile = function(projectPath, options) {
  function collect(data) {
    function writeDemoToFile(data, filename) {
      const binPath = p.join(projectPath, '/bin/');
      mkdirp(binPath, function() {
        fs.writeFileSync(projectPath + '/bin/' + filename, data);
      });
    }

    const settings = projectSettings.load(projectPath);
    const projectVersion = execSync('git rev-parse HEAD');
    const projectOrigin = execSync('git config --get remote.origin.url');
    const NINVersion = execSync(`cd ${__dirname} && git rev-parse HEAD`);
    const NINOrigin = execSync(`cd ${__dirname} && git config --get remote.origin.url`);

    const metadata = {
      'Title': settings.title,
      'Author': settings.authors.join(', '),
      'Description': settings.description,
      'Creation time': '' + new Date(),
      'Software': `Project version: ${projectVersion} @ ${projectOrigin}\nNIN version: ${NINVersion} @ ${NINOrigin}`,
      previewImage: settings.previewImage
    };

    const metadataAsHTMLComments = Object.keys(metadata)
      .map(key => `<!-- ${key}: ${metadata[key]} -->`)
      .join('\n');

    const ogTags =
      `<meta property="og:title" content="${utils.unsafeHTMLEscape(metadata.Title)}" />
      <meta property="og:description" content="${utils.unsafeHTMLEscape(metadata.Description)}" />
      <meta property="og:image" content="${metadata.previewImage}" />
      <meta name="author" content="${utils.unsafeHTMLEscape(metadata.Author)}" />`;

    const htmlPreamble =
      fs.readFileSync(projectPath + '/index.html', {encoding: 'utf8'})
      .replace(
        'NIN_WILL_REPLACE_THIS_TAG_WITH_YOUR_ANALYTICS_ID',
        settings.googleAnalyticsID)
      .replace(
        'NIN_WILL_REPLACE_THIS_TAG_WITH_AUTOGENERATED_COMMENT_TAGS',
        metadataAsHTMLComments)
      .replace(
        'NIN_WILL_REPLACE_THIS_TAG_WITH_AUTOGENERATED_META_TAGS',
        ogTags);

    if(options.pngCompress) {
      process.stdout.write(chalk.yellow('\nCompressing demo to .png.html'));
      compress(projectPath, data, htmlPreamble, metadata, function(data) {
        renderOK();
        writeDemoToFile(data, 'demo.png.html');
        console.log(chalk.white('\n★ ---------------------------------------- ★'));
        console.log(chalk.white('| ') +
          chalk.green('Successfully compiled ') +
            chalk.grey('bin/') +
            chalk.green('demo.png.html!') +
            chalk.white(' |'));
        console.log(chalk.white('★ ---------------------------------------- ★\n'));
      });
    } else {
      const html =
        htmlPreamble +
        '<script>' +
        'GU=1;' + /* hack to make sure GU exisits from the get-go */
        'BEAN=0;' +
        'BEAT=false;' +
        'FRAME_FOR_BEAN=function placeholder(){};' +
        'BEAN_FOR_FRAME=function placeholder(){};' +
        data +
        'var layers = JSON.parse(atob(FILES["res/layers.json"]));' +
        'demo=bootstrap({layers:layers, onprogress: ONPROGRESS, oncomplete: ONCOMPLETE});' +
        '</script>';
      writeDemoToFile(html, 'demo.html') +
        process.stdout.write('Successfully compiled demo.html!\n');
    }
  }
  res(projectPath, function(data) {
    const genPath = p.join(projectPath, '/gen/');
    rmdir(genPath, function() {
      mkdirp(genPath, function() {
        fs.writeFileSync(projectPath + '/gen/files.js', new Buffer(data));
        projectSettings.generate(projectPath);
        shaderGen(projectPath, function() {
          process.stdout.write(chalk.yellow('\nRunning closure compiler'));
          exec('java -jar -Xmx2048m ' + __dirname + '/compiler.jar -O SIMPLE --language_in ECMASCRIPT6 --language_out=ES5 --logging_level INFO ' + __dirname + '/../dasBoot/lib/*.js ' + __dirname + '/../dasBoot/*.js ' + projectPath + '/lib/*.js ' + projectPath + '/gen/*.js ' + projectPath + '/src/*.js',
            {encoding: 'binary', maxBuffer: 1024 * 1024 * 1024},
            function(error, stdout, stderr) {
              if(error) {
                renderError();
                console.error(error);
                process.exit(1);
              } else if(stderr) {
                renderWarn();
                console.error(stderr);
              } else {
                renderOK();
              }
              collect(stdout);
            });
        });
      });
    });
  });
};


module.exports = {compile: compile};

@@ -626,16 +626,19 @@
 .green('
+%E2%9C%94%EF%B8%8F 
 OK') + c
@@ -646,32 +646,32 @@
 alk.grey('%5D'));%0A
-
 %7D%0A%0Afunction rend
@@ -756,16 +756,19 @@
 yellow('
+%E2%9A%A0%EF%B8%8F 
 WARN') +
@@ -849,24 +849,24 @@
 olumn(69) +%0A
-
     chalk.gr
@@ -886,16 +886,18 @@
 lk.red('
+%E2%9D%8C 
 ERROR') 

Add some emoji to compile.js console output