mirror of
https://github.com/storybookjs/storybook.git
synced 2025-04-01 05:05:25 +08:00
REFACTOR to use fs-extra because I hear people like async/await && REFACTOR writing stats to file
This commit is contained in:
parent
b35aafb5dc
commit
7b8c7ae23c
@ -55,6 +55,7 @@
|
||||
"file-loader": "^2.0.0",
|
||||
"file-system-cache": "^1.0.5",
|
||||
"find-cache-dir": "^2.0.0",
|
||||
"fs-extra": "^7.0.1",
|
||||
"global": "^4.3.2",
|
||||
"html-webpack-plugin": "^4.0.0-beta.2",
|
||||
"inquirer": "^6.2.0",
|
||||
|
@ -3,7 +3,7 @@ import https from 'https';
|
||||
import ip from 'ip';
|
||||
import favicon from 'serve-favicon';
|
||||
import path from 'path';
|
||||
import fs from 'fs';
|
||||
import fs from 'fs-extra';
|
||||
import chalk from 'chalk';
|
||||
import { logger, colors } from '@storybook/node-logger';
|
||||
import fetch from 'node-fetch';
|
||||
@ -27,7 +27,15 @@ const cache = Cache({
|
||||
ns: 'storybook', // Optional. A grouping namespace for items.
|
||||
});
|
||||
|
||||
function getServer(app, options) {
|
||||
const writeStats = async (name, stats) => {
|
||||
await fs.writeFile(
|
||||
path.join(cacheDir, `${name}-stats.json`),
|
||||
JSON.stringify(stats.toJson(), null, 2),
|
||||
'utf8'
|
||||
);
|
||||
};
|
||||
|
||||
async function getServer(app, options) {
|
||||
if (!options.https) {
|
||||
return app;
|
||||
}
|
||||
@ -43,38 +51,40 @@ function getServer(app, options) {
|
||||
}
|
||||
|
||||
const sslOptions = {
|
||||
ca: (options.sslCa || []).map(ca => fs.readFileSync(ca, 'utf-8')),
|
||||
cert: fs.readFileSync(options.sslCert, 'utf-8'),
|
||||
key: fs.readFileSync(options.sslKey, 'utf-8'),
|
||||
ca: await Promise.all((options.sslCa || []).map(ca => fs.readFile(ca, 'utf-8'))),
|
||||
cert: await fs.readFile(options.sslCert, 'utf-8'),
|
||||
key: await fs.readFile(options.sslKey, 'utf-8'),
|
||||
};
|
||||
|
||||
return https.createServer(sslOptions, app);
|
||||
}
|
||||
|
||||
function applyStatic(app, options) {
|
||||
async function applyStatic(app, options) {
|
||||
const { staticDir } = options;
|
||||
|
||||
let hasCustomFavicon = false;
|
||||
|
||||
if (staticDir) {
|
||||
staticDir.forEach(dir => {
|
||||
const staticPath = path.resolve(dir);
|
||||
await Promise.all(
|
||||
staticDir.map(async dir => {
|
||||
const staticPath = path.resolve(dir);
|
||||
|
||||
if (!fs.existsSync(staticPath)) {
|
||||
logger.error(`Error: no such directory to load static files: ${staticPath}`);
|
||||
process.exit(-1);
|
||||
}
|
||||
if (await !fs.exists(staticPath)) {
|
||||
logger.error(`Error: no such directory to load static files: ${staticPath}`);
|
||||
process.exit(-1);
|
||||
}
|
||||
|
||||
logger.info(`=> Loading static files from: ${staticPath} .`);
|
||||
app.use(express.static(staticPath, { index: false }));
|
||||
logger.info(`=> Loading static files from: ${staticPath} .`);
|
||||
app.use(express.static(staticPath, { index: false }));
|
||||
|
||||
const faviconPath = path.resolve(staticPath, 'favicon.ico');
|
||||
const faviconPath = path.resolve(staticPath, 'favicon.ico');
|
||||
|
||||
if (fs.existsSync(faviconPath)) {
|
||||
hasCustomFavicon = true;
|
||||
app.use(favicon(faviconPath));
|
||||
}
|
||||
});
|
||||
if (await fs.exists(faviconPath)) {
|
||||
hasCustomFavicon = true;
|
||||
app.use(favicon(faviconPath));
|
||||
}
|
||||
})
|
||||
);
|
||||
}
|
||||
|
||||
if (!hasCustomFavicon) {
|
||||
@ -139,9 +149,9 @@ export async function buildDevStandalone(options) {
|
||||
}
|
||||
|
||||
const app = express();
|
||||
const server = getServer(app, options);
|
||||
const server = await getServer(app, options);
|
||||
|
||||
applyStatic(app, options);
|
||||
await applyStatic(app, options);
|
||||
|
||||
const storybookMiddleware = await storybook(options);
|
||||
|
||||
@ -150,7 +160,7 @@ export async function buildDevStandalone(options) {
|
||||
const serverListening = listenToServer(server, listenAddr);
|
||||
|
||||
const [
|
||||
{ iframeStats, managerStats, managerTotalTime, iframeTotalTime },
|
||||
{ previewStats, managerStats, managerTotalTime, previewTotalTime },
|
||||
updateInfo,
|
||||
] = await Promise.all([
|
||||
webpackValid,
|
||||
@ -216,7 +226,7 @@ export async function buildDevStandalone(options) {
|
||||
${colors.green(`Storybook ${chalk.bold(options.packageJson.version)} started`)}
|
||||
${chalk.gray(stripIndents`
|
||||
${chalk.underline(prettyTime(managerTotalTime))} for manager and ${chalk.underline(
|
||||
prettyTime(iframeTotalTime)
|
||||
prettyTime(previewTotalTime)
|
||||
)} for preview`)}
|
||||
|
||||
${serveMessage.toString()}${updateMessage ? `\n\n${updateMessage}` : ''}
|
||||
@ -226,19 +236,11 @@ export async function buildDevStandalone(options) {
|
||||
);
|
||||
|
||||
if (options.smokeTest) {
|
||||
fs.writeFileSync(
|
||||
path.join(cacheDir, 'preview-stats.json'),
|
||||
JSON.stringify(iframeStats.toJson(), null, 2),
|
||||
'utf8'
|
||||
);
|
||||
fs.writeFileSync(
|
||||
path.join(cacheDir, 'manager-stats.json'),
|
||||
JSON.stringify(managerStats.toJson(), null, 2),
|
||||
'utf8'
|
||||
);
|
||||
await writeStats('preview', previewStats);
|
||||
await writeStats('manager', managerStats);
|
||||
logger.info(`stats written to => ${chalk.cyan(path.join(cacheDir, '[name].json'))}`);
|
||||
|
||||
process.exit(iframeStats.toJson().warnings.length ? 1 : 0);
|
||||
process.exit(previewStats.toJson().warnings.length ? 1 : 0);
|
||||
process.exit(managerStats.toJson().warnings.length ? 1 : 0);
|
||||
} else if (!options.ci) {
|
||||
opn(address).catch(() => {
|
||||
|
21
yarn.lock
21
yarn.lock
@ -10422,6 +10422,15 @@ fs-extra@^5.0.0:
|
||||
jsonfile "^4.0.0"
|
||||
universalify "^0.1.0"
|
||||
|
||||
fs-extra@^7.0.1:
|
||||
version "7.0.1"
|
||||
resolved "https://registry.yarnpkg.com/fs-extra/-/fs-extra-7.0.1.tgz#4f189c44aa123b895f722804f55ea23eadc348e9"
|
||||
integrity sha512-YJDaCJZEnBmcbw13fvdAM9AwNOJwOzrE4pqMqBq5nFiEqXUqHwlK4B+3pUw6JNvfSPtX05xFHtYy/1ni01eGCw==
|
||||
dependencies:
|
||||
graceful-fs "^4.1.2"
|
||||
jsonfile "^4.0.0"
|
||||
universalify "^0.1.0"
|
||||
|
||||
fs-minipass@^1.2.5:
|
||||
version "1.2.5"
|
||||
resolved "https://registry.yarnpkg.com/fs-minipass/-/fs-minipass-1.2.5.tgz#06c277218454ec288df77ada54a03b8702aacb9d"
|
||||
@ -10967,12 +10976,12 @@ graphql-request@^1.5.0:
|
||||
dependencies:
|
||||
cross-fetch "2.2.2"
|
||||
|
||||
graphql@^0.13.2, graphql@^14.0.2:
|
||||
version "0.13.2"
|
||||
resolved "https://registry.yarnpkg.com/graphql/-/graphql-0.13.2.tgz#4c740ae3c222823e7004096f832e7b93b2108270"
|
||||
integrity sha512-QZ5BL8ZO/B20VA8APauGBg3GyEgZ19eduvpLWoq5x7gMmWnHoy8rlQWPLmWgFvo1yNgjSEFMesmS4R6pPr7xog==
|
||||
graphql@^14.0.2:
|
||||
version "14.0.2"
|
||||
resolved "https://registry.yarnpkg.com/graphql/-/graphql-14.0.2.tgz#7dded337a4c3fd2d075692323384034b357f5650"
|
||||
integrity sha512-gUC4YYsaiSJT1h40krG3J+USGlwhzNTXSb4IOZljn9ag5Tj+RkoXrWp+Kh7WyE3t1NCfab5kzCuxBIvOMERMXw==
|
||||
dependencies:
|
||||
iterall "^1.2.1"
|
||||
iterall "^1.2.2"
|
||||
|
||||
grizzly@^3.0.3:
|
||||
version "3.0.3"
|
||||
@ -12719,7 +12728,7 @@ istextorbinary@2.1.0:
|
||||
editions "^1.1.1"
|
||||
textextensions "1 || 2"
|
||||
|
||||
iterall@^1.2.1:
|
||||
iterall@^1.2.2:
|
||||
version "1.2.2"
|
||||
resolved "https://registry.yarnpkg.com/iterall/-/iterall-1.2.2.tgz#92d70deb8028e0c39ff3164fdbf4d8b088130cd7"
|
||||
integrity sha512-yynBb1g+RFUPY64fTrFv7nsjRrENBQJaX2UL+2Szc9REFrSNm1rpSXHGzhmAy7a9uv3vlvgBlXnf9RqmPH1/DA==
|
||||
|
Loading…
x
Reference in New Issue
Block a user