{"size": 2677, "ext": "js", "max_stars_repo_path": "index.js", "max_stars_repo_name": "charpeni/ios-simulator-app-exporter", "max_stars_repo_stars_event_min_datetime": "2019-04-11T13:31:57.000Z", "max_stars_repo_stars_event_max_datetime": "2022-02-08T11:24:33.000Z", "max_issues_repo_path": "index.js", "max_issues_repo_name": "charpeni/ios-simulator-app-exporter", "max_issues_repo_issues_event_min_datetime": "2019-09-11T06:28:55.000Z", "max_issues_repo_issues_event_max_datetime": "2021-08-01T06:07:30.000Z", "max_forks_repo_path": "index.js", "max_forks_repo_name": "charpeni/ios-simulator-app-exporter", "max_forks_repo_forks_event_min_datetime": "2019-04-12T14:06:16.000Z", "max_forks_repo_forks_event_max_datetime": "2019-06-25T13:59:43.000Z"}
{"max_stars_count": 75, "max_issues_count": 45, "max_forks_count": 2, "avg_line_length": 22.8803418803, "max_line_length": 73, "alphanum_fraction": 0.5677997759}
#!/usr/bin/env node

const inquirer = require('inquirer');
const chalk = require('chalk');
const program = require('commander');

const getDevices = require('./getAppsByDevice');
const { version } = require('./package.json');

program
  .version(version, '-v, --version', 'output the version number')
  .parse(process.argv);

function activeDevicesOnlyFilter(device) {
  return device.state === 3;
}

function checkRequirements(devices) {
  if (devices.length === 0) {
    console.log('🛑  There are no devices.');
    process.exit();
  }

  if (devices.every(device => device.apps.length === 0)) {
    console.log('🛑  There are no available apps.');
    process.exit();
  }
}

function prettifyRuntimeVersion(runtime) {
  return runtime
    .split('.')
    .pop()
    .split('-')
    .reduce((acc, value, index) => {
      if (index === 0) {
        return `${value} `;
      }
      if (index > 1) {
        return `${acc}.${value}`;
      }

      return acc + value;
    }, '');
}

async function selectAnApp(devices) {
  checkRequirements(devices);

  const { application } = await inquirer.prompt([
    {
      type: 'list',
      name: 'application',
      message: 'Select the application you would like to export',
      choices: devices.reduce((acc, device) => {
        if (device.apps.length === 0) {
          return acc;
        }

        return [
          ...acc,
          ...device.apps.map(app => ({
            name: `${device.name} (${prettifyRuntimeVersion(
              device.runtime
            )}) - ${app.CFBundleDisplayName || app.CFBundleName} [${
              app.CFBundleIdentifier
            } - ${app.CFBundleVersion}]`,
            value: {
              device,
              app,
            },
          })),
        ];
      }, []),
    },
  ]);

  console.log(`🥧   ${chalk.bold('Full path:')} ${application.app.file}`);
  console.log(
    `📁  ${chalk.bold('Directory:')} ${application.app.file
      .split('/')
      .slice(0, -1)
      .join('/')}`
  );

  return application;
}

async function main() {
  console.log('🔍  Scanning iOS simulators...');

  const devices = getDevices();
  let activeDevicesOnly = false;

  checkRequirements(devices);

  if (devices.some(activeDevicesOnlyFilter)) {
    const answer = await inquirer.prompt([
      {
        type: 'confirm',
        name: 'activeDevicesOnly',
        message: 'Would you like to filter on active devices?',
        default: true,
      },
    ]);

    // eslint-disable-next-line prefer-destructuring
    activeDevicesOnly = answer.activeDevicesOnly;
  }

  return selectAnApp(
    activeDevicesOnly ? devices.filter(activeDevicesOnlyFilter) : devices
  );
}

main();
