|
| 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('../generator/copyProjectTemplateAndReplace'); |
| 12 | +const path = require('path'); |
| 13 | +const fs = require('fs'); |
| 14 | + |
| 15 | +/** |
| 16 | + * The eject command re-creates the `android` and `ios` native folders. Because native code can be |
| 17 | + * difficult to maintain, this new script allows an `app.json` to be defined for the project, which |
| 18 | + * is used to configure the native app. |
| 19 | + * |
| 20 | + * The `app.json` config may contain the following keys: |
| 21 | + * |
| 22 | + * - `name` - The short name used for the project, should be TitleCase |
| 23 | + * - `displayName` - The app's name on the home screen |
| 24 | + */ |
| 25 | + |
| 26 | +function eject() { |
| 27 | + |
| 28 | + const doesIOSExist = fs.existsSync(path.resolve('ios')); |
| 29 | + const doesAndroidExist = fs.existsSync(path.resolve('android')); |
| 30 | + if (doesIOSExist && doesAndroidExist) { |
| 31 | + console.error( |
| 32 | + 'Both the iOS and Android folders already exist! Please delete `ios` and/or `android` ' + |
| 33 | + 'before ejecting.' |
| 34 | + ); |
| 35 | + process.exit(1); |
| 36 | + } |
| 37 | + |
| 38 | + let appConfig = null; |
| 39 | + try { |
| 40 | + appConfig = require(path.resolve('app.json')); |
| 41 | + } catch(e) { |
| 42 | + console.error( |
| 43 | + `Eject requires an \`app.json\` config file to be located at ` + |
| 44 | + `${path.resolve('app.json')}, and it must at least specify a \`name\` for the project ` + |
| 45 | + `name, and a \`displayName\` for the app's home screen label.` |
| 46 | + ); |
| 47 | + process.exit(1); |
| 48 | + } |
| 49 | + |
| 50 | + const appName = appConfig.name; |
| 51 | + if (!appName) { |
| 52 | + console.error( |
| 53 | + `App \`name\` must be defined in the \`app.json\` config file to define the project name. `+ |
| 54 | + `It must not contain any spaces or dashes.` |
| 55 | + ); |
| 56 | + process.exit(1); |
| 57 | + } |
| 58 | + const displayName = appConfig.displayName; |
| 59 | + if (!displayName) { |
| 60 | + console.error( |
| 61 | + `App \`displayName\` must be defined in the \`app.json\` config file, to define the label ` + |
| 62 | + `of the app on the home screen.` |
| 63 | + ); |
| 64 | + process.exit(1); |
| 65 | + } |
| 66 | + |
| 67 | + const templateOptions = { displayName }; |
| 68 | + |
| 69 | + if (!doesIOSExist) { |
| 70 | + console.log('Generating the iOS folder.'); |
| 71 | + copyProjectTemplateAndReplace( |
| 72 | + path.resolve('node_modules', 'react-native', 'local-cli', 'templates', 'HelloWorld', 'ios'), |
| 73 | + path.resolve('ios'), |
| 74 | + appName, |
| 75 | + templateOptions |
| 76 | + ); |
| 77 | + } |
| 78 | + |
| 79 | + if (!doesAndroidExist) { |
| 80 | + console.log('Generating the Android folder.'); |
| 81 | + copyProjectTemplateAndReplace( |
| 82 | + path.resolve('node_modules', 'react-native', 'local-cli', 'templates', 'HelloWorld', 'android'), |
| 83 | + path.resolve('android'), |
| 84 | + appName, |
| 85 | + templateOptions |
| 86 | + ); |
| 87 | + } |
| 88 | + |
| 89 | +} |
| 90 | + |
| 91 | +module.exports = { |
| 92 | + name: 'eject', |
| 93 | + description: 'Re-create the iOS and Android folders and native code', |
| 94 | + func: eject, |
| 95 | + options: [], |
| 96 | +}; |
0 commit comments