refactor(cli): migrate extract file to TS

This commit is contained in:
Gaëtan Maisse 2020-05-14 21:28:12 +02:00
parent e5c39984a6
commit ce1a49542c
No known key found for this signature in database
GPG Key ID: D934C0EF3714A8A8
3 changed files with 22 additions and 7 deletions

View File

@ -87,6 +87,7 @@
"@storybook/vue": "6.0.0-beta.7",
"@storybook/web-components": "6.0.0-beta.7",
"@types/cross-spawn": "^6.0.1",
"@types/puppeteer-core": "^2.0.0",
"@types/semver": "^7.2.0"
},
"peerDependencies": {

View File

@ -5,7 +5,7 @@ import express from 'express';
import getPort from 'get-port';
import { logger } from '@storybook/node-logger';
const read = async (url) => {
const read = async (url: string) => {
const browser = await usePuppeteerBrowser();
const page = await browser.newPage();
@ -17,10 +17,10 @@ const read = async (url) => {
const data = JSON.parse(
await page.evaluate(async () => {
// eslint-disable-next-line no-undef
const d = window.__STORYBOOK_STORY_STORE__.extract();
const d = (window as any).__STORYBOOK_STORY_STORE__.extract();
const result = Object.entries(d).reduce(
(acc, [k, v]) => ({
(acc, [k, v]: [string, any]) => ({
...acc,
[k]: {
...v,
@ -48,7 +48,7 @@ const read = async (url) => {
return data;
};
const useLocation = async (input) => {
const useLocation: (input: string) => Promise<[string, () => void]> = async (input: string) => {
if (input.match(/^http/)) {
return [input, async () => {}];
}
@ -74,7 +74,7 @@ const useLocation = async (input) => {
});
};
const usePuppeteerBrowser = async () => {
const usePuppeteerBrowser: () => Promise<puppeteerCore.Browser> = async () => {
const args = ['--no-sandbox ', '--disable-setuid-sandbox'];
try {
return await puppeteerCore.launch({ args });
@ -85,13 +85,13 @@ const usePuppeteerBrowser = async () => {
// eslint-disable-next-line global-require
require('child_process').exec(
`node ${require.resolve(path.join('puppeteer-core', 'install.js'))}`,
(error) => (error ? reject(error) : resolve(puppeteerCore.launch({ args })))
(error: any) => (error ? reject(error) : resolve(puppeteerCore.launch({ args })))
);
});
}
};
export async function extract(input, targetPath) {
export async function extract(input: string, targetPath: string) {
if (input && targetPath) {
const [location, exit] = await useLocation(input);

View File

@ -4779,6 +4779,20 @@
resolved "https://registry.yarnpkg.com/@types/prop-types/-/prop-types-15.7.3.tgz#2ab0d5da2e5815f94b0b9d4b95d1e5f243ab2ca7"
integrity sha512-KfRL3PuHmqQLOG+2tGpRO26Ctg+Cq1E01D2DMriKEATHgWLfeNDmq9e29Q9WIky0dQ3NPkd1mzYH8Lm936Z9qw==
"@types/puppeteer-core@^2.0.0":
version "2.0.0"
resolved "https://registry.yarnpkg.com/@types/puppeteer-core/-/puppeteer-core-2.0.0.tgz#3b7fbbac53d56b566f5ef096116e1d60d504aa45"
integrity sha512-JvoEb7KgEkUet009ZDrtpUER3hheXoHgQByuYpJZ5WWT7LWwMH+0NTqGQXGgoOKzs+G5NA1T4DZwXK79Bhnejw==
dependencies:
"@types/puppeteer" "*"
"@types/puppeteer@*":
version "2.1.0"
resolved "https://registry.yarnpkg.com/@types/puppeteer/-/puppeteer-2.1.0.tgz#31367580654632f87f86df565f1bde0533577401"
integrity sha512-QIRQXl0VaSgnwOZ1LwxD321Tfb1jLOzCWuF2BrwjEkWq2IhxSicPOddUywLV7dRSO6mcU4sWKRdoGdci6gk0Aw==
dependencies:
"@types/node" "*"
"@types/puppeteer@^2.0.0":
version "2.1.0"
resolved "https://registry.yarnpkg.com/@types/puppeteer/-/puppeteer-2.1.0.tgz#31367580654632f87f86df565f1bde0533577401"