Best JavaScript code snippet using best
normalize.ts
Source:normalize.ts
1/*2 * Copyright (c) 2019, salesforce.com, inc.3 * All rights reserved.4 * SPDX-License-Identifier: MIT5 * For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/MIT6*/7import path from 'path';8import chalk from 'chalk';9import DEFAULT_CONFIG from './defaults';10import { replacePathSepForRegex } from '@best/regex-util';11import { CliConfig, UserConfig, NormalizedConfig, RunnerConfig, BrowserSpec } from '@best/types';12const TARGET_COMMIT = process.env.TARGET_COMMIT;13const BASE_COMMIT = process.env.BASE_COMMIT;14function normalizeModulePathPatterns(options: any, key: string) {15 return options[key].map((pattern: any) => replacePathSepForRegex(normalizeRootDirPattern(pattern, options.rootDir)));16}17function normalizeRunner(runner: string, runners: RunnerConfig[]) {18 const defaultRunners = runners.filter((c) => c.alias === undefined || c.alias === 'default');19 if (defaultRunners.length > 1) {20 throw new Error('Wrong configuration: More than one default configuration declared');21 }22 if (runner === "default") {23 if (defaultRunners.length) {24 return defaultRunners[0].runner;25 }26 } else {27 const selectedRunner = runners.find((c) => c.alias === runner || c.runner === runner);28 if (selectedRunner) {29 return selectedRunner.runner;30 }31 }32 return 'unknown';33}34function normalizeRunnerConfig(runner: string, runners: RunnerConfig[], specs?: BrowserSpec) {35 if (!runners) {36 return {};37 }38 let selectedRunner;39 if (runner === "default") {40 const defaultRunners = runners.filter((c: RunnerConfig) => c.alias === undefined || c.alias === 'default');41 if (defaultRunners.length > 0) {42 selectedRunner = defaultRunners[0];43 }44 } else {45 const selectedAliasRunner = runners.find((c: RunnerConfig) => c.alias === runner);46 selectedRunner = selectedAliasRunner || runners.find((c: RunnerConfig) => c.runner === runner);47 }48 if (selectedRunner) {49 const selectedRunnerConfig = selectedRunner.config || {};50 return { ...selectedRunnerConfig, specs: selectedRunner.specs || specs };51 }52}53function setCliOptionOverrides(initialOptions: UserConfig, argsCLI: CliConfig): UserConfig {54 const argvToOptions = Object.keys(argsCLI)55 .reduce((options: any, key: string) => {56 switch (key) {57 case '_':58 options.nonFlagArgs = argsCLI[key] || [];59 break;60 case 'disableInteractive':61 options.isInteractive = argsCLI[key] !== undefined ? false : undefined;62 break;63 case 'iterations':64 if (argsCLI[key] !== undefined) {65 options.benchmarkIterations = argsCLI[key];66 }67 break;68 case 'runInBatch':69 options.runInBatch = !!argsCLI[key];70 break;71 case 'runInBand':72 options.runInBand = !!argsCLI[key];73 break;74 case 'projects':75 if (argsCLI.projects && argsCLI.projects.length) {76 options.projects = argsCLI.projects;77 }78 break;79 case 'compareStats':80 options.compareStats = argsCLI.compareStats && argsCLI.compareStats.filter(Boolean);81 break;82 case 'gitIntegration':83 options.gitIntegration = Boolean(argsCLI[key]);84 break;85 case 'generateHTML':86 options.generateHTML = Boolean(argsCLI[key]);87 break;88 case 'dbAdapter':89 if (argsCLI[key] !== undefined) {90 options.apiDatabase = { 91 adapter: argsCLI[key], 92 uri: argsCLI['dbURI'],93 token: argsCLI['dbToken']94 }95 }96 break;97 case 'dbURI':98 case 'dbToken':99 break100 default:101 options[key] = argsCLI[key];102 break;103 }104 return options;105 }, {});106 return { ...initialOptions, ...argvToOptions };107}108function normalizeObjectPathPatterns(options: { [key: string]: any }, rootDir: string) {109 return Object.keys(options).reduce((m: any, key) => {110 const value = options[key];111 if (typeof value === 'string') {112 m[key] = normalizeRootDirPattern(value, rootDir);113 } else {114 m[key] = value;115 }116 return m;117 }, {});118}119function normalizePlugins(plugins: any, { rootDir }: UserConfig) {120 return plugins.map((plugin: any) => {121 if (typeof plugin === 'string') {122 return normalizeRootDirPattern(plugin, rootDir);123 } else if (Array.isArray(plugin)) {124 return [normalizeRootDirPattern(plugin[0], rootDir), normalizeObjectPathPatterns(plugin[1], rootDir)];125 }126 return plugin;127 });128}129function normalizeRootDir(options: UserConfig): UserConfig {130 // Assert that there *is* a rootDir131 if (!options.hasOwnProperty('rootDir')) {132 throw new Error(` Configuration option ${chalk.bold('rootDir')} must be specified.`);133 }134 options.rootDir = path.normalize(options.rootDir);135 return options;136}137function normalizeCommits(commits?: string[]): string[] | undefined {138 if (!commits) {139 return undefined;140 }141 let [base, target] = commits;142 base = (base || BASE_COMMIT || '');143 target = (target || TARGET_COMMIT || '');144 return [base.slice(0, 7), target.slice(0, 7)];145}146export function normalizeRootDirPattern(str: string, rootDir: string) {147 return str.replace(/<rootDir>/g, rootDir);148}149export function normalizeRegexPattern(names: string | string[] | RegExp) {150 if (typeof names === 'string') {151 names = names.split(',');152 }153 if (Array.isArray(names)) {154 names = names.map(name => name.replace(/\*/g, '.*'));155 names = new RegExp(`^(${names.join('|')})$`);156 }157 if (!(names instanceof RegExp)) {158 throw new Error(` Names must be provided as a string, array or regular expression.`);159 }160 return typeof names === 'string' ? new RegExp(names) : names;161}162export function normalizeConfig(userConfig: UserConfig, cliOptions: CliConfig): NormalizedConfig {163 const userCliMergedConfig = normalizeRootDir(setCliOptionOverrides(userConfig, cliOptions));164 const normalizedConfig: NormalizedConfig = { ...DEFAULT_CONFIG, ...userCliMergedConfig };165 const aliasRunner = normalizedConfig.runner;166 Object.keys(normalizedConfig).reduce((mergeConfig: NormalizedConfig, key: string) => {167 switch (key) {168 case 'projects':169 mergeConfig[key] = normalizeModulePathPatterns(normalizedConfig, key);170 break;171 case 'plugins':172 mergeConfig[key] = normalizePlugins(normalizedConfig[key], normalizedConfig);173 break;174 case 'runnerConfig':175 mergeConfig['runnerConfig'] = normalizeRunnerConfig(aliasRunner, mergeConfig.runners, mergeConfig.specs);176 break;177 case 'runner':178 mergeConfig[key] = normalizeRunner(normalizedConfig[key], mergeConfig.runners);179 break;180 case 'compareStats':181 mergeConfig[key] = normalizeCommits(normalizedConfig[key]);182 break;183 case 'specs':184 mergeConfig[key] = normalizedConfig['runnerConfig'].specs || mergeConfig[key];185 break;186 case 'apiDatabase': {187 const apiDatabaseConfig = normalizedConfig[key];188 mergeConfig[key] = apiDatabaseConfig ? normalizeObjectPathPatterns(apiDatabaseConfig, normalizedConfig.rootDir) : apiDatabaseConfig;189 break;190 }191 default:192 break;193 }194 return mergeConfig;195 }, normalizedConfig);196 return normalizedConfig;...
Using AI Code Generation
1const BestPractices = require('./bestPractices.js');2const bestPractices = new BestPractices();3let userConfig = bestPractices.userCliMergedConfig();4bestPractices.runConfig(userConfig);5const BestPractices = require('./bestPractices.js');6const bestPractices = new BestPractices();7let userConfig = bestPractices.userCliMergedConfig();8bestPractices.runConfig(userConfig);9const BestPractices = require('./bestPractices.js');10const bestPractices = new BestPractices();11let userConfig = bestPractices.userCliMergedConfig();12bestPractices.runConfig(userConfig);13const BestPractices = require('./bestPractices.js');14const bestPractices = new BestPractices();15let userConfig = bestPractices.userCliMergedConfig();16bestPractices.runConfig(userConfig);17const BestPractices = require('./bestPractices.js');18const bestPractices = new BestPractices();19let userConfig = bestPractices.userCliMergedConfig();20bestPractices.runConfig(userConfig);
Using AI Code Generation
1const BestPractices = require('./BestPractices');2const path = require('path');3let configPath = BestPractices.getConfigPath(process.argv);4let mergedConfig = BestPractices.userCliMergedConfig(configPath);5console.log(JSON.stringify(mergedConfig, null, 2));6{7 "rules": {8 }9}10const BestPractices = require('./BestPractices');11const path = require('path');12let configPath = BestPractices.getConfigPath(process.argv);13let mergedConfig = BestPractices.userCliMergedConfig(configPath);14console.log(JSON.stringify(mergedConfig, null, 2));15{16 "rules": {17 }18}19const BestPractices = require('./BestPractices');20const path = require('path');21let configPath = BestPractices.getConfigPath(process.argv);22let mergedConfig = BestPractices.userCliMergedConfig(configPath);23console.log(JSON.stringify(mergedConfig, null, 2));
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!!