UNPKG

3.05 kBJavaScriptView Raw
1"use strict";
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6exports.extract = extract;
7
8require("core-js/modules/es.promise.js");
9
10var _path = _interopRequireDefault(require("path"));
11
12var _fsExtra = require("fs-extra");
13
14var _puppeteerCore = _interopRequireDefault(require("puppeteer-core"));
15
16var _express = _interopRequireDefault(require("express"));
17
18var _getPort = _interopRequireDefault(require("get-port"));
19
20var _nodeLogger = require("@storybook/node-logger");
21
22function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
23
24const read = async url => {
25 const browser = await usePuppeteerBrowser();
26 const page = await browser.newPage();
27 await page.goto(url); // we don't know whether we are running against a new or old storybook
28 // FIXME: add tests for both
29
30 await page.waitForFunction(`
31 (window.__STORYBOOK_PREVIEW__ && window.__STORYBOOK_PREVIEW__.extract && window.__STORYBOOK_PREVIEW__.extract()) ||
32 (window.__STORYBOOK_STORY_STORE__ && window.__STORYBOOK_STORY_STORE__.extract && window.__STORYBOOK_STORY_STORE__.extract())
33 `);
34 const data = JSON.parse(await page.evaluate(async () => {
35 // eslint-disable-next-line no-undef
36 return JSON.stringify(window.__STORYBOOK_STORY_STORE__.getStoriesJsonData(), null, 2);
37 }));
38 setImmediate(() => {
39 browser.close();
40 });
41 return data;
42};
43
44const useLocation = async input => {
45 // check for input's existence
46 await (0, _fsExtra.stat)(_path.default.resolve(input));
47
48 if (input.match(/^http/)) {
49 return [input, async () => {}];
50 }
51
52 const app = (0, _express.default)();
53 app.use(_express.default.static(input));
54 const port = await (0, _getPort.default)();
55 return new Promise(resolve => {
56 const server = app.listen(port, () => {
57 const result = `http://localhost:${port}/iframe.html`;
58
59 _nodeLogger.logger.info(`connecting to: ${result}`);
60
61 resolve([result, server.close.bind(server)]);
62 });
63 });
64};
65
66const usePuppeteerBrowser = async () => {
67 const args = ['--no-sandbox ', '--disable-setuid-sandbox'];
68
69 try {
70 return await _puppeteerCore.default.launch({
71 args,
72 executablePath: process.env.SB_CHROMIUM_PATH
73 });
74 } catch (e) {
75 // it's not installed
76 _nodeLogger.logger.info('installing puppeteer...');
77
78 return new Promise((resolve, reject) => {
79 // eslint-disable-next-line global-require
80 require('child_process').exec(`node ${require.resolve(_path.default.join('puppeteer-core', 'install.js'))}`, error => error ? reject(error) : resolve(_puppeteerCore.default.launch({
81 args
82 })));
83 });
84 }
85};
86
87async function extract(input, targetPath) {
88 if (input && targetPath) {
89 const [location, exit] = await useLocation(input);
90 const data = await read(location);
91 await (0, _fsExtra.writeFile)(targetPath, JSON.stringify(data, null, 2));
92 await exit();
93 } else {
94 throw new Error('Extract: please specify a path where your built-storybook is (can be a public url) and a target directory');
95 }
96}
\No newline at end of file