-
Notifications
You must be signed in to change notification settings - Fork 24.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Eject CLI command to re-create native folders #12162
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
/** | ||
* 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'; | ||
|
||
const copyProjectTemplateAndReplace = require('../generator/copyProjectTemplateAndReplace'); | ||
const path = require('path'); | ||
const fs = require('fs'); | ||
|
||
/** | ||
* The eject command re-creates the `android` and `ios` native folders. Because native code can be | ||
* difficult to maintain, this new script allows an `app.json` to be defined for the project, which | ||
* is used to configure the native app. | ||
* | ||
* The `app.json` config may contain the following keys: | ||
* | ||
* - `name` - The short name used for the project, should be TitleCase | ||
* - `displayName` - The app's name on the home screen | ||
*/ | ||
|
||
function eject() { | ||
|
||
const doesIOSExist = fs.existsSync(path.resolve('ios')); | ||
const doesAndroidExist = fs.existsSync(path.resolve('android')); | ||
if (doesIOSExist && doesAndroidExist) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What if just one exists? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. then we don't want to show this error because maybe the user just wants to generate one |
||
console.error( | ||
'Both the iOS and Android folders already exist! Please delete `ios` and/or `android` ' + | ||
'before ejecting.' | ||
); | ||
process.exit(1); | ||
} | ||
|
||
let appConfig = null; | ||
try { | ||
appConfig = require(path.resolve('app.json')); | ||
} catch(e) { | ||
console.error( | ||
`Eject requires an \`app.json\` config file to be located at ` + | ||
`${path.resolve('app.json')}, and it must at least specify a \`name\` for the project ` + | ||
`name, and a \`displayName\` for the app's home screen label.` | ||
); | ||
process.exit(1); | ||
} | ||
|
||
const appName = appConfig.name; | ||
if (!appName) { | ||
console.error( | ||
`App \`name\` must be defined in the \`app.json\` config file to define the project name. `+ | ||
`It must not contain any spaces or dashes.` | ||
); | ||
process.exit(1); | ||
} | ||
const displayName = appConfig.displayName; | ||
if (!displayName) { | ||
console.error( | ||
`App \`displayName\` must be defined in the \`app.json\` config file, to define the label ` + | ||
`of the app on the home screen.` | ||
); | ||
process.exit(1); | ||
} | ||
|
||
const templateOptions = { displayName }; | ||
|
||
if (!doesIOSExist) { | ||
console.log('Generating the iOS folder.'); | ||
copyProjectTemplateAndReplace( | ||
path.resolve('node_modules', 'react-native', 'local-cli', 'templates', 'HelloWorld', 'ios'), | ||
path.resolve('ios'), | ||
appName, | ||
templateOptions | ||
); | ||
} | ||
|
||
if (!doesAndroidExist) { | ||
console.log('Generating the Android folder.'); | ||
copyProjectTemplateAndReplace( | ||
path.resolve('node_modules', 'react-native', 'local-cli', 'templates', 'HelloWorld', 'android'), | ||
path.resolve('android'), | ||
appName, | ||
templateOptions | ||
); | ||
} | ||
|
||
} | ||
|
||
module.exports = { | ||
name: 'eject', | ||
description: 'Re-create the iOS and Android folders and native code', | ||
func: eject, | ||
options: [], | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
<resources> | ||
<string name="app_name">HelloWorld</string> | ||
<string name="app_name">Hello App Display Name</string> | ||
</resources> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
{ | ||
"name": "HelloWorld", | ||
"displayName": "HelloWorld" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this be "Hello World"? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nope, because this is generated in the init step, where the name is all that exists. |
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit:
doesIOSFolderExist
,doesAndroidFolderExist
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok