<commit_before>/**
 * @flow
 */

import slug from 'slugify';

import path from 'path';
import jsonfile from 'jsonfile';
import fse from 'fs-extra';
import spawnAsync from '@exponent/spawn-async';
import expJsonTemplate from './templates/exp';
import babelRcTemplate from './templates/babelrc';

type params = {
  projectName: string,
  projectDescription: string,
  projectEntryPoint: string,
}

export default async function convertProjectAsync(projectDir:string, {projectName, projectDescription, projectEntryPoint}:params) {
  let projectSlug = slug(projectName.toLowerCase())

  let expJsonTargetPath = path.join(projectDir, '/exp.json');
  let babelRcTargetPath = path.join(projectDir, '/.babelrc');

  let packageJsonSourcePath = path.join(projectDir, '/package.json');
  let packageJsonTargetPath = path.join(projectDir, '/package.json');

  // Add values to exp.json,) save to given path
  let expJson = {...expJsonTemplate};
  expJson.name = projectName;
  expJson.description = projectDescription || 'No description';
  expJson.slug = projectSlug;
  jsonfile.writeFileSync(expJsonTargetPath, expJson, {spaces: 2});
  console.log('Wrote exp.json');

  // Add entry point and dependencies to package.json
  let unsupportedPackagesUsed = [];
  let packageJson = jsonfile.readFileSync(packageJsonSourcePath);
  packageJson.dependencies = {...packageJson.dependencies, ...dependencies};

  // Remove
  Object.keys(packageJson.dependencies).forEach(dep => {
    if (unsupportedPackages[dep]) {
      delete packageJson.dependencies[dep];
      unsupportedPackagesUsed.push(dep);
    }
  });

  if (projectEntryPoint !== 'index.*.js' && projectEntryPoint !== 'index.js' && projectEntryPoint !== 'index.ios.js' && projectEntryPoint !== 'index.android.js') {
    packageJson.main = projectEntryPoint;
  }
  jsonfile.writeFileSync(packageJsonTargetPath, packageJson, {spaces: 2});
  console.log('Updated package.json');

  // TODO: Add import Exponent from 'exponent'; at the top of main file
  // TODO: Add .exponent/* to gitignore

  // Copy babelrc
  jsonfile.writeFileSync(babelRcTargetPath, babelRcTemplate, {spaces: 2});
  console.log('Updated .babelrc');

  // Save next steps to a file, display, exit
  await installAndInstructAsync(projectDir, unsupportedPackagesUsed);
}

const dependencies = {
  "@exponent/vector-icons": "^1.0.1",
  "exponent": "^10.0.0",
  "react": "15.3.1",
  "react-native": "github:exponentjs/react-native#sdk-10.0.0"
};

const unsupportedPackages = {
  'react-native-vector-icons': `We installed @exponent/vector-icons for you instead. Change any use of react-native-vector-icons to this.
For example: "import Icon from 'react-native-vector-icons/Ionicons'" becomes "import { Ionicons as Icon } from '@exponent/vector-icons'" `,
  'react-native-video': `Exponent provides a video component for you with the same API as react-native-video. You can use it with "import { Components } from 'exponent';" and <Components.Video /> in your render function. `,
  'react-native-svg': `Exponent provides react-native-svg for you. You can use it with "import { Components } from 'exponent';" and <Components.Svg /> in your render function.`,
  'react-native-maps': `Exponent provides react-native-maps for you. You can use it with "import { Components } from 'exponent';" and <Components.Map /> in your render function.`,
};

function showCompatibilityMessage(packages) {
  if (packages.length) {
    return `Resolve any issues with potentially incompatible packages: \n\n` + packages.map(pkg => {
      return `** ${pkg}: ${unsupportedPackages[pkg]}`
    }).join('\n') + `\n** This may not be an exhaustive list of packages you will need to address -- any package that has a native code dependency will need to be converted to an Exponent equivalent or removed. Refer to the SDK API reference here: https://docs.getexponent.com/versions/latest/sdk/index.html`;
  } else {
    return `We didn't detect any known incompatible packages, but if you have any with native dependencies installed, you will need to remove them from your project.`
  }
}

