4
4
5
5
const minimist = require ( 'minimist' ) ;
6
6
const chalk = require ( 'chalk' ) ;
7
+ const ora = require ( 'ora' ) ;
7
8
const stringify = require ( 'q-i' ) . stringify ;
9
+ const formatWebpackMessages = require ( 'react-dev-utils/formatWebpackMessages' ) ;
10
+ const webpackDevServerUtils = require ( 'react-dev-utils/WebpackDevServerUtils' ) ;
8
11
const logger = require ( 'glogg' ) ( 'rsg' ) ;
9
12
const getConfig = require ( '../scripts/config' ) ;
10
13
const setupLogger = require ( '../scripts/logger' ) ;
11
14
const consts = require ( '../scripts/consts' ) ;
12
- const formatWebpackMessages = require ( 'react-dev-utils/formatWebpackMessages' ) ;
13
15
const StyleguidistError = require ( '../scripts/utils/error' ) ;
14
- const devServerUtils = require ( '../scripts/utils/devServerUtils' ) ;
15
16
16
17
const argv = minimist ( process . argv . slice ( 2 ) ) ;
17
18
const command = argv . _ [ 0 ] ;
@@ -82,15 +83,15 @@ function updateConfig(config) {
82
83
}
83
84
84
85
function commandBuild ( ) {
85
- logger . info ( 'Building style guide...' ) ;
86
+ console . log ( 'Building style guide...' ) ;
86
87
87
88
const build = require ( '../scripts/build' ) ;
88
89
const compiler = build ( config , err => {
89
90
if ( err ) {
90
91
console . error ( err ) ;
91
92
process . exit ( 1 ) ;
92
93
} else {
93
- logger . info ( 'Style guide published to:\n' + chalk . underline ( config . styleguideDir ) ) ;
94
+ console . log ( 'Style guide published to:\n' + chalk . underline ( config . styleguideDir ) ) ;
94
95
}
95
96
} ) ;
96
97
@@ -107,37 +108,44 @@ function commandBuild() {
107
108
}
108
109
109
110
function commandServer ( ) {
111
+ let spinner ;
112
+
110
113
const server = require ( '../scripts/server' ) ;
111
114
const compiler = server ( config , err => {
112
115
if ( err ) {
113
116
console . error ( err ) ;
114
117
} else {
115
118
const isHttps = compiler . options . devServer && compiler . options . devServer . https ;
116
- devServerUtils . printInstructions ( isHttps , config . serverHost , config . serverPort ) ;
119
+ printInstructions ( isHttps , config . serverHost , config . serverPort ) ;
117
120
}
118
121
} ) ;
119
122
120
123
verbose ( 'Webpack config:' , compiler . options ) ;
121
124
122
- // Show message when Webpack is recompiling the bundle
125
+ // Show message when webpack is recompiling the bundle
123
126
compiler . plugin ( 'invalid' , function ( ) {
124
- logger . info ( 'Compiling…' ) ;
127
+ console . log ( ) ;
128
+ spinner = ora ( 'Compiling...' ) . start ( ) ;
125
129
} ) ;
126
130
127
131
// Custom error reporting
128
132
compiler . plugin ( 'done' , function ( stats ) {
133
+ if ( spinner ) {
134
+ spinner . stop ( ) ;
135
+ }
136
+
129
137
const messages = formatWebpackMessages ( stats . toJson ( { } , true ) ) ;
130
138
131
139
if ( ! messages . errors . length && ! messages . warnings . length ) {
132
- logger . info ( chalk . green ( 'Compiled successfully!' ) ) ;
140
+ printStatus ( 'Compiled successfully!' , 'success' ) ;
133
141
}
134
142
135
143
printAllErrorsAndWarnings ( messages , stats . compilation ) ;
136
144
} ) ;
137
145
}
138
146
139
147
function commandHelp ( ) {
140
- logger . info (
148
+ console . log (
141
149
[
142
150
chalk . underline ( 'Usage' ) ,
143
151
'' ,
@@ -162,18 +170,53 @@ function commandHelp() {
162
170
) ;
163
171
}
164
172
173
+ /**
174
+ * @param {boolean } isHttps
175
+ * @param {string } host
176
+ * @param {number } port
177
+ */
178
+ function printInstructions ( isHttps , host , port ) {
179
+ const urls = webpackDevServerUtils . prepareUrls ( isHttps ? 'https' : 'http' , host , port ) ;
180
+ console . log ( `You can now view your style guide in the browser:` ) ;
181
+ console . log ( ) ;
182
+ console . log ( ` ${ chalk . bold ( 'Local:' ) } ${ urls . localUrlForTerminal } ` ) ;
183
+ console . log ( ` ${ chalk . bold ( 'On your network:' ) } ${ urls . lanUrlForTerminal } ` ) ;
184
+ console . log ( ) ;
185
+ }
186
+
165
187
function printErrorWithLink ( message , linkTitle , linkUrl ) {
166
188
console . error ( `${ chalk . bold . red ( message ) } \n\n${ linkTitle } \n${ chalk . underline ( linkUrl ) } \n` ) ;
167
189
}
168
190
169
- function printErrors ( header , errors , originalErrors , printer ) {
170
- console . error ( printer ( header ) ) ;
191
+ /**
192
+ * @param {string } header
193
+ * @param {object } errors
194
+ * @param {object } originalErrors
195
+ * @param {'success'|'error'|'warning' } type
196
+ */
197
+ function printErrors ( header , errors , originalErrors , type ) {
198
+ printStatus ( header , type ) ;
199
+ console . error ( ) ;
171
200
const messages = argv . verbose ? originalErrors : errors ;
172
201
messages . forEach ( message => {
173
202
console . error ( message . message || message ) ;
174
203
} ) ;
175
204
}
176
205
206
+ /**
207
+ * @param {string } text
208
+ * @param {'success'|'error'|'warning' } type
209
+ */
210
+ function printStatus ( text , type ) {
211
+ if ( type === 'success' ) {
212
+ console . log ( chalk . inverse . bold . green ( ' DONE ' ) + ' ' + text ) ;
213
+ } else if ( type === 'error' ) {
214
+ console . error ( chalk . reset . inverse . bold . red ( ' FAIL ' ) + ' ' + chalk . reset . red ( text ) ) ;
215
+ } else {
216
+ console . error ( chalk . reset . inverse . bold . yellow ( ' WARN ' ) + ' ' + chalk . reset . yellow ( text ) ) ;
217
+ }
218
+ }
219
+
177
220
function printAllErrorsAndWarnings ( messages , compilation ) {
178
221
// If errors exist, only show errors.
179
222
if ( messages . errors . length ) {
@@ -192,11 +235,11 @@ function printAllErrorsAndWarnings(messages, compilation) {
192
235
function printAllErrors ( errors , originalErrors ) {
193
236
printStyleguidistError ( errors ) ;
194
237
printNoLoaderError ( errors ) ;
195
- printErrors ( 'Failed to compile. ' , errors , originalErrors , chalk . red ) ;
238
+ printErrors ( 'Failed to compile' , errors , originalErrors , 'error' ) ;
196
239
}
197
240
198
241
function printAllWarnings ( warnings , originalWarnings ) {
199
- printErrors ( 'Compiled with warnings. ' , warnings , originalWarnings , chalk . yellow ) ;
242
+ printErrors ( 'Compiled with warnings' , warnings , originalWarnings , 'warning' ) ;
200
243
}
201
244
202
245
function printStyleguidistError ( errors ) {
0 commit comments