-
Notifications
You must be signed in to change notification settings - Fork 2
/
build_storybook.js
executable file
·72 lines (55 loc) · 2.06 KB
/
build_storybook.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
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/usr/bin/env node
const webpack = require('webpack');
const program = require('commander');
const pack = require('./package');
const path = require('path');
const fs = require('fs');
const customFs = require('./lib/fs');
const { CUSTOM_PREVIEW_HEADER, FAVICON_FILE, FALLBACK_FAVICON_FILE } = require('./config').paths;
const getWebpackConfig = require('./lib/get_webpack_config');
const managerPageTemplate = require('./templates/manager_page.html');
const previewPageTemplate = require('./templates/preview_page.html');
const parseOutputPath = dir => path.resolve(process.cwd(), dir);
// read arguments
program
.version(pack.version)
.option('-o, --output <dir>', 'Output Path', parseOutputPath)
.parse(process.argv);
// build
Promise.all([
customFs.readFile(CUSTOM_PREVIEW_HEADER),
getWebpackConfig(),
customFs.checkFile(FAVICON_FILE),
]).then(([header, webpackConfig, faviconFileExists]) => {
if (program.output) {
if (!fs.existsSync(program.output)) {
// Make output Directory
fs.mkdirSync(program.output);
}
webpackConfig.output.path = program.output;
}
const BUILD_DIR = webpackConfig.output.path;
console.log(`Building static storybook into ${BUILD_DIR}`);
const compiler = webpack(webpackConfig, (err, stats) => {
if (err) {
console.error(err.stack || err);
if (err.details) {
console.error(err.details);
}
return;
}
console.log(stats.toString('normal'));
const statsJson = stats.toJson();
customFs.saveFile(path.resolve(BUILD_DIR, 'index.html'), managerPageTemplate({
assets: [].concat(statsJson.assetsByChunkName.manager).map(asset => `${statsJson.publicPath}${asset}`),
}));
customFs.saveFile(path.resolve(BUILD_DIR, 'preview.html'), previewPageTemplate({
header: header,
assets: [].concat(statsJson.assetsByChunkName.preview).map(asset => `${statsJson.publicPath}${asset}`),
}));
customFs.copyFile(
faviconFileExists ? FAVICON_FILE : FALLBACK_FAVICON_FILE,
path.resolve(BUILD_DIR, 'favicon.ico')
);
});
}).catch(console.log);