/**
 * Copyright (c) Facebook, Inc. and its affiliates.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 *
 */ import fs from 'fs' ; import minimist from 'minimist' ; import path from 'path' ; import process from 'process' ; import printRunInstructions from './printRunInstructions' ; import { createProjectFromTemplate } from '../../tools/generator/templates' ; import * as PackageManager from '../../tools/packageManager' ; import { logger } from '@react-native-community/cli-tools' ; import installPods from '../../tools/installPods' ; param projectDir param argsOrName param options async function initCompat ( projectDir , argsOrName ) { const args = Array . isArray ( argsOrName ) ? argsOrName : [ argsOrName ] . concat ( process . argv . slice ( 4 ) ) ; if ( ! args || args . length === 0 ) { logger . error ( 'react-native init requires a project name.' ) ; return ; } const newProjectName = args [ 0 ] ; const options = minimist ( args ) ; logger . info ( `Setting up new React Native app in ${ projectDir }` ) ; await generateProject ( projectDir , newProjectName , options ) ; } param Absolute param options async function generateProject ( destinationRoot , newProjectName , options ) { const pkgJson = require ( 'react-native/package.json' ) ; const reactVersion = pkgJson . peerDependencies . react ; await createProjectFromTemplate ( destinationRoot , newProjectName , options . template ) ; logger . info ( 'Adding required dependencies' ) ; await PackageManager . install ( [ `react@${ reactVersion }` ] , { root : destinationRoot , } ) ; logger . info ( 'Adding required dev dependencies' ) ; await PackageManager . installDev ( [ '@babel/core' , '@babel/runtime' , '@react-native-community/eslint-config' , 'eslint' , 'jest' , 'babel-jest' , 'metro-react-native-babel-preset' , `react-test-renderer@${ reactVersion }` , ] , { root : destinationRoot , } ) ; addJestToPackageJson ( destinationRoot ) ; if ( process . platform === 'darwin' ) { logger . info ( 'Installing required CocoaPods dependencies' ) ; await installPods ( { directory : destinationRoot } ) ; } printRunInstructions ( destinationRoot , newProjectName ) ; } /**
 * Add Jest-related stuff to package.json, which was created by the react-native-cli.
 */ function addJestToPackageJson ( destinationRoot ) { const packageJSONPath = path . join ( destinationRoot , 'package.json' ) ; const packageJSON = JSON . parse ( fs . readFileSync ( packageJSONPath ) . toString ( ) ) ; packageJSON . scripts . test = 'jest' ; packageJSON . scripts . lint = 'eslint .' ; packageJSON . jest = { preset : 'react-native' , } ; fs . writeFileSync ( packageJSONPath , `${ JSON . stringify ( packageJSON , null , 2 ) }\n` ) ; } export default initCompat ;