<commit_before>// @flow

import chalk from 'chalk';
import fsp from 'fs-promise';
import inquirer from 'inquirer';
import path from 'path';

import { Detach, User as UserManager } from 'xdl';

import type { User } from 'xdl/build/User';

const AUTH_CLIENT_ID = 'MGQh3rK3WZFWhJ91BShagHggMOhrE6nR';
UserManager.initialize(AUTH_CLIENT_ID);

export async function detach() {
  const user = await loginOrRegister();

  const appJsonPath = path.join(process.cwd(), 'app.json');
  const appJson = JSON.parse(await fsp.readFile(appJsonPath));

  if (
    (!appJson.exponent.ios || !appJson.exponent.ios.bundleIdentifier) &&
    process.platform === 'darwin'
  ) {
    console.log(
      `
You'll need to specify an iOS bundle identifier. It must be unique on the App Store if you want to
publish it there. See this StackOverflow question for more information:
  ${chalk.cyan('https://stackoverflow.com/questions/11347470/what-does-bundle-identifier-mean-in-the-ios-project')}
`
    );
    const { iosBundleIdentifier } = await inquirer.prompt([
      {
        name: 'iosBundleIdentifier',
        message: 'What would you like your iOS bundle identifier to be?',
      },
    ]);

    appJson.exponent.ios = appJson.exponent.ios || {};
    appJson.exponent.ios.bundleIdentifier = iosBundleIdentifier;
  }

  // check for android.package field, prompt interactively
  if (!appJson.exponent.android || !appJson.exponent.android.package) {
    console.log(
      `
You'll need to specify an Android package name. It must be unique on the Play Store if you want to
publish it there. See this StackOverflow question for more information:
  ${chalk.cyan('https://stackoverflow.com/questions/6273892/android-package-name-convention')}
`
    );

    const { androidPackage } = await inquirer.prompt([
      {
        name: 'androidPackage',
        message: 'What would you like your Android package name to be?',
      },
    ]);

    appJson.exponent.android = appJson.exponent.android || {};
    appJson.exponent.android.package = androidPackage;
  }

  // update app.json file with new contents
  await fsp.writeFile(appJsonPath, JSON.stringify(appJson, null, 2));

  await Detach.detachAsync(process.cwd());
  // yesno lib doesn't properly shut down. without this the command won't exit
  process.stdin.pause();

  const pkgJson = JSON.parse((await fsp.readFile(path.resolve('package.json'))).toString());

  const entryPoint = `import Exponent from "exponent";
import App from "./App";

Exponent.registerRootComponent(App);
`;
  await fsp.writeFile('index.js', entryPoint);
  pkgJson.main = 'index.js';

  delete pkgJson.scripts.start;
  delete pkgJson.scripts.build;
  delete pkgJson.scripts.eject;
  delete pkgJson.scripts.ios;
  await fsp.writeFile('package.json', JSON.stringify(pkgJson, null, 2));

  console.log(
    `${chalk.green('Successfully set up ExponentKit!')}

You'll need to use Exponent's XDE to run this project:
  ${chalk.cyan('https://docs.getexponent.com/versions/latest/introduction/installation.html')}

For further instructions, please read ExponentKit's build documentation:
  ${chalk.cyan('https://docs.getexponent.com/versions/latest/guides/exponentkit.html#rerun-the-project-in-xde-or-exp')}
`
  );
}

