1 | "use strict";
|
2 |
|
3 | require("core-js/modules/es.promise.js");
|
4 |
|
5 | Object.defineProperty(exports, "__esModule", {
|
6 | value: true
|
7 | });
|
8 | exports.extract = extract;
|
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 | await page.waitForFunction('window.__STORYBOOK_STORY_STORE__ && window.__STORYBOOK_STORY_STORE__.extract && window.__STORYBOOK_STORY_STORE__.extract()');
|
29 | const data = JSON.parse(await page.evaluate(async () => {
|
30 |
|
31 | return JSON.stringify(window.__STORYBOOK_STORY_STORE__.getStoriesJsonData(), null, 2);
|
32 | }));
|
33 | setImmediate(() => {
|
34 | browser.close();
|
35 | });
|
36 | return data;
|
37 | };
|
38 |
|
39 | const useLocation = async input => {
|
40 | if (input.match(/^http/)) {
|
41 | return [input, async () => {}];
|
42 | }
|
43 |
|
44 | const app = (0, _express.default)();
|
45 | app.use(_express.default.static(input));
|
46 | const port = await (0, _getPort.default)();
|
47 | return new Promise(resolve => {
|
48 | const server = app.listen(port, () => {
|
49 | const result = `http://localhost:${port}/iframe.html`;
|
50 |
|
51 | _nodeLogger.logger.info(`connecting to: ${result}`);
|
52 |
|
53 | resolve([result, server.close.bind(server)]);
|
54 | });
|
55 | });
|
56 | };
|
57 |
|
58 | const usePuppeteerBrowser = async () => {
|
59 | const args = ['--no-sandbox ', '--disable-setuid-sandbox'];
|
60 |
|
61 | try {
|
62 | return await _puppeteerCore.default.launch({
|
63 | args
|
64 | });
|
65 | } catch (e) {
|
66 |
|
67 | _nodeLogger.logger.info('installing puppeteer...');
|
68 |
|
69 | return new Promise((resolve, reject) => {
|
70 |
|
71 | require('child_process').exec(`node ${require.resolve(_path.default.join('puppeteer-core', 'install.js'))}`, error => error ? reject(error) : resolve(_puppeteerCore.default.launch({
|
72 | args
|
73 | })));
|
74 | });
|
75 | }
|
76 | };
|
77 |
|
78 | async function extract(input, targetPath) {
|
79 | if (input && targetPath) {
|
80 | const [location, exit] = await useLocation(input);
|
81 | const data = await read(location);
|
82 | await (0, _fsExtra.writeFile)(targetPath, JSON.stringify(data, null, 2));
|
83 | await exit();
|
84 | } else {
|
85 | throw new Error('Extract: please specify a path where your built-storybook is (can be a public url) and a target directory');
|
86 | }
|
87 | } |
\ | No newline at end of file |