Best JavaScript code snippet using storybook-root
remove-component.js
Source:remove-component.js
1const os = require('os');2const eol = os.EOL;3const yargs = require('yargs');4const fs = require('fs');5const path = require('path');6const readline = require('readline');7const rl = readline.createInterface(process.stdin, process.stdout);8const paths = require(`${process.cwd()}/config`).paths;9const slash = require('./slash');10const COMPONENTS_DIR = !yargs.argv.vue ? paths.src.components : paths.src.componentsVue;11////////////////////////////////////////////////////////////////////////////////////////////////////12// default content for files in component13const fileSources = {14 pug: {15 importSource: `include ${slash(path.relative(paths.src.templates,16 `${paths.src.components}/{componentName}/{componentName}`))}`,17 importPath: `${paths.src.templates}/components.pug`,18 },19 sass: {20 importSource: `@import "@/${slash(path.relative(paths.src.base, paths.src.components))}/{componentName}/{componentName}"`,21 importPath: `${paths.src.styles}/components.sass`,22 },23};24function validateComponentName(componentName) {25 return new Promise((resolve, reject) => {26 const isValid = /^(\d|\w|-)+$/.test(componentName);27 if (isValid) {28 resolve(isValid);29 } else {30 const errMsg =31 `ERR>>> An incorrect component name '${componentName}'${eol}` +32 'ERR>>> A component name must include letters, numbers & the minus symbol.'33 ;34 reject(errMsg);35 }36 });37}38function directoryExist(componentPath, componentName) {39 return new Promise((resolve, reject) => {40 fs.stat(componentPath, (notExist) => {41 if (notExist) {42 reject(`ERR>>> The component '${componentName}' does not exist.`);43 } else {44 resolve();45 }46 });47 });48}49function removeDir(dirPath) {50 return new Promise((resolve, reject) => {51 function removeFolderRecursive(path) {52 fs.readdirSync(path).forEach(function(file) {53 let curPath = path + "/" + file;54 if (fs.lstatSync(curPath).isDirectory()) {55 removeFolderRecursive(curPath);56 } else {57 fs.unlinkSync(curPath);58 }59 });60 fs.rmdir(path, (error) => {61 if (error) {62 reject(`ERR>>> Failed to remove a folder '${dirPath}'`);63 } else {64 resolve();65 }66 });67 };68 removeFolderRecursive(dirPath);69 });70}71function removeImports(componentName) {72 if (yargs.argv.vue) return new Promise((resolve) => {resolve()});73 const promises = [];74 Object.keys(fileSources).forEach((ext) => {75 const fileImportSource = fileSources[ext].importSource.replace(/\{componentName}/g, componentName);76 const fileImportPath = fileSources[ext].importPath;77 promises.push(78 new Promise((resolve, reject) => {79 fs.stat(fileImportPath, (error, stats) => {80 if (error) {81 reject(`ERR>>> Failed to read a file '${fileImportPath}'`);82 } else {83 if (stats.size !== 0) {84 fs.readFile(fileImportPath, 'utf8', (error, data) => {85 if (error) {86 reject(`ERR>>> Failed to remove from file '${fileImportPath}'`);87 } else {88 if (data.includes(fileImportSource)) {89 const newData = data.replace(new RegExp(`${fileImportSource}`, 'g'), '');90 fs.writeFile(fileImportPath, newData, 'utf8', (error) => {91 if (error) {92 reject(`ERR>>> Failed to remove from file '${fileImportPath}'`);93 } else {94 resolve();95 }96 });97 } else {98 resolve();99 }100 }101 });102 } else {103 resolve();104 }105 }106 });107 })108 );109 });110 return Promise.all(promises);111}112function printErrorMessage(errText) {113 console.log(errText);114 rl.close();115}116// //////////////////////////////////////////////////////////////////////////117function initRemoveComponent(candidateComponentName) {118 const componentNames = candidateComponentName.trim().split(/\s+/);119 const removeComponent = (componentName) => {120 const componentPath = path.join(COMPONENTS_DIR, componentName);121 return validateComponentName(componentName)122 .then(() => directoryExist(componentPath, componentName))123 .then(() => removeDir(componentPath))124 .then(() => removeImports(componentName))125 .then(() => {126 const line = '-'.repeat(45 + componentName.length);127 console.log(line);128 console.log(`The component "${componentName}" has been removed`);129 console.log(line);130 rl.close();131 });132 };133 if (componentNames.length === 1) {134 return removeComponent(componentNames[0]);135 }136 return componentNames.reduce((promise, name) => {137 return promise.then(() => removeComponent(name));138 }, Promise.resolve());139}140// //////////////////////////////////////////////////////////////////////////141//142// Start here143//144// Command line arguments145const componentNameFromCli = yargs.argv._146// join all arguments to one string (to simplify the capture user input errors)147 .join(' ');148// If the user pass the name of the component in the command-line options149// that remove a component. Otherwise - activates interactive mode150if (componentNameFromCli !== '') {151 initRemoveComponent(componentNameFromCli).catch(printErrorMessage);152} else {153 rl.setPrompt('Component(s) name: ');154 rl.prompt();155 rl.on('line', (line) => {156 initRemoveComponent(line).catch(printErrorMessage);157 });...
app.service.js
Source:app.service.js
1const path = require('path');2const { last, upperFirst, camelCase } = require('lodash');3const getGeneratedFilePath = (fileType, options) => {4 /**5 * 1. --component comment6 * -> src/comment/comment.vue7 *8 * 2. --component comment-index9 * -> src/comment/index/comment-index.vue10 *11 * 3. --component comment-list --path comment/index/components12 * -> src/comment/index/components/comment-list.vue13 */14 let typeOption = fileType;15 if (fileType === 'style') {16 typeOption = 'component';17 }18 const { [typeOption]: fileName, path: filePath } = options;19 let fileFullName;20 switch (fileType) {21 case 'component':22 fileFullName = `${fileName}.vue`;23 break;24 case 'style':25 fileFullName = path.join('styles', `${fileName}.css`);26 break;27 case 'store':28 fileFullName = `${fileName}.store.ts`;29 break;30 }31 const fileNameArray = fileName.split('-');32 const isMultiWordsFile = fileNameArray.length > 1;33 // æ件åæ¾ä½ç½®34 let fileFullPath = [];35 if (filePath) {36 // 337 const filePathArray = filePath.split('/');38 fileFullPath = ['src', ...filePathArray, fileFullName];39 } else if (isMultiWordsFile) {40 // 241 fileFullPath = ['src', ...fileNameArray, fileFullName];42 } else {43 // 144 fileFullPath = ['src', fileName, fileFullName];45 }46 return path.join(...fileFullPath);47};48/**49 * è·åç¶è¾æ件路å¾50 */51const getParentFilePath = (fileType, options) => {52 // --parent comment/index/comment-index53 const { parent } = options;54 let fileExtension = '';55 switch (fileType) {56 case 'component':57 fileExtension = '.vue';58 break;59 case 'store':60 fileExtension = '.store.ts';61 break;62 }63 let parentFilePath = [];64 const parentArray = parent.split('/');65 // è¿åæ°ç»æåä¸é¡¹66 const parentFileName = last(parentArray) + fileExtension;67 if (parentArray.length > 1) {68 parentArray.pop();69 parentFilePath = ['src', ...parentArray, parentFileName];70 } else {71 parentFilePath = ['src', parent, parentFileName];72 }73 return path.join(...parentFilePath);74};75/**76 * è·åç¶è¾åå77 */78const getParentName = (options) => {79 // --parent comment/index/comment-index80 return last(options.parent.split('/'));81};82/**83 * è·å导å
¥è·¯å¾84 */85const getGeneratedFileImportPath = (fileType, options) => {86 const { [fileType]: fileName, path: filePath } = options;87 let fileFullName;88 switch (fileType) {89 case 'component':90 fileFullName = fileName;91 break;92 case 'store':93 fileFullName = `${fileName}.store`;94 break;95 }96 const fileNameArray = fileName.split('-');97 const isMultiWordsFile = fileNameArray.length > 1;98 let fileImportPath = [];99 if (filePath) {100 const filePathArray = filePath.split('/');101 fileImportPath = ['@', ...filePathArray, fileFullName];102 } else if (isMultiWordsFile) {103 fileImportPath = ['@', ...fileNameArray, fileFullName];104 } else {105 fileImportPath = ['@', fileName, fileFullName];106 }107 return fileImportPath.join('/');108};109/**110 * è·å项ç®æ件å
容111 */112const getProjectFileContent = (filePath, api) => {113 const file = api.generator.files[filePath];114 return file.split(/\r?\n/g);115};116/**117 * æå
¥å
容118 */119const insertFileContent = (options = {}) => {120 const { fileContent, find, insert } = options;121 const lineIndex = fileContent.findIndex((line) => line.match(RegExp(find)));122 fileContent[lineIndex] += insert;123 return fileContent;124};125/**126 * PascalCase127 */128const pascalCase = (string) => {129 return upperFirst(camelCase(string));130};131module.exports = {132 getGeneratedFilePath,133 getParentFilePath,134 getParentName,135 getGeneratedFileImportPath,136 getProjectFileContent,137 insertFileContent,138 pascalCase,...
Using AI Code Generation
1import { fileImportPath } from 'storybook-root';2import { fileImportPath } from 'storybook-root';3import { fileImportPath } from 'storybook-root';4import { fileImportPath } from 'storybook-root';5import { fileImportPath } from 'storybook-root';6import { fileImportPath } from 'storybook-root';7import { fileImportPath } from 'storybook-root';8import { fileImportPath } from 'storybook-root';9import { fileImportPath } from 'storybook-root';10import { fileImportPath } from 'storybook-root';11import { fileImportPath } from 'storybook-root';12import { fileImportPath } from 'storybook-root';13import { fileImportPath } from 'storybook-root';14import { fileImportPath } from 'storybook-root';15import { fileImportPath } from 'storybook-root';16import { fileImportPath } from 'storybook-root';17import { fileImportPath } from 'storybook-root';18import { fileImportPath } from 'storybook-root';19import { fileImportPath } from 'storybook-root';
Using AI Code Generation
1import { fileImportPath } from 'storybook-root-decorator';2import { withInfo } from '@storybook/addon-info';3import { withKnobs } from '@storybook/addon-knobs';4import { addDecorator, addParameters } from '@storybook/react';5import { withA11y } from '@storybook/addon-a11y';6import { withTests } from '@storybook/addon-jest';7import results from '../.jest-test-results.json';8import { withBackgrounds } from '@storybook/addon-backgrounds';9import { withViewport } from '@storybook/addon-viewport';10import { withConsole } from '@storybook/addon-console';11import { withPerformance } from 'storybook-addon-performance';12import { withCode } from 'storybook-addon-code';13import { withPropsTable } from 'storybook-addon-react-docgen';14const req = require.context('../src', true, /.stories.js$/);15const loadStories = () => {16 req.keys().forEach(filename => req(filename));17};18const withInfoOptions = {19 styles: {20 infoBody: {21 },22 infoStory: {23 },24 source: {25 h1: {26 },27 },28 },29};30const withCodeOptions = {31};32addDecorator(33 withConsole({34 })35);36addDecorator(37 withViewport({38 viewports: {39 Small: {40 styles: {41 },42 },43 Medium: {44 styles: {45 },46 },47 Large: {48 styles: {49 },50 },
Using AI Code Generation
1import { fileImportPath } from 'storybook-root'2import { storiesOf } from '@storybook/react';3import { withKnobs, select } from '@storybook/addon-knobs/react';4import { withReadme } from 'storybook-readme';5import { withDocs } from 'storybook-readme';6import { withDocsCustom } from 'storybook-readme';7import { withInfo } from '@storybook/addon-info';8import { withInfoCustom } from '@storybook/addon-info';9import { withA11y } from '@storybook/addon-a11y';10import { withA11yCustom } from '@storybook/addon-a11y';11import { withViewport } from '@storybook/addon-viewport';12import { withViewportCustom } from '@storybook/addon-viewport';13import { withTests } from '@storybook/addon-jest';14import { withTestsCustom } from '@storybook/addon-jest';15import { withOptions } from '@storybook/addon-options';16import { withOptionsCustom } from '@storybook/addon-options';17import { withLinks } from '@storybook/addon-links';18import { withLinksCustom } from '@storybook/addon-links';19import { withNotes } from '@storybook/addon-notes';20import { withNotesCustom } from '@storybook/addon-notes';21import { withBackgrounds } from '@storybook/addon-backgrounds';22import { withBackgroundsCustom } from '@storybook/addon-backgrounds';23import { withConsole } from '@storybook/addon-console';24import { withConsoleCustom } from '@storybook/addon-console';25import { withContexts } from '@storybook/addon-contexts/react';26import { withContextsCustom } from '@storybook/addon-contexts/react';27import { withGraphQL } from 'storybook-addon-graphql';28import { withGraphQLCustom } from 'storybook-addon-graphql';29import { withRedux } from 'addon-redux';30import { withReduxCustom } from 'addon-redux';31import { withStorysource } from '@storybook/addon-storysource';32import { withStorysourceCustom } from '@storybook/addon-storysource';33import { withStoryshots } from '@storybook/addon-storyshots';34import { withStoryshotsCustom } from '@storybook/addon-storyshots';35import { withStoryshotsPuppeteer } from '@storybook/addon-storyshots-puppeteer';36import { withStoryshotsPuppeteerCustom } from '@storybook/addon-storyshots-puppeteer';37import { withStoryshotsImage } from
Using AI Code Generation
1import { fileImportPath } from 'storybook-root-decorator';2import { fileImportPath } from 'storybook-root-decorator/fileImportPath';3import { fileImportPath } from 'storybook-root-decorator/dist/fileImportPath';4import { fileImportPath } from 'storybook-root-decorator/dist/fileImportPath.js';5import { fileImportPath } from 'storybook-root-decorator/dist/fileImportPath.jsx';6import { fileImportPath } from 'storybook-root-decorator/dist/fileImportPath.ts';7import { fileImportPath } from 'storybook-root-decorator/dist/fileImportPath.tsx';8import { fileImportPath } from 'storybook-root-decorator/dist/fileImportPath.json';9import { fileImportPath } from 'storybook-root-decorator/dist/fileImportPath.css';10import { fileImportPath } from 'storybook-root-decorator/dist/fileImportPath.scss';11import { fileImportPath } from 'storybook-root-decorator/dist/fileImportPath.less';12import { fileImportPath } from 'storybook-root-decorator/dist/fileImportPath.styl';13import { fileImportPath } from 'storybook-root-decorator/dist/fileImportPath.md';14import { fileImportPath } from 'storybook-root-decorator/dist/fileImportPath.txt';15import { fileImportPath } from 'storybook-root-decorator/dist/fileImportPath.svg';16import { fileImportPath } from 'storybook-root-decorator/dist/fileImportPath.png';17import { fileImportPath } from 'storybook-root-decorator/dist/fileImportPath.jpg';18import { fileImportPath } from 'storybook-root-decorator/dist/fileImportPath.jpeg';19import { fileImportPath } from 'storybook-root-decorator/dist/fileImportPath.gif';20import { fileImportPath } from 'storybook-root-decorator/dist/fileImportPath.webp';21import { fileImportPath } from 'storybook-root-decorator/dist/fileImportPath.webm';22import { fileImportPath } from 'storybook-root-decorator/dist/fileImportPath.ogv';23import { fileImportPath } from 'storybook-root-decorator/dist
Using AI Code Generation
1var storybookRoot = require('storybook-root');2storybookRoot.fileImportPath('src/components/MyComponent');3var storybookRoot = require('storybook-root');4storybookRoot.fileImportPath('src/components/MyComponent');5var storybookRoot = require('storybook-root');6storybookRoot.srcPath();7var storybookRoot = require('storybook-root');8storybookRoot.srcPath();9var storybookRoot = require('storybook-root');10storybookRoot.storybookPath();11var storybookRoot = require('storybook-root');12storybookRoot.storybookPath();13var storybookRoot = require('storybook-root');14storybookRoot.storybookRootPath();15var storybookRoot = require('storybook-root');16storybookRoot.storybookRootPath();17var storybookRoot = require('storybook-root');18storybookRoot.rootPath();19var storybookRoot = require('storybook-root');20storybookRoot.rootPath();21var storybookRoot = require('storybook-root');22storybookRoot.testPath();23var storybookRoot = require('storybook-root');24storybookRoot.testPath();25var storybookRoot = require('storybook-root');26storybookRoot.testRootPath();27var storybookRoot = require('storybook-root');
Using AI Code Generation
1import { fileImportPath } from "storybook-root";2const path = fileImportPath("test.js");3import { fileImportPath } from "storybook-root";4const path = fileImportPath("test.js");5import { fileImportPath } from "storybook-root";6const path = fileImportPath("test.js");7import { fileImportPath } from "storybook-root";8const path = fileImportPath("components/test.js");9import { fileImportPath } from "storybook-root";10const path = fileImportPath("components/test.js");11import { fileImportPath } from "storybook-root";12const path = fileImportPath("src/components/test.js");13import { fileImportPath } from "storybook-root";14const path = fileImportPath("src/components/test.js");15import { fileImportPath } from "storybook-root";16const path = fileImportPath("src/components/test.js");17import { fileImportPath } from "storybook-root";18const path = fileImportPath("src/components/test.js");19import { fileImportPath } from
Using AI Code Generation
1const testFile = fileImportPath('test.js');2const storyFile = fileImportPath('stories/index.js');3const storyFile = fileImportPath('stories/MyComponent/index.js');4const storyFile = fileImportPath('stories/MyComponent/MyComponent.js');5const storyFile = fileImportPath('stories/MyComponent/MyComponent.stories.js');6const storyFile = fileImportPath('stories/MyComponent/MyComponent.stories.jsx');7const storyFile = fileImportPath('stories/MyComponent/MyComponent.stories.ts');8const storyFile = fileImportPath('stories/MyComponent/MyComponent.stories.tsx');9const storyFile = fileImportPath('stories/MyComponent/MyComponent.test.js');10const storyFile = fileImportPath('stories/MyComponent/MyComponent.test.jsx');
Using AI Code Generation
1import { fileImportPath } from 'storybook-root';2import path from 'path';3const absolutePath = fileImportPath('src/components/MyComponent/index.js');4const relativePath = path.relative(__dirname, absolutePath);5import { fileImportPath } from 'storybook-root';6import path from 'path';7const absolutePath = fileImportPath('src/components/MyComponent/index.js');8const relativePath = path.relative(__dirname, absolutePath);9import { fileImportPath } from 'storybook-root';10import path from 'path';11const absolutePath = fileImportPath('src/components/MyComponent/index.js');12const relativePath = path.relative(__dirname, absolutePath);13The path.relative method is also useful in cases where you want to import a file from a directory that is outside the current directory but is a parent of the current directory. For
Using AI Code Generation
1import { fileImportPath } from 'storybook-root';2const path = fileImportPath('src/components/atoms/Button/Button.js');3import { resolve } from 'path';4import { config } from 'dotenv';5const env = config();6export const fileImportPath = (filePath) => {7 const { rootPath } = env.parsed;8 const path = resolve(rootPath, filePath);9 return path;10};11import { fileImportPath } from 'storybook-root';12const path = fileImportPath('src/components/atoms/Button/Button.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!!