2017-02-06 20:21:13 +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.
|
2018-05-11 19:43:49 +00:00
|
|
|
*
|
|
|
|
* @format
|
2017-02-06 20:21:13 +00:00
|
|
|
*/
|
2018-05-11 19:43:49 +00:00
|
|
|
|
2017-02-06 20:21:13 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const copyProjectTemplateAndReplace = require('./copyProjectTemplateAndReplace');
|
|
|
|
const execSync = require('child_process').execSync;
|
|
|
|
const fs = require('fs');
|
|
|
|
const path = require('path');
|
|
|
|
|
2017-02-27 17:12:14 +00:00
|
|
|
/**
|
|
|
|
* Templates released as part of react-native in local-cli/templates.
|
|
|
|
*/
|
|
|
|
const builtInTemplates = {
|
2017-02-06 20:21:13 +00:00
|
|
|
navigation: 'HelloNavigation',
|
|
|
|
};
|
|
|
|
|
|
|
|
function listTemplatesAndExit(newProjectName, options) {
|
|
|
|
if (options.template === true) {
|
|
|
|
// Just listing templates using 'react-native init --template'.
|
|
|
|
// Not creating a new app.
|
|
|
|
// Print available templates and exit.
|
2017-02-27 17:12:14 +00:00
|
|
|
const templateKeys = Object.keys(builtInTemplates);
|
2017-02-06 20:21:13 +00:00
|
|
|
if (templateKeys.length === 0) {
|
2017-02-27 17:12:14 +00:00
|
|
|
// Just a guard, should never happen as long builtInTemplates
|
2017-02-06 20:21:13 +00:00
|
|
|
// above is defined correctly :)
|
|
|
|
console.log(
|
|
|
|
'There are no templates available besides ' +
|
2018-05-11 19:43:49 +00:00
|
|
|
'the default "Hello World" one.',
|
2017-02-06 20:21:13 +00:00
|
|
|
);
|
|
|
|
} else {
|
|
|
|
console.log(
|
|
|
|
'The available templates are:\n' +
|
2018-05-11 19:43:49 +00:00
|
|
|
templateKeys.join('\n') +
|
|
|
|
'\nYou can use these to create an app based on a template, for example: ' +
|
|
|
|
'you could run: ' +
|
|
|
|
'react-native init ' +
|
|
|
|
newProjectName +
|
|
|
|
' --template ' +
|
|
|
|
templateKeys[0],
|
2017-02-06 20:21:13 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
// Exit 'react-native init'
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
// Continue 'react-native init'
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-02-27 17:12:14 +00:00
|
|
|
* @param destPath Create the new project at this path.
|
2017-02-06 20:21:13 +00:00
|
|
|
* @param newProjectName For example 'AwesomeApp'.
|
2017-02-27 17:12:14 +00:00
|
|
|
* @param template Template to use, for example 'navigation'.
|
2017-02-06 20:21:13 +00:00
|
|
|
* @param yarnVersion Version of yarn available on the system, or null if
|
|
|
|
* yarn is not available. For example '0.18.1'.
|
|
|
|
*/
|
2018-05-11 19:43:49 +00:00
|
|
|
function createProjectFromTemplate(
|
|
|
|
destPath,
|
|
|
|
newProjectName,
|
|
|
|
template,
|
|
|
|
yarnVersion,
|
|
|
|
) {
|
2017-02-06 20:21:13 +00:00
|
|
|
// Expand the basic 'HelloWorld' template
|
|
|
|
copyProjectTemplateAndReplace(
|
2018-05-11 19:43:49 +00:00
|
|
|
path.resolve(
|
|
|
|
'node_modules',
|
|
|
|
'react-native',
|
|
|
|
'local-cli',
|
|
|
|
'templates',
|
|
|
|
'HelloWorld',
|
|
|
|
),
|
2017-02-06 20:21:13 +00:00
|
|
|
destPath,
|
2018-05-11 19:43:49 +00:00
|
|
|
newProjectName,
|
2017-02-06 20:21:13 +00:00
|
|
|
);
|
|
|
|
|
2017-02-27 17:12:14 +00:00
|
|
|
if (template === undefined) {
|
|
|
|
// No specific template, use just the HelloWorld template above
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Keep the files from the 'HelloWorld' template, and overwrite some of them
|
|
|
|
// with the specified project template.
|
|
|
|
// The 'HelloWorld' template contains the native files (these are used by
|
|
|
|
// all templates) and every other template only contains additional JS code.
|
|
|
|
// Reason:
|
|
|
|
// This way we don't have to duplicate the native files in every template.
|
|
|
|
// If we duplicated them we'd make RN larger and risk that people would
|
|
|
|
// forget to maintain all the copies so they would go out of sync.
|
|
|
|
const builtInTemplateName = builtInTemplates[template];
|
|
|
|
if (builtInTemplateName) {
|
|
|
|
// template is e.g. 'navigation',
|
|
|
|
// use the built-in local-cli/templates/HelloNavigation folder
|
2018-05-11 19:43:49 +00:00
|
|
|
createFromBuiltInTemplate(
|
|
|
|
builtInTemplateName,
|
|
|
|
destPath,
|
|
|
|
newProjectName,
|
|
|
|
yarnVersion,
|
|
|
|
);
|
2017-02-27 17:12:14 +00:00
|
|
|
} else {
|
|
|
|
// template is e.g. 'ignite',
|
|
|
|
// use the template react-native-template-ignite from npm
|
|
|
|
createFromRemoteTemplate(template, destPath, newProjectName, yarnVersion);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// (We might want to get rid of built-in templates in the future -
|
|
|
|
// publish them to npm and install from there.)
|
2018-05-11 19:43:49 +00:00
|
|
|
function createFromBuiltInTemplate(
|
|
|
|
templateName,
|
|
|
|
destPath,
|
|
|
|
newProjectName,
|
|
|
|
yarnVersion,
|
|
|
|
) {
|
2017-02-27 17:12:14 +00:00
|
|
|
const templatePath = path.resolve(
|
2018-05-11 19:43:49 +00:00
|
|
|
'node_modules',
|
|
|
|
'react-native',
|
|
|
|
'local-cli',
|
|
|
|
'templates',
|
|
|
|
templateName,
|
2017-02-27 17:12:14 +00:00
|
|
|
);
|
2018-05-11 19:43:49 +00:00
|
|
|
copyProjectTemplateAndReplace(templatePath, destPath, newProjectName);
|
2017-02-27 17:12:14 +00:00
|
|
|
installTemplateDependencies(templatePath, yarnVersion);
|
2018-04-17 23:12:03 +00:00
|
|
|
installTemplateDevDependencies(templatePath, yarnVersion);
|
2017-02-27 17:12:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The following formats are supported for the template:
|
|
|
|
* - 'demo' -> Fetch the package react-native-template-demo from npm
|
|
|
|
* - git://..., http://..., file://... or any other URL supported by npm
|
|
|
|
*/
|
2018-05-11 19:43:49 +00:00
|
|
|
function createFromRemoteTemplate(
|
|
|
|
template,
|
|
|
|
destPath,
|
|
|
|
newProjectName,
|
|
|
|
yarnVersion,
|
|
|
|
) {
|
2017-02-27 17:12:14 +00:00
|
|
|
let installPackage;
|
|
|
|
let templateName;
|
|
|
|
if (template.includes('://')) {
|
2018-05-11 19:43:49 +00:00
|
|
|
// URL, e.g. git://, file://
|
|
|
|
installPackage = template;
|
|
|
|
templateName = template.substr(template.lastIndexOf('/') + 1);
|
2017-02-27 17:12:14 +00:00
|
|
|
} else {
|
|
|
|
// e.g 'demo'
|
|
|
|
installPackage = 'react-native-template-' + template;
|
|
|
|
templateName = installPackage;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the template exists
|
|
|
|
console.log(`Fetching template ${installPackage}...`);
|
|
|
|
try {
|
|
|
|
if (yarnVersion) {
|
2018-05-11 19:43:49 +00:00
|
|
|
execSync(`yarn add ${installPackage} --ignore-scripts`, {
|
|
|
|
stdio: 'inherit',
|
|
|
|
});
|
2017-02-06 20:21:13 +00:00
|
|
|
} else {
|
2018-05-11 19:43:49 +00:00
|
|
|
execSync(
|
|
|
|
`npm install ${installPackage} --save --save-exact --ignore-scripts`,
|
|
|
|
{stdio: 'inherit'},
|
|
|
|
);
|
2017-02-27 17:12:14 +00:00
|
|
|
}
|
2018-05-11 19:43:49 +00:00
|
|
|
const templatePath = path.resolve('node_modules', templateName);
|
|
|
|
copyProjectTemplateAndReplace(templatePath, destPath, newProjectName, {
|
|
|
|
// Every template contains a dummy package.json file included
|
|
|
|
// only for publishing the template to npm.
|
|
|
|
// We want to ignore this dummy file, otherwise it would overwrite
|
|
|
|
// our project's package.json file.
|
2018-08-06 19:14:37 +00:00
|
|
|
ignorePaths: ['package.json', 'dependencies.json','devDependencies.json'],
|
2018-05-11 19:43:49 +00:00
|
|
|
});
|
2017-02-27 17:12:14 +00:00
|
|
|
installTemplateDependencies(templatePath, yarnVersion);
|
2018-04-17 23:12:03 +00:00
|
|
|
installTemplateDevDependencies(templatePath, yarnVersion);
|
2017-02-27 17:12:14 +00:00
|
|
|
} finally {
|
|
|
|
// Clean up the temp files
|
|
|
|
try {
|
|
|
|
if (yarnVersion) {
|
|
|
|
execSync(`yarn remove ${templateName} --ignore-scripts`);
|
|
|
|
} else {
|
|
|
|
execSync(`npm uninstall ${templateName} --ignore-scripts`);
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
// Not critical but we still want people to know and report
|
|
|
|
// if this the clean up fails.
|
|
|
|
console.warn(
|
|
|
|
`Failed to clean up template temp files in node_modules/${templateName}. ` +
|
2018-05-11 19:43:49 +00:00
|
|
|
'This is not a critical error, you can work on your app.',
|
2017-02-27 17:12:14 +00:00
|
|
|
);
|
2017-02-06 20:21:13 +00:00
|
|
|
}
|
2017-02-27 17:12:14 +00:00
|
|
|
}
|
|
|
|
}
|
2017-02-06 20:21:13 +00:00
|
|
|
|
2017-02-27 17:12:14 +00:00
|
|
|
function installTemplateDependencies(templatePath, yarnVersion) {
|
|
|
|
// dependencies.json is a special file that lists additional dependencies
|
|
|
|
// that are required by this template
|
2018-05-11 19:43:49 +00:00
|
|
|
const dependenciesJsonPath = path.resolve(templatePath, 'dependencies.json');
|
2017-02-27 17:12:14 +00:00
|
|
|
console.log('Adding dependencies for the project...');
|
|
|
|
if (!fs.existsSync(dependenciesJsonPath)) {
|
|
|
|
console.log('No additional dependencies.');
|
|
|
|
return;
|
|
|
|
}
|
2017-02-06 20:21:13 +00:00
|
|
|
|
2017-02-27 17:12:14 +00:00
|
|
|
let dependencies;
|
|
|
|
try {
|
|
|
|
dependencies = JSON.parse(fs.readFileSync(dependenciesJsonPath));
|
|
|
|
} catch (err) {
|
|
|
|
throw new Error(
|
2018-05-11 19:43:49 +00:00
|
|
|
"Could not parse the template's dependencies.json: " + err.message,
|
2017-02-06 20:21:13 +00:00
|
|
|
);
|
2017-02-27 17:12:14 +00:00
|
|
|
}
|
|
|
|
for (let depName in dependencies) {
|
|
|
|
const depVersion = dependencies[depName];
|
|
|
|
const depToInstall = depName + '@' + depVersion;
|
|
|
|
console.log('Adding ' + depToInstall + '...');
|
|
|
|
if (yarnVersion) {
|
|
|
|
execSync(`yarn add ${depToInstall}`, {stdio: 'inherit'});
|
|
|
|
} else {
|
2018-05-11 19:43:49 +00:00
|
|
|
execSync(`npm install ${depToInstall} --save --save-exact`, {
|
|
|
|
stdio: 'inherit',
|
|
|
|
});
|
2017-02-06 20:21:13 +00:00
|
|
|
}
|
|
|
|
}
|
2018-05-11 19:43:49 +00:00
|
|
|
console.log("Linking native dependencies into the project's build files...");
|
2017-02-27 17:12:14 +00:00
|
|
|
execSync('react-native link', {stdio: 'inherit'});
|
2017-02-06 20:21:13 +00:00
|
|
|
}
|
|
|
|
|
2018-04-17 23:12:03 +00:00
|
|
|
function installTemplateDevDependencies(templatePath, yarnVersion) {
|
|
|
|
// devDependencies.json is a special file that lists additional develop dependencies
|
|
|
|
// that are required by this template
|
|
|
|
const devDependenciesJsonPath = path.resolve(
|
2018-05-11 19:43:49 +00:00
|
|
|
templatePath,
|
|
|
|
'devDependencies.json',
|
2018-04-17 23:12:03 +00:00
|
|
|
);
|
|
|
|
console.log('Adding develop dependencies for the project...');
|
|
|
|
if (!fs.existsSync(devDependenciesJsonPath)) {
|
|
|
|
console.log('No additional develop dependencies.');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
let dependencies;
|
|
|
|
try {
|
|
|
|
dependencies = JSON.parse(fs.readFileSync(devDependenciesJsonPath));
|
|
|
|
} catch (err) {
|
|
|
|
throw new Error(
|
2018-05-11 19:43:49 +00:00
|
|
|
"Could not parse the template's devDependencies.json: " + err.message,
|
2018-04-17 23:12:03 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
for (let depName in dependencies) {
|
|
|
|
const depVersion = dependencies[depName];
|
|
|
|
const depToInstall = depName + '@' + depVersion;
|
|
|
|
console.log('Adding ' + depToInstall + '...');
|
|
|
|
if (yarnVersion) {
|
|
|
|
execSync(`yarn add ${depToInstall} -D`, {stdio: 'inherit'});
|
|
|
|
} else {
|
2018-05-11 19:43:49 +00:00
|
|
|
execSync(`npm install ${depToInstall} --save-dev --save-exact`, {
|
|
|
|
stdio: 'inherit',
|
|
|
|
});
|
2018-04-17 23:12:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-06 20:21:13 +00:00
|
|
|
module.exports = {
|
|
|
|
listTemplatesAndExit,
|
|
|
|
createProjectFromTemplate,
|
|
|
|
};
|