Best JavaScript code snippet using storybook-root
cli.js
Source:cli.js
1#!/usr/bin/env node2const { existsSync, mkdirSync, writeFileSync } = require('fs');3const globby = require('globby');4const importCwd = require('import-cwd');5const { dirname } = require('path');6const allStoriesPaths = globby.sync(['src/**/*.stories.(j|t)sx']);7const CONF_FILE = '.ginterdevrc.js';8let showcaseDir = '_next-showcase';9if (existsSync(CONF_FILE)) {10 try {11 const ginterdevrc = importCwd(`./${CONF_FILE}`);12 if (ginterdevrc['next-showcase']) {13 showcaseDir = ginterdevrc['next-showcase'].entryDir || showcaseDir;14 }15 } catch {16 // Couldn't read config file17 }18}19function getGroupNameFromPath(storiesPath) {20 if (/\/atoms\//i.test(storiesPath)) {21 return 'Components (Atoms)';22 }23 if (/\/molecules\//i.test(storiesPath)) {24 return 'Components (Molecules)';25 }26 if (/\/organisms\//i.test(storiesPath)) {27 return 'Components (Organisms)';28 }29 if (/\/templates\//i.test(storiesPath)) {30 return 'Components (Templates)';31 }32 if (/\/forms\//i.test(storiesPath)) {33 return 'Forms';34 }35 if (/\/pages\//i.test(storiesPath)) {36 return 'Pages';37 }38 if (/\/views\//i.test(storiesPath)) {39 return 'Views';40 }41 if (/\/components\//i.test(storiesPath)) {42 return 'Components';43 }44 if (/\/icons\//i.test(storiesPath)) {45 return 'Icons';46 }47 if (/\/assets\//i.test(storiesPath)) {48 return 'Assets';49 }50 return 'Other';51}52const modules = allStoriesPaths.map((storiesPath) => {53 // slice the 'src/' part54 // src/components/Button/Button.stories.ts55 // -> components/Button/Button.stories.ts56 const pathNoSource = storiesPath.replace(/^src\//, '');57 const lastChunk = pathNoSource.split('/').pop();58 const modName = lastChunk.replace(/\.stories\.[jt]sx/, '');59 // Try to obtain group based on path, very opinionated.60 const group = getGroupNameFromPath(storiesPath);61 // Replace slashed with double underscore.62 // Example: components/Button -> components__Button.63 const importName = modName.replace(/\//g, '__');64 return {65 // Modules group.66 group,67 // This will be used for calculating name and group displayed in68 // showcase sidebar/nav.69 name: importName,70 // Prepare import path for 'import' (just cut the extension,71 // so TypeScript won't complain).72 source: `${storiesPath.replace(/\.[jt]sx?$/, '')}`,73 };74});75// Switch import source for local testing.76const importPath = process.env.NO_NPM77 ? '../../src'78 : '@ginterdev/next-showcase';79const file = `80// THIS FILE IS AUTOGENERATED.81// IT SHOULD NEITHER BE MODIFIED MANUALLY NOR COMMITTED TO ANY VERSION82// CONTROL SYSTEM.83/* prettier-ignore-start */84/* eslint-disable */85import Showcase from '${importPath}';86export default function ShowcasePage({ SidebarHeader }) {87 const storiesModules = {88 ${modules89 .map(90 (mod) => ` '${mod.source}': {91 name: '${mod.name}',92 group: '${mod.group}',93 source: '${mod.source}',94 loader: () => import('../../${mod.source}'),95 },`,96 )97 .join('\n')}98 };99 return (100 <Showcase101 renderId={${Date.now()}}102 SidebarHeader={SidebarHeader}103 storiesModules={storiesModules}104 />105 );106}107ShowcasePage.IS_SHOWCASE_COMPONENT = true;108/* eslint-enable */109/* prettier-ignore-end */110`;111// written in process.cwd() context112const targetFile = `pages/${showcaseDir}/index.tsx`;113mkdirSync(dirname(targetFile), { recursive: true });...
updateComponent.ts
Source:updateComponent.ts
1#!/usr/bin/env yarn ts-node2const path = require('path');3const fs = require('fs');4const { execSync } = require('child_process');5if (!module.parent) {6 const target = process.argv[3] || 'Montaan';7 const name = process.argv[2];8 updateComponent(target, name);9}10export function updateComponent(target: string, name: string) {11 let componentPath = path.join('src', target, name, name + '.tsx');12 if (!fs.existsSync(componentPath)) {13 componentPath = path.join('src', target, name, name + '.ts');14 }15 if (!fs.existsSync(componentPath)) {16 return;17 }18 console.log(`Parsing ${componentPath}`);19 const source = fs.readFileSync(componentPath).toString();20 const propsRe = new RegExp(`^(export )?interface ${name}Props (.|\n[^\\}])+\n\\}`, 'mg');21 const exportsRe = new RegExp(22 `^export( default)? (((class|function|interface) \\S+)|((const|type|var|let) \\S+ = .*$))`,23 'mg'24 );25 const interfaceRe = new RegExp(`^(export )?interface (.|\n[^\\}])+\n\\}`, 'mg');26 const typeRe = new RegExp(`^(export )?type [a-zA-Z0-9_-] = .+$`, 'mg');27 const declareRe = new RegExp(`^declare (.|\n[^\\}])+\n\\}`, 'mg');28 const exports = source.match(exportsRe);29 const props = source.match(propsRe);30 const interfaces = source.match(interfaceRe);31 const types = source.match(typeRe);32 const declares = source.match(declareRe);33 function fillExport(statement: string) {34 if (/class|function|interface/.test(statement)) {35 return statement + ' {...}';36 } else {37 return statement38 .replace(/\{$/, '{...}')39 .replace(/\($/, '(...)')40 .replace(/\[$/, '[...]');41 }42 }43 const apiHTML = `44 ${45 exports46 ? `<h5>Exports</h5><pre><code>{\`${exports.map(fillExport).join('\n')}\`}</code></pre>`47 : ''48 }49 ${props ? `<h5>Props</h5><pre><code>{\`${props.join('\n')}\`}</code></pre>` : ''}50 ${interfaces ? `<h5>Interfaces</h5><pre><code>{\`${interfaces.join('\n')}\`}</code></pre>` : ''}51 ${types ? `<h5>Types</h5><pre><code>{\`${types.join('\n')}\`}</code></pre>` : ''}52 ${declares ? `<h5>Declares</h5><pre><code>{\`${declares.join('\n')}\`}</code></pre>` : ''}53 `;54 const apiCode = `55${exports ? `### Exports\n\`\`\`tsx\n${exports.join('\n')}\n\`\`\`` : ''}56${props ? `### Props\n\`\`\`tsx\n${props.join('\n')}\n\`\`\`` : ''}57${interfaces ? `### Interfaces\n\`\`\`tsx\n${interfaces.join('\n')}\n\`\`\`` : ''}58${types ? `### Types\n\`\`\`tsx\n${types.join('\n')}\n\`\`\`` : ''}59${declares ? `### Declares\n\`\`\`tsx\n${declares.join('\n')}\n\`\`\`` : ''}60 `;61 const storiesPath = path.join('src', target, name, name + '.stories.tsx');62 if (fs.existsSync(storiesPath)) {63 console.log(`Updating ${storiesPath}`);64 const storiesSource = fs.readFileSync(storiesPath).toString();65 const newStories = storiesSource.replace(66 /<h4>API<\/h4>(.|\n)*^\t\t<\/div>/m,67 '<h4>API</h4>' + apiHTML + '\n\t\t</div>'68 );69 fs.writeFileSync(storiesPath, newStories);70 execSync(`yarn prettier --write "${storiesPath}" && git add "${storiesPath}"`);71 }72 const readmePath = path.join('src', target, name, 'README.md');73 if (fs.existsSync(readmePath)) {74 console.log(`Updating ${readmePath}`);75 const readme = fs.readFileSync(readmePath).toString();76 const newReadme = readme.replace(/^## API\s*$(.|\n)*/m, '## API\n\n' + apiCode);77 fs.writeFileSync(readmePath, newReadme);78 execSync(`yarn prettier --write "${readmePath}" && git add "${readmePath}"`);79 }80 console.log('Component updated');...
main.js
Source:main.js
1function storiesPath(component) {2 return `../../../packages/${component}/stories/*.mdx`;3}4module.exports = {5 core: {6 builder: "webpack5",7 },8 addons: [9 "@storybook/addon-essentials",10 "@storybook/addon-a11y",11 "@storybook/addon-storysource",12 "@apptane/react-ui-storybook-dark-mode",13 ],14 stories: [15 storiesPath("avatar"),16 storiesPath("badge"),17 storiesPath("banner"),18 storiesPath("behaviors"),19 storiesPath("bullet"),20 storiesPath("button"),21 storiesPath("calendar"),22 storiesPath("charts"),23 storiesPath("cue"),24 storiesPath("dialog"),25 storiesPath("divider"),26 storiesPath("form"),27 storiesPath("icon"),28 storiesPath("input"),29 storiesPath("layout"),30 storiesPath("menu"),31 storiesPath("pane"),32 storiesPath("progress"),33 storiesPath("scroller"),34 storiesPath("selector"),35 storiesPath("side-panel"),36 storiesPath("sidebar"),37 storiesPath("skeleton"),38 storiesPath("spinner"),39 storiesPath("tableview"),40 storiesPath("tabs"),41 storiesPath("tag"),42 storiesPath("theme"),43 storiesPath("toaster"),44 storiesPath("toggle-group"),45 storiesPath("tooltip"),46 storiesPath("typography"),47 storiesPath("virtual-list"),48 ],49 webpackFinal: (config) => {50 return {51 ...config,52 module: {53 ...config.module,54 },55 resolve: {56 ...config.resolve,57 exportsFields: ["devExports", "exports"],58 },59 };60 },61};
Using AI Code Generation
1import { storiesPath } from 'storybook-root-config';2storiesOf('Button', module)3 .add('with text', () => (4 <Button onClick={action('clicked')}>Hello Button</Button>5 .add('with some emoji', () => (6 <Button onClick={action('clicked')}>7 ));8storiesOf('Button', module)9 .add('with text', () => (10 <Button onClick={action('clicked')}>Hello Button</Button>11 .add('with some emoji', () => (12 <Button onClick={action('clicked')}>13 ));14storiesOf('Button', module)15 .add('with text', () => (16 <Button onClick={action('clicked')}>Hello Button</Button>17 .add('with some emoji', () => (18 <Button onClick={action('clicked')}>19 ));20storiesOf('Button', module)21 .add('with text', () => (22 <Button onClick={action('clicked')}>Hello Button</Button>23 .add('with some emoji', () => (24 <Button onClick={action('clicked')}>25 ));26storiesOf('Button', module)27 .add('with text', () => (28 <Button onClick={action('clicked')}>Hello Button</Button>29 .add('with some emoji', () => (30 <Button onClick={action('clicked')}>31 ));32storiesOf('Button', module)33 .add('with text', () => (34 <Button onClick={action('clicked')}>Hello Button</Button>35 .add('with some emoji', () =>
Using AI Code Generation
1import { storiesPath } from 'storybook-root-config';2storiesOf('Button', module)3 .add('with text', () => (4 <Button onClick={action('clicked')}>Hello Button</Button>5 .add('with some emoji', () => (6 <Button onClick={action('clicked')}>7 ));8import { configure } from '@storybook/react';9import { storiesPath } from 'storybook-root-config';10const req = require.context(storiesPath, true, /\.stories\.js$/);11function loadStories() {12 req.keys().forEach(filename => req(filename));13}14configure(loadStories, module);
Using AI Code Generation
1const stories = require('storybook-root-config').storiesPath('src');2module.exports = {3};4{5 "scripts": {6 }7}8import React from 'react';9import { storiesOf } from '@storybook/react';10import { withKnobs, text } from '@storybook/addon-knobs';11import Button from './Button';12storiesOf('Button', module)13 .addDecorator(withKnobs)14 .add('default', () => (15 {text('Button Text', 'Click Me!')}16 ));17import React
Using AI Code Generation
1import { storiesPath } from 'storybook-root-decorator';2import { storiesOf } from '@storybook/react';3import { withInfo } from '@storybook/addon-info';4const stories = storiesOf('Button', module);5stories.add(6 withInfo({7 styles: {8 header: {9 h1: {10 },11 body: {12 },13 h2: {14 },15 },16 infoBody: {17 },18 },19 })(() => <Button onClick={action('clicked')}>Hello Button</Button>),20 {21 info: {22 styles: {23 header: {24 h1: {25 },26 body: {27 },28 h2: {29 },30 },31 infoBody: {32 },33 },34 },35 },36);37stories.add(38 withInfo({39 styles: {40 header: {41 h1: {42 },43 body: {44 },45 h2: {46 },47 },
Using AI Code Generation
1import { storiesPath } from 'storybook-root';2console.log(storiesPath);3import { storiesPath } from 'storybook-root';4console.log(storiesPath);5import { storiesPath } from 'storybook-root';6console.log(storiesPath);7import { storiesPath } from 'storybook-root';8console.log(storiesPath);9import { storiesPath } from 'storybook-root';10console.log(storiesPath);11import { storiesPath } from 'storybook-root';12console.log(storiesPath);13import { storiesPath } from 'storybook-root';14console.log(storiesPath);15import { storiesPath } from 'storybook-root';16console.log(storiesPath);17import { storiesPath } from 'storybook-root';18console.log(storiesPath);19import { storiesPath } from 'storybook-root';20console.log(storiesPath);21import { storiesPath } from 'storybook-root';22console.log(storiesPath);23import { storiesPath } from 'storybook-root';24console.log(storiesPath);25import { storiesPath } from 'storybook-root';26console.log(storiesPath);
Using AI Code Generation
1const storiesPath = require('storybook-root').storiesPath;2console.log(storiesPath);3const storiesPath = require('storybook-root').storiesPath;4module.exports = {5};6module.exports = {7 stories: [require('storybook-root').storiesPath],8};9module.exports = {10 stories: [require('storybook-root').storiesPath],11};12module.exports = {13 stories: [require('storybook-root').storiesPath],14};15module.exports = {16 stories: [require('storybook-root').storiesPath],17};18module.exports = {19 stories: [require('storybook-root').storiesPath],20};
Using AI Code Generation
1import { storiesPath } from "storybook-root-config";2storiesOf("Test", module).add("test", () => (3));4const path = require("path");5const storiesPath = path.resolve(__dirname, "../test.js");6module.exports = {7};8const { storiesPath } = require("storybook-root-config");9module.exports = {10};
Using AI Code Generation
1const storiesPath = require('storybook-root').storiesPath;2module.exports = {3 stories: [storiesPath('stories/**/*.stories.js')],4};5import React from 'react';6import Button from '../src/Button';7export default {8};9export const Basic = () => <Button>Button</Button>;10import React from 'react';11import PropTypes from 'prop-types';12const Button = ({ children }) => <button>{children}</button>;13Button.propTypes = {14};15export default Button;16import React from 'react';17import PropTypes from 'prop-types';18const Link = ({ children }) => <a>{children}</a>;19Link.propTypes = {20};21export default Link;22import React from 'react';23import Link from '../src/Link';24export default {25};26export const Basic = () => <Link>Link</Link>;27import './button.stories.js';28import './link.stories.js';29import { configure } from '@storybook/react';30import './index.js';31configure(require.context('../stories', true, /\.stories\.js$/), module);32import { configure } from '@storybook/react';33import { setOptions } from '@storybook/addon-options';34setOptions({35});36configure(require.context('../stories', true, /\.stories\.js$/), module);37import '@storybook/addon-actions/register';38import '@storybook/addon
Using AI Code Generation
1import { storiesPath } from 'storybook-root';2console.log(storiesPath);3const { storiesPath } = require('storybook-root');4console.log(storiesPath);5const { storiesPath } = require('storybook-root');6console.log(storiesPath);7import { storiesPath } from 'storybook-root';8console.log(storiesPath);9const { storiesPath } = require('storybook-root');10console.log(storiesPath);11const { storiesPath } = require('storybook-root');12console.log(storiesPath);13const { storiesPath } = require('storybook-root');14console.log(storiesPath);15const { storiesPath } = require('storybook-root');16console.log(storiesPath);17const { storiesPath } = require('storybook-root');18console.log(storiesPath);19const { storiesPath } = require('storybook-root');20console.log(storiesPath);
Using AI Code Generation
1const storiesPath = require('storybook-root').storiesPath;2module.exports = {3 storiesPath('stories')4}5import { storiesOf } from '@storybook/react';6import { withKnobs } from '@storybook/addon-knobs';7storiesOf('Button', module)8 .addDecorator(withKnobs)9 .add('with text', () => <Button>Hello Button</Button>)10 .add('with some emoji', () => <Button><span role="img" aria-label="so cool">😀 😎 👍 💯</span></Button>);11import { storiesOf } from '@storybook/react';12import { withKnobs } from '@storybook/addon-knobs';13storiesOf('Button', module)14 .addDecorator(withKnobs)15 .add('with text', () => <Button>Hello Button</Button>)16 .add('with some emoji', () => <Button><span role="img" aria-label="so cool">😀 😎 👍 💯</span></Button>);17import { storiesOf } from '@storybook/react';18import { withKnobs } from '@storybook/addon-knobs';19storiesOf('Button', module)20 .addDecorator(withKnobs)21 .add('with text', () => <Button>Hello Button</Button>)22 .add('with some emoji', () => <Button><span role="img" aria-label="so cool">😀 😎 👍 💯</span></Button>);23import { storiesOf } from '@storybook/react';24import { withKnobs } from '@storybook/addon-knobs';25storiesOf('Button', module)26 .addDecorator(withKnobs)27 .add('with text', () => <Button>Hello Button</Button>)28 .add('with some emoji', () => <Button><span role="img" aria-label="so cool">😀 😎 👍 💯</span></Button>);29import { storiesOf } from '@storybook/react';30import { withKnobs } from '@storybook/addon-knobs';31storiesOf('Button', module
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!