2015-03-20 00:46:43 +00:00
|
|
|
#!/usr/bin/env node
|
|
|
|
|
|
|
|
/**
|
2015-10-05 18:16:52 +00:00
|
|
|
* Copyright (c) 2015-present, Facebook, Inc.
|
|
|
|
*
|
2018-02-17 02:24:55 +00:00
|
|
|
* This source code is licensed under the MIT license found in the
|
|
|
|
* LICENSE file in the root directory of this source tree.
|
2015-03-20 00:46:43 +00:00
|
|
|
*/
|
|
|
|
|
2015-10-05 18:16:52 +00:00
|
|
|
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
// /!\ DO NOT MODIFY THIS FILE /!\
|
|
|
|
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
//
|
|
|
|
// react-native-cli is installed globally on people's computers. This means
|
|
|
|
// that it is extremely difficult to have them upgrade the version and
|
|
|
|
// because there's only one global version installed, it is very prone to
|
|
|
|
// breaking changes.
|
|
|
|
//
|
|
|
|
// The only job of react-native-cli is to init the repository and then
|
|
|
|
// forward all the commands to the local version of react-native.
|
|
|
|
//
|
|
|
|
// If you need to add a new command, please add it to local-cli/.
|
|
|
|
//
|
|
|
|
// The only reason to modify this file is to add more warnings and
|
|
|
|
// troubleshooting information for the `react-native init` command.
|
|
|
|
//
|
2017-06-06 18:19:00 +00:00
|
|
|
// To allow for graceful failure on older node versions, this file should
|
|
|
|
// retain ES5 compatibility.
|
|
|
|
//
|
2015-10-05 18:16:52 +00:00
|
|
|
// Do not make breaking changes! We absolutely don't want to have to
|
|
|
|
// tell people to update their global version of react-native-cli.
|
|
|
|
//
|
|
|
|
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
// /!\ DO NOT MODIFY THIS FILE /!\
|
|
|
|
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
2015-09-06 21:06:33 +00:00
|
|
|
'use strict';
|
|
|
|
|
2015-03-22 20:27:32 +00:00
|
|
|
var fs = require('fs');
|
2015-03-20 00:46:43 +00:00
|
|
|
var path = require('path');
|
2015-08-07 17:59:59 +00:00
|
|
|
var exec = require('child_process').exec;
|
2016-11-01 16:13:49 +00:00
|
|
|
var execSync = require('child_process').execSync;
|
2015-10-21 19:47:38 +00:00
|
|
|
var chalk = require('chalk');
|
2015-09-06 21:06:33 +00:00
|
|
|
var prompt = require('prompt');
|
2015-10-21 19:47:38 +00:00
|
|
|
var semver = require('semver');
|
2016-03-24 12:30:49 +00:00
|
|
|
/**
|
|
|
|
* Used arguments:
|
|
|
|
* -v --version - to print current version of react-native-cli and react-native dependency
|
|
|
|
* if you are in a RN app folder
|
|
|
|
* init - to create a new project and npm install it
|
|
|
|
* --verbose - to print logs while init
|
2017-02-06 20:21:13 +00:00
|
|
|
* --template - name of the template to use, e.g. --template navigation
|
2016-03-24 12:30:49 +00:00
|
|
|
* --version <alternative react-native package> - override default (https://registry.npmjs.org/react-native@latest),
|
|
|
|
* package to install, examples:
|
|
|
|
* - "0.22.0-rc1" - A new app will be created using a specific version of React Native from npm repo
|
|
|
|
* - "https://registry.npmjs.org/react-native/-/react-native-0.20.0.tgz" - a .tgz archive from any npm repo
|
|
|
|
* - "/Users/home/react-native/react-native-0.22.0.tgz" - for package prepared with `npm pack`, useful for e2e tests
|
|
|
|
*/
|
2016-12-06 21:21:35 +00:00
|
|
|
|
|
|
|
var options = require('minimist')(process.argv.slice(2));
|
2015-03-20 00:46:43 +00:00
|
|
|
|
2015-03-22 21:04:55 +00:00
|
|
|
var CLI_MODULE_PATH = function() {
|
|
|
|
return path.resolve(
|
|
|
|
process.cwd(),
|
|
|
|
'node_modules',
|
|
|
|
'react-native',
|
2015-09-16 16:17:13 +00:00
|
|
|
'cli.js'
|
2015-03-22 21:04:55 +00:00
|
|
|
);
|
|
|
|
};
|
2015-03-20 00:46:43 +00:00
|
|
|
|
2015-10-21 19:47:38 +00:00
|
|
|
var REACT_NATIVE_PACKAGE_JSON_PATH = function() {
|
|
|
|
return path.resolve(
|
|
|
|
process.cwd(),
|
|
|
|
'node_modules',
|
|
|
|
'react-native',
|
|
|
|
'package.json'
|
|
|
|
);
|
|
|
|
};
|
2016-12-16 14:41:38 +00:00
|
|
|
|
|
|
|
if (options._.length === 0 && (options.v || options.version)) {
|
|
|
|
printVersionsAndExit(REACT_NATIVE_PACKAGE_JSON_PATH());
|
|
|
|
}
|
2015-10-21 19:47:38 +00:00
|
|
|
|
2016-11-01 16:13:49 +00:00
|
|
|
// Use Yarn if available, it's much faster than the npm client.
|
|
|
|
// Return the version of yarn installed on the system, null if yarn is not available.
|
|
|
|
function getYarnVersionIfAvailable() {
|
2016-11-28 11:56:26 +00:00
|
|
|
var yarnVersion;
|
2016-11-01 16:13:49 +00:00
|
|
|
try {
|
|
|
|
// execSync returns a Buffer -> convert to string
|
|
|
|
if (process.platform.startsWith('win')) {
|
2017-05-26 22:55:43 +00:00
|
|
|
yarnVersion = (execSync('yarn --version 2> NUL').toString() || '').trim();
|
2016-11-01 16:13:49 +00:00
|
|
|
} else {
|
|
|
|
yarnVersion = (execSync('yarn --version 2>/dev/null').toString() || '').trim();
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
// yarn < 0.16 has a 'missing manifest' bug
|
|
|
|
try {
|
|
|
|
if (semver.gte(yarnVersion, '0.16.0')) {
|
|
|
|
return yarnVersion;
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
console.error('Cannot parse yarn version: ' + yarnVersion);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-20 00:46:43 +00:00
|
|
|
var cli;
|
2015-09-16 16:17:13 +00:00
|
|
|
var cliPath = CLI_MODULE_PATH();
|
|
|
|
if (fs.existsSync(cliPath)) {
|
|
|
|
cli = require(cliPath);
|
|
|
|
}
|
2015-03-20 00:46:43 +00:00
|
|
|
|
2016-12-06 21:21:35 +00:00
|
|
|
var commands = options._;
|
2015-03-20 00:46:43 +00:00
|
|
|
if (cli) {
|
|
|
|
cli.run();
|
|
|
|
} else {
|
2016-12-06 21:21:35 +00:00
|
|
|
if (options._.length === 0 && (options.h || options.help)) {
|
2016-11-12 07:47:51 +00:00
|
|
|
console.log([
|
|
|
|
'',
|
|
|
|
' Usage: react-native [command] [options]',
|
|
|
|
'',
|
|
|
|
'',
|
|
|
|
' Commands:',
|
|
|
|
'',
|
|
|
|
' init <ProjectName> [options] generates a new project and installs its dependencies',
|
|
|
|
'',
|
|
|
|
' Options:',
|
|
|
|
'',
|
|
|
|
' -h, --help output usage information',
|
2017-02-06 20:21:13 +00:00
|
|
|
' -v, --version use a specific version of React Native',
|
|
|
|
' --template use an app template. Use --template to see available templates.',
|
2016-11-12 07:47:51 +00:00
|
|
|
'',
|
|
|
|
].join('\n'));
|
|
|
|
process.exit(0);
|
|
|
|
}
|
|
|
|
|
2016-03-24 12:30:49 +00:00
|
|
|
if (commands.length === 0) {
|
2015-03-20 00:46:43 +00:00
|
|
|
console.error(
|
2016-11-12 07:47:51 +00:00
|
|
|
'You did not pass any commands, run `react-native --help` to see a list of all available commands.'
|
2015-03-20 00:46:43 +00:00
|
|
|
);
|
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
|
2016-03-24 12:30:49 +00:00
|
|
|
switch (commands[0]) {
|
2015-04-04 01:00:23 +00:00
|
|
|
case 'init':
|
2016-03-24 12:30:49 +00:00
|
|
|
if (!commands[1]) {
|
2015-03-22 20:27:32 +00:00
|
|
|
console.error(
|
2016-01-09 15:26:29 +00:00
|
|
|
'Usage: react-native init <ProjectName> [--verbose]'
|
2015-03-22 20:27:32 +00:00
|
|
|
);
|
|
|
|
process.exit(1);
|
2016-03-24 12:30:49 +00:00
|
|
|
} else {
|
2016-12-06 21:21:35 +00:00
|
|
|
init(commands[1], options);
|
2015-03-22 20:27:32 +00:00
|
|
|
}
|
2015-04-04 01:00:23 +00:00
|
|
|
break;
|
|
|
|
default:
|
2015-03-20 00:46:43 +00:00
|
|
|
console.error(
|
2015-03-27 04:12:18 +00:00
|
|
|
'Command `%s` unrecognized. ' +
|
2016-06-03 00:43:00 +00:00
|
|
|
'Make sure that you have run `npm install` and that you are inside a react-native project.',
|
2016-03-24 12:30:49 +00:00
|
|
|
commands[0]
|
2015-03-20 00:46:43 +00:00
|
|
|
);
|
|
|
|
process.exit(1);
|
2015-04-04 01:00:23 +00:00
|
|
|
break;
|
2015-03-20 00:46:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-01 16:13:49 +00:00
|
|
|
function validateProjectName(name) {
|
2017-02-18 03:59:12 +00:00
|
|
|
if (!String(name).match(/^[$A-Z_][0-9A-Z_$]*$/i)) {
|
2015-05-15 08:21:38 +00:00
|
|
|
console.error(
|
|
|
|
'"%s" is not a valid name for a project. Please use a valid identifier ' +
|
|
|
|
'name (alphanumeric).',
|
|
|
|
name
|
|
|
|
);
|
|
|
|
process.exit(1);
|
|
|
|
}
|
2015-05-23 20:28:04 +00:00
|
|
|
|
|
|
|
if (name === 'React') {
|
|
|
|
console.error(
|
2015-05-23 22:59:33 +00:00
|
|
|
'"%s" is not a valid name for a project. Please do not use the ' +
|
|
|
|
'reserved word "React".',
|
2015-05-23 20:28:04 +00:00
|
|
|
name
|
|
|
|
);
|
|
|
|
process.exit(1);
|
|
|
|
}
|
2015-05-15 08:21:38 +00:00
|
|
|
}
|
|
|
|
|
2016-11-01 16:13:49 +00:00
|
|
|
/**
|
|
|
|
* @param name Project name, e.g. 'AwesomeApp'.
|
2016-12-06 21:21:35 +00:00
|
|
|
* @param options.verbose If true, will run 'npm install' in verbose mode (for debugging).
|
|
|
|
* @param options.version Version of React Native to install, e.g. '0.38.0'.
|
|
|
|
* @param options.npm If true, always use the npm command line client,
|
2016-11-01 16:13:49 +00:00
|
|
|
* don't use yarn even if available.
|
|
|
|
*/
|
2016-12-06 21:21:35 +00:00
|
|
|
function init(name, options) {
|
2016-11-01 16:13:49 +00:00
|
|
|
validateProjectName(name);
|
2015-03-26 18:19:18 +00:00
|
|
|
|
2015-05-15 07:51:55 +00:00
|
|
|
if (fs.existsSync(name)) {
|
2016-12-06 21:21:35 +00:00
|
|
|
createAfterConfirmation(name, options);
|
2015-05-15 07:51:55 +00:00
|
|
|
} else {
|
2016-12-06 21:21:35 +00:00
|
|
|
createProject(name, options);
|
2015-05-15 07:51:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-06 21:21:35 +00:00
|
|
|
function createAfterConfirmation(name, options) {
|
2015-05-18 17:25:17 +00:00
|
|
|
prompt.start();
|
2015-05-15 07:51:55 +00:00
|
|
|
|
2015-05-18 17:25:17 +00:00
|
|
|
var property = {
|
|
|
|
name: 'yesno',
|
2015-12-12 12:10:36 +00:00
|
|
|
message: 'Directory ' + name + ' already exists. Continue?',
|
2015-05-18 17:25:17 +00:00
|
|
|
validator: /y[es]*|n[o]?/,
|
|
|
|
warning: 'Must respond yes or no',
|
|
|
|
default: 'no'
|
|
|
|
};
|
2015-05-15 07:51:55 +00:00
|
|
|
|
2015-05-18 17:25:17 +00:00
|
|
|
prompt.get(property, function (err, result) {
|
|
|
|
if (result.yesno[0] === 'y') {
|
2016-12-06 21:21:35 +00:00
|
|
|
createProject(name, options);
|
2015-05-18 17:25:17 +00:00
|
|
|
} else {
|
|
|
|
console.log('Project initialization canceled');
|
|
|
|
process.exit();
|
|
|
|
}
|
|
|
|
});
|
2015-05-15 07:51:55 +00:00
|
|
|
}
|
|
|
|
|
2016-12-06 21:21:35 +00:00
|
|
|
function createProject(name, options) {
|
2015-03-22 20:27:32 +00:00
|
|
|
var root = path.resolve(name);
|
|
|
|
var projectName = path.basename(root);
|
|
|
|
|
2015-03-20 00:46:43 +00:00
|
|
|
console.log(
|
2015-03-22 20:27:32 +00:00
|
|
|
'This will walk you through creating a new React Native project in',
|
|
|
|
root
|
2015-03-20 00:46:43 +00:00
|
|
|
);
|
|
|
|
|
2015-03-22 20:27:32 +00:00
|
|
|
if (!fs.existsSync(root)) {
|
|
|
|
fs.mkdirSync(root);
|
|
|
|
}
|
|
|
|
|
|
|
|
var packageJson = {
|
|
|
|
name: projectName,
|
|
|
|
version: '0.0.1',
|
2015-10-21 10:27:37 +00:00
|
|
|
private: true,
|
|
|
|
scripts: {
|
2017-01-20 14:42:16 +00:00
|
|
|
start: 'node node_modules/react-native/local-cli/cli.js start',
|
|
|
|
ios: 'react-native run-ios',
|
|
|
|
android: 'react-native run-android',
|
2015-10-21 10:27:37 +00:00
|
|
|
}
|
2015-03-22 20:27:32 +00:00
|
|
|
};
|
|
|
|
fs.writeFileSync(path.join(root, 'package.json'), JSON.stringify(packageJson));
|
|
|
|
process.chdir(root);
|
|
|
|
|
2016-12-06 21:21:35 +00:00
|
|
|
run(root, projectName, options);
|
2016-03-24 12:30:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function getInstallPackage(rnPackage) {
|
|
|
|
var packageToInstall = 'react-native';
|
2016-11-01 16:13:49 +00:00
|
|
|
var isValidSemver = semver.valid(rnPackage);
|
|
|
|
if (isValidSemver) {
|
|
|
|
packageToInstall += '@' + isValidSemver;
|
2016-03-24 12:30:49 +00:00
|
|
|
} else if (rnPackage) {
|
|
|
|
// for tar.gz or alternative paths
|
|
|
|
packageToInstall = rnPackage;
|
2016-01-07 03:43:01 +00:00
|
|
|
}
|
2016-03-24 12:30:49 +00:00
|
|
|
return packageToInstall;
|
2016-01-09 15:26:29 +00:00
|
|
|
}
|
|
|
|
|
2016-12-06 21:21:35 +00:00
|
|
|
function run(root, projectName, options) {
|
2017-06-06 18:19:00 +00:00
|
|
|
var rnPackage = options.version; // e.g. '0.38' or '/path/to/archive.tgz'
|
|
|
|
var forceNpmClient = options.npm;
|
|
|
|
var yarnVersion = (!forceNpmClient) && getYarnVersionIfAvailable();
|
2016-11-28 11:56:26 +00:00
|
|
|
var installCommand;
|
2016-12-06 21:21:35 +00:00
|
|
|
if (options.installCommand) {
|
|
|
|
// In CI environments it can be useful to provide a custom command,
|
|
|
|
// to set up and use an offline mirror for installing dependencies, for example.
|
|
|
|
installCommand = options.installCommand;
|
2016-11-01 16:13:49 +00:00
|
|
|
} else {
|
2016-12-06 21:21:35 +00:00
|
|
|
if (yarnVersion) {
|
|
|
|
console.log('Using yarn v' + yarnVersion);
|
|
|
|
console.log('Installing ' + getInstallPackage(rnPackage) + '...');
|
|
|
|
installCommand = 'yarn add ' + getInstallPackage(rnPackage) + ' --exact';
|
|
|
|
if (options.verbose) {
|
|
|
|
installCommand += ' --verbose';
|
|
|
|
}
|
|
|
|
} else {
|
2016-12-09 21:04:57 +00:00
|
|
|
console.log('Installing ' + getInstallPackage(rnPackage) + '...');
|
2016-12-06 21:21:35 +00:00
|
|
|
if (!forceNpmClient) {
|
|
|
|
console.log('Consider installing yarn to make this faster: https://yarnpkg.com');
|
|
|
|
}
|
|
|
|
installCommand = 'npm install --save --save-exact ' + getInstallPackage(rnPackage);
|
|
|
|
if (options.verbose) {
|
|
|
|
installCommand += ' --verbose';
|
|
|
|
}
|
2016-11-01 16:13:49 +00:00
|
|
|
}
|
|
|
|
}
|
2016-12-06 21:21:35 +00:00
|
|
|
try {
|
|
|
|
execSync(installCommand, {stdio: 'inherit'});
|
|
|
|
} catch (err) {
|
|
|
|
console.error(err);
|
|
|
|
console.error('Command `' + installCommand + '` failed.');
|
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
checkNodeVersion();
|
|
|
|
cli = require(CLI_MODULE_PATH());
|
|
|
|
cli.init(root, projectName);
|
2015-03-20 00:46:43 +00:00
|
|
|
}
|
2015-09-06 21:06:33 +00:00
|
|
|
|
2015-10-21 19:47:38 +00:00
|
|
|
function checkNodeVersion() {
|
|
|
|
var packageJson = require(REACT_NATIVE_PACKAGE_JSON_PATH());
|
|
|
|
if (!packageJson.engines || !packageJson.engines.node) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!semver.satisfies(process.version, packageJson.engines.node)) {
|
|
|
|
console.error(chalk.red(
|
|
|
|
'You are currently running Node %s but React Native requires %s. ' +
|
2015-10-27 23:42:25 +00:00
|
|
|
'Please use a supported version of Node.\n' +
|
|
|
|
'See https://facebook.github.io/react-native/docs/getting-started.html'
|
2015-10-21 19:47:38 +00:00
|
|
|
),
|
|
|
|
process.version,
|
|
|
|
packageJson.engines.node);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-16 14:41:38 +00:00
|
|
|
function printVersionsAndExit(reactNativePackageJsonPath) {
|
|
|
|
console.log('react-native-cli: ' + require('./package.json').version);
|
|
|
|
try {
|
|
|
|
console.log('react-native: ' + require(reactNativePackageJsonPath).version);
|
|
|
|
} catch (e) {
|
|
|
|
console.log('react-native: n/a - not inside a React Native project directory');
|
2015-09-06 21:06:33 +00:00
|
|
|
}
|
2016-12-16 14:41:38 +00:00
|
|
|
process.exit();
|
2015-09-06 21:06:33 +00:00
|
|
|
}
|