Best JavaScript code snippet using storybook-root
index.js
Source:index.js
1import fs, { readdirSync, lstatSync } from 'fs'2import ejs from 'ejs'3import path from 'path'4import execa from 'execa'5import ReadRC from '../read-rc'6import copydir from '../../helpers/copy-dir'7// where config-* is8const configRoot = path.join(__dirname, '..', '..', '..', 'caches')9const oriConfigPath = path.join(__dirname, 'storybook')10export default class ConfigConsumer {11 constructor(props = {}) {12 this.contextRoot = props.contextRoot13 // sub folder bug14 this.packageName = props.name.replace('/', '-')15 this.storybookConfigPath = `${configRoot}/config-${this.packageName}`16 // copy configs17 this.cloneConfigs()18 }19 getConfigPath() {20 return this.storybookConfigPath21 }22 cloneConfigs() {23 // create caches/ if none24 if (!fs.existsSync(configRoot)) {25 execa.sync('mkdir', [ configRoot ])26 }27 // create config dir28 if (fs.existsSync(this.storybookConfigPath)) {29 execa.sync('rm', [ '-rf', this.storybookConfigPath ])30 }31 execa.sync('mkdir', [ this.storybookConfigPath ])32 // copy config33 copydir(oriConfigPath, this.storybookConfigPath)34 // override storybook config35 this.override(36 `${this.contextRoot}/.storybook`,37 this.storybookConfigPath,38 [39 'manager-head.html',40 'preview-head.html',41 'addons.js',42 'config.js',43 {44 'ori': 'webpack.config.js',45 'dest': 'webpack.extend.config.js'46 }47 ]48 )49 // override default dev config50 this.override(51 this.contextRoot,52 this.storybookConfigPath,53 [54 'tsconfig.json',55 {56 'ori': '.babelrc',57 'dest': 'babelrc.json'58 }59 ]60 )61 }62 generateHttpHAREntry(httpHARPath) {63 if (!httpHARPath) {64 return false65 }66 if (httpHARPath) {67 httpHARPath = path.join(this.contextRoot, httpHARPath)68 } else {69 httpHARPath = this.contextRoot70 }71 if (!fs.existsSync(httpHARPath)) {72 return false73 }74 let entries = []75 fs.readdirSync(httpHARPath)76 .filter(filename => !filename.match(/^\./))77 .forEach(filename => {78 const harHttpJson = require(path.join(httpHARPath, filename))79 entries.push(harHttpJson)80 })81 fs.writeFileSync(82 `${this.storybookConfigPath}/http-mock/https.json`,83 JSON.stringify({84 'log': {85 'version': '0.0.1',86 'creator': {87 'name': '@beisen/http-mocker',88 'version': '0.0.1'89 },90 'pages': {},91 'entries': entries92 }93 }, null, 2),94 'utf8'95 )96 }97 generateStoriesJs(cmpPaths) {98 return ejs.renderFile(99 path.join(__dirname, 'stories.ejs'),100 {101 'storyMetas': this.makeStoriesJson(cmpPaths)102 },103 {},104 (err, storiesjs) => {105 if (err) throw err106 fs.writeFile(path.join(this.storybookConfigPath, 'stories.js'), storiesjs, err => {107 if (err) return console.log(err)108 })109 }110 )111 }112 makeStoriesJson(cmpPaths) {113 return cmpPaths.map(contextRoot => {114 const examples = getExamples(contextRoot)115 const rc = new ReadRC({ contextRoot })116 let readme = ''117 if (fs.existsSync(`${contextRoot}/README.md`)) {118 readme = `require('${contextRoot}/README.md')`119 }120 let stories = []121 if (!examples.length) {122 stories.push({123 'name': 'default',124 'story': {125 'component': `require('${contextRoot}/src')`126 }127 })128 } else {129 stories = examples.map(exp => ({130 'name': exp.name,131 'story': {132 'component': `require('${contextRoot}/examples/${exp.name}')`133 }134 }))135 }136 return {137 'name': rc.get('name'),138 'stories': stories,139 'readme': readme140 }141 })142 }143 override (configPath, destinationPath, configs) {144 let retFilesPath = []145 configs.forEach(config => {146 const { ori = config, dest } = config147 const configFilePath = `${configPath}/${ori}`148 if (fs.existsSync(configFilePath)) {149 fs.writeFileSync(150 path.join(destinationPath, dest || ori),151 fs.readFileSync(configFilePath, 'utf8'),152 'utf8'153 )154 retFilesPath.push(configFilePath)155 }156 })157 return retFilesPath158 }159 clean() {160 execa.sync('rm', [ '-rf', this.storybookConfigPath ])161 }162}163export const getExamples = (contextRoot) => {164 const epath = path.join(contextRoot, 'examples')165 if (!fs.existsSync(epath)) return []166 return readdirSync(epath)167 // filter directory name168 .filter(ffname => {169 let fullFFPath = path.join(epath, ffname)170 return lstatSync(fullFFPath).isDirectory()171 })172 // filter config-link folder, e.g. .git/173 .map(name => ({ name }))174 .filter(file => !file.name.match(/^\./))...
load-custom-config-file.js
Source:load-custom-config-file.js
1import path from 'path';2import { getInterpretedFile } from './interpret-files';3function loadConfigFiles({ configDir }) {4 const storybookConfigPath = getInterpretedFile(path.resolve(configDir, 'config'));5 if (storybookConfigPath) {6 return [storybookConfigPath];7 }8 return [];9}...
Using AI Code Generation
1const storybookConfigPath = require('storybook-root').storybookConfigPath;2const storybookConfigPath = require('storybook-root').storybookConfigPath;3const storybookConfigPath = require('storybook-root').storybookConfigPath;4const storybookConfigPath = require('storybook-root').storybookConfigPath;5const storybookConfigPath = require('storybook-root').storybookConfigPath;6const storybookConfigPath = require('storybook-root').storybookConfigPath;7const storybookConfigPath = require('storybook-root').storybookConfigPath;8const storybookConfigPath = require('storybook-root').storybookConfigPath;9const storybookConfigPath = require('storybook-root').storybookConfigPath;10const storybookConfigPath = require('storybook-root').storybookConfigPath;11const storybookConfigPath = require('storybook-root').storybookConfigPath;12const storybookConfigPath = require('storybook-root').storybookConfigPath;13const storybookConfigPath = require('storybook-root').storybookConfigPath;
Using AI Code Generation
1const storybookConfigPath = require('storybook-root-config').storybookConfigPath;2module.exports = {3 stories: [storybookConfigPath('stories')],4};5import { configure } from '@storybook/react';6const req = require.context('../src/components', true, /\.stories\.js$/);7function loadStories() {8 req.keys().forEach(filename => req(filename));9}10configure(loadStories, module);11import React from 'react';12import { storiesOf } from '@storybook/react';13import { withKnobs } from '@storybook/addon-knobs';14import { MyComponent } from '../src/components';15storiesOf('MyComponent', module)16 .addDecorator(withKnobs)17 .add('Default', () => <MyComponent />);18import React from 'react';19import { storiesOf } from '@storybook/react';20import { withKnobs } from '@storybook/addon-knobs';21import { MyComponent } from '../src/components';22storiesOf('MyComponent', module)23 .addDecorator(withKnobs)24 .add('Default', () => <MyComponent />);25import React from 'react';26import PropTypes from 'prop-types';27const MyComponent = () => {28 return <div>MyComponent</div>;29};30MyComponent.propTypes = {};31export default MyComponent;32import React from 'react';33import { storiesOf } from '@storybook/react';34import { withKnobs } from '@storybook/addon-knobs';35import { MyComponent } from '../src/components';36storiesOf('MyComponent', module)37 .addDecorator(withKnobs)38 .add('Default', () => <MyComponent />);39import React from 'react';40import { storiesOf } from '@storybook/react';41import { withKnobs
Using AI Code Generation
1const storybookConfigPath = require('storybook-root-config/storybookConfigPath');2const storybookRootConfigPath = require('storybook-root-config/storybookRootConfigPath');3const storybookRootConfig = require('storybook-root-config/storybookRootConfig');4module.exports = storybookConfigPath(__dirname, storybookRootConfigPath(__dirname), storybookRootConfig(__dirname));5"scripts": {6}
Using AI Code Generation
1const storybookRoot = require('storybook-root');2const path = require('path');3const storybookConfigPath = storybookRoot.storybookConfigPath();4const storybookRootPath = storybookRoot.storybookRootPath();5console.log('Storybook config path is: ' + storybookConfigPath);6console.log('Storybook root path is: ' + storybookRootPath);7{8 "scripts": {9 },10 "dependencies": {11 }12}
Using AI Code Generation
1const storybookConfigPath = require('storybook-root').storybookConfigPath;2const storybookConfig = storybookConfigPath();3module.exports = {4 stories: [`${storybookConfig}/../stories/*.stories.js`],5};6module.exports = {7};8import { configure } from '@storybook/react';9import { setOptions } from '@storybook/addon-options';10import { setDefaults } from '@storybook/addon-info';11import { addDecorator } from '@storybook/react';12import { withInfo } from '@storybook/addon-info';13import { withKnobs } from '@storybook/addon-knobs';14setDefaults({15 styles: {16 header: {17 h1: {18 },19 body: {20 },21 h2: {22 },23 },24 infoBody: {25 },26 },27});28addDecorator(withInfo);29setOptions({
Using AI Code Generation
1const storybookConfigPath = require('storybook-root').storybookConfigPath;2const storybookConfig = storybookConfigPath();3module.exports = storybookConfig;4{5 "scripts": {6 }7}8const storybookConfigPath = require('storybook-root').storybookConfigPath;9const storybookConfig = storybookConfigPath();10module.exports = storybookConfig;11const storybookConfigPath = require('storybook-root').storybookConfigPath;12const storybookConfig = storybookConfigPath();13module.exports = storybookConfig;14const storybookConfigPath = require('storybook-root').storybookConfigPath;15const storybookConfig = storybookConfigPath();16module.exports = storybookConfig;17const storybookConfigPath = require('storybook-root').storybookConfigPath;18const storybookConfig = storybookConfigPath();19module.exports = storybookConfig;20const storybookConfigPath = require('storybook-root').storybookConfigPath;21const storybookConfig = storybookConfigPath();22module.exports = storybookConfig;23const storybookConfigPath = require('storybook-root').storybookConfigPath;24const storybookConfig = storybookConfigPath();25module.exports = storybookConfig;
Using AI Code Generation
1const storybookConfigPath = require('storybook-root').storybookConfigPath;2const storybookConfig = storybookConfigPath();3module.exports = storybookConfig;4const storybookConfig = require('storybook-root').storybookConfig;5module.exports = storybookConfig;6const path = require('path');7module.exports = {8 webpackFinal: async (config) => {9 config.module.rules.push({10 include: path.resolve(__dirname, '../'),11 });12 return config;13 },14};15const path = require('path');16module.exports = async ({ config, mode }) => {17 config.module.rules.push({18 include: path.resolve(__dirname, '../'),19 });20 return config;21};22module.exports = {23};24const path = require('path');25module.exports = {26 webpackFinal: async (config) => {27 config.module.rules.push({28 include: path.resolve(__dirname, '../'),29 });30 return config;31 },32};
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!!