Best JavaScript code snippet using storybook-root
javascript.rule.js
Source:javascript.rule.js
1//// javascript.rule.js --- Javascript Module substitution handler.2//3// Handle substituting javascript modified locally by the user.4// We don't provide the javascript modules for OOTB features, so this will5// only take effect on uploaded javascript, or extension modules.6"use strict"7const constants = require("../../constants").constants8const warn = require('../../logger').warn9const dataMaps = require('../datamaps')10const substituteLocalFileContents = require('../proxyUtils').substituteLocalFileContents11const path = require('path')12exports.name = "Javascript"13exports.doc = "Handler for Javascript modules"14exports.rule = {15 method: 'GET',16 phase: 'response',17 // only intercept js pages18 fullUrl: "__NODE__/file/*",19 mimeType: 'application/javascript',20 // expose the response body as a big string21 as: 'string'22}23/**24 * Handle locating and substituting Javascript resources.25 * Javascript files may be:26 * - Application level Javascript27 * - Widget Javascript (inc Global)28 * - Element Javascript (inc Global)29 *30 * Additionally resources may be versioned so we need to resolve31 * the version from the URL and locate the appropriate files32 * under basePath.33 */34exports.handler = function (req, resp) {35 // Look out for widget files.36 const uri = req._data.url37 const urlSegments = uri.substring(1).split("/")38 // Look for static JS files e.g. /file/static/something.js.39 if (urlSegments[1] === "static") {40 // We can't predict the number of segments in the path but the last segment has to be the filename.41 let filename = urlSegments[urlSegments.length - 1]42 // Chop off the query string if present.43 const queryParamsIndex = filename.indexOf('?')44 if (queryParamsIndex >= 0) {45 filename = filename.substr(0, queryParamsIndex)46 }47 // Put the path to the file on disk together, leaving out the first and last segments and stick the filename.48 // on the end.49 const localPathSegments = urlSegments.slice(1, urlSegments.length - 1)50 localPathSegments.push(filename)51 // Now try and get the local disk contents.52 const contents = substituteLocalFileContents(filename, localPathSegments.join("/"))53 contents && (resp.string = contents)54 return55 }56 if (urlSegments[2] === "widget") {57 if (urlSegments[3] === "module") {58 const widgetShortName = (urlSegments.length === 9) ? urlSegments[5] : urlSegments[4]59 let jsFileName = (urlSegments.length === 9) ? urlSegments[8] : urlSegments[7]60 const queryParamsIndex = jsFileName.indexOf('?')61 if (queryParamsIndex >= 0) {62 jsFileName = jsFileName.substr(0, queryParamsIndex)63 }64 const minifyIndex = jsFileName.indexOf('.min.js')65 if (minifyIndex >= 0) {66 jsFileName = jsFileName.substr(0, minifyIndex) + ".js"67 }68 const basePath = dataMaps.widget(widgetShortName)69 if (!basePath) {70 warn("noLocalSubstituteFoundWarning", {item: jsFileName})71 return72 }73 const contents = substituteLocalFileContents(jsFileName, path.join(basePath, "module", "js", jsFileName))74 resp.string = contents || ""75 return76 }77 if (urlSegments.length === 6 || urlSegments.length === 7) { // Widget JS78 // Suck out the bits we need.79 const widgetShortName = (urlSegments.length === 7) ? urlSegments[4] : urlSegments[3]80 let jsFileName = (urlSegments.length === 7) ? urlSegments[6] : urlSegments[5]81 const queryParamsIndex = jsFileName.indexOf('?')82 if (queryParamsIndex >= 0) {83 jsFileName = jsFileName.substr(0, queryParamsIndex)84 }85 const minifyIndex = jsFileName.indexOf('.min.js')86 if (minifyIndex >= 0) {87 jsFileName = jsFileName.substr(0, minifyIndex) + ".js"88 }89 // Get the base path from the ID90 const basePath = dataMaps.widget(widgetShortName)91 if (!basePath) {92 warn("noLocalSubstituteFoundWarning", {item: jsFileName})93 return94 }95 const contents = substituteLocalFileContents(jsFileName, path.join(basePath, "js", jsFileName))96 if (contents) {97 resp.string = contents98 }99 }100 if (urlSegments.length === 8 || urlSegments.length === 9) { // Widget Element JS101 // Suck out the bits we need.102 const elShortName = (urlSegments.length === 9) ? urlSegments[6] : urlSegments[5]103 // Get the base path from the ID104 const basePath = dataMaps.element(elShortName)105 if (!basePath) {106 warn("noLocalSubstituteFoundWarning", {item: elShortName})107 return108 }109 const contents = substituteLocalFileContents(elShortName, path.join(basePath, constants.elementJavaScript))110 if (contents) {111 resp.string = contents112 }113 }114 }115 if (urlSegments[2] === "element") {116 // Suck out the bits we need.117 const elShortName = urlSegments[3]118 // Get the base path from the ID119 const basePath = dataMaps.element(elShortName)120 if (!basePath) {121 warn("noLocalSubstituteFoundWarning", {item: elShortName})122 return123 }124 const contents = substituteLocalFileContents(elShortName, path.join(basePath, constants.elementJavaScript))125 if (contents) {126 resp.string = contents127 }128 }129 if (urlSegments[2] === "global") {130 // Suck out the bits we need.131 let jsFileName = urlSegments[3]132 const queryParamsIndex = jsFileName.indexOf('?')133 if (queryParamsIndex >= 0) {134 jsFileName = jsFileName.substr(0, queryParamsIndex)135 }136 const minifyIndex = jsFileName.indexOf('.min.js')137 if (minifyIndex >= 0) {138 jsFileName = jsFileName.substr(0, minifyIndex) + ".js"139 }140 const basePath = path.join("global", jsFileName)141 const contents = substituteLocalFileContents(jsFileName, basePath)142 if (contents) {143 resp.string = contents144 }145 }...
logger.js
Source:logger.js
1"use strict";2const winston = require("winston");3const moment = require("moment-timezone");4const LOG_ID = "CHIEFBOT/LOG - ";5const { createLogger, format, transports } = require('winston');6const { combine, timestamp, printf, colorize, splat } = format;7const appendTimestamp = format((info, opts) => {8 if (opts.tz)9 info.timestamp = moment().tz(opts.tz).format('YYYY-MM-DD HH:mm:ss');10 return info;11});12const myFormat = printf((info) => {13 if (info.meta && info.meta instanceof Error) {14 return `${info.timestamp} ${info.level} ${info.message} : ${info.meta.stack}`;15 }16 return `${info.timestamp} ${info.level}: ${info.message}`;17});18/*19Object.defineProperty(global, '__stack', {20 get: function() {21 var orig = Error.prepareStackTrace;22 Error.prepareStackTrace = function(_, stack) {23 return stack;24 };25 var err = new Error;26 Error.captureStackTrace(err, arguments.callee);27 var stack = err.stack;28 Error.prepareStackTrace = orig;29 return stack;30 }31});32Object.defineProperty(global, '__line', {33 get: function() {34 return __stack[1].getLineNumber();35 }36});37Object.defineProperty(global, '__function', {38 get: function() {39 return __stack[1].getFunctionName();40 }41});42*/43const LOG_LEVEL = process.env.LOG_LEVEL || 'debug';44var that = null;45class Logger {46 constructor(filename) {47 var p = /^\s*^$/;48 this._jsfilename = '';49 var rootPath = process.mainModule.paths[0].split(/\bnode_modules\b/)[0].slice(0, -1);50 if (!p.test(filename)) {51 this._jsfilename = filename.replace(rootPath, '');52 }53 this._LOG_ID = '';54 that = this;55 this._tz = Intl.DateTimeFormat().resolvedOptions().timeZone;56 this._logger = createLogger({57 transports: [58 new(transports.Console)({59 level: LOG_LEVEL,60 format: combine(61 colorize(),62 appendTimestamp({ tz: this._tz }),63 splat(),64 myFormat65 )66 })67 ]68 });69 }70 setId(logId) {71 this._LOG_ID = logId;72 }73 enter(message) {74 this._logger.log('info', '[' + this._jsfilename + '] IN :' + message);75 }76 exit(message) {77 this._logger.log('info', '[' + this._jsfilename + '] OUT :' + message);78 }79 exitWithError(message) {80 this._logger.log('error', '[' + this._jsfilename + '] OUT :' + message);81 }82 info(message) {83 this._logger.log('info', '[' + this._jsfilename + '] ' + message);84 }85 warn(message) {86 this._logger.log('warn', '[' + this._jsfilename + '] ' + message);87 }88 debug(message) {89 if (typeof message == 'Object') {90 this._logger.log('debug', '[' + this._jsfilename + '] ' + JSON.stringify(message, null, 2));91 } else {92 this._logger.log('debug', '[' + this._jsfilename + '] ' + message);93 }94 }95 debug(funcname, message) {96 if (typeof message == 'Object') {97 this._logger.log('debug', '[' + this._jsfilename + '][' + funcname + '] ' + JSON.stringify(message, null, 2));98 } else {99 this._logger.log('debug', '[' + this._jsfilename + '][' + funcname + '] ' + message);100 }101 }102 error(message) {103 this._logger.log('error', '[' + this._jsfilename + '] ' + message);104 }105 fatal(message) {106 this._logger.log('fatal', '[' + this._jsfilename + '] ' + message);107 }108}109module.exports = (filename) => {110 return new Logger(filename);...
Using AI Code Generation
1var storybookRoot = require('storybook-root');2var fileName = storybookRoot.jsFileName('test.js');3console.log(fileName);4var storybookRoot = require('storybook-root');5var fileName = storybookRoot.jsFileName('test.js');6console.log(fileName);7var storybookRoot = require('storybook-root');8var fileName = storybookRoot.jsFileName('test.js');9console.log(fileName);10var storybookRoot = require('storybook-root');11var fileName = storybookRoot.jsFileName('test.js');12console.log(fileName);13var storybookRoot = require('storybook-root');14var fileName = storybookRoot.jsFileName('test.js');15console.log(fileName);16var storybookRoot = require('storybook-root');17var fileName = storybookRoot.jsFileName('test.js');18console.log(fileName);19var storybookRoot = require('storybook-root');20var fileName = storybookRoot.jsFileName('test.js');21console.log(fileName);22var storybookRoot = require('storybook-root');23var fileName = storybookRoot.jsFileName('test.js');24console.log(fileName);25var storybookRoot = require('storybook-root');26var fileName = storybookRoot.jsFileName('test.js');27console.log(fileName);28var storybookRoot = require('storybook-root');29var fileName = storybookRoot.jsFileName('test.js');30console.log(fileName);31var storybookRoot = require('storybook-root');32var fileName = storybookRoot.jsFileName('test.js');33console.log(fileName);34var storybookRoot = require('storybook-root');
Using AI Code Generation
1var storybookRoot = require('storybook-root');2var jsFileName = storybookRoot.jsFileName;3console.log(jsFileName('test.js'));4var storybookRoot = require('storybook-root');5var jsFileName = storybookRoot.jsFileName;6console.log(jsFileName('test.js'));7var storybookRoot = require('storybook-root');8var jsFileName = storybookRoot.jsFileName;9console.log(jsFileName('test.js'));10var storybookRoot = require('storybook-root');11var jsFileName = storybookRoot.jsFileName;12console.log(jsFileName('test.js'));13var storybookRoot = require('storybook-root');14var jsFileName = storybookRoot.jsFileName;15console.log(jsFileName('test.js'));16var storybookRoot = require('storybook-root');17var jsFileName = storybookRoot.jsFileName;18console.log(jsFileName('test.js'));19var storybookRoot = require('storybook-root');20var jsFileName = storybookRoot.jsFileName;21console.log(jsFileName('test.js'));22var storybookRoot = require('storybook-root');23var jsFileName = storybookRoot.jsFileName;24console.log(jsFileName('test.js'));25var storybookRoot = require('storybook-root');26var jsFileName = storybookRoot.jsFileName;27console.log(jsFileName('test.js'));28var storybookRoot = require('storybook-root');29var jsFileName = storybookRoot.jsFileName;30console.log(jsFileName('test.js'));31var storybookRoot = require('storybook-root');32var jsFileName = storybookRoot.jsFileName;33console.log(jsFileName('test.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!!