Skip to content

Commit cbf3f9e

Browse files
committed
Force installation of Expo fork of react-native when ejecting with ExpoKit
1 parent e697101 commit cbf3f9e

File tree

6 files changed

+120
-65
lines changed

6 files changed

+120
-65
lines changed

react-native-scripts/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-native-scripts",
3-
"version": "1.2.1",
3+
"version": "1.3.0",
44
"description": "Configuration and scripts for Create React Native App.",
55
"license": "BSD-3-Clause",
66
"keywords": [

react-native-scripts/src/scripts/eject.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,9 @@ Android Studio to build the native code for your project.`
239239

240240
if (newDevDependencies.length > 0) {
241241
log('Installing new packages with npm...');
242-
spawn.sync('npm', ['install', '--save-dev', ...newDevDependencies], { stdio });
242+
spawn.sync('npm', ['install', '--save-dev', ...newDevDependencies], {
243+
stdio,
244+
});
243245
}
244246
}
245247
} else if (ejectMethod === 'expoKit') {

react-native-scripts/src/scripts/init.js

Lines changed: 24 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import path from 'path';
66
import pathExists from 'path-exists';
77
import spawn from 'cross-spawn';
88
import log from '../util/log';
9+
import install from '../util/install';
910

1011
// UPDATE DEPENDENCY VERSIONS HERE
1112
const DEFAULT_DEPENDENCIES = {
@@ -24,6 +25,7 @@ module.exports = async (appPath: string, appName: string, verbose: boolean, cwd:
2425
const ownPackageName: string = require('../../package.json').name;
2526
const ownPath: string = path.join(appPath, 'node_modules', ownPackageName);
2627
const useYarn: boolean = await pathExists(path.join(appPath, 'yarn.lock'));
28+
const npmOrYarn = useYarn ? 'yarn' : 'npm';
2729

2830
// FIXME(perry) remove when npm 5 is supported
2931
if (!useYarn) {
@@ -107,44 +109,24 @@ https://github.com/npm/npm/issues/16991
107109
throw err;
108110
}
109111
}
112+
const { code, command, args } = await install(appPath);
113+
if (code !== 0) {
114+
console.error('Failed to install');
115+
// console.error(`\`${command} ${args.join(' ')}\` failed`);
116+
return;
117+
}
110118

111-
// Run yarn or npm
112-
let command = '';
113-
let args = [];
114-
115-
if (useYarn) {
116-
command = 'yarnpkg';
119+
// display the cleanest way to get to the app dir
120+
// if the cwd + appName is equal to the full path, then just cd into appName
121+
let cdpath;
122+
if (path.resolve(cwd, appName) === appPath) {
123+
cdpath = appName;
117124
} else {
118-
command = 'npm';
119-
args = ['install', '--save'];
120-
121-
if (verbose) {
122-
args.push('--verbose');
123-
}
125+
cdpath = appPath;
124126
}
125127

126-
const npmOrYarn = useYarn ? 'yarn' : 'npm';
127-
log(`Installing dependencies using ${npmOrYarn}...`);
128-
log(); // why is this here
129-
130-
const proc = spawn(command, args, { stdio: 'inherit' });
131-
proc.on('close', code => {
132-
if (code !== 0) {
133-
console.error(`\`${command} ${args.join(' ')}\` failed`);
134-
return;
135-
}
136-
137-
// display the cleanest way to get to the app dir
138-
// if the cwd + appName is equal to the full path, then just cd into appName
139-
let cdpath;
140-
if (path.resolve(cwd, appName) === appPath) {
141-
cdpath = appName;
142-
} else {
143-
cdpath = appPath;
144-
}
145-
146-
log(
147-
`
128+
log(
129+
`
148130
149131
Success! Created ${appName} at ${appPath}
150132
Inside that directory, you can run several commands:
@@ -173,16 +155,15 @@ We suggest that you begin by typing:
173155
174156
${chalk.cyan('cd ' + cdpath)}
175157
${chalk.cyan(npmOrYarn + ' start')}`
176-
);
158+
);
177159

178-
if (readmeExists) {
179-
log(
180-
`
160+
if (readmeExists) {
161+
log(
162+
`
181163
${chalk.yellow('You had a `README.md` file, we renamed it to `README.old.md`')}`
182-
);
183-
}
164+
);
165+
}
184166

185-
log();
186-
log('Happy hacking!');
187-
});
167+
log();
168+
log('Happy hacking!');
188169
};

react-native-scripts/src/util/expo.js

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import chalk from 'chalk';
44
import fse from 'fs-extra';
55
import inquirer from 'inquirer';
66
import path from 'path';
7+
import install from '../util/install';
78

89
import { Detach, User as UserManager, Versions } from 'xdl';
910

@@ -67,13 +68,7 @@ publish it there. See this StackOverflow question for more information:
6768

6869
const pkgJson = JSON.parse((await fse.readFile(path.resolve('package.json'))).toString());
6970

