Skip to content

Commit dda3d28

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 1d1386e commit dda3d28

File tree

5 files changed

+168
-11
lines changed

5 files changed

+168
-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: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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 (!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. There are several ways to upgrade' +
23+
'to io.js depending on your preference.\n' +
24+
'\n' +
25+
'nvm: nvm install iojs && nvm alias default iojs\n' +
26+
'Homebrew: brew unlink node; brew install iojs && brew ln iojs --force\n' +
27+
'Installer: download the Mac .pkg from https://iojs.org/\n' +
28+
'\n' +
29+
'About io.js: https://iojs.org\n' +
30+
'Follow along at: https://github.com/facebook/react-native/issues/1737';
31+
console.log(formatBanner(message, {
32+
chalkFunction: chalk.green,
33+
marginLeft: 1,
34+
marginRight: 1,
35+
paddingBottom: 1,
36+
}));
37+
}
38+
}
39+
40+
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": "git://github.com/facebook/jest#0.5.x",
2931
"eslint": "0.9.2"

packager/packager.js

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ var chalk = require('chalk');
3030
var connect = require('connect');
3131
var ReactPackager = require('./react-packager');
3232
var blacklist = require('./blacklist.js');
33+
var checkNodeVersion = require('./checkNodeVersion');
34+
var formatBanner = require('./formatBanner');
3335
var launchEditor = require('./launchEditor.js');
3436
var parseCommandLine = require('./parseCommandLine.js');
3537
var webSocketProxy = require('./webSocketProxy.js');
@@ -108,16 +110,19 @@ if (options.assetRoots) {
108110
}
109111
}
110112

111-
console.log('\n' +
112-
' ===============================================================\n' +
113-
' | Running packager on port ' + options.port + '. \n' +
114-
' | Keep this packager running while developing on any JS \n' +
115-
' | projects. Feel free to close this tab and run your own \n' +
116-
' | packager instance if you prefer. \n' +
117-
' | \n' +
118-
' | https://github.com/facebook/react-native \n' +
119-
' | \n' +
120-
' ===============================================================\n'
113+
checkNodeVersion();
114+
115+
console.log(formatBanner(
116+
'Running packager on port ' + options.port + '.\n'+
117+
'\n' +
118+
'Keep this packager running while developing on any JS projects. Feel free ' +
119+
'to close this tab and run your own packager instance if you prefer.\n' +
120+
'\n' +
121+
'https://github.com/facebook/react-native', {
122+
marginLeft: 1,
123+
marginRight: 1,
124+
paddingBottom: 1,
125+
})
121126
);
122127

123128
console.log(

0 commit comments

Comments
 (0)