|
| 1 | +/** |
| 2 | + * Copyright (c) 2015-present, Facebook, Inc. |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * This source code is licensed under the BSD-style license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. An additional grant |
| 7 | + * of patent rights can be found in the PATENTS file in the same directory. |
| 8 | + */ |
| 9 | +'use strict'; |
| 10 | + |
| 11 | +const copyProjectTemplateAndReplace = require('./copyProjectTemplateAndReplace'); |
| 12 | +const execSync = require('child_process').execSync; |
| 13 | +const fs = require('fs'); |
| 14 | +const path = require('path'); |
| 15 | + |
| 16 | +const availableTemplates = { |
| 17 | + navigation: 'HelloNavigation', |
| 18 | +}; |
| 19 | + |
| 20 | +function listTemplatesAndExit(newProjectName, options) { |
| 21 | + if (options.template === true) { |
| 22 | + // Just listing templates using 'react-native init --template'. |
| 23 | + // Not creating a new app. |
| 24 | + // Print available templates and exit. |
| 25 | + const templateKeys = Object.keys(availableTemplates); |
| 26 | + if (templateKeys.length === 0) { |
| 27 | + // Just a guard, should never happen as long availableTemplates |
| 28 | + // above is defined correctly :) |
| 29 | + console.log( |
| 30 | + 'There are no templates available besides ' + |
| 31 | + 'the default "Hello World" one.' |
| 32 | + ); |
| 33 | + } else { |
| 34 | + console.log( |
| 35 | + 'The available templates are:\n' + |
| 36 | + templateKeys.join('\n') + |
| 37 | + '\nYou can use these to create an app based on a template, for example: ' + |
| 38 | + 'you could run: ' + |
| 39 | + 'react-native init ' + newProjectName + ' --template ' + templateKeys[0] |
| 40 | + ); |
| 41 | + } |
| 42 | + // Exit 'react-native init' |
| 43 | + return true; |
| 44 | + } |
| 45 | + // Continue 'react-native init' |
| 46 | + return false; |
| 47 | +} |
| 48 | + |
| 49 | +/** |
| 50 | + * @param newProjectName For example 'AwesomeApp'. |
| 51 | + * @param templateKey Template to use, for example 'navigation'. |
| 52 | + * @param yarnVersion Version of yarn available on the system, or null if |
| 53 | + * yarn is not available. For example '0.18.1'. |
| 54 | + */ |
| 55 | +function createProjectFromTemplate(destPath, newProjectName, templateKey, yarnVersion) { |
| 56 | + // Expand the basic 'HelloWorld' template |
| 57 | + copyProjectTemplateAndReplace( |
| 58 | + path.resolve('node_modules', 'react-native', 'local-cli', 'templates', 'HelloWorld'), |
| 59 | + destPath, |
| 60 | + newProjectName |
| 61 | + ); |
| 62 | + |
| 63 | + if (templateKey !== undefined) { |
| 64 | + // Keep the files from the 'HelloWorld' template, and overwrite some of them |
| 65 | + // with the specified project template. |
| 66 | + // The 'HelloWorld' template contains the native files (these are used by |
| 67 | + // all templates) and every other template only contains additional JS code. |
| 68 | + // Reason: |
| 69 | + // This way we don't have to duplicate the native files in every template. |
| 70 | + // If we duplicated them we'd make RN larger and risk that people would |
| 71 | + // forget to maintain all the copies so they would go out of sync. |
| 72 | + const templateName = availableTemplates[templateKey]; |
| 73 | + if (templateName) { |
| 74 | + copyProjectTemplateAndReplace( |
| 75 | + path.resolve( |
| 76 | + 'node_modules', 'react-native', 'local-cli', 'templates', templateName |
| 77 | + ), |
| 78 | + destPath, |
| 79 | + newProjectName |
| 80 | + ); |
| 81 | + } else { |
| 82 | + throw new Error('Uknown template: ' + templateKey); |
| 83 | + } |
| 84 | + |
| 85 | + // Add dependencies: |
| 86 | + |
| 87 | + // dependencies.json is a special file that lists additional dependencies |
| 88 | + // that are required by this template |
| 89 | + const dependenciesJsonPath = path.resolve( |
| 90 | + 'node_modules', 'react-native', 'local-cli', 'templates', templateName, 'dependencies.json' |
| 91 | + ); |
| 92 | + if (fs.existsSync(dependenciesJsonPath)) { |
| 93 | + console.log('Adding dependencies for the project...'); |
| 94 | + const dependencies = JSON.parse(fs.readFileSync(dependenciesJsonPath)); |
| 95 | + for (let depName in dependencies) { |
| 96 | + const depVersion = dependencies[depName]; |
| 97 | + const depToInstall = depName + '@' + depVersion; |
| 98 | + console.log('Adding ' + depToInstall + '...'); |
| 99 | + if (yarnVersion) { |
| 100 | + execSync(`yarn add ${depToInstall}`, {stdio: 'inherit'}); |
| 101 | + } else { |
| 102 | + execSync(`npm install ${depToInstall} --save --save-exact`, {stdio: 'inherit'}); |
| 103 | + } |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +module.exports = { |
| 110 | + listTemplatesAndExit, |
| 111 | + createProjectFromTemplate, |
| 112 | +}; |
0 commit comments