-
-
Notifications
You must be signed in to change notification settings - Fork 9.3k
/
index.js
executable file
·58 lines (48 loc) · 1.66 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/usr/bin/env node
import express from 'express';
import program from 'commander';
import path from 'path';
import fs from 'fs';
import storybook from './middleware';
import packageJson from '../../package.json';
const logger = console;
program
.version(packageJson.version)
.option('-p, --port [number]', 'Port to run Storybook (Required)', parseInt)
.option('-h, --host [string]', 'Host to run Storybook')
.option('-s, --static-dir [dir-name]', 'Directory where to load static files from')
.option('-c, --config-dir [dir-name]', 'Directory where to load Storybook configurations from')
.parse(process.argv);
if (!program.port) {
logger.error('Error: port to run Storybook is required!\n');
program.help();
process.exit(-1);
}
// Used with `app.listen` below
const listenAddr = [program.port];
if (program.host) {
listenAddr.push(program.host);
}
const app = express();
if (program.staticDir) {
const staticPath = path.resolve(program.staticDir);
if (fs.existsSync(staticPath)) {
logger.log(`=> Loading static files from: ${staticPath} .`);
app.use(express.static(staticPath, { index: false }));
} else {
logger.error(`Error: no such directory to load static files: ${staticPath}`);
process.exit(-1);
}
}
// Build the webpack configuration using the `baseConfig`
// custom `.babelrc` file and `webpack.config.js` files
const configDir = program.configDir || './.storybook';
app.use(storybook(configDir));
app.listen(...listenAddr, function (error) {
if (error) {
throw error;
} else {
const address = `http://${program.host || 'localhost'}:${program.port}/`;
logger.info(`\nReact Storybook started on => ${address}\n`);
}
});