Best JavaScript code snippet using storybook-root
dependency-worker.ts
Source:dependency-worker.ts
1import * as fs from 'fs';2import { Dictionary } from '@eigenspace/common-types';3import * as childProcess from 'child_process';4export class DependencyWorker {5 private static LATEST_DEPENDENCY = 'latest';6 private static DEPENDENCY_FLAGS = new Map([7 ['dependencies', ''],8 ['devDependencies', '-D'],9 ['optionalDependencies', '-O'],10 ['peerDependencies', '-P']11 ]);12 constructor(private readonly exec = childProcess.execSync) {13 }14 /**15 * Script automates reinstall of packages.16 * Packages with snapshot postfix do not change version.17 * Hence we need to reinstall them manually.18 * This script delete them then install.19 *20 * @param {string[]} [packages=(all packages with snapshot postfix)]21 * Stream of package names separated by space character you want to reinstall.22 */23 update(packages: string[]): void {24 const isFullUpdate = !Boolean(packages.length);25 const dependencyFlags = new Map([26 ['dependencies', ''],27 ['devDependencies', '-D'],28 ['optionalDependencies', '-O'],29 ['peerDependencies', '-P']30 ]);31 const dependencyTypes = Array.from(dependencyFlags.keys());32 const currentDir = process.cwd();33 const packageJson = require(`${currentDir}/package.json`);34 dependencyTypes.forEach(dependencyType => {35 const dependenciesMap = packageJson[dependencyType];36 if (!dependenciesMap) {37 return;38 }39 const dependenciesToUpdate = this.findDependenciesToUpdate(dependenciesMap, packages, isFullUpdate);40 if (!dependenciesToUpdate.length) {41 return;42 }43 const latestDependencies = this.findLatestDependencies(dependenciesMap);44 console.log('dependencies to update', dependenciesToUpdate);45 const dependenciesWithVersion = this.getDependenciesWithVersion(dependenciesMap, dependenciesToUpdate);46 this.updateDependencies(dependenciesToUpdate, dependenciesWithVersion, dependencyType);47 this.restoreLatestDependencies(dependencyType, latestDependencies);48 });49 }50 private findLatestDependencies(dependencyStore: Dictionary<string>): string[] {51 return Object.keys(dependencyStore)52 .filter(this.isLatestDependency);53 }54 private findDependenciesToUpdate(55 dependencyStore: Dictionary<string>,56 packages: string[],57 isFullUpdate: boolean58 ): string[] {59 if (!isFullUpdate) {60 return packages.filter(dependency => dependencyStore[dependency]);61 }62 const snapshotPattern = /-/;63 return Object.keys(dependencyStore)64 .filter(key => {65 const isSnapshot = snapshotPattern.test(dependencyStore[key]);66 const isUrl = dependencyStore[key].startsWith('http');67 const isLatest = this.isLatestDependency(dependencyStore[key]);68 return isSnapshot || isUrl || isLatest;69 });70 }71 private getDependenciesWithVersion(dependencyStore: Dictionary<string>, dependenciesToUpdate: string[]): string[] {72 return dependenciesToUpdate.map(packageToRemove => `${packageToRemove}@${dependencyStore[packageToRemove]}`);73 }74 private updateDependencies(75 dependenciesToUpdate: string[],76 dependenciesWithVersion: string[],77 dependencyType: string): void {78 const dependencies = dependenciesToUpdate.join(' ');79 this.run(`yarn remove ${dependencies}`);80 const depsWithVersion = dependenciesWithVersion.join(' ');81 this.run(`yarn add ${depsWithVersion} ${DependencyWorker.DEPENDENCY_FLAGS.get(dependencyType)}`);82 }83 private restoreLatestDependencies(dependencyType: string, latestDependencies: string[]): void {84 if (!latestDependencies.length) {85 return;86 }87 const parsedPackageJson = JSON.parse(fs.readFileSync('package.json', 'utf8'));88 const latestDependenciesMap: Dictionary<string> = {};89 latestDependencies.forEach(dependency => {90 latestDependenciesMap[dependency] = DependencyWorker.LATEST_DEPENDENCY;91 });92 parsedPackageJson[dependencyType] = { ...parsedPackageJson[dependencyType], ...latestDependenciesMap };93 const indent = 4;94 const packageJsonStringified = JSON.stringify(parsedPackageJson, undefined, indent);95 fs.writeFileSync('package.json', packageJsonStringified);96 }97 // noinspection JSMethodCanBeStatic98 private isLatestDependency(version: string): boolean {99 return version === DependencyWorker.LATEST_DEPENDENCY;100 }101 private run(command: string): void {102 console.log('run command:', command);103 const stdout = this.exec(command, { encoding: 'utf8' });104 console.log(stdout);105 }...
analyse-module.js
Source:analyse-module.js
1/**2 * @file åæAMD模åçAST è·åç¸å
³ç模åä¿¡æ¯3 * @author errorrik[errorrik@gmail.com]4 * treelite[c.xinle@gmail.com]5 */6var estraverse = require('estraverse');7var SYNTAX = estraverse.Syntax;8var LITERAL_DEFINE = 'define';9var LITERAL_REQUIRE = 'require';10/**11 * å¤æç»ç¹æ¯å¦å符串ç´æ¥é12 *13 * @inner14 * @param {Object} node è¯æ³æ ç»ç¹15 * @return {boolean}16 */17function isStringLiteral(node) {18 return node19 && node.type === SYNTAX.Literal20 && typeof node.value === 'string';21}22/**23 * åædefineè°ç¨24 * è·å模åä¿¡æ¯25 *26 * @inner27 * @param {Object} expr define ast28 * @return {Object} 模åä¿¡æ¯29 */30function analyseDefineExpr(expr) {31 var moduleId;32 var dependencies;33 var factoryAst;34 var dependenciesMap = {};35 var actualDependencies = [];36 var args = expr['arguments'];37 var argument;38 function dependenciesWalker(item) {39 if (!isStringLiteral(item)) {40 return;41 }42 dependencies.push(item.value);43 if (!dependenciesMap[item.value]) {44 actualDependencies.push(item.value);45 dependenciesMap[item.value] = 1;46 }47 }48 // 解æåæ°49 for (var i = 0; i < args.length; i++) {50 argument = args[i];51 if (!moduleId && isStringLiteral(argument)) {52 // è·åmodule id53 moduleId = argument.value;54 }55 else if (!dependencies && argument.type === SYNTAX.ArrayExpression) {56 // è·åä¾èµ57 dependencies = [];58 argument.elements.forEach(dependenciesWalker);59 }60 else {61 factoryAst = argument;62 break;63 }64 }65 // 计ç®factory functionçå½¢å个æ°66 var factoryParamCount = 0;67 if (factoryAst && factoryAst.type === SYNTAX.FunctionExpression) {68 factoryParamCount = factoryAst.params.length;69 }70 if (!dependencies) {71 actualDependencies = ['require', 'exports', 'module']72 .slice(0, factoryParamCount);73 }74 // 解æ模åå®ä¹å½æ°75 if (factoryAst.type === SYNTAX.FunctionExpression) {76 // è·åå
é¨`require`çä¾èµ77 estraverse.traverse(factoryAst, {78 enter: function (item) {79 // require('xxx')80 // new require('xxx')81 if (item.type !== SYNTAX.CallExpression82 && item.type !== SYNTAX.NewExpression83 ) {84 return;85 }86 if (item.callee.name === LITERAL_REQUIRE87 && (argument = item['arguments'][0])88 && isStringLiteral(argument)89 && !dependenciesMap[argument.value]90 ) {91 actualDependencies.push(argument.value);92 dependenciesMap[argument.value] = 1;93 }94 }95 });96 }97 return {98 id: moduleId,99 dependencies: dependencies,100 actualDependencies: actualDependencies,101 factoryAst: factoryAst102 };103}104/**105 * åæ模å106 *107 * @param {Object} ast 模å代ç çast108 * @return {Object|Array} 模åä¿¡æ¯ï¼æ模åä¿¡æ¯æ°ç»ã109 * æ¯ä¸ªæ¨¡åä¿¡æ¯å
å«id, dependencies, factoryAst, actualDependencies110 */111module.exports = exports = function (ast) {112 var defineExprs = [];113 estraverse.traverse(ast, {114 enter: function (node) {115 if (node.type === SYNTAX.CallExpression116 && node.callee.name === LITERAL_DEFINE117 ) {118 defineExprs.push(node);119 this.skip();120 }121 }122 });123 var modules = [];124 defineExprs.forEach(function (expr) {125 modules.push(analyseDefineExpr(expr));126 });127 switch (modules.length) {128 case 0:129 return null;130 case 1:131 return modules[0];132 }133 return modules;...
collapse.ts
Source:collapse.ts
1import DependenciesMap from "./DependenciesMap";2import * as util from "./util";3export default function collapse(map: DependenciesMap, levels: number = 1): DependenciesMap {4 const multiMap = upAll(map, levels);5 return toDependenciesMap(multiMap);6}7interface DependenciesMultiMap {8 [key: string]: string[][];9}10function upAll(map: DependenciesMap, levels = 1): DependenciesMultiMap {11 const result: DependenciesMultiMap = {};12 for (const module of Object.keys(map)) {13 const moduleUpped = up(module, levels);14 const dependenciesUpped = map[module].map(it => up(it, levels));15 if (!result[moduleUpped]) {16 result[moduleUpped] = [];17 }18 result[moduleUpped].push(dependenciesUpped);19 }20 return result;21}22function up(path: string, levels: number = 1): string {23 const parts = path.split("/");24 return parts.slice(0, parts.length - levels).join("/");25}26function toDependenciesMap(multiMap: DependenciesMultiMap): DependenciesMap {27 const result: DependenciesMap = {};28 for (const folder of Object.keys(multiMap)) {29 const dependencies = util.flatten(multiMap[folder]);30 result[folder] = util.distinct(dependencies).filter(it => it !== folder);31 }32 return result;...
Using AI Code Generation
1const { dependenciesMap } = require('@storybook/root-config');2const { resolve } = require('path');3const { dependenciesMap } = require('@storybook/root-config');4const { resolve } = require('path');5module.exports = {6 '../src/**/*.stories.@(js|jsx|ts|tsx)',7 webpackFinal: async (config) => {8 const { dependenciesMap } = require('@storybook/root-config');9 const { resolve } = require('path');10 const { dependencies } = require('../package.json');11 const { dependencies: devDependencies } = require('../package.json');12 const { dependencies: peerDependencies } = require('../package.json');13 const rootConfig = dependenciesMap({14 });15 config.resolve.alias = {16 };17 return config;18 },19};20module.exports = {21 '../src/**/*.stories.@(js|jsx|ts|tsx)',22 webpackFinal: async (config) => {23 const { dependenciesMap } = require('@storybook/root-config');24 const { resolve } = require('path');25 const { dependencies } = require('../package.json');26 const { dependencies: devDependencies } = require('../package.json');27 const { dependencies: peerDependencies } = require('../package.json');28 const rootConfig = dependenciesMap({29 });30 config.resolve.alias = {31 };32 return config;33 },34};35{36 "scripts": {
Using AI Code Generation
1import { dependenciesMap } from 'storybook-root';2import { storiesOf } from '@storybook/react';3const stories = storiesOf('Test', module);4stories.add('Test', () => <div>Test</div>, {5 dependencies: dependenciesMap({6 }),7});8import { dependenciesMap } from 'storybook-root';9export default dependenciesMap({10});11const { dependenciesMap } = require('storybook-root');12module.exports = {13 module: {14 {15 {16 },17 {18 options: {19 importLoaders: 1,20 },21 },22 {23 options: {24 plugins: () => [require('autoprefixer')],25 },26 },27 },28 },29 plugins: [dependenciesMap()],30};31module.exports = {32 webpackFinal: require('storybook-root').dependenciesMap(),33};34const { dependenciesMap } = require('storybook-root');35module.exports = {36 ...dependenciesMap(),37};38const { dependenciesMap } = require('storybook-root');39module.exports = {40 ...dependenciesMap(),41};42const { dependenciesMap } = require('storybook-root');
Using AI Code Generation
1import { dependenciesMap } from "storybook-root";2const dependencies = dependenciesMap();3import { dependenciesMap } from "storybook-root";4const dependencies = dependenciesMap();5import { ComponentB } from "storybook-root";6import { ComponentC } from "storybook-root";7import { ComponentD } from "storybook-root";8import { ComponentC } from "storybook-root";9import { ComponentD } from "storybook-root";10import { ComponentA } from "storybook-root";11import { ComponentA } from "storybook-root";12import { ComponentA } from "storybook-root";13import { ComponentA } from "storybook-root";14import { ComponentA } from "storybook-root";15import { ComponentA } from "storybook-root";16import { ComponentA } from "storybook-root";17import { ComponentA } from "storybook-root";18import { ComponentA } from "storybook-root";19import { ComponentA } from "storybook-root";20import { ComponentA } from "storybook-root";21import { ComponentA } from "storybook-root";22import { ComponentA } from "storybook-root";23import { ComponentA } from "storybook-root";24import { ComponentA } from "storybook-root";25import { ComponentA } from "storybook-root";26import { ComponentA } from "storybook-root";27import { ComponentA } from "storybook-root";28import { ComponentA }
Using AI Code Generation
1import { dependenciesMap } from 'storybook-root';2export { dependenciesMap } from 'storybook-root';3"jest": {4 "moduleNameMapper": {5 }6 }
Using AI Code Generation
1const storybookRoot = require('storybook-root');2const path = require('path');3const dependenciesMap = storybookRoot.dependenciesMap();4const componentPath = path.resolve(__dirname, './component.js');5const componentDependencies = dependenciesMap[componentPath];6console.log(componentDependencies);7const storybookRoot = require('storybook-root');8const path = require('path');9const dependenciesMap = storybookRoot.dependenciesMap();10const componentPath = path.resolve(__dirname, './test.js');11const componentDependencies = dependenciesMap[componentPath];12console.log(componentDependencies);13const storybookRoot = require('storybook-root');14const path = require('path');15const dependenciesMap = storybookRoot.dependenciesMap();16const componentPath = path.resolve(__dirname, './component.js');17const componentDependencies = dependenciesMap[componentPath];18console.log(componentDependencies);19const storybookRoot = require('storybook-root');20const path = require('path');21const dependenciesMap = storybookRoot.dependenciesMap();22const componentPath = path.resolve(__dirname, './test.js');23const componentDependencies = dependenciesMap[componentPath];24console.log(componentDependencies);
Using AI Code Generation
1const { dependenciesMap } = require('storybook-root');2const dependenciesMap = dependenciesMap();3const componentList = Object.keys(dependenciesMap);4const dependentComponents = dependenciesMap[1];5const dependencyComponents = Object.keys(dependenciesMap).filter(6 (id) => dependenciesMap[id].includes(1)7);8const leafDependencyComponents = Object.keys(dependenciesMap).filter(9 (id) => dependenciesMap[id].includes(1) && dependenciesMap[id].length === 110);11const nonLeafDependencyComponents = Object.keys(dependenciesMap).filter(12 (id) => dependenciesMap[id].includes(1) && dependenciesMap[id].length > 113);14const leafDependentComponents = Object.keys(dependenciesMap).filter(15 (id) => dependenciesMap[id].includes(1) && dependenciesMap[id].length === 116);17const nonLeafDependentComponents = Object.keys(dependenciesMap).filter(18 (id) => dependenciesMap[id].includes(1) && dependenciesMap[id].length > 119);20const componentsDependentOnBoth1And2 = Object.keys(dependenciesMap).filter(21 (id) => dependenciesMap[id].includes(1) && dependenciesMap[id].includes(2)22);
Using AI Code Generation
1import { dependenciesMap } from 'storybook-root'2const dependencies = dependenciesMap('path/to/file.js')3import { dependenciesMap } from 'storybook-root'4const dependencies = dependenciesMap('path/to/file.js')5import { dependenciesMap } from 'storybook-root'6const dependencies = dependenciesMap('path/to/file.js')7import { dependenciesMap } from 'storybook-root'8const dependencies = dependenciesMap('path/to/file.js')9import { dependenciesMap } from 'storybook-root'10const dependencies = dependenciesMap('path/to/file.js')11import { dependenciesMap } from 'storybook-root'12const dependencies = dependenciesMap('path/to/file.js')13import { dependenciesMap } from 'storybook-root'14const dependencies = dependenciesMap('path/to/file.js')15import { dependenciesMap } from 'storybook-root'16const dependencies = dependenciesMap('path/to/file.js')17import { dependenciesMap } from 'storybook-root'18const dependencies = dependenciesMap('path/to/file.js')19import { dependenciesMap } from 'storybook-root'20const dependencies = dependenciesMap('path/to/file.js')
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!!