Skip to content

Commit 6bc78b1

Browse files
author
Thomas Mchugh
committed
created install command and moved cli to local-cli directory
1 parent 3f0e3b7 commit 6bc78b1

File tree

5 files changed

+186
-38
lines changed

5 files changed

+186
-38
lines changed

cli.js

+4-36
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,16 @@
1+
12
/**
23
* Copyright 2004-present Facebook. All Rights Reserved.
34
*/
45

56
'use strict';
67

7-
var spawn = require('child_process').spawn;
8-
var path = require('path');
9-
10-
function printUsage() {
11-
console.log([
12-
'Usage: react-native <command>',
13-
'',
14-
'Commands:',
15-
' start: starts the webserver',
16-
].join('\n'));
17-
process.exit(1);
18-
}
8+
var cli = require('./local-cli/cli.js');
199

2010
function run() {
21-
var args = process.argv.slice(2);
22-
if (args.length === 0) {
23-
printUsage();
24-
}
25-
26-
switch (args[0]) {
27-
case 'start':
28-
spawn('sh', [
29-
path.resolve(__dirname, 'packager', 'packager.sh'),
30-
'--projectRoots',
31-
process.cwd(),
32-
], {stdio: 'inherit'});
33-
break;
34-
default:
35-
console.error('Command `%s` unrecognized', args[0]);
36-
printUsage();
37-
}
38-
// Here goes any cli commands we need to
39-
}
40-
41-
function init(root, projectName) {
42-
spawn(path.resolve(__dirname, 'init.sh'), [projectName], {stdio:'inherit'});
11+
cli.run();
4312
}
4413

4514
module.exports = {
46-
run: run,
47-
init: init,
15+
run: run
4816
};

local-cli/cli.js

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/**
2+
* Copyright 2004-present Facebook. All Rights Reserved.
3+
*/
4+
5+
'use strict';
6+
7+
var spawn = require('child_process').spawn;
8+
var path = require('path');
9+
var install = require('./install.js');
10+
11+
function printUsage() {
12+
console.log([
13+
'Usage: react-native <command>',
14+
'',
15+
'Commands:',
16+
' start: starts the webserver',
17+
' install: installs npm react components'
18+
].join('\n'));
19+
process.exit(1);
20+
}
21+
22+
function run() {
23+
var args = process.argv.slice(2);
24+
if (args.length === 0) {
25+
printUsage();
26+
}
27+
28+
switch (args[0]) {
29+
case 'start':
30+
spawn('sh', [
31+
path.resolve(__dirname, 'packager', 'packager.sh'),
32+
'--projectRoots',
33+
process.cwd(),
34+
], {stdio: 'inherit'});
35+
break;
36+
case 'install':
37+
install.init();
38+
break;
39+
default:
40+
console.error('Command `%s` unrecognized', args[0]);
41+
printUsage();
42+
}
43+
// Here goes any cli commands we need to
44+
}
45+
46+
function init(root, projectName) {
47+
spawn(path.resolve(__dirname, 'init.sh'), [projectName], {stdio:'inherit'});
48+
}
49+
50+
module.exports = {
51+
run: run,
52+
init: init,
53+
};

local-cli/install.js

