const {
  FuseBox,
  WebIndexPlugin,
  QuantumPlugin,
  EnvPlugin,
  CopyPlugin,
  JSONPlugin,
  StyledComponentsPlugin,
} = require('fuse-box');

const { spawn } = require('child_process');

const production = process.env.NODE_ENV === 'dev' ? false : true;

const getConfig = target => {
  return {
    homeDir: 'src/',
    cache: !production,
    target,
    output: `build/$name.js`,
    useTypescriptCompiler: true,
    plugins: [
      EnvPlugin({ NODE_ENV: production ? 'production' : 'development' }),
      production &&
        QuantumPlugin({
          bakeApiIntoBundle: true,
          treeshake: true,
          uglify: {
            es6: true,
          },
        }),
    ],
    alias: {
      '~': '~/',
    },
    log: {
      showBundledFiles: false,
    },
  };
};

const getRendererConfig = (target, name) => {
  const cfg = Object.assign({}, getConfig(target, name), {
    sourceMaps: !production,
  });

  return cfg;
};

const getCopyPlugin = () => {
  return CopyPlugin({
    files: ['*.woff2', '*.png', '*.svg'],
    dest: 'assets',
    resolve: production ? './assets' : '/assets',
  });
};

const main = () => {
  const fuse = FuseBox.init(getConfig('server', 'main'));

  const app = fuse.bundle('main').instructions(`> [main/index.ts]`);

  if (!production) {
    app.watch();
  }

  fuse.run();
};

const renderer = () => {
  const cfg = getRendererConfig('electron');

  cfg.plugins.push(
    WebIndexPlugin({
      template: `static/pages/app.html`,
      path: production ? '.' : '/',
      target: `app.html`,
      bundles: ['app'],
    }),
  );

  cfg.plugins.push(
    WebIndexPlugin({
      template: `static/pages/app.html`,
      path: production ? '.' : '/',
      target: `permissions.html`,
      bundles: ['permissions'],
    }),
  );

  cfg.plugins.push(JSONPlugin());
  cfg.plugins.push(getCopyPlugin());
  cfg.plugins.push(StyledComponentsPlugin());

  const fuse = FuseBox.init(cfg);

  const app = fuse.bundle('app').instructions(`> [renderer/app/index.tsx]`);

  const permissions = fuse
    .bundle('permissions')
    .instructions(`> [renderer/permissions/index.tsx]`);

  if (!production) {
    fuse.dev({ httpServer: true });

    app.hmr().watch();
    permissions.watch();

    fuse.run().then(() => {
      spawn('npm', ['start'], {
        shell: true,
        stdio: 'inherit',
      });
    });
  } else {
    fuse.run();
  }
};

const preload = name => {
  const cfg = getRendererConfig('electron', name);
  const fuse = FuseBox.init(cfg);
  const app = fuse.bundle(name).instructions(`> [preloads/${name}.ts]`);

  if (!production) {
    app.watch();
  }

  fuse.run();
};

renderer();
preload('view-preload');
main();

@@ -1332,96 +1332,46 @@
 nst 
-renderer = () =%3E %7B%0A  const cfg = getRendererConfig('electron');%0A%0A  cfg.plugins.push(%0A   
+getWebIndexPlugin = name =%3E %7B%0A  return
  Web
@@ -1376,34 +1376,32 @@
 ebIndexPlugin(%7B%0A
-  
     template: %60s
@@ -1419,34 +1419,32 @@
 /app.html%60,%0A    
- 
- 
 path: production
@@ -1453,34 +1453,32 @@
  '.' : '/',%0A    
-  
 target: %60app.htm
@@ -1470,19 +1470,23 @@
 arget: %60
-app
+$%7Bname%7D
 .html%60,%0A
@@ -1481,34 +1481,32 @@
 ame%7D.html%60,%0A    
-  
 bundles: %5B'app'%5D
@@ -1503,26 +1503,92 @@
 s: %5B
-'app'%5D,%0A    %7D),%0A  
+name%5D,%0A  %7D);%0A%7D;%0A%0Aconst renderer = () =%3E %7B%0A  const cfg = getRendererConfig('electron'
 );%0A%0A
@@ -1606,21 +1606,19 @@
 ns.push(
-%0A    
+get
 WebIndex
@@ -1628,163 +1628,115 @@
 gin(
-%7B%0A      template: %60static/pages/app.html%60,%0A      path: production ? '.' : '/',%0A      target: %60permissions.html%60,%0A      bundles: %5B'permissions'%5D,%0A    %7D),%0A  
+'app'));%0A  cfg.plugins.push(getWebIndexPlugin('permissions'));%0A  cfg.plugins.push(getWebIndexPlugin('auth')
 );%0A%0A
@@ -2074,24 +2074,105 @@
 ex.tsx%5D%60);%0A%0A
+  const auth = fuse.bundle('auth').instructions(%60%3E %5Brenderer/auth/index.tsx%5D%60);%0A%0A
   if (!produ
@@ -2264,16 +2264,34 @@
 watch();
+%0A    auth.watch();
 %0A%0A    fu

prepare fuse config for auth dialog