/* eslint-env node */
/* eslint-disable camelcase */

const {spawn} = require('child_process'); // eslint-disable-line import/unambiguous

const JAVA_BIN = 'java';
const COMPILER_PATH = 'node_modules/google-closure-compiler/compiler.jar';

const {stdout, argv} = process;
const debug = argv.indexOf('--debug') > 0;

compile({
  compilation_level: debug ? 'SIMPLE' : 'ADVANCED',
  language_in: 'ECMASCRIPT6_STRICT',
  language_out: 'ECMASCRIPT5_STRICT',
  rewrite_polyfills: false,
  output_wrapper_file: 'umd.template',
  assume_function_wrapper: true,
  formatting: debug ? ['SINGLE_QUOTES', 'PRETTY_PRINT', 'PRINT_INPUT_DELIMITER'] : 'SINGLE_QUOTES',
  externs: 'externs.js',
  jscomp_off: 'checkTypes', // TODO fix compiler warnings
  js: ['src/**.js', '../core/src/**.js'],
  js_output_file: 'dist/' + (debug ? 'cfxnes.debug.js' : 'cfxnes.js'),
});

function compile(params) {
  const javaArgs = ['-jar', COMPILER_PATH];
  const compilerArgs = makeArgs(params);
  const args = javaArgs.concat(compilerArgs);

  stdout.write(JAVA_BIN + ' ' + javaArgs.join(' '));
  stdout.write('\n    ');
  stdout.write(compilerArgs.join('\n    '));
  stdout.write('\n');

  spawn(JAVA_BIN, args, {stdio: 'inherit'});
}

function makeArgs(params) {
  const args = [];
  for (const key in params) {
    const value = params[key];
    if (Array.isArray(value)) {
      args.push(...value.map(item => makeArg(key, item)));
    } else {
      args.push(makeArg(key, value));
    }
  }
  return args;
}

function makeArg(key, value) {
  return `--${key}="${value}"`;
}

@@ -46,77 +46,9 @@
 case
- */%0A%0Aconst %7Bspawn%7D = require('child_process'); // eslint-disable-line
+,
  imp
@@ -62,24 +62,27 @@
 mbiguous
+ */
 %0A%0Aconst 
 JAVA_BIN
@@ -77,63 +77,28 @@
 nst 
-JAVA_BIN = 'java';%0Aconst COMPILER_PATH = 'node_modules/
+Compiler = require('
 goog
@@ -120,17 +120,19 @@
 iler
-/
+').
 compiler
 .jar
@@ -131,13 +131,8 @@
 iler
-.jar'
 ;%0A%0Ac
@@ -144,16 +144,24 @@
 %7Bstdout,
+ stderr,
  argv%7D =
@@ -217,16 +217,129 @@
  0;%0A
-%0Acompile
+const filename = debug ? 'cfxnes.debug.js' : 'cfxnes.js';%0Aconst file = %60dist/$%7Bfilename%7D%60;%0A%0Aconst compiler = new Compiler
 (%7B%0A 
@@ -811,759 +811,118 @@
 le: 
-'dist/' + (debug ? 'cfxnes.debug.js' : 'cfxnes.js'),%0A%7D);%0A%0Afunction compile(params) %7B%0A  const javaArgs = %5B'-jar', COMPILER_PATH%5D;%0A  const compilerArgs = makeArgs(params);%0A  const args = javaArgs.concat(compilerArgs);%0A%0A  stdout.write(JAVA_BIN + ' ' + javaArgs.join(' '));%0A  stdout.write('%5Cn    ');%0A  stdout.write(compilerArgs.join('%5Cn    '));%0A  stdout.write('%5Cn');%0A%0A  spawn(JAVA_BIN, args, %7Bstdio: 'inherit'%7D);%0A%7D%0A%0Afunction makeArgs(params) %7B%0A  const args = %5B%5D;%0A  for (const key in params) %7B%0A    const value = params%5Bkey%5D;%0A    if (Array.isArray(value)) %7B%0A      args.push(...value.map(item =%3E makeArg(key, item)));%0A    %7D else %7B%0A      args.push(makeArg(key, value));%0A    %7D%0A  %7D%0A  return args;%0A%7D%0A%0Afunction makeArg(key, value) %7B%0A  return %60--$%7Bkey%7D=%22$%7Bvalue%7D%22%60;%0A%7D
+file,%0A%7D);%0A%0Acompiler.run((code, out, err) =%3E %7B%0A  stdout.write(out);%0A  stderr.write(err);%0A  process.exit(code);%0A%7D);
 %0A

Use closure compiler node module for building.