const async = require('async');
const nexe = require('../vendor/nexe');
const monkeyPatch = require('../vendor/nexe/monkeypatch');
const fs = require('fs');
const path = require('path');
const os = require('os');
const zlib = require('zlib');
const embed = require('./embed');

const argv = process.argv.slice(0);
const nodeVersion = argv[2];
const staticBinary = /^true$/.test(argv[3]);

function getDirContents(dir, accumPath = dir) {
  let filenames = fs.readdirSync(dir);

  return filenames.reduce((ret, filename) => {
    const fname = path.resolve(accumPath, filename);
    const fStat = fs.statSync(fname);

    if (fStat.isDirectory()) {
      const newAccum = path.join(accumPath, filename);
      return ret.concat(getDirContents(newAccum, newAccum));
    }

    ret.push(path.join(accumPath, filename));
    return ret;
  }, []);
}

function deflate(fname) {
  return new Promise((resolve, reject) => {
    fs.readFile(fname, (err, input) => {
      fs.writeFileSync(fname, zlib.deflateSync(input));
      resolve();
    });
  });
}

const isWindows = /^Windows/.test(os.type());
const outputPath = `build/${isWindows ? 'lumo.exe' : 'lumo'}`;

const resources = getDirContents('target').filter(
  fname =>
    fname.endsWith('.aot.js.map') ||
    (!fname.endsWith('main.js') &&
      !fname.endsWith('bundle.js') &&
      !fname.endsWith('bundle.min.js') &&
      !fname.endsWith('google-closure-compiler-js.js') &&
      !fname.endsWith('aot.edn') &&
      !/target[\\\/]cljs[\\/]core.js/.test(fname) &&
      !fname.endsWith('.map')),
);

Promise.all(resources.map(deflate)).then(() => {
  embed(resources, 'target');

  nexe.compile(
    {
      input: 'target/bundle.min.js',
      output: outputPath,
      nodeTempDir: 'tmp',
      nodeConfigureArgs: [
        '--without-dtrace',
        '--without-npm',
        '--without-inspector',
        '--without-etw',
        '--with-snapshot',
      ].concat(
        isWindows
          ? ['--openssl-no-asm']
          : staticBinary
          ? ['--fully-static']
          : [],
      ),
      nodeMakeArgs: ['-j', '8'],
      nodeVCBuildArgs: ['nosign', 'x64', 'noetw'],
      flags: true,
      startupSnapshot: 'target/main.js',
      noBundle: true,
      framework: 'node',
      nodeVersion,
    },
    err => {
      if (err) {
        throw err;
      }
      console.log(
        `Finished bundling. Nexe binary can be found in ${outputPath}`,
      );
    },
  );
});

@@ -1629,37 +1629,32 @@
 ');%0A
-%0A
   
-nexe.compile(%0A   
+const options =
  %7B%0A    
- 
- 
 inpu
@@ -1684,18 +1684,16 @@
 s',%0A    
-  
 output: 
@@ -1704,26 +1704,24 @@
 utPath,%0A    
- 
- 
 nodeTempDir:
@@ -1728,18 +1728,16 @@
  'tmp',%0A
-  
     node
@@ -1755,26 +1755,24 @@
 gs: %5B%0A      
-  
 '--without-d
@@ -1775,26 +1775,24 @@
 ut-dtrace',%0A
-  
       '--wit
@@ -1800,34 +1800,32 @@
 out-npm',%0A      
-  
 '--without-inspe
@@ -1831,18 +1831,16 @@
 ector',%0A
-  
       '-
@@ -1856,26 +1856,24 @@
 etw',%0A      
-  
 '--with-snap
@@ -1879,18 +1879,16 @@
 pshot',%0A
-  
     %5D.co
@@ -1899,18 +1899,16 @@
 (%0A      
-  
 isWindow
@@ -1909,18 +1909,16 @@
 Windows%0A
-  
         
@@ -1948,18 +1948,16 @@
         
-  
 : static
@@ -1963,18 +1963,16 @@
 cBinary%0A
-  
         
@@ -2004,18 +2004,14 @@
     
-  
 : %5B%5D,%0A
-  
     
@@ -2013,26 +2013,24 @@
 %0A    ),%0A    
-  
 nodeMakeArgs
@@ -2040,26 +2040,24 @@
 '-j', '8'%5D,%0A
-  
     nodeVCBu
@@ -2097,18 +2097,16 @@
 '%5D,%0A    
-  
 flags: t
@@ -2114,18 +2114,16 @@
 ue,%0A    
- 
- 
 startupS
@@ -2153,18 +2153,16 @@
 s',%0A    
-  
 noBundle
@@ -2173,18 +2173,16 @@
 ue,%0A    
- 
- 
 framewor
@@ -2196,18 +2196,16 @@
 e',%0A    
-  
 nodeVers
@@ -2211,19 +2211,72 @@
 sion,%0A  
-  %7D
+%7D;%0A%0A  console.dir(options);%0A%0A  nexe.compile(%0A    options
 ,%0A    er

Print to stdout the options passed to nexe (#482)