Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Spike on rearranging the build and server methods. #878

Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 23 additions & 7 deletions bin/styleguidist.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,10 @@ function commandBuild() {
if (err) {
console.error(err);
process.exit(1);
} else if (typeof config.printBuildInstructions === 'function') {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can be sure that if a config option has a value, then it's a function (it'd fail before otherwise), so if (config.printBuildInstructions) would be enough.

Copy link
Contributor Author

@roblevintennis roblevintennis Mar 20, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed in 0eda93b

config.printBuildInstructions(config);
} else {
console.log('Style guide published to:\n' + chalk.underline(config.styleguideDir));
printBuildInstructions(config);
}
});

Expand All @@ -120,7 +122,12 @@ function commandServer() {
console.error(err);
} else {
const isHttps = compiler.options.devServer && compiler.options.devServer.https;
printInstructions(isHttps, config.serverHost, config.serverPort);
const configuration = Object.assign({ isHttps }, config);
if (typeof config.printServerInstructions === 'function') {
config.printServerInstructions(configuration);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd do config.printServerInstructions(config, { isHttps });, so config would be always a styleguide config, and extra options would be separated — less cognitive load ;-)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gotcha. Fixed in 0eda93b

} else {
printServerInstructions(configuration);
}
}
});

Expand Down Expand Up @@ -175,19 +182,28 @@ function commandHelp() {
}

/**
* @param {boolean} isHttps
* @param {string} host
* @param {number} port
* @param {object} config
*/
function printInstructions(isHttps, host, port) {
const urls = webpackDevServerUtils.prepareUrls(isHttps ? 'https' : 'http', host, port);
function printServerInstructions(config) {
const urls = webpackDevServerUtils.prepareUrls(
config.isHttps ? 'https' : 'http',
config.serverHost,
config.serverPort
);
console.log(`You can now view your style guide in the browser:`);
console.log();
console.log(` ${chalk.bold('Local:')} ${urls.localUrlForTerminal}`);
console.log(` ${chalk.bold('On your network:')} ${urls.lanUrlForTerminal}`);
console.log();
}

/**
* @param {object} config
*/
function printBuildInstructions(config) {
console.log('Style guide published to:\n' + chalk.underline(config.styleguideDir));
}

/**
* @param {string} message
* @param {string} linkTitle
Expand Down
14 changes: 14 additions & 0 deletions docs/Configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ By default, Styleguidist will look for `styleguide.config.js` file in your proje
* [`handlers`](#handlers)
* [`ignore`](#ignore)
* [`logger`](#logger)
* [`printBuildInstructions`](#printBuildInstructions)
* [`printServerInstructions`](#printServerInstructions)
* [`previewDelay`](#previewdelay)
* [`propsParser`](#propsparser)
* [`require`](#require)
Expand Down Expand Up @@ -259,6 +261,18 @@ module.exports = {
}
```

#### `printBuildInstructions`

Type: `Function`, optional

Function that allows you to override the printing of build messages to console.log. Takes the `config` object with `isHttps` merged in.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I’d show an example instead of Takes the configobject withisHttps merged in..

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great point. I removed and added example for both methods in 0eda93b


#### `printServerInstructions`

Type: `Function`, optional

Function that allows you to override the printing of build messages to console.log. Takes the `config` object with `isHttps` merged in.

#### `previewDelay`

Type: `Number`, default: 500
Expand Down
9 changes: 9 additions & 0 deletions examples/customised/styleguide.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,15 @@ module.exports = {
const name = path.basename(componentPath, '.js');
return `import { ${name} } from 'my-awesome-library';`;
},

// Example of overriding the CLI message in local development.
// Uncomment/edit the following `serverHost` entry to see in output
// serverHost: 'your-domain',
printServerInstructions(config) {
// eslint-disable-next-line no-console
console.log(`'View your styleguide at: http://${config.serverHost}`);
},

// Override Styleguidist components
styleguideComponents: {
LogoRenderer: path.join(__dirname, 'styleguide/components/Logo'),
Expand Down
2 changes: 2 additions & 0 deletions loaders/styleguide-loader.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ const CLIENT_CONFIG_OPTIONS = [
'showCode',
'showUsage',
'showSidebar',
'printBuildInstructions',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think you need this, we use this array to pass config options to React.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed and ran locally and nothing bad happened. Thanks for heads up

'printServerInstructions',
'previewDelay',
'theme',
'styles',
Expand Down
6 changes: 6 additions & 0 deletions scripts/schemas/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,12 @@ module.exports = {
type: 'number',
default: 500,
},
printBuildInstructions: {
type: 'function',
},
printServerInstructions: {
type: 'function',
},
propsParser: {
type: 'function',
},
Expand Down