1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.extract = extract;
|
7 |
|
8 | require("core-js/modules/es.promise.js");
|
9 |
|
10 | var _path = _interopRequireDefault(require("path"));
|
11 |
|
12 | var _fsExtra = require("fs-extra");
|
13 |
|
14 | var _puppeteerCore = _interopRequireDefault(require("puppeteer-core"));
|
15 |
|
16 | var _express = _interopRequireDefault(require("express"));
|
17 |
|
18 | var _getPort = _interopRequireDefault(require("get-port"));
|
19 |
|
20 | var _nodeLogger = require("@storybook/node-logger");
|
21 |
|
22 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
23 |
|
24 | const read = async url => {
|
25 | const browser = await usePuppeteerBrowser();
|
26 | const page = await browser.newPage();
|
27 | await page.goto(url);
|
28 |
|
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 |
|
36 | return JSON.stringify(window.__STORYBOOK_STORY_STORE__.getStoriesJsonData(), null, 2);
|
37 | }));
|
38 | setImmediate(() => {
|
39 | browser.close();
|
40 | });
|
41 | return data;
|
42 | };
|
43 |
|
44 | const useLocation = async input => {
|
45 |
|
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 |
|
66 | const 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 |
|
76 | _nodeLogger.logger.info('installing puppeteer...');
|
77 |
|
78 | return new Promise((resolve, reject) => {
|
79 |
|
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 |
|
87 | async 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 |