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
Check out the latest blogs from LambdaTest on this topic:
Hey everyone! We hope you had a great Hacktober. At LambdaTest, we thrive to bring you the best with each update. Our engineering and tech teams work at lightning speed to deliver you a seamless testing experience.
In todayβs world, an organizationβs most valuable resource is its customers. However, acquiring new customers in an increasingly competitive marketplace can be challenging while maintaining a strong bond with existing clients. Implementing a customer relationship management (CRM) system will allow your organization to keep track of important customer information. This will enable you to market your services and products to these customers better.
When software developers took years to create and introduce new products to the market is long gone. Users (or consumers) today are more eager to use their favorite applications with the latest bells and whistles. However, users today donβt have the patience to work around bugs, errors, and design flaws. People have less self-control, and if your product or application doesnβt make life easier for users, theyβll leave for a better solution.
Estimates are critical if you want to be successful with projects. If you begin with a bad estimating approach, the project will almost certainly fail. To produce a much more promising estimate, direct each estimation-process issue toward a repeatable standard process. A smart approach reduces the degree of uncertainty. When dealing with presales phases, having the most precise estimation findings can assist you to deal with the project plan. This also helps the process to function more successfully, especially when faced with tight schedules and the danger of deviation.
When I started writing tests with Cypress, I was always going to use the user interface to interact and change the applicationβs state when running tests.
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!!