|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +require('shelljs/global'); |
| 4 | +const paths = require('path'); |
| 5 | +const fs = require('fs'); |
| 6 | +const figlet = require('figlet'); |
| 7 | +const chalk = require('chalk'); |
| 8 | +const execSync = require('child_process').execSync; |
| 9 | +const spawn = require('cross-spawn'); |
| 10 | + |
| 11 | +function shouldUseYarn() { |
| 12 | + try { |
| 13 | + execSync('yarnpkg --version', { stdio: 'ignore' }); |
| 14 | + return true; |
| 15 | + } catch (e) { |
| 16 | + return false; |
| 17 | + } |
| 18 | +} |
| 19 | + |
| 20 | +const installPackages = () => { |
| 21 | + console.log(chalk.white.bold('Installing Packages')); |
| 22 | + return new Promise((resolve, reject) => { |
| 23 | + let command; |
| 24 | + let args = ['install']; |
| 25 | + |
| 26 | + if (shouldUseYarn()) { |
| 27 | + command = 'yarn'; |
| 28 | + } else { |
| 29 | + command = 'npm'; |
| 30 | + } |
| 31 | + |
| 32 | + const child = spawn(command, args, { stdio: 'inherit' }); |
| 33 | + child.on('close', code => { |
| 34 | + if (code !== 0) { |
| 35 | + reject({ |
| 36 | + command: `${command} ${args.join(' ')}` |
| 37 | + }); |
| 38 | + return; |
| 39 | + } |
| 40 | + resolve(); |
| 41 | + }) |
| 42 | + }) |
| 43 | +} |
| 44 | + |
| 45 | +const build = (appName) => { |
| 46 | + cp('-r', __dirname + '/../src/.', appName); |
| 47 | + console.log('----------------------------------------------------------'); |
| 48 | + figlet.text('React Native Web', { |
| 49 | + horizontalLayout: 'default', |
| 50 | + verticalLayout: 'default' |
| 51 | + }, function(err, data) { |
| 52 | + if (err) { |
| 53 | + return; |
| 54 | + } |
| 55 | + console.log(data); |
| 56 | + console.log('----------------------------------------------------------'); |
| 57 | + console.log(chalk.cyan.bold('Welcome to React Native Web')); |
| 58 | + console.log('----------------------------------------------------------'); |
| 59 | + cd(appName); |
| 60 | + installPackages().then(() => { |
| 61 | + console.log(chalk.white.bold('Let\'s get started')); |
| 62 | + console.log(chalk.cyan('cd into the newly created ' + appName + ' directory')); |
| 63 | + console.log('----------------------------------------------------------'); |
| 64 | + console.log(chalk.white.bold('For Web')); |
| 65 | + console.log(chalk.cyan('Step 1. npm run web')); |
| 66 | + console.log(chalk.black.bold('This starts the webpack dev server and serves up index.web.js')) |
| 67 | + console.log(chalk.cyan('Step 2. Open localhost:8080 in your browser to view the app.')); |
| 68 | + console.log('----------------------------------------------------------'); |
| 69 | + console.log(chalk.white.bold('For React Native')); |
| 70 | + console.log(chalk.cyan('iOS: run react-native run-ios')); |
| 71 | + console.log(chalk.black.bold('This starts the packager and runs the iOS simulator.')) |
| 72 | + console.log(chalk.cyan('android. run react-native run-android')); |
| 73 | + console.log(chalk.black.bold('This starts the packager and runs the android emulator.')) |
| 74 | + console.log('----------------------------------------------------------'); |
| 75 | + }) |
| 76 | + .catch(error => { |
| 77 | + console.log(chalk.red('An unexpected error occurred')) |
| 78 | + console.log(chalk.red(error)); |
| 79 | + }); |
| 80 | + }); |
| 81 | +} |
| 82 | + |
| 83 | +module.exports = build; |
0 commit comments