async function installAndInstructAsync(projectDir, unsupportedPackagesUsed) {
  let nodeModulesPath = path.join(projectDir, '/node_modules');<commit_msg>Install exponent fork of vector-icons and warn about linear-gradient<commit_after>/**
 * @flow
 */

import slug from 'slugify';

import path from 'path';
import jsonfile from 'jsonfile';
import fse from 'fs-extra';
import spawnAsync from '@exponent/spawn-async';
import expJsonTemplate from './templates/exp';
import babelRcTemplate from './templates/babelrc';

type params = {
  projectName: string,
  projectDescription: string,
  projectEntryPoint: string,
}

export default async function convertProjectAsync(projectDir:string, {projectName, projectDescription, projectEntryPoint}:params) {
  let projectSlug = slug(projectName.toLowerCase())

  let expJsonTargetPath = path.join(projectDir, '/exp.json');
  let babelRcTargetPath = path.join(projectDir, '/.babelrc');

  let packageJsonSourcePath = path.join(projectDir, '/package.json');
  let packageJsonTargetPath = path.join(projectDir, '/package.json');

  // Add values to exp.json,) save to given path
  let expJson = {...expJsonTemplate};
  expJson.name = projectName;
  expJson.description = projectDescription || 'No description';
  expJson.slug = projectSlug;
  jsonfile.writeFileSync(expJsonTargetPath, expJson, {spaces: 2});
  console.log('Wrote exp.json');

  // Add entry point and dependencies to package.json
  let unsupportedPackagesUsed = [];
  let packageJson = jsonfile.readFileSync(packageJsonSourcePath);
  packageJson.dependencies = {...packageJson.dependencies, ...dependencies};

  // Remove
  Object.keys(packageJson.dependencies).forEach(dep => {
    if (unsupportedPackages[dep]) {
      if (dep === 'react-native-vector-icons') {
        packageJson.dependencies[dep] = 'github:exponentjs/react-native-vector-icons';
      } else {
        delete packageJson.dependencies[dep];
      }
      unsupportedPackagesUsed.push(dep);
    }
  });

  if (projectEntryPoint !== 'index.*.js' && projectEntryPoint !== 'index.js' && projectEntryPoint !== 'index.ios.js' && projectEntryPoint !== 'index.android.js') {
    packageJson.main = projectEntryPoint;
  }
  jsonfile.writeFileSync(packageJsonTargetPath, packageJson, {spaces: 2});
  console.log('Updated package.json');

  // TODO: Add import Exponent from 'exponent'; at the top of main file
  // TODO: Add .exponent/* to gitignore

  // Copy babelrc
  jsonfile.writeFileSync(babelRcTargetPath, babelRcTemplate, {spaces: 2});
  console.log('Updated .babelrc');

  // Save next steps to a file, display, exit
  await installAndInstructAsync(projectDir, unsupportedPackagesUsed);
}

const dependencies = {
  "@exponent/vector-icons": "^1.0.1",
  "exponent": "^10.0.0",
  "react": "15.3.1",
  "react-native": "github:exponentjs/react-native#sdk-10.0.0"
};

const unsupportedPackages = {
  'react-native-vector-icons': `We installed @exponent/vector-icons for you instead. When you feel like it, you can change any use of react-native-vector-icons to this. But no rush, we also installed our fork of react-native-vector-icons that uses @exponent/vector-icons behind the scenes, so you don't actually *need* to make any changes.
When you want to convert it, you'll just need to do something like this: "import Icon from 'react-native-vector-icons/Ionicons'" becomes "import { Ionicons as Icon } from '@exponent/vector-icons'" `,
  'react-native-video': `Exponent provides a video component for you with the same API as react-native-video. You can use it with "import { Components } from 'exponent';" and <Components.Video /> in your render function. `,
  'react-native-svg': `Exponent provides react-native-svg for you. You can use it with "import { Components } from 'exponent';" and <Components.Svg /> in your render function.`,
  'react-native-maps': `Exponent provides react-native-maps for you. You can use it with "import { Components } from 'exponent';" and <Components.Map /> in your render function.`,
  'react-native-linear-gradient': `Exponent provides react-native-linear-gradient for you. You can use it with "import { Components } from 'exponent';" and <Components.LinearGradient /> in your render function.`,
};

function showCompatibilityMessage(packages) {
  if (packages.length) {
    return `Resolve any issues with potentially incompatible packages: \n\n` + packages.map(pkg => {
      return `** ${pkg}: ${unsupportedPackages[pkg]}`
    }).join('\n') + `\n** This may not be an exhaustive list of packages you will need to address -- any package that has a native code dependency will need to be converted to an Exponent equivalent or removed. Refer to the SDK API reference here: https://docs.getexponent.com/versions/latest/sdk/index.html`;
  } else {
    return `We didn't detect any known incompatible packages, but if you have any with native dependencies installed, you will need to remove them from your project.`
  }
}

async function installAndInstructAsync(projectDir, unsupportedPackagesUsed) {
  let nodeModulesPath = path.join(projectDir, '/node_modules');
{'fraction_non_alphanumeric': 0.124110758454635, 'fraction_numerical': 0.00394002407792492, 'mean_word_length': 4.548269581056466, 'pattern_counts': {'":': 8, '<': 10, '<?xml version=': 0, '>': 14, 'https://': 2, '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': '4083709', 'n_tokens_mistral': 2689, 'n_tokens_neox': 2619, 'n_words': 818}