1
#!/usr/bin/env node
2
/* eslint-disable no-console */
3

4 0
import minimist from 'minimist'
5 0
import kleur from 'kleur'
6 0
import createLogger from 'glogg'
7 0
import StyleguidistError from 'react-styleguidist/lib/scripts/utils/error'
8
import { SanitizedStyleguidistConfig } from '../types/StyleGuide'
9 0
import getConfig from '../scripts/config'
10 0
import consts from '../scripts/consts'
11 0
import * as binutils from '../scripts/binutils'
12 0
import isPromise from '../scripts/utils/isPromise'
13

14 0
const logger = createLogger('rsg')
15

16 0
const argv = minimist(process.argv.slice(2))
17 0
const command = argv._[0]
18

19
// Do not show nasty stack traces for Styleguidist errors
20 0
process.on('uncaughtException', (err: any) => {
21 1
	if (err.code === 'EADDRINUSE') {
22 0
		binutils.printErrorWithLink(
23
			`Another server is running at port ${config.serverPort} already. Please stop it or change the default port to continue.`,
24
			'You can change the port using the `serverPort` option in your style guide config:',
25
			consts.DOCS_CONFIG
26
		)
27 1
	} else if (err instanceof StyleguidistError) {
28 0
		console.error(kleur.bold.red(err.message))
29 1
		if (err.stack) {
30 0
			logger.debug(err.stack)
31
		}
32
	} else {
33 0
		console.error(err.toString())
34 0
		console.error(err.stack)
35
	}
36 0
	process.exit(1)
37
})
38

39
// Make sure user has webpack installed
40 0
require('react-styleguidist/lib/scripts/utils/ensureWebpack')
41

42
// Set environment before loading style guide config because user’s webpack config may use it
43 1
const env = command === 'build' ? 'production' : 'development'
44 1
process.env.NODE_ENV = process.env.NODE_ENV || env
45

46
// Load style guide config
47
let config: SanitizedStyleguidistConfig
48 0
try {
49 1
	if (argv.verbose) {
50 0
		process.env.VUESG_VERBOSE = 'true'
51
	}
52

53 0
	const conf = getConfig(argv.config, binutils.updateConfig)
54

55 1
	if (isPromise(conf)) {
56 0
		conf.then(runIt).catch(e => {
57 0
			throw e
58
		})
59
	} else {
60 0
		runIt(conf)
61
	}
62
} catch (err) {
63 1
	if (err instanceof StyleguidistError) {
64 1
		const link = consts.DOCS_CONFIG + (err.extra ? `#${err.extra.toLowerCase()}` : '')
65 0
		binutils.printErrorWithLink(
66
			err.message,
67
			`${err.extra}\n\nLearn how to configure your style guide:`,
68
			link
69
		)
70 0
		process.exit(1)
71
	} else {
72 0
		throw err
73
	}
74
}
75

76 0
function runIt(conf: SanitizedStyleguidistConfig) {
77 0
	config = conf
78 0
	binutils.verbose('Styleguidist config:', config)
79

80 0
	switch (command) {
81 1
		case 'build':
82 0
			binutils.commandBuild({
83
				...config,
84 1
				progressBar: argv.ci !== undefined ? !argv.ci : config.progressBar
85
			})
86 0
			break
87 0
		case 'server':
88 0
			binutils.commandServer(config, argv.open)
89 0
			break
90 0
		default:
91 0
			binutils.commandHelp()
92
	}
93
}

Read our documentation on viewing source code .

Loading