Skip to content

Commit 7ddc624

Browse files
committed
[io.js] Print a warning message if the user is not on io.js 2.x
Detects if the user is on Node or io.js 1.x and prints a banner explaining how to upgrade. We probably should link to more detailed upgrade docs so this is just a start.
1 parent 951b5f9 commit 7ddc624

File tree

5 files changed

+172
-11
lines changed

5 files changed

+172
-11
lines changed

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,12 @@
6363
"react-tools": "0.13.2",
6464
"rebound": "^0.0.12",
6565
"sane": "^1.1.2",
66+
"semver": "^4.3.6",
6667
"source-map": "0.1.31",
6768
"stacktrace-parser": "frantic/stacktrace-parser#493c5e5638",
6869
"uglify-js": "~2.4.16",
6970
"underscore": "1.7.0",
71+
"word-wrap": "^1.0.3",
7072
"worker-farm": "^1.3.1",
7173
"ws": "0.4.31",
7274
"yargs": "1.3.2"

packager/checkNodeVersion.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
* Copyright (c) 2015-present, Facebook, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree. An additional grant
7+
* of patent rights can be found in the PATENTS file in the same directory.
8+
*/
9+
'use strict';
10+
11+
var chalk = require('chalk');
12+
var semver = require('semver');
13+
14+
var formatBanner = require('./formatBanner');
15+
16+
function checkNodeVersion() {
17+
if (true || !semver.satisfies(process.version, '>=2.0.0')) {
18+
var engine = semver.lt(process.version, '1.0.0') ? 'Node' : 'io.js';
19+
var message = 'You are currently running ' + engine + ' ' +
20+
process.version + '.\n' +
21+
'\n' +
22+
'React Native is moving to io.js 2.x in the next release. There are ' +
23+
'several ways to upgrade to io.js depending on your preference.\n' +
24+
'\n' +
25+
'nvm: Run: nvm install iojs && nvm alias default iojs\n' +
26+
'Homebrew: Run: brew install iojs && brew link iojs --force\n' +
27+
'Installer: Download the Mac .pkg from https://iojs.org/\n' +
28+
'\n' +
29+
'https://github.com/facebook/react-native/issues/1737';
30+
console.log(formatBanner(message, {
31+
chalkFunction: chalk.green,
32+
marginLeft: 1,
33+
marginRight: 1,
34+
paddingBottom: 1,
35+
}));
36+
}
37+
}
38+
39+
module.exports = checkNodeVersion;

