text: /* eslint-disable @typescript-eslint/no-var-requires */
  const prompts = require('prompts');
  const { execSync } = require('child_process');
  const fs = require('fs');
  
  /**
   * @param {string} cmd 
   */
  const runCommand = (cmd) => execSync(`pushd ${__dirname + '\\..\\'} && ${cmd}`);
  
  /**
   * @param {string} pm 
   * @param {string | undefined} packages 
   */
  const addPackages = (pm, packages) => runCommand(pm === 'yarn' ? ( packages === undefined ? 'yarn' : 'yarn add ' + packages ) : ( packages === undefined ? 'npm install' : 'npm install' + packages ));
  
  (async () => {
      const result = await prompts({
          name: 'check',
          type: 'confirm',
          message: 'Do you really want to setup Electron for this project?'
      });
  
      if (result.check) { 
          const result2 = await prompts({
              name: 'pm',
              type: 'select',
              message: 'Which PackageManager do you want to use for install packages?',
              choices: [
                  {
                      title: 'npm',
                      value: 'npm',
                      selected: true,
                      description: 'https://www.npmjs.com/'
                  },
                  {
                      title: 'yarn',
                      value: 'yarn',
                      description: 'https://yarnpkg.com/'
                  }
              ]
          });
  
          const packageManager = result2.pm;
  
          console.log('Installing and updating default dependencies...');
  
          addPackages(packageManager);
          addPackages(packageManager, 'electron');
  
          console.log('Started rewriting of package.json...');
  
          // function for rewriting package.json file
          (() => {
              const content = fs.readFileSync(__dirname  + '/../package.json');
              const json = JSON.parse(content);
              json['scripts']['electron'] = 'electron build\\electron\\index.js';
              json['scripts']['dev:electron'] = 'tsc && electron build\\electron\\index.js';
              fs.unlinkSync(__dirname + '/../package.json');
              fs.writeFileSync(__dirname + '/../package.json', JSON.stringify(json, undefined, 4));
          })();
  
          console.log('Finished rewriting\nCreate default files...');
  
          // function for rewriting package.json file
          (() => {
              runCommand('cd src && mkdir electron');
              fs.writeFileSync(__dirname + '/../src/electron/index.ts', `
                  import { app, BrowserWindow } from 'electron';
                  import { queryArgument } from '../util';
                  import { start } from '../web/index';
  
                  const parsePort: () => number = () => {
                      const argument = queryArgument(process.argv, 'port');
                      if (argument === undefined || typeof argument !== 'string') {
                          return 8080;
                      }
                      try {
                          return parseInt(argument);
                      } catch (error) {
                          return 8080;
                      }
                  };
  
                  const nextWindow = () => {
                      const window = new BrowserWindow({
                          center: true,
                          title: 'Angular App Creator'
                      });
  
                      window.loadURL(\`http://localhost:$\{parsePort()}/\`);
                  };
  
                  start();
  
                  app.on('ready', () => {
                      nextWindow();
                  });
  
                  app.on('window-all-closed', () => {
                      if (process.platform !== 'darwin') {
                          app.quit();
                      }
                  });
              `);
          })();
  
          console.log('Default files were created.\nExit Program');
          process.exit(0);
          
      }
  })();