70-
const entryPoint = `import Expo from 'expo';
71-
import App from './App';
72-
73-
Expo.registerRootComponent(App);
74-
`;
75-
await fse.writeFile('index.js', entryPoint);
76-
pkgJson.main = 'index.js';
71+
pkgJson.main = 'node_modules/expo/AppEntry.js';
7772

7873
delete pkgJson.devDependencies['react-native-scripts'];
7974
delete pkgJson.scripts.start;
@@ -85,21 +80,35 @@ Expo.registerRootComponent(App);
8580
const versions = await Versions.versionsAsync();
8681
const sdkTag = versions.sdkVersions[appJson.expo.sdkVersion].expoReactNativeTag;
8782

88-
pkgJson.dependencies[
89-
'react-native'
90-
] = `https://github.com/expo/react-native/archive/${sdkTag}.tar.gz`;
91-
9283
await fse.writeFile('package.json', JSON.stringify(pkgJson, null, 2));
9384

94-
console.log(
95-
`${chalk.green('Successfully set up ExpoKit!')}
85+
console.log('Installing the Expo fork of react-native...');
86+
const reactNativeVersion = `https://github.com/expo/react-native/archive/${sdkTag}.tar.gz`;
87+
const {
88+
code,
89+
} = await install(process.cwd(), 'react-native', reactNativeVersion, {
90+
silent: true,
91+
});
9692

97-
You'll need to use Expo's XDE to run this project:
98-
${chalk.cyan('https://docs.expo.io/versions/latest/introduction/installation.html')}
93+
if (code === 0) {
94+
console.log(`${chalk.green('Successfully set up ExpoKit!')}`);
95+
} else {
96+
console.warn(
97+
`
98+
${chalk.yellow('Unable to install the Expo fork of react-native.')}
99+
${chalk.yellow(`Please install react-native@${reactNativeVersion} before continuing.`)}
100+
`
101+
);
102+
}
99103

100-
For further instructions, please read ExpoKit's build documentation:
101-
${chalk.cyan('https://docs.expo.io/versions/latest/guides/expokit.html')}
102-
`
104+
console.log(
105+
`
106+
You'll need to use Expo's XDE to run this project:
107+
${chalk.cyan('https://docs.expo.io/versions/latest/introduction/installation.html')}
108+
109+
For further instructions, please read ExpoKit's build documentation:
110+
${chalk.cyan('https://docs.expo.io/versions/latest/guides/expokit.html')}
111+
`
103112
);
104113
}
105114

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// @flow
2+
3+
import spawn from 'cross-spawn';
4+
import pathExists from 'path-exists';
5+
import path from 'path';
6+
import log from '../util/log';
7+
8+
type InstallResult = {
9+
code: number,
10+
command: string,
11+
args: Array<string>,
12+
};
13+
14+
export default (async function install(
15+
appPath: string,
16+
packageName?: string,
17+
packageVersion?: string,
18+
options?: any = {}
19+
): Promise<InstallResult> {
20+
const useYarn: boolean = await pathExists(path.join(appPath, 'yarn.lock'));
21+
22+
let command = '';
23+
let args = [];
24+
25+
if (useYarn) {
26+
command = 'yarnpkg';
27+
if (packageName) {
28+
args = ['add'];
29+
}
30+
} else {
31+
command = 'npm';
32+
args = ['install', '--save'];
33+
34+
// if (verbose) {
35+
// args.push('--verbose');
36+
// }
37+
}
38+
39+
let pkg = packageName;
40+
if (pkg) {
41+
if (packageVersion) {
42+
pkg = `${pkg}@${packageVersion}`;
43+
}
44+
45+
args.push(pkg);
46+
}
47+
48+
const npmOrYarn = useYarn ? 'yarn' : 'npm';
49+
log(`Installing ${pkg ? pkg : 'dependencies'} using ${npmOrYarn}...`);
50+
log();
51+
52+
let spawnOpts = {};
53+
if (options.silent) {
54+
spawnOpts.silent = true;
55+
} else {
56+
spawnOpts.stdio = 'inherit';
57+
}
58+
59+
const proc = spawn(command, args, spawnOpts);
60+
return new Promise(resolve => {
61+
proc.on('close', code => resolve({ code, command, args }));
62+
});
63+
});

react-native-scripts/src/util/log.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22

33
import chalk from 'chalk';
44

5-
function log() {
5+
function log(data: any) {
66
const args = Array.prototype.slice.call(arguments, 0);
77

88
respectProgressBars(() => {
99
console.log(...args);
1010
});
1111
}
1212

13-
log.withTimestamp = function() {
13+
log.withTimestamp = function(data: any) {
1414
const prefix = chalk.dim(new Date().toLocaleTimeString()) + ':';
1515
const args = [prefix].concat(Array.prototype.slice.call(arguments, 0));
1616

0 commit comments

Comments
 (0)