2016-07-30 15:59:16 +00:00
|
|
|
/**
|
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This source code is licensed under the BSD-style license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree. An additional grant
|
|
|
|
* of patent rights can be found in the PATENTS file in the same directory.
|
|
|
|
*/
|
|
|
|
'use strict';
|
|
|
|
|
2017-02-06 20:21:13 +00:00
|
|
|
const {
|
|
|
|
listTemplatesAndExit,
|
|
|
|
createProjectFromTemplate,
|
|
|
|
} = require('../generator/templates');
|
2016-11-19 02:25:02 +00:00
|
|
|
const execSync = require('child_process').execSync;
|
|
|
|
const fs = require('fs');
|
|
|
|
const minimist = require('minimist');
|
2016-07-30 15:59:16 +00:00
|
|
|
const path = require('path');
|
2016-11-19 02:25:02 +00:00
|
|
|
const printRunInstructions = require('../generator/printRunInstructions');
|
|
|
|
const process = require('process');
|
|
|
|
const yarn = require('../util/yarn');
|
2016-07-30 15:59:16 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates the template for a React Native project given the provided
|
|
|
|
* parameters:
|
2016-11-19 02:25:02 +00:00
|
|
|
* @param projectDir Templates will be copied here.
|
|
|
|
* @param argsOrName Project name or full list of custom arguments
|
|
|
|
* for the generator.
|
2017-02-06 20:21:13 +00:00
|
|
|
* @param options Command line options passed from the react-native-cli directly.
|
|
|
|
* E.g. `{ version: '0.43.0', template: 'navigation' }`
|
2016-07-30 15:59:16 +00:00
|
|
|
*/
|
|
|
|
function init(projectDir, argsOrName) {
|
2016-11-19 02:25:02 +00:00
|
|
|
const args = Array.isArray(argsOrName)
|
|
|
|
? argsOrName // argsOrName was e.g. ['AwesomeApp', '--verbose']
|
|
|
|
: [argsOrName].concat(process.argv.slice(4)); // argsOrName was e.g. 'AwesomeApp'
|
|
|
|
|
2017-02-06 20:21:13 +00:00
|
|
|
// args array is e.g. ['AwesomeApp', '--verbose', '--template', 'navigation']
|
2016-11-21 08:01:55 +00:00
|
|
|
if (!args || args.length === 0) {
|
2016-11-19 02:25:02 +00:00
|
|
|
console.error('react-native init requires a project name.');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const newProjectName = args[0];
|
|
|
|
const options = minimist(args);
|
|
|
|
|
2017-02-06 20:21:13 +00:00
|
|
|
if (listTemplatesAndExit(newProjectName, options)) {
|
|
|
|
// Just listing templates using 'react-native init --template'
|
|
|
|
// Not creating a new app.
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
console.log('Setting up new React Native app in ' + projectDir);
|
|
|
|
generateProject(projectDir, newProjectName, options);
|
|
|
|
}
|
2016-11-19 02:25:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generates a new React Native project based on the template.
|
|
|
|
* @param Absolute path at which the project folder should be created.
|
|
|
|
* @param options Command line arguments parsed by minimist.
|
|
|
|
*/
|
|
|
|
function generateProject(destinationRoot, newProjectName, options) {
|
|
|
|
var reactNativePackageJson = require('../../package.json');
|
|
|
|
var { peerDependencies } = reactNativePackageJson;
|
|
|
|
if (!peerDependencies) {
|
|
|
|
console.error('Missing React peer dependency in React Native\'s package.json. Aborting.');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var reactVersion = peerDependencies.react;
|
|
|
|
if (!reactVersion) {
|
|
|
|
console.error('Missing React peer dependency in React Native\'s package.json. Aborting.');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const yarnVersion =
|
2016-11-21 08:01:55 +00:00
|
|
|
(!options.npm) &&
|
2016-11-19 02:25:02 +00:00
|
|
|
yarn.getYarnVersionIfAvailable() &&
|
|
|
|
yarn.isGlobalCliUsingYarn(destinationRoot);
|
|
|
|
|
2017-02-06 20:21:13 +00:00
|
|
|
createProjectFromTemplate(destinationRoot, newProjectName, options.template, yarnVersion);
|
2016-07-30 15:59:16 +00:00
|
|
|
|
2016-11-19 02:25:02 +00:00
|
|
|
if (yarnVersion) {
|
|
|
|
console.log('Adding React...');
|
2016-12-06 21:47:49 +00:00
|
|
|
execSync(`yarn add react@${reactVersion}`, {stdio: 'inherit'});
|
2016-11-19 02:25:02 +00:00
|
|
|
} else {
|
|
|
|
console.log('Installing React...');
|
2016-12-06 21:47:49 +00:00
|
|
|
execSync(`npm install react@${reactVersion} --save --save-exact`, {stdio: 'inherit'});
|
2016-11-19 02:25:02 +00:00
|
|
|
}
|
|
|
|
if (!options['skip-jest']) {
|
2016-11-21 08:01:55 +00:00
|
|
|
const jestDeps = (
|
|
|
|
`jest babel-jest babel-preset-react-native react-test-renderer@${reactVersion}`
|
|
|
|
);
|
2016-11-19 02:25:02 +00:00
|
|
|
if (yarnVersion) {
|
|
|
|
console.log('Adding Jest...');
|
2016-12-06 21:47:49 +00:00
|
|
|
execSync(`yarn add ${jestDeps} --dev --exact`, {stdio: 'inherit'});
|
2016-11-19 02:25:02 +00:00
|
|
|
} else {
|
|
|
|
console.log('Installing Jest...');
|
2016-12-06 21:47:49 +00:00
|
|
|
execSync(`npm install ${jestDeps} --save-dev --save-exact`, {stdio: 'inherit'});
|
2016-11-19 02:25:02 +00:00
|
|
|
}
|
|
|
|
addJestToPackageJson(destinationRoot);
|
|
|
|
}
|
|
|
|
printRunInstructions(destinationRoot, newProjectName);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add Jest-related stuff to package.json, which was created by the react-native-cli.
|
|
|
|
*/
|
|
|
|
function addJestToPackageJson(destinationRoot) {
|
|
|
|
var packageJSONPath = path.join(destinationRoot, 'package.json');
|
|
|
|
var packageJSON = JSON.parse(fs.readFileSync(packageJSONPath));
|
2016-07-30 15:59:16 +00:00
|
|
|
|
2016-11-19 02:25:02 +00:00
|
|
|
packageJSON.scripts.test = 'jest';
|
|
|
|
packageJSON.jest = {
|
|
|
|
preset: 'react-native'
|
|
|
|
};
|
2017-10-15 17:42:53 +00:00
|
|
|
fs.writeFileSync(packageJSONPath, JSON.stringify(packageJSON, null, 2));
|
2016-07-30 15:59:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = init;
|