async function loginOrRegister(): Promise<?User> {
  console.log(chalk.yellow('\nAn Exponent account is required to proceed.\n'));
  const currentUser = await UserManager.getCurrentUserAsync();

  if (currentUser) {
    const loggedInQuestions = [
      {
        type: 'list',
        name: 'stayLoggedIn',
        message: `It appears you're already logged in to Exponent as \
${chalk.green(currentUser.nickname)}, would you like to continue with this account?`,
        choices: [
          {
            name: `Yes, continue as ${currentUser.nickname}.`,
            value: true,
          },
          {
            name: "No, I'd like to start a new session.",
            value: false,
          },
        ],
      },
    ];

    const { stayLoggedIn } = await inquirer.prompt(loggedInQuestions);

    if (stayLoggedIn) {
      return currentUser;
    } else {
      await UserManager.logoutAsync();
      console.log(chalk.green('\nSuccessfully logged out!\n'));
    }
  }

  const questions = [
    {
      type: 'list',
      name: 'action',
      message: 'How would you like to authenticate?',
      choices: [
        {
          name: 'Log in with GitHub (opens a browser tab)',
          value: 'github',
        },
        {
          name: 'Make a new Exponent account',
          value: 'register',
        },<commit_msg>Hide GitHub auth with detach until a proper auth workflow is built.<commit_after>// @flow

import chalk from 'chalk';
import fsp from 'fs-promise';
import inquirer from 'inquirer';
import path from 'path';

import { Detach, User as UserManager } from 'xdl';

import type { User } from 'xdl/build/User';

const AUTH_CLIENT_ID = 'MGQh3rK3WZFWhJ91BShagHggMOhrE6nR';
UserManager.initialize(AUTH_CLIENT_ID);

export async function detach() {
  const user = await loginOrRegister();

  const appJsonPath = path.join(process.cwd(), 'app.json');
  const appJson = JSON.parse(await fsp.readFile(appJsonPath));

  if (
    (!appJson.exponent.ios || !appJson.exponent.ios.bundleIdentifier) &&
    process.platform === 'darwin'
  ) {
    console.log(
      `
You'll need to specify an iOS bundle identifier. It must be unique on the App Store if you want to
publish it there. See this StackOverflow question for more information:
  ${chalk.cyan('https://stackoverflow.com/questions/11347470/what-does-bundle-identifier-mean-in-the-ios-project')}
`
    );
    const { iosBundleIdentifier } = await inquirer.prompt([
      {
        name: 'iosBundleIdentifier',
        message: 'What would you like your iOS bundle identifier to be?',
      },
    ]);

    appJson.exponent.ios = appJson.exponent.ios || {};
    appJson.exponent.ios.bundleIdentifier = iosBundleIdentifier;
  }

  // check for android.package field, prompt interactively
  if (!appJson.exponent.android || !appJson.exponent.android.package) {
    console.log(
      `
You'll need to specify an Android package name. It must be unique on the Play Store if you want to
publish it there. See this StackOverflow question for more information:
  ${chalk.cyan('https://stackoverflow.com/questions/6273892/android-package-name-convention')}
`
    );

    const { androidPackage } = await inquirer.prompt([
      {
        name: 'androidPackage',
        message: 'What would you like your Android package name to be?',
      },
    ]);

    appJson.exponent.android = appJson.exponent.android || {};
    appJson.exponent.android.package = androidPackage;
  }

  // update app.json file with new contents
  await fsp.writeFile(appJsonPath, JSON.stringify(appJson, null, 2));

  await Detach.detachAsync(process.cwd());
  // yesno lib doesn't properly shut down. without this the command won't exit
  process.stdin.pause();

  const pkgJson = JSON.parse((await fsp.readFile(path.resolve('package.json'))).toString());

  const entryPoint = `import Exponent from "exponent";
import App from "./App";

Exponent.registerRootComponent(App);
`;
  await fsp.writeFile('index.js', entryPoint);
  pkgJson.main = 'index.js';

  delete pkgJson.scripts.start;
  delete pkgJson.scripts.build;
  delete pkgJson.scripts.eject;
  delete pkgJson.scripts.ios;
  await fsp.writeFile('package.json', JSON.stringify(pkgJson, null, 2));

  console.log(
    `${chalk.green('Successfully set up ExponentKit!')}

You'll need to use Exponent's XDE to run this project:
  ${chalk.cyan('https://docs.getexponent.com/versions/latest/introduction/installation.html')}

For further instructions, please read ExponentKit's build documentation:
  ${chalk.cyan('https://docs.getexponent.com/versions/latest/guides/exponentkit.html#rerun-the-project-in-xde-or-exp')}
`
  );
}

async function loginOrRegister(): Promise<?User> {
  console.log(chalk.yellow('\nAn Exponent account is required to proceed.\n'));
  const currentUser = await UserManager.getCurrentUserAsync();

  if (currentUser) {
    const loggedInQuestions = [
      {
        type: 'list',
        name: 'stayLoggedIn',
        message: `It appears you're already logged in to Exponent as \
${chalk.green(currentUser.nickname)}, would you like to continue with this account?`,
        choices: [
          {
            name: `Yes, continue as ${currentUser.nickname}.`,
            value: true,
          },
          {
            name: "No, I'd like to start a new session.",
            value: false,
          },
        ],
      },
    ];

    const { stayLoggedIn } = await inquirer.prompt(loggedInQuestions);

    if (stayLoggedIn) {
      return currentUser;
    } else {
      await UserManager.logoutAsync();
      console.log(chalk.green('\nSuccessfully logged out!\n'));
    }
  }

  const questions = [
    {
      type: 'list',
      name: 'action',
      message: 'How would you like to authenticate?',
      choices: [
        {
          name: 'Make a new Exponent account',
          value: 'register',
        },
{'fraction_non_alphanumeric': 0.12439347154830172, 'fraction_numerical': 0.004852227613586237, 'mean_word_length': 3.17926267281106, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 8, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6528402', 'n_tokens_mistral': 2840, 'n_tokens_neox': 2745, 'n_words': 724}