Skip to content

Commit 36b05e3

Browse files
authored
Merge pull request #7 from react-native-training/feature/install-packages
Install Packages using npm or yarn
2 parents 6fb243c + 277d76a commit 36b05e3

File tree

3 files changed

+77
-16
lines changed

3 files changed

+77
-16
lines changed

bin/build.js

+57-15
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,42 @@ const paths = require('path');
55
const fs = require('fs');
66
const figlet = require('figlet');
77
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+
}
844

945
const build = (appName) => {
1046
cp('-r', __dirname + '/../node_modules/rxapp/.', appName);
@@ -19,21 +55,27 @@ const build = (appName) => {
1955
console.log('----------------------------------------------------------');
2056
console.log(chalk.green.bold('Welcome to ReactXP'));
2157
console.log('----------------------------------------------------------');
22-
console.log(chalk.white.bold('Let\'s get started'));
23-
console.log(chalk.green('Step 1: cd into the newly created ' + appName + ' directory'));
24-
console.log(chalk.green('Step 2: install dependencies using yarn or npm'));
25-
console.log('----------------------------------------------------------');
26-
console.log(chalk.white.bold('For Web'));
27-
console.log(chalk.green('Step 1. npm run web-watch'));
28-
console.log(chalk.black.bold('This compiles the TypeScript code and recompiles it whenever any files are changed.'))
29-
console.log(chalk.green('Step 2. Open index.html in your browser to view the result.'));
30-
console.log('----------------------------------------------------------');
31-
console.log(chalk.white.bold('For React Native'));
32-
console.log(chalk.green('Step 1. run npm run rn-watch'));
33-
console.log(chalk.black.bold('This compiles the TypeScript code and recompiles it whenever any files are changed.'));
34-
console.log(chalk.green('Step 2. run npm start'));
35-
console.log(chalk.black.bold('This starts the React Native Packager.'));
36-
console.log('----------------------------------------------------------');
58+
cd(appName);
59+
installPackages().then(() => {
60+
console.log(chalk.white.bold('Let\'s get started'));
61+
console.log(chalk.green('Step 1: cd into the newly created ' + appName + ' directory'));
62+
console.log('----------------------------------------------------------');
63+
console.log(chalk.white.bold('For Web'));
64+
console.log(chalk.green('Step 1. npm run web-watch'));
65+
console.log(chalk.black.bold('This compiles the TypeScript code and recompiles it whenever any files are changed.'))
66+
console.log(chalk.green('Step 2. Open index.html in your browser to view the result.'));
67+
console.log('----------------------------------------------------------');
68+
console.log(chalk.white.bold('For React Native'));
69+
console.log(chalk.green('Step 1. run npm run rn-watch'));
70+
console.log(chalk.black.bold('This compiles the TypeScript code and recompiles it whenever any files are changed.'));
71+
console.log(chalk.green('Step 2. run npm start'));
72+
console.log(chalk.black.bold('This starts the React Native Packager.'));
73+
console.log('----------------------------------------------------------');
74+
})
75+
.catch(error => {
76+
console.log(chalk.red('An unexpected error occurred'))
77+
console.log(chalk.red(error));
78+
});
3779
});
3880
}
3981

package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
},
2121
"devDependencies": {
2222
"chalk": "^1.1.3",
23-
"commander": "^2.9.0"
23+
"commander": "^2.9.0",
24+
"cross-spawn": "^5.1.0"
2425
}
2526
}

yarn.lock

+18
Original file line numberDiff line numberDiff line change
@@ -1178,6 +1178,14 @@ cross-spawn@^3.0.1:
11781178
lru-cache "^4.0.1"
11791179
which "^1.2.9"
11801180

1181+
cross-spawn@^5.1.0:
1182+
version "5.1.0"
1183+
resolved "https://registry.yarnpkg.com/cross-spawn/-/cross-spawn-5.1.0.tgz#e8bd0efee58fcff6f8f94510a0a554bbfa235449"
1184+
dependencies:
1185+
lru-cache "^4.0.1"
1186+
shebang-command "^1.2.0"
1187+
which "^1.2.9"
1188+
11811189
11821190
version "2.0.5"
11831191
resolved "https://registry.yarnpkg.com/cryptiles/-/cryptiles-2.0.5.tgz#3bdfecdc608147c1c67202fa291e7dca59eaa3b8"
@@ -3725,6 +3733,16 @@ setimmediate@^1.0.5:
37253733
version "1.0.5"
37263734
resolved "https://registry.yarnpkg.com/setimmediate/-/setimmediate-1.0.5.tgz#290cbb232e306942d7d7ea9b83732ab7856f8285"
37273735

3736+
shebang-command@^1.2.0:
3737+
version "1.2.0"
3738+
resolved "https://registry.yarnpkg.com/shebang-command/-/shebang-command-1.2.0.tgz#44aac65b695b03398968c39f363fee5deafdf1ea"
3739+
dependencies:
3740+
shebang-regex "^1.0.0"
3741+
3742+
shebang-regex@^1.0.0:
3743+
version "1.0.0"
3744+
resolved "https://registry.yarnpkg.com/shebang-regex/-/shebang-regex-1.0.0.tgz#da42f49740c0b42db2ca9728571cb190c98efea3"
3745+
37283746
37293747
version "1.6.1"
37303748
resolved "https://registry.yarnpkg.com/shell-quote/-/shell-quote-1.6.1.tgz#f4781949cce402697127430ea3b3c5476f481767"

0 commit comments

Comments
 (0)