+123
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
/**
2+
* Copyright 2004-present Facebook. All Rights Reserved.
3+
*/
4+
5+
'use strict';
6+
7+
var fs = require('fs');
8+
var exec = require('child_process').exec;
9+
10+
var NODE_MODULE_PATH = './node_modules';
11+
12+
var PODFILE_PATH = './Podfile';
13+
14+
function addDependency(name, path) {
15+
console.log('Found dependency: ' + name);
16+
17+
var podfileText;
18+
try {
19+
podfileText = fs.readFileSync(PODFILE_PATH, 'utf8');
20+
} catch(e) {}
21+
22+
if (podfileText.indexOf('pod \'' + name + '\'') === -1) {
23+
var indexOfReactComponents = podfileText.indexOf('#</React-Native>') - 1;
24+
25+
var insertedDependency = '\npod \'' + name + '\', :path => \'' + path + '\'\n';
26+
var newPodfileText = [podfileText.slice(0, indexOfReactComponents),
27+
insertedDependency,
28+
podfileText.slice(indexOfReactComponents)].join('');
29+
30+
fs.writeFileSync(PODFILE_PATH, newPodfileText);
31+
console.log('Added ' + name + ' to Podfile.');
32+
} else {
33+
console.log(name + ' already in Podfile');
34+
}
35+
}
36+
37+
function installDependecies() {
38+
console.log('Installing dependencies...');
39+
exec('pod install', function(error, stdout, stderr) {
40+
if (!stderr) {
41+
console.log('Installed Pod dependencies.');
42+
} else {
43+
console.error('Error installing Pod dependencies.', stderr);
44+
}
45+
process.exit(1);
46+
});
47+
}
48+
49+
module.exports = {
50+
setupPodfile: function() {
51+
var podfileText;
52+
try {
53+
podfileText = fs.readFileSync(PODFILE_PATH, 'utf8');
54+
} catch(e) {}
55+
56+
var openingReactTag = '#<React-Native>';
57+
var closingReactTag = '\n#</React-Native>';
58+
var reactPodfileBoilerplate = openingReactTag + closingReactTag;
59+
60+
if (!podfileText) {
61+
fs.appendFileSync(PODFILE_PATH, reactPodfileBoilerplate);
62+
} else {
63+
if (podfileText.indexOf(openingReactTag) === -1 || podfileText.indexOf(closingReactTag) === -1) {
64+
fs.appendFileSync(PODFILE_PATH, reactPodfileBoilerplate);
65+
}
66+
}
67+
68+
try {
69+
podfileText = fs.readFileSync(PODFILE_PATH, 'utf8');
70+
} catch(e) {}
71+
72+
if (podfileText.indexOf('pod \'React\'') === -1) {
73+
var indexOfReactComponents = podfileText.indexOf(openingReactTag) + openingReactTag.length;
74+
75+
var insertedReactDependency = '\npod \'React\', :path => \'node_modules/react-native\'\n';
76+
try {
77+
var newPodfileText = [podfileText.slice(0, indexOfReactComponents),
78+
insertedReactDependency,
79+
podfileText.slice(indexOfReactComponents)].join('');
80+
81+
fs.writeFileSync(PODFILE_PATH, newPodfileText);
82+
} catch(e) {
83+
throw e;
84+
}
85+
}
86+
},
87+
init: function(arguement) {
88+
// arguement is available for future arguement commands
89+
console.log('Searching for installable React Native components...');
90+
this.setupPodfile();
91+
92+
var nodeModuleList = fs.readdirSync(NODE_MODULE_PATH);
93+
94+
if (nodeModuleList.length > 0) {
95+
nodeModuleList.forEach(function(nodeModule) {
96+
// Module would not start with '.' hidden file identifier
97+
if (nodeModule.charAt(0) !== '.') {
98+
var modulePath = './node_modules/' + nodeModule;
99+
100+
var nodeModulePackage;
101+
try {
102+
nodeModulePackage = fs.readFileSync(modulePath + '/package.json', 'utf8');
103+
} catch(error) {
104+
console.error('Error reading Node Module: `%s` package.json', nodeModule);
105+
throw error;
106+
}
107+
108+
var packageJSON = JSON.parse(nodeModulePackage);
109+
console.log(packageJSON.hasOwnProperty('react-native-component'));
110+
if (packageJSON.hasOwnProperty('react-native-component')) {
111+
addDependency(nodeModule, modulePath);
112+
}
113+
}
114+
});
115+
116+
installDependecies();
117+
} else {
118+
console.error('./node_modules directory contains 0 modules');
119+
console.log('No React Native components found.');
120+
process.exit(1);
121+
}
122+
}
123+
};

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
"Examples/SampleApp",
2727
"Libraries",
2828
"packager",
29+
"local-cli",
2930
"cli.js",
3031
"init.sh",
3132
"LICENSE",

react-native-cli/index.js

+5-2
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ if (cli) {
3333
process.exit(1);
3434
}
3535

36-
if (args[0] === 'init') {
36+
switch (args[0]) {
37+
case 'init':
3738
if (args[1]) {
3839
init(args[1]);
3940
} else {
@@ -42,13 +43,15 @@ if (cli) {
4243
);
4344
process.exit(1);
4445
}
45-
} else {
46+
break;
47+
default:
4648
console.error(
4749
'Command `%s` unrecognized. ' +
4850
'Did you mean to run this inside a react-native project?',
4951
args[0]
5052
);
5153
process.exit(1);
54+
break;
5255
}
5356
}
5457

0 commit comments

Comments
 (0)