Skip to content

Commit 3617c3d

Browse files
committed
Feat: Tweak CLI a bit
* Nicer success / failure messages * Use ora for spinner (compiling..) * Tweak instructions layout
1 parent 48bf693 commit 3617c3d

File tree

5 files changed

+113
-69
lines changed

5 files changed

+113
-69
lines changed

bin/styleguidist.js

Lines changed: 56 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@
44

55
const minimist = require('minimist');
66
const chalk = require('chalk');
7+
const ora = require('ora');
78
const stringify = require('q-i').stringify;
9+
const formatWebpackMessages = require('react-dev-utils/formatWebpackMessages');
10+
const webpackDevServerUtils = require('react-dev-utils/WebpackDevServerUtils');
811
const logger = require('glogg')('rsg');
912
const getConfig = require('../scripts/config');
1013
const setupLogger = require('../scripts/logger');
1114
const consts = require('../scripts/consts');
12-
const formatWebpackMessages = require('react-dev-utils/formatWebpackMessages');
1315
const StyleguidistError = require('../scripts/utils/error');
14-
const devServerUtils = require('../scripts/utils/devServerUtils');
1516

1617
const argv = minimist(process.argv.slice(2));
1718
const command = argv._[0];
@@ -82,15 +83,15 @@ function updateConfig(config) {
8283
}
8384

8485
function commandBuild() {
85-
logger.info('Building style guide...');
86+
console.log('Building style guide...');
8687

8788
const build = require('../scripts/build');
8889
const compiler = build(config, err => {
8990
if (err) {
9091
console.error(err);
9192
process.exit(1);
9293
} 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));
9495
}
9596
});
9697

@@ -107,37 +108,44 @@ function commandBuild() {
107108
}
108109

109110
function commandServer() {
111+
let spinner;
112+
110113
const server = require('../scripts/server');
111114
const compiler = server(config, err => {
112115
if (err) {
113116
console.error(err);
114117
} else {
115118
const isHttps = compiler.options.devServer && compiler.options.devServer.https;
116-
devServerUtils.printInstructions(isHttps, config.serverHost, config.serverPort);
119+
printInstructions(isHttps, config.serverHost, config.serverPort);
117120
}
118121
});
119122

120123
verbose('Webpack config:', compiler.options);
121124

122-
// Show message when Webpack is recompiling the bundle
125+
// Show message when webpack is recompiling the bundle
123126
compiler.plugin('invalid', function() {
124-
logger.info('Compiling…');
127+
console.log();
128+
spinner = ora('Compiling...').start();
125129
});
126130

127131
// Custom error reporting
128132
compiler.plugin('done', function(stats) {
133+
if (spinner) {
134+
spinner.stop();
135+
}
136+
129137
const messages = formatWebpackMessages(stats.toJson({}, true));
130138

131139
if (!messages.errors.length && !messages.warnings.length) {
132-
logger.info(chalk.green('Compiled successfully!'));
140+
printStatus('Compiled successfully!', 'success');
133141
}
134142

135143
printAllErrorsAndWarnings(messages, stats.compilation);
136144
});
137145
}
138146

139147
function commandHelp() {
140-
logger.info(
148+
console.log(
141149
[
142150
chalk.underline('Usage'),
143151
'',
@@ -162,18 +170,53 @@ function commandHelp() {
162170
);
163171
}
164172

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+
165187
function printErrorWithLink(message, linkTitle, linkUrl) {
166188
console.error(`${chalk.bold.red(message)}\n\n${linkTitle}\n${chalk.underline(linkUrl)}\n`);
167189
}
168190

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();
171200
const messages = argv.verbose ? originalErrors : errors;
172201
messages.forEach(message => {
173202
console.error(message.message || message);
174203
});
175204
}
176205

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+
177220
function printAllErrorsAndWarnings(messages, compilation) {
178221
// If errors exist, only show errors.
179222
if (messages.errors.length) {
@@ -192,11 +235,11 @@ function printAllErrorsAndWarnings(messages, compilation) {
192235
function printAllErrors(errors, originalErrors) {
193236
printStyleguidistError(errors);
194237
printNoLoaderError(errors);
195-
printErrors('Failed to compile.', errors, originalErrors, chalk.red);
238+
printErrors('Failed to compile', errors, originalErrors, 'error');
196239
}
197240

198241
function printAllWarnings(warnings, originalWarnings) {
199-
printErrors('Compiled with warnings.', warnings, originalWarnings, chalk.yellow);
242+
printErrors('Compiled with warnings', warnings, originalWarnings, 'warning');
200243
}
201244

202245
function printStyleguidistError(errors) {

package-lock.json

Lines changed: 56 additions & 33 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
"lowercase-keys": "^1.0.0",
6868
"markdown-to-jsx": "6.2.1",
6969
"minimist": "^1.2.0",
70+
"ora": "^1.3.0",
7071
"prop-types": "^15.6.0",
7172
"q-i": "^1.2.0",
7273
"react-codemirror2": "^3.0.7",

scripts/utils/__tests__/devServerUtils.spec.js

Lines changed: 0 additions & 6 deletions
This file was deleted.

scripts/utils/devServerUtils.js

Lines changed: 0 additions & 17 deletions
This file was deleted.

0 commit comments

Comments
 (0)