UNPKG

wix-storybook-utils

Version:

Utilities for automated component documentation within Storybook

98 lines 4.79 kB
"use strict"; var types = require('@babel/types'); var pathLib = require('path'); var visit = require('react-autodocs-utils/src/parser/visit'); var parse = require('react-autodocs-utils/src/parser/parse'); var print = require('react-autodocs-utils/src/parser/print'); var get = require('react-autodocs-utils/src/get'); var prepareStory = function (storyConfig, sourcePath) { return function (source) { return new Promise(function (resolve, reject) { var isError = !source && !storyConfig && !sourcePath; return isError ? reject('ERROR: unable to prepare story, both `storyConfig` and `source` must be provided') : resolve(source); }) .then(parse) .then(function (ast) { var isES5 = true; visit(ast)({ ExportDefaultDeclaration: function () { isES5 = false; return false; }, }); if (isES5) { // add requires ast.program.body.unshift(parse('const { storiesOf } = require("@storybook/react")')); ast.program.body.unshift(parse('const story = require("wix-storybook-utils/Story").default')); } else { // add imports ast.program.body.unshift(parse('import { storiesOf } from "@storybook/react"')); ast.program.body.unshift(parse('import story from "wix-storybook-utils/Story"')); } return ast; }) .then(function (ast) { // TODO: this is not too good, unfortunatelly, i cant return // rejected promise from within visitor, babylon complains var error = null; var configAST = parse("(".concat(JSON.stringify(storyConfig), ")")); var configProperties; visit(configAST)({ ObjectExpression: function (path) { var storiesOfProperty = types.objectProperty(types.identifier('storiesOf'), types.identifier('storiesOf')); if (storyConfig.playgroundComponentsPath) { var playgroundComponentsPath = pathLib.relative(sourcePath, storyConfig.playgroundComponentsPath); var requireExpression = parse("require('".concat(playgroundComponentsPath, "').default")).program.body[0].expression; var playgroundComponentsProperty = types.objectProperty(types.identifier('playgroundComponents'), requireExpression); path.node.properties.push(playgroundComponentsProperty); } path.node.properties.push(storiesOfProperty); configProperties = path.node.properties; path.stop(); }, }); var handleExportObject = function (path, node) { var exportsObject = types.isObjectExpression(node); var exportsIdentifier = types.isIdentifier(node); if (exportsIdentifier) { var referenceName = node.name; var configObject = path.scope.bindings[referenceName].path.node.init; if (!configObject.properties) { error = "ERROR: storybook config must export an object, exporting ".concat(configObject.type, " instead"); return false; } configObject.properties.push(types.objectProperty(types.identifier('_config'), types.objectExpression(configProperties))); return types.callExpression(types.identifier('story'), [node]); } if (exportsObject) { node.properties.push(types.objectProperty(types.identifier('_config'), types.objectExpression(configProperties))); // wrap exported object with `story()` return types.callExpression(types.identifier('story'), [node]); } }; visit(ast)({ ExportDefaultDeclaration: function (path) { ast.program.body.push(handleExportObject(path, path.node.declaration)); path.remove(); return false; }, ExpressionStatement: function (path) { var isModuleExports = [ types.isMemberExpression(path.node.expression.left), get(path)('node.expression.left.object.name') === 'module', get(path)('node.expression.left.property.name') === 'exports', ].every(Boolean); if (isModuleExports) { ast.program.body.push(handleExportObject(path, path.node.expression.right)); path.remove(); } }, }); return error ? Promise.reject(error) : ast; }) .then(print); }; }; module.exports = prepareStory; //# sourceMappingURL=prepareStory.js.map