From 277d76ad421684324ba0a0f58e6a1ead36b3e12f Mon Sep 17 00:00:00 2001 From: Binoy Patel Date: Sat, 3 Jun 2017 18:57:30 -0400 Subject: [PATCH] Install Packages using npm or yarn --- bin/build.js | 72 +++++++++++++++++++++++++++++++++++++++++----------- package.json | 3 ++- yarn.lock | 18 +++++++++++++ 3 files changed, 77 insertions(+), 16 deletions(-) diff --git a/bin/build.js b/bin/build.js index 3c7a824..1e6fecc 100644 --- a/bin/build.js +++ b/bin/build.js @@ -5,6 +5,42 @@ const paths = require('path'); const fs = require('fs'); const figlet = require('figlet'); const chalk = require('chalk'); +const execSync = require('child_process').execSync; +const spawn = require('cross-spawn'); + +function shouldUseYarn() { + try { + execSync('yarnpkg --version', { stdio: 'ignore' }); + return true; + } catch (e) { + return false; + } +} + +const installPackages = () => { + console.log(chalk.white.bold('Installing Packages')); + return new Promise((resolve, reject) => { + let command; + let args = ['install']; + + if (shouldUseYarn()) { + command = 'yarn'; + } else { + command = 'npm'; + } + + const child = spawn(command, args, { stdio: 'inherit' }); + child.on('close', code => { + if (code !== 0) { + reject({ + command: `${command} ${args.join(' ')}` + }); + return; + } + resolve(); + }) + }) +} const build = (appName) => { cp('-r', __dirname + '/../node_modules/rxapp/.', appName); @@ -19,21 +55,27 @@ const build = (appName) => { console.log('----------------------------------------------------------'); console.log(chalk.green.bold('Welcome to ReactXP')); console.log('----------------------------------------------------------'); - console.log(chalk.white.bold('Let\'s get started')); - console.log(chalk.green('Step 1: cd into the newly created ' + appName + ' directory')); - console.log(chalk.green('Step 2: install dependencies using yarn or npm')); - console.log('----------------------------------------------------------'); - console.log(chalk.white.bold('For Web')); - console.log(chalk.green('Step 1. npm run web-watch')); - console.log(chalk.black.bold('This compiles the TypeScript code and recompiles it whenever any files are changed.')) - console.log(chalk.green('Step 2. Open index.html in your browser to view the result.')); - console.log('----------------------------------------------------------'); - console.log(chalk.white.bold('For React Native')); - console.log(chalk.green('Step 1. run npm run rn-watch')); - console.log(chalk.black.bold('This compiles the TypeScript code and recompiles it whenever any files are changed.')); - console.log(chalk.green('Step 2. run npm start')); - console.log(chalk.black.bold('This starts the React Native Packager.')); - console.log('----------------------------------------------------------'); + cd(appName); + installPackages().then(() => { + console.log(chalk.white.bold('Let\'s get started')); + console.log(chalk.green('Step 1: cd into the newly created ' + appName + ' directory')); + console.log('----------------------------------------------------------'); + console.log(chalk.white.bold('For Web')); + console.log(chalk.green('Step 1. npm run web-watch')); + console.log(chalk.black.bold('This compiles the TypeScript code and recompiles it whenever any files are changed.')) + console.log(chalk.green('Step 2. Open index.html in your browser to view the result.')); + console.log('----------------------------------------------------------'); + console.log(chalk.white.bold('For React Native')); + console.log(chalk.green('Step 1. run npm run rn-watch')); + console.log(chalk.black.bold('This compiles the TypeScript code and recompiles it whenever any files are changed.')); + console.log(chalk.green('Step 2. run npm start')); + console.log(chalk.black.bold('This starts the React Native Packager.')); + console.log('----------------------------------------------------------'); + }) + .catch(error => { + console.log(chalk.red('An unexpected error occurred')) + console.log(chalk.red(error)); + }); }); } diff --git a/package.json b/package.json index dd5bbc2..6456b81 100644 --- a/package.json +++ b/package.json @@ -20,6 +20,7 @@ }, "devDependencies": { "chalk": "^1.1.3", - "commander": "^2.9.0" + "commander": "^2.9.0", + "cross-spawn": "^5.1.0" } } diff --git a/yarn.lock b/yarn.lock index 644fadd..bc828c5 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1178,6 +1178,14 @@ cross-spawn@^3.0.1: lru-cache "^4.0.1" which "^1.2.9" +cross-spawn@^5.1.0: + version "5.1.0" + resolved "https://registry.yarnpkg.com/cross-spawn/-/cross-spawn-5.1.0.tgz#e8bd0efee58fcff6f8f94510a0a554bbfa235449" + dependencies: + lru-cache "^4.0.1" + shebang-command "^1.2.0" + which "^1.2.9" + cryptiles@2.x.x: version "2.0.5" resolved "https://registry.yarnpkg.com/cryptiles/-/cryptiles-2.0.5.tgz#3bdfecdc608147c1c67202fa291e7dca59eaa3b8" @@ -3725,6 +3733,16 @@ setimmediate@^1.0.5: version "1.0.5" resolved "https://registry.yarnpkg.com/setimmediate/-/setimmediate-1.0.5.tgz#290cbb232e306942d7d7ea9b83732ab7856f8285" +shebang-command@^1.2.0: + version "1.2.0" + resolved "https://registry.yarnpkg.com/shebang-command/-/shebang-command-1.2.0.tgz#44aac65b695b03398968c39f363fee5deafdf1ea" + dependencies: + shebang-regex "^1.0.0" + +shebang-regex@^1.0.0: + version "1.0.0" + resolved "https://registry.yarnpkg.com/shebang-regex/-/shebang-regex-1.0.0.tgz#da42f49740c0b42db2ca9728571cb190c98efea3" + shell-quote@1.6.1: version "1.6.1" resolved "https://registry.yarnpkg.com/shell-quote/-/shell-quote-1.6.1.tgz#f4781949cce402697127430ea3b3c5476f481767"