UNPKG

3.71 kBJavaScriptView Raw
1'use strict';
2// import export wdyu
3let reactopt = require('./src/index.js');
4reactopt = reactopt.whyDidYouUpdate;
5export {reactopt};
6
7//chalk requirements
8const chalk = require('./node_modules/chalk');
9const chalkAnimation = require('./node_modules/chalk-animation');
10const log = console.log;
11
12const chromeLauncher = require('./node_modules/chrome-launcher');
13const readline = require('readline');
14const rl = readline.createInterface({
15 input: process.stdin,
16 output: process.stdout
17});
18
19//import data file
20let data = require('./data').data;
21
22//start chrome-launcher to allow user to interact with React app
23chromeLauncher.launch({
24 startingUrl: 'http://localhost:3000',
25}).then((chrome) => {
26 rl.on('line', (line) => {
27 if (line === 'exit') {
28 chrome.kill();
29 endUserInteraction();
30 }
31 });
32});
33
34//runs on start of reactopt
35function startReactopt() {
36 log(chalk.bgCyan.bold('Reactopt is running - Interact with your app and then type/enter "end"'));
37 log('');
38 const loading = chalkAnimation.radar('----------------------------------------------------------------------');
39 loading.start();
40}
41
42startReactopt(); // runs on npm run reactopt
43
44// when user 'ends' interaction, execute this code
45function endUserInteraction() {
46 //execute functions to test/print other logic
47 printLine();
48 componentRerenders();
49 printLine();
50 versionOfReact();
51 printLine();
52 productionMode();
53}
54
55// styling for different console logs
56function printHeading(string) {
57 log(chalk.black.bgWhite.dim(string));
58 log('');
59}
60
61function printPass(string) {
62 log(chalk.cyan.bold(string));
63}
64
65function printFail(string) {
66 log(chalk.magenta.bold(string));
67}
68
69function printSuggestion(string) {
70 log(chalk.gray(string));
71}
72
73function printLine() {
74 log('');
75 log(chalk.gray('------------------------------------------------------'));
76 log('');
77}
78
79// test functions
80function componentRerenders() {
81 let events = Object.keys(data);
82
83 if (events.length !== 0) {
84 let components;
85
86 printHeading('Unnecessary Component Re-rendering');
87 printFail('There are components that unnecessarily re-rendered, and the events that triggered them:');
88 log('');
89
90 //print events and components rerendered for each
91 for (let x = 0; x < events.length; x += 1) {
92 components = Object.keys(data[events[x]]);
93 log(chalk.underline(events[x]), chalk.reset.white(' : ' + components) );
94 }
95 printSuggestion("Consider implementing 'shouldComponentUpdate' to prevent re-rendering when \nthe states or props of a component don't change.");
96 } else {
97 printPass('Your version of React is the most current and quickest.');
98 }
99}
100
101function versionOfReact() {
102 //scrape for version
103 let version = '16';
104 printHeading('Version of React');
105 if (version === '16') {
106 printPass('Your version of React is the most current and quickest.');
107 } else {
108 printFail('Your version of React is out of date and slower than newer versions');
109 printSuggestion('Consider upgrading to React v 16, which has the fastest production speed.');
110 }
111}
112
113function productionMode() {
114 //scrape for version
115 let processChecks = true;
116 printHeading('Rendering in Production/Development Mode');
117 if (processChecks === false) {
118 printPass('Your version of React is the most current and quickest.');
119 } else {
120 printFail('Your code contains unneccesary process.env.NODE_ENV checks.');
121 printSuggestion('These checks are useful but can slow down your application. \n Be sure these are removed when application is put into production.');
122 }
123}
124
125module.export = reactopt;
126