|
1 | 1 | #!/usr/bin/env node |
2 | | -// @flow |
| 2 | +'use strict'; |
3 | 3 | import spawn from 'cross-spawn'; |
| 4 | +import chalk from 'chalk'; |
4 | 5 |
|
5 | 6 | const script = process.argv[2]; |
6 | 7 | const args = process.argv.slice(3); |
7 | 8 |
|
8 | | -const validCommands = ['eject', 'android', 'ios', 'start', 'test']; |
9 | | - |
10 | | -if (validCommands.indexOf(script) !== -1) { |
11 | | - // the command is valid |
12 | | - const result = spawn.sync( |
13 | | - 'node', |
14 | | - ['--no-deprecation', require.resolve('../scripts/' + script)].concat(args), |
15 | | - { stdio: 'inherit' } |
16 | | - ); |
17 | | - process.exit(result.status); |
| 9 | +if (script === 'start' || script === 'eject') { |
| 10 | + printDeprecationMessage(); |
| 11 | + runExpoCli(script, ...args); |
| 12 | +} else if (script === 'android' || script === 'ios') { |
| 13 | + printDeprecationMessage(); |
| 14 | + runExpoCli('start', `--${script}`, ...args); |
18 | 15 | } else { |
19 | 16 | console.log( |
20 | 17 | `Invalid command '${script}'. Please check if you need to update react-native-scripts.` |
21 | 18 | ); |
| 19 | + process.exit(1); |
| 20 | +} |
| 21 | + |
| 22 | +function printDeprecationMessage() { |
| 23 | + console.warn(chalk.bold('Note: react-native-scripts is deprecated.\n')); |
| 24 | + console.warn(chalk.underline('Upgrading your project to use Expo CLI:\n')); |
| 25 | + console.warn('Make these changes to package.json:'); |
| 26 | + console.warn( |
| 27 | + chalk.bold("1) Replace 'react-native-scripts' with 'expo' in the 'scripts' config.") |
| 28 | + ); |
| 29 | + console.warn( |
| 30 | + ` Example: |
| 31 | + "scripts": { |
| 32 | + "start": "expo start", |
| 33 | + "eject": "expo eject", |
| 34 | + "android": "expo start --android", |
| 35 | + "ios": "expo start --ios", |
| 36 | + "test": "jest" |
| 37 | + }` |
| 38 | + ); |
| 39 | + console.warn(chalk.bold('2) Remove react-native-scripts from devDependencies.\n')); |
| 40 | + console.warn( |
| 41 | + chalk.bold("That's all! Expo CLI will install automatically when you run `npm start`.\n") |
| 42 | + ); |
| 43 | +} |
| 44 | + |
| 45 | +function runExpoCli(...args) { |
| 46 | + spawn('expo-cli', args, { stdio: 'inherit' }) |
| 47 | + .on('exit', function(code) { |
| 48 | + process.exit(code); |
| 49 | + }) |
| 50 | + .on('error', function() { |
| 51 | + console.warn('This command requires Expo CLI.'); |
| 52 | + var rl = require('readline').createInterface({ |
| 53 | + input: process.stdin, |
| 54 | + output: process.stdout, |
| 55 | + }); |
| 56 | + rl.question('Do you want to install it globally [Y/n]? ', function(answer) { |
| 57 | + rl.close(); |
| 58 | + if (/^n/i.test(answer.trim())) { |
| 59 | + process.exit(1); |
| 60 | + } else { |
| 61 | + console.log("Installing the package 'expo-cli'..."); |
| 62 | + spawn('npm', ['install', '--global', '--loglevel', 'error', 'expo-cli@latest'], { |
| 63 | + stdio: ['inherit', 'ignore', 'inherit'], |
| 64 | + }).on('close', function(code) { |
| 65 | + if (code !== 0) { |
| 66 | + console.error('Installing Expo CLI failed. You can install it manually with:'); |
| 67 | + console.error(' npm install --global expo-cli'); |
| 68 | + process.exit(code); |
| 69 | + } else { |
| 70 | + console.log('Expo CLI installed. You can run `expo --help` for instructions.'); |
| 71 | + runExpoCli(...args); |
| 72 | + } |
| 73 | + }); |
| 74 | + } |
| 75 | + }); |
| 76 | + }); |
22 | 77 | } |
0 commit comments