packager/formatBanner.js

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
/**
2+
* Copyright (c) 2015-present, Facebook, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the BSD-style license found in the
6+
* LICENSE file in the root directory of this source tree. An additional grant
7+
* of patent rights can be found in the PATENTS file in the same directory.
8+
*/
9+
'use strict';
10+
11+
var _ = require('underscore');
12+
var wordwrap = require('wordwrap');
13+
14+
var HORIZONTAL_LINE = '\u2500';
15+
var VERTICAL_LINE = '\u2502';
16+
var TOP_LEFT = '\u250c';
17+
var TOP_RIGHT = '\u2510';
18+
var BOTTOM_LEFT = '\u2514';
19+
var BOTTOM_RIGHT = '\u2518';
20+
21+
/**
22+
* Prints a banner with a border around it containing the given message. The
23+
* following options are supported:
24+
*
25+
* type Options = {
26+
* // A function to apply to each line of text to decorate it
27+
* chalkFunction: (string: message) => string;
28+
* // The total width (max line length) of the banner, including margin and
29+
* // padding (default = 80)
30+
* width: number;
31+
* // How much leading space to prepend to each line (default = 0)
32+
* marginLeft: number;
33+
* // How much trailing space to append to each line (default = 0)
34+
* marginRight: number;
35+
* // Space between the top banner border and the text (default = 0)
36+
* paddingTop: number;
37+
* // Space between the bottom banner border and the text (default = 0)
38+
* paddingBottom: number;
39+
* // Space between the left banner border and the text (default = 2)
40+
* paddingLeft: number;
41+
* // Space between the right banner border and the text (default = 2)
42+
* paddingRight: number;
43+
* };
44+
*/
45+
function formatBanner(message, options) {
46+
options = options || {};
47+
_.defaults(options, {
48+
chalkFunction: _.identity,
49+
width: 80,
50+
marginLeft: 0,
51+
marginRight: 0,
52+
paddingTop: 0,
53+
paddingBottom: 0,
54+
paddingLeft: 2,
55+
paddingRight: 2,
56+
});
57+
58+
var width = options.width;
59+
var marginLeft = options.marginLeft;
60+
var marginRight = options.marginRight;
61+
var paddingTop = options.paddingTop;
62+
var paddingBottom = options.paddingBottom;
63+
var paddingLeft = options.paddingLeft;
64+
var paddingRight = options.paddingRight;
65+
66+
var horizSpacing = marginLeft + paddingLeft + paddingRight + marginRight;
67+
// 2 for the banner borders
68+
var maxLineWidth = width - horizSpacing - 2;
69+
var wrap = wordwrap(maxLineWidth);
70+
var body = wrap(message);
71+
72+
var left = spaces(marginLeft) + VERTICAL_LINE + spaces(paddingLeft);
73+
var right = spaces(paddingRight) + VERTICAL_LINE + spaces(marginRight);
74+
var bodyLines = _.flatten([
75+
arrayOf('', paddingTop),
76+
body.split('\n'),
77+
arrayOf('', paddingBottom),
78+
]).map(function(line) {
79+
var padding = spaces(Math.max(0, maxLineWidth - line.length));
80+
return left + options.chalkFunction(line) + padding + right;
81+
});
82+
83+
var horizontalBorderLine = repeatString(
84+
HORIZONTAL_LINE,
85+
width - marginLeft - marginRight - 2
86+
);
87+
var top = spaces(marginLeft) + TOP_LEFT + horizontalBorderLine + TOP_RIGHT +
88+
spaces(marginRight);
89+
var bottom = spaces(marginLeft) + BOTTOM_LEFT + horizontalBorderLine +
90+
BOTTOM_RIGHT + spaces(marginRight);
91+
return _.flatten([top, bodyLines, bottom]).join('\n');
92+
}
93+
94+
function spaces(number) {
95+
return repeatString(' ', number);
96+
}
97+
98+
function repeatString(string, number) {
99+
return new Array(number + 1).join(string);
100+
}
101+
102+
function arrayOf(value, number) {
103+
return _.range(number).map(function() {
104+
return value;
105+
});
106+
}
107+
108+
module.exports = formatBanner;

packager/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@
2323
"lint": "node linter.js Examples/",
2424
"start": "./packager/packager.sh"
2525
},
26-
"dependencies": {},
26+
"dependencies": {
27+
"wordwrap": "^1.0.0"
28+
},
2729
"devDependencies": {
2830
"jest-cli": "0.4.5",
2931
"eslint": "0.9.2"

packager/packager.js

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,11 @@ if (!fs.existsSync(path.resolve(__dirname, '..', 'node_modules'))) {
2727

2828
var chalk = require('chalk');
2929
var connect = require('connect');
30+
var semver = require('semver');
3031
var ReactPackager = require('./react-packager');
3132
var blacklist = require('./blacklist.js');
33+
var checkNodeVersion = require('./checkNodeVersion');
34+
var formatBanner = require('./formatBanner');
3235
var launchEditor = require('./launchEditor.js');
3336
var parseCommandLine = require('./parseCommandLine.js');
3437
var webSocketProxy = require('./webSocketProxy.js');
@@ -100,18 +103,25 @@ if (options.assetRoots) {
100103
}
101104
}
102105

103-
console.log('\n' +
104-
' ===============================================================\n' +
105-
' | Running packager on port ' + options.port + '. \n' +
106-
' | Keep this packager running while developing on any JS \n' +
107-
' | projects. Feel free to close this tab and run your own \n' +
108-
' | packager instance if you prefer. \n' +
109-
' | \n' +
110-
' | https://github.com/facebook/react-native \n' +
111-
' | \n' +
112-
' ===============================================================\n'
106+
checkNodeVersion();
107+
108+
console.log(formatBanner(
109+
'Running packager on port ' + options.port + '.\n\n' +
110+
'Keep this packager running while developing on any JS projects. Feel free ' +
111+
'to close this tab and run your own packager instance if you prefer.\n\n' +
112+
'https://github.com/facebook/react-native', {
113+
marginLeft: 1,
114+
marginRight: 1,
115+
paddingBottom: 1,
116+
})
113117
);
114118

119+
if (!semver.satisfies(process.version, '>=1.0.0')) {
120+
121+
} else if (!semver.satisfies(process.version, '>=2.0.0')) {
122+
123+
}
124+
115125
console.log(
116126
'Looking for JS files in\n ',
117127
chalk.dim(options.projectRoots.join('\n ')),

0 commit comments

Comments